package com.ruoyi.ibs.cmpm.service;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ibs.cmpm.mapper.GridCmpmMapper;
import com.ruoyi.ibs.customerselect.domain.ContinuousParam;
import com.ruoyi.ibs.customerselect.domain.CustBaseInfo;
import com.ruoyi.ibs.customerselect.domain.DiscreteParam;
import com.ruoyi.ibs.customerselect.domain.vo.GridRelateVO;
import com.ruoyi.ibs.customerselect.mapper.GridSelectMapper;
import com.ruoyi.ibs.customerselect.mapper.GridSummarCountMapper;
import com.ruoyi.ibs.customerselect.service.MyCustomerGridSelectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 吴凯程
 * @Date 2025/11/21
 **/
@Service
public class GridCmpmCustService {

    @Resource
    private GridCmpmMapper gridCmpmMapper;

    @Resource
    private GridCmpmService gridCmpmService;

    @Resource
    private GridSelectMapper gridSelectMapper;

    @Resource
    private MyCustomerGridSelectService myCustomerGridSelectService;

    @Autowired
    private GridSummarCountMapper gridSummarCountMapper;

    /**
     * 海宁查询绩效管户
     * 查询归属于该用户的客户，不考虑机构层级
     * @param custBaseInfoParam
     * @return
     */
    public List<CustBaseInfo> selectCustInfoList(CustBaseInfo custBaseInfoParam) {
        custBaseInfoParam.setCmpmUserName(SecurityUtils.getUsername());
        custBaseInfoParam.setDeptCode(SecurityUtils.getHeadId());

        //分页参数
        custBaseInfoParam.setStart((custBaseInfoParam.getNum() - 1) * custBaseInfoParam.getSize());
        custBaseInfoParam.setEnd(custBaseInfoParam.getNum() * custBaseInfoParam.getSize());

        StringBuilder metricStrBuilder = new StringBuilder();
        if (custBaseInfoParam.getContinuousParams() != null && custBaseInfoParam.getContinuousParams().size() > 0){
            for (ContinuousParam info : custBaseInfoParam.getContinuousParams()){
                if (metricStrBuilder.length() > 0) {
                    metricStrBuilder.append(",");
                }
                metricStrBuilder.append(gridSummarCountMapper.selectDictLabelByDictValue(info.getKey()));
            }
        }
        if (custBaseInfoParam.getDiscreteParams() != null && custBaseInfoParam.getDiscreteParams().size() > 0){
            for (DiscreteParam info:custBaseInfoParam.getDiscreteParams()){
                if (metricStrBuilder.length() > 0) {
                    metricStrBuilder.append(",");
                }
                metricStrBuilder.append(gridSummarCountMapper.selectDictLabelByDictValue(info.getKey()));
            }
        }
        List<CustBaseInfo> collect = gridCmpmMapper.selectCustInfoFromGridCmpm(custBaseInfoParam);

        List<String> custIds = collect.stream().map(CustBaseInfo::getCustId).collect(Collectors.toList());
        if (!custIds.isEmpty()){
            List<GridRelateVO> drawGridRelate = gridSelectMapper.getDrawGridRelate(custIds);
            List<GridRelateVO> regionGridRelate = gridSelectMapper.getRegionGridRelate(custIds);
            List<GridRelateVO> virtualGridRelate = myCustomerGridSelectService.getVirtualGridRelate(custIds);
            for (CustBaseInfo custBaseInfo : collect) {
                drawGridRelate.stream().filter(gridRelateVO -> gridRelateVO.getCustId().equals(custBaseInfo.getCustId())).findFirst().ifPresent(gridRelateVO -> {
                    custBaseInfo.setDrawBranchNames(gridRelateVO.getBranchNames());
                    custBaseInfo.setDrawOutletNames(gridRelateVO.getOutletNames());
                    custBaseInfo.setDrawUserNames(gridRelateVO.getUserNames());
                    custBaseInfo.setDrawGridName(gridRelateVO.getGridNames());
                });
                regionGridRelate.stream().filter(gridRelateVO -> gridRelateVO.getCustId().equals(custBaseInfo.getCustId())).findFirst().ifPresent(gridRelateVO -> {
                    custBaseInfo.setRegionBranchNames(gridRelateVO.getBranchNames());
                    custBaseInfo.setRegionOutletNames(gridRelateVO.getOutletNames());
                    custBaseInfo.setRegionUserNames(gridRelateVO.getUserNames());
                    custBaseInfo.setRegionTopGridName(gridRelateVO.getTopGridName());
                    custBaseInfo.setRegionSecGridName(gridRelateVO.getSecGridName());

                    custBaseInfo.setBelongBranchName(gridRelateVO.getBranchNames());
                    custBaseInfo.setBelongOutletName(gridRelateVO.getOutletNames());
                    custBaseInfo.setBelongUserNameList(gridRelateVO.getUserNames());
                });
                virtualGridRelate.stream().filter(gridRelateVO -> gridRelateVO.getCustId().equals(custBaseInfo.getCustId())).findFirst().ifPresent(gridRelateVO -> {
                    custBaseInfo.setVirtualBranchNames(gridRelateVO.getBranchNames());
                    custBaseInfo.setVirtualOutletNames(gridRelateVO.getOutletNames());
                    custBaseInfo.setVirtualUserNames(gridRelateVO.getUserNames());
                    custBaseInfo.setVirtualGridName(gridRelateVO.getGridNames());
                });
            }
        }
        return collect;
    }

    /**
     * 通用查询绩效管户关系，考虑机构层级
     * @param custBaseInfoParam
     * @return
     */
    public List<CustBaseInfo> queryCustInfoList(CustBaseInfo custBaseInfoParam) {
        custBaseInfoParam.setDeptCode(SecurityUtils.getHeadId());
        String userRole = SecurityUtils.userRole();
        if("branch".equalsIgnoreCase(userRole)){
            custBaseInfoParam.setCmpmBranchId(SecurityUtils.getDeptId());
        }else if("outlet".equalsIgnoreCase(userRole)){
            custBaseInfoParam.setCmpmOutletId(SecurityUtils.getDeptId());
        }else if ("manager".equalsIgnoreCase(userRole)){
            custBaseInfoParam.setCmpmUserName(SecurityUtils.getUsername());
        }
        //分页参数
        custBaseInfoParam.setStart((custBaseInfoParam.getNum() - 1) * custBaseInfoParam.getSize());
        custBaseInfoParam.setEnd(custBaseInfoParam.getNum() * custBaseInfoParam.getSize());

        StringBuilder metricStrBuilder = new StringBuilder();
        if (custBaseInfoParam.getContinuousParams() != null && custBaseInfoParam.getContinuousParams().size() > 0){
            for (ContinuousParam info : custBaseInfoParam.getContinuousParams()){
                if (metricStrBuilder.length() > 0) {
                    metricStrBuilder.append(",");
                }
                metricStrBuilder.append(gridSummarCountMapper.selectDictLabelByDictValue(info.getKey()));
            }
        }
        if (custBaseInfoParam.getDiscreteParams() != null && custBaseInfoParam.getDiscreteParams().size() > 0){
            for (DiscreteParam info:custBaseInfoParam.getDiscreteParams()){
                if (metricStrBuilder.length() > 0) {
                    metricStrBuilder.append(",");
                }
                metricStrBuilder.append(gridSummarCountMapper.selectDictLabelByDictValue(info.getKey()));
            }
        }
        List<CustBaseInfo> collect = gridCmpmMapper.queryCustInfoFromGridCmpm(custBaseInfoParam);

        List<String> custIds = collect.stream().map(CustBaseInfo::getCustId).collect(Collectors.toList());
        if (!custIds.isEmpty()){
            List<GridRelateVO> drawGridRelate = gridSelectMapper.getDrawGridRelate(custIds);
            List<GridRelateVO> regionGridRelate = gridSelectMapper.getRegionGridRelate(custIds);
            List<GridRelateVO> virtualGridRelate = myCustomerGridSelectService.getVirtualGridRelate(custIds);
            for (CustBaseInfo custBaseInfo : collect) {
                drawGridRelate.stream().filter(gridRelateVO -> gridRelateVO.getCustId().equals(custBaseInfo.getCustId())).findFirst().ifPresent(gridRelateVO -> {
                    custBaseInfo.setDrawBranchNames(gridRelateVO.getBranchNames());
                    custBaseInfo.setDrawOutletNames(gridRelateVO.getOutletNames());
                    custBaseInfo.setDrawUserNames(gridRelateVO.getUserNames());
                    custBaseInfo.setDrawGridName(gridRelateVO.getGridNames());
                });
                regionGridRelate.stream().filter(gridRelateVO -> gridRelateVO.getCustId().equals(custBaseInfo.getCustId())).findFirst().ifPresent(gridRelateVO -> {
                    custBaseInfo.setRegionBranchNames(gridRelateVO.getBranchNames());
                    custBaseInfo.setRegionOutletNames(gridRelateVO.getOutletNames());
                    custBaseInfo.setRegionUserNames(gridRelateVO.getUserNames());
                    custBaseInfo.setRegionTopGridName(gridRelateVO.getTopGridName());
                    custBaseInfo.setRegionSecGridName(gridRelateVO.getSecGridName());

                    custBaseInfo.setBelongBranchName(gridRelateVO.getBranchNames());
                    custBaseInfo.setBelongOutletName(gridRelateVO.getOutletNames());
                    custBaseInfo.setBelongUserNameList(gridRelateVO.getUserNames());
                });
                virtualGridRelate.stream().filter(gridRelateVO -> gridRelateVO.getCustId().equals(custBaseInfo.getCustId())).findFirst().ifPresent(gridRelateVO -> {
                    custBaseInfo.setVirtualBranchNames(gridRelateVO.getBranchNames());
                    custBaseInfo.setVirtualOutletNames(gridRelateVO.getOutletNames());
                    custBaseInfo.setVirtualUserNames(gridRelateVO.getUserNames());
                    custBaseInfo.setVirtualGridName(gridRelateVO.getGridNames());
                });
            }
        }
        return collect;
    }
}
