package com.gree.mall.miniapp.logic.workorder;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.plus.entity.SysDictCompany;
import com.gree.mall.miniapp.plus.entity.SysDictTypeCompany;
import com.gree.mall.miniapp.plus.service.SysDictCompanyService;
import com.gree.mall.miniapp.plus.service.SysDictRefService;
import com.gree.mall.miniapp.plus.service.SysDictTypeCompanyService;
import com.gree.mall.miniapp.utils.RedisUtil;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;


@Service
public class SysDictCompanyLogic {

    @Autowired
    CommonLogic commonLogic;


    @Autowired
    SysDictCompanyService sysDictCompanyService;
    @Autowired
    SysDictRefService sysDictRefService;

    @Autowired
    SysDictTypeCompanyService sysDictTypeCompanyService;
    @Autowired
    RedisUtil redisUtil;


    /**
     * 字典类型
     *
     * @param open 是否开放业务人配置 , 为空不限制
     * @return
     */
    public List<SysDictTypeCompany> typeList(Boolean open) {
        List<SysDictTypeCompany> list = sysDictTypeCompanyService
                .lambdaQuery()
                .eq(Objects.nonNull(open), SysDictTypeCompany::getOpen, open)
                .list();
        return list;
    }



    /**
     * 删除字典
     *
     * @param id
     * @param open 是否开放业务, 为空或者false不限制
     */
    public void delete(String id, Boolean open) {
        if (BooleanUtils.isTrue(open)) {
            String dictType = sysDictCompanyService.lambdaQuery()
                    .select(SysDictCompany::getDictType)
                    .eq(SysDictCompany::getSysDictId, id)
                    .oneOpt()
                    .map(SysDictCompany::getDictType)
                    .orElseThrow(() -> new RemoteServiceException("字典值不存在"));
            sysDictTypeCompanyService.lambdaQuery()
                    .select(SysDictTypeCompany::getOpen)
                    .eq(SysDictTypeCompany::getDictType, dictType)
                    .oneOpt()
                    .map(SysDictTypeCompany::getOpen)
                    .filter(item -> item)
                    .orElseThrow(() -> new RemoteServiceException("该字典类型暂不开放配置"));
        }
        sysDictCompanyService.removeById(id);

    }

    /**
     * 字典详情
     */
    public SysDictCompany detail(String id) {
        return sysDictCompanyService.getById(id);
    }


    /**
     * 字典查询，key = dictCode  value = dictObject
     * @param type
     * @return
     */
    public Map<String, SysDictCompany> dictByTypeOfCode(String type,String companyWechatId){
        List<SysDictCompany> sysDicts = this.dictByType(type,true,companyWechatId);
        Map<String, SysDictCompany> sysDictMap = sysDicts.stream().collect(Collectors.toMap(v -> v.getDictCode(), Function.identity()));
        return sysDictMap;
    }
    public SysDictCompany dictByTypeOfCode(String type,String dictCode,String companyWechatId){
        List<SysDictCompany> sysDicts = this.dictByType(type,true,companyWechatId);
        Map<String, SysDictCompany> sysDictMap = sysDicts.stream().collect(Collectors.toMap(v -> v.getDictCode(), Function.identity()));
        return sysDictMap.get(dictCode);
    }

    /**
     * 字典查询，key = dictValue value = dictObject
     * @param type
     * @return
     */
    public Map<String, SysDictCompany> dictByTypeOfValue(String type,String companyWechatId){
        List<SysDictCompany> sysDicts = this.dictByType(type,true,companyWechatId);
        Map<String, SysDictCompany> sysDictMap = sysDicts.stream().collect(Collectors.toMap(v -> v.getDictValue(), Function.identity()));
        return sysDictMap;
    }
    public SysDictCompany dictByTypeOfValue(String type,String dictValue,String companyWechatId){
        List<SysDictCompany> sysDicts = this.dictByType(type,true,companyWechatId);
        Map<String, SysDictCompany> sysDictMap = sysDicts.stream().collect(Collectors.toMap(v -> v.getDictValue(), Function.identity()));
        return sysDictMap.get(dictValue);
    }

    /**
     * 字典值查询
     *
     * @param type
     * @param open 是否开放业务, 为空查询所有
     * @return
     */
    public List<SysDictCompany> dictByType(String type, Boolean open,String companyWechatId) {
        SysDictTypeCompany dictType = sysDictTypeCompanyService.lambdaQuery()
                .eq(SysDictTypeCompany::getDictType, type)
                .eq(Objects.nonNull(open), SysDictTypeCompany::getOpen, open)
                .one();
        if (dictType == null) {
            return Collections.emptyList();
        }
        List<SysDictCompany> dictList = sysDictCompanyService.lambdaQuery()
                .eq(SysDictCompany::getDictType, type)
                .eq(SysDictCompany::getCompanyWechatId,companyWechatId)
                .orderByDesc(SysDictCompany::getSortNum)
                .list();

        return dictList;
    }




    private void checkRepeatDictCode(String dictType, String value, String companyName) {
        Integer count = sysDictCompanyService.lambdaQuery()
                .eq(SysDictCompany::getDictType, dictType)
                .eq(SysDictCompany::getDictValue, value)
                .eq(SysDictCompany::getCompanyName, companyName)
                .count();
        if (count > 0) {
            throw new RemoteServiceException(value + "重复存在");
        }
    }
}
