package com.bonus.canteen.core.allocation.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.canteen.core.allocation.canteen.model.DzAllocCanteenAuth;
import com.bonus.canteen.core.allocation.canteen.service.DzAllocCanteenAuthService;
import com.bonus.canteen.core.common.enums.AllocCanteenTypeEnum;
import com.bonus.canteen.core.customer.api.CustCasualApi;
import com.bonus.canteen.core.customer.api.CustInfoApi;
import com.bonus.canteen.core.customer.model.CustInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * 定制 食堂访问权限api
 *
 * @author shihao.li
 * @date 2024/8/22 18:36
 */
@Component
public class DzAllocCanteenAuthApi {
    @Autowired
    @Lazy
    private CustCasualApi custCasualApi;
    @Autowired
    @Lazy
    private CustInfoApi custInfoApi;
    @Autowired
    @Lazy
    private GlobalMetadataApi globalMetadataApi;
    @Autowired
    @Lazy
    private DzAllocCanteenAuthService dzAllocCanteenAuthService;

    /**
     * 根据组织id，查询有权限的食堂id
     * 食堂访问权限，没有配置，则默认为可以查看所有食堂；
     * 1、返回null，等同于，无权限 看不到任何食堂；
     * 2、返回空集合，等同于，有所有权限，可以看到所有食堂
     * 默认查询食堂，如果查商超，请赋值
     */
    public List<Long> getAuthCanteenIdsByOpenId(String openid, Integer sourceType, AllocCanteenTypeEnum allocCanteenTypeEnum) {
        Long custId = custCasualApi.getCustIdByOpenidAndSourceType(openid, sourceType);
        if (custId == null) {
            throw new ServiceException("没有查询到人员绑定信息");
        }
        return this.getAuthCanteenIdsByCustId(custId, allocCanteenTypeEnum);
    }

    public List<Long> getAuthCanteenIdsByCustId(Long custId, AllocCanteenTypeEnum allocCanteenTypeEnum) {
        Integer canteenType;
        if (ObjectUtil.isNull(allocCanteenTypeEnum)) {
            canteenType = AllocCanteenTypeEnum.CANTEEN.getKey();
        } else {
            canteenType = allocCanteenTypeEnum.getKey();
        }

        CustInfo custInfo = custInfoApi.getNormalOneByCustId(custId);
        if (custInfo == null) {
            throw new ServiceException("没有查询到人员信息");
        }
        Long orgId = custInfo.getOrgId();
        List<DzAllocCanteenAuth> canteenAuthList = dzAllocCanteenAuthService.list(Wrappers.lambdaQuery(DzAllocCanteenAuth.class)
                .eq(DzAllocCanteenAuth::getCanteenType, canteenType));
        if (CollUtil.isEmpty(canteenAuthList)) {
            // 可以看到全部食堂
            return new ArrayList<>();
        }
        List<Long> canteenIdList = new ArrayList<>();
        for (DzAllocCanteenAuth canteenAuth : canteenAuthList) {
            List<Long> orgIdList = JSONUtil.toList(canteenAuth.getOrgIds(), Long.class);
            if (orgIdList.contains(orgId)) {
                canteenIdList.add(canteenAuth.getCanteenId());
            }
        }
        if (CollUtil.isNotEmpty(canteenIdList)) {
            return canteenIdList;
        }
        return null;
    }
//
//    /**
//     * 该食堂是否支持退款
//     *
//     * @param canteenId
//     * @return
//     */
//    public boolean supportRefund(@NotNull Long canteenId) {
//        //未配置，默认支持退款
//        boolean supportRefund = true;
//        // 查询移动端 不支持退款的食堂id
//        String supportRefundCanteenIds = globalMetadataApi.getAvailableValue(MetadataModelTypeEnum.MOBILE, "supportRefundCanteenIds", null, null);
//        if (CharSequenceUtil.isNotBlank(supportRefundCanteenIds) && supportRefundCanteenIds.contains(canteenId.toString())) {
//            supportRefund = false;
//        }
//        LogUtil.info("该食堂是否支持退款", supportRefund);
//        return supportRefund;
//    }


}
