package com.ruoyi.ibs.grid.service.impl;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.ibs.customerselect.mapper.CustInfoBusinessMapper;
import com.ruoyi.ibs.draw.mapper.DrawGridMapper;
import com.ruoyi.ibs.grid.domain.dto.*;
import com.ruoyi.ibs.grid.domain.dto.virtual.VirtualGridSelectInfo;
import com.ruoyi.ibs.grid.mapper.*;
import com.ruoyi.ibs.grid.service.IGridSelectService;
import com.ruoyi.ibs.grid.service.RegionGridUtilsService;
import com.ruoyi.ibs.list.mapper.CustInfoMerchantMapper;
import com.ruoyi.ibs.list.mapper.CustInfoRetailMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

import static com.ruoyi.common.utils.SecurityUtils.*;

/**
 * 网格查询 服务实现
 *
 * @author ruoyi
 */
@Service
public class GridSelectServiceImpl  implements IGridSelectService {
    private static Logger logger= LoggerFactory.getLogger(GridSelectServiceImpl.class);


    @Autowired
    private CustInfoRetailMapper custInfoRetailMapper;

    @Autowired
    private CustInfoMerchantMapper custInfoMerchantMapper;

    @Autowired
    private CustInfoBusinessMapper custInfoBusinessMapper;

    @Autowired
    private GridRegionGridMapper gridRegionGridMapper;

    @Resource
    private RegionGridMapper regionGridMapper;

    @Autowired
    private GridRegionUserRelateMapper gridRegionUserRelateMapper;

    @Autowired
    GridVirtualGridMapper gridVirtualGridMapper;

    @Autowired
    VirtualUserMapper gridVirtualUserMapper;

    @Autowired
    VirtualCustMapper gridVirtualCustomerMapper;

    @Autowired
    VirtualCustUserMapper gridVirtualCustUserMapper;

    @Autowired
    SysGridUserCustMapper sysGridUserCustMapper;

    @Autowired
    SysDeptMapper sysDeptMapper;

    @Resource
    private RegionGridUtilsService regionGridUtilsService;

    @Resource
    private DrawGridMapper drawGridMapper;
    /**
     * 查询获取总行下机构数量及归属机构
     *
     * @return 数量集合
     */
    @Override
    public GridDeptNumSelect getGridDeptNum(GridDeptNumSelect gridDeptNumSelect){
        String gridPattern = gridDeptNumSelect.getGridPattern();
        gridDeptNumSelect = new GridDeptNumSelect();
        Long UserDeptId = getUserDept();
        //根据用户权限不同以区分其能获取的信息
        if(userRole().equals("admin")||userRole().equals("head")||userRole().equals("branch")) {//各管理员根据其管理员层级查询
            logger.info("操作员为" + userRole());
            gridDeptNumSelect.setUserDeprId(getDeptId());
            gridDeptNumSelect.setUserDeptName(sysDeptMapper.selectDeptNameById(getDeptId()));
            gridDeptNumSelect.setOutletNum(sysDeptMapper.getDeptOutletById(UserDeptId));
            gridDeptNumSelect.setTopGridNum(gridRegionGridMapper.getTopGridNumByDeptId(UserDeptId));
            gridDeptNumSelect.setSecGridNum(gridRegionGridMapper.getSecGridNumByDeptId(UserDeptId));
            gridDeptNumSelect.setVirtuaGridNum(gridVirtualGridMapper.getVirtualGridNumByDeptId(UserDeptId));
            if(gridPattern.equals("1")) {
                gridDeptNumSelect.setRetailNum(custInfoRetailMapper.getRetailNumByDeptId(UserDeptId));
                gridDeptNumSelect.setRetailBusinessNum(custInfoMerchantMapper.getMerchantNumByDeptId(UserDeptId));
                gridDeptNumSelect.setBusinessNum(custInfoBusinessMapper.getBusinessNumByDeptId(UserDeptId));
            }else if(gridPattern.equals("3")){
                gridDeptNumSelect.setRetailNum(gridVirtualCustomerMapper.getCustNumByVitualDeptId(UserDeptId,"0"));
                gridDeptNumSelect.setRetailBusinessNum(gridVirtualCustomerMapper.getCustNumByVitualDeptId(UserDeptId,"1"));
                gridDeptNumSelect.setBusinessNum(gridVirtualCustomerMapper.getCustNumByVitualDeptId(UserDeptId,"2"));
            }
            if(userRole().equals("admin")||userRole().equals("head")){
                gridDeptNumSelect.setBranchNum(sysDeptMapper.getDeptBranchById(UserDeptId));
            }
        }else if(userRole().equals("manager")){//客户经理根据用户号查询
            logger.info("操作员为普通客户经理" + getUsername());
            String UserId = getUsername();
            gridDeptNumSelect.setTopGridNum(gridRegionGridMapper.getTopGridNumByUserId(UserId));
            gridDeptNumSelect.setSecGridNum(gridRegionGridMapper.getSecGridNumByUserId(UserId));
            gridDeptNumSelect.setVirtuaGridNum(gridVirtualGridMapper.getVirtualGridNumByUserId(UserId));
            if(gridPattern.equals("1")) {
                gridDeptNumSelect.setRetailNum(custInfoRetailMapper.getRetailNumByUserId(UserId));
                gridDeptNumSelect.setRetailBusinessNum(custInfoMerchantMapper.getMerchantNumByUserId(UserId));
                gridDeptNumSelect.setBusinessNum(custInfoBusinessMapper.getBusinessNumByUserId(UserId));

            }else if(gridPattern.equals("3")){
//                gridDeptNumSelect.setRetailNum(gridVirtualCustomerMapper.getVirtualCustNumByUserName(SecurityUtils.getUsername(),"0"));
//                gridDeptNumSelect.setRetailBusinessNum(gridVirtualCustomerMapper.getVirtualCustNumByUserName(SecurityUtils.getUsername(),"1"));
//                gridDeptNumSelect.setBusinessNum(gridVirtualCustomerMapper.getVirtualCustNumByUserName(SecurityUtils.getUsername(),"2"));
                gridDeptNumSelect.setRetailNum(gridVirtualCustomerMapper.getCustNumByVitualDeptId(UserDeptId, "0"));
                gridDeptNumSelect.setRetailBusinessNum(gridVirtualCustomerMapper.getCustNumByVitualDeptId(UserDeptId, "1"));
                gridDeptNumSelect.setBusinessNum(gridVirtualCustomerMapper.getCustNumByVitualDeptId(UserDeptId, "2"));
            }
        }else {
            logger.info("操作员为其他人员");
            throw new ServiceException("该操作员无法查看清单列表！");
        }
        return gridDeptNumSelect;
    }


    /**
     * 查询行政与特色网格信息
     *
     * @param gridSelectInfo 网格信息
     * @return 网格信息集合
     */
    @Override
    public List<GridSelectInfo> selectgridLevelInfoList(GridSelectInfo gridSelectInfo){
        List<GridSelectInfo> gridSelectInfoList = new ArrayList<>();
        //根据登入用户权限对机构进行处理
        gridSelectInfo.setUserDept(getUserDept());
        logger.info("操作员机构id为" + gridSelectInfo.getUserDept());
//该置灰代码原为需要对客户数、下级网格数做排序，因此将排序项写到sql中，但会使sql查询速度变的很慢，现在暂时弃用，后续使用中间表后可以考虑重用
//        if(gridSelectInfo.getGridLevel().equals("1")){
//            gridSelectInfoList = gridRegionGridMapper.selectTopGridList(gridSelectInfo);
//        }else if(gridSelectInfo.getGridLevel().equals("2")){
//            gridSelectInfoList = gridRegionGridMapper.selectSecGridList(gridSelectInfo);
//        }
//        for (int i = 0; i < gridSelectInfoList.size(); i++) {
//            List<String> headList= gridRegionUserRelateMapper.selectHeadDeptExist(gridSelectInfoList.get(i).getGridId());
//            if(headList.isEmpty()){//校验该一级网格是分配给支行还是总行以区分操作权限
//                gridSelectInfoList.get(i).setRelationDeptList(gridRegionUserRelateMapper.selectGridRegionDeptByGridId(gridSelectInfoList.get(i).getGridId()));//获取一级网格关联支行
//            }else{
//                String headBank = headList.get(0) + "(总行）";
//                headList.set(0,headBank);
//                gridSelectInfoList.get(i).setRelationDeptList(headList);
//            }
//            if(gridSelectInfo.getGridLevel().equals("2")) {//二级网格部分字段逻辑
//                gridSelectInfoList.get(i).setTopGridName(gridRegionGridMapper.selectGridName(gridSelectInfoList.get(i).getTopGridId()));
//                gridSelectInfoList.get(i).setRelationOutletList(gridRegionUserRelateMapper.selectGridRegionDeptTypeByGridId(gridSelectInfoList.get(i).getGridId(),getUserDept()));//获取二级网格关联网点
//                gridSelectInfoList.get(i).setRelationManagerList(gridRegionUserRelateMapper.selectGridRegionUserByGridId(gridSelectInfoList.get(i).getGridId(),getUserDept()));//获取二级网格关联客户经理
//            }else{//一级网格部分字段逻辑
//                gridSelectInfoList.get(i).setTopGridId(gridSelectInfoList.get(i).getGridId());
//                gridSelectInfoList.get(i).setTopGridName(gridSelectInfoList.get(i).getSecGridName());
//                gridSelectInfoList.get(i).setSecGridName(gridRegionGridMapper.getSecGridName(gridSelectInfoList.get(i).getGridId(),getUserDept()));//获取一级网格下属于自己机构的二级网格名称
//            }
//            //用户权限判断
//            if(hasRole("headAdmin") && gridSelectInfoList.get(i).getGridLevel().equals("1")){//总行可以编辑与删除所有一级网格
//                gridSelectInfoList.get(i).setIsEdit("1");
//                gridSelectInfoList.get(i).setIsDel("1");
//            }
//            if(hasRole("headAdmin") && gridSelectInfoList.get(i).getGridLevel().equals("1") && !headList.isEmpty()){//总行对分配给自己的一级网格有创建二级的权限
//                gridSelectInfoList.get(i).setIsAddSec("1");
//            }
//            if(hasRole("branchAdmin") && gridSelectInfoList.get(i).getGridLevel().equals("1") && headList.isEmpty()){//支行对分配给自己的一级网格有创建二级的权限
//                gridSelectInfoList.get(i).setIsEditSec("1");
//            }
//            if(hasRole("headAdmin") && gridSelectInfoList.get(i).getGridLevel().equals("2") && !headList.isEmpty()){//总行对分配给自己的二级网格鱼油编辑与删除的权限
//                gridSelectInfoList.get(i).setIsEditSec("1");
//                gridSelectInfoList.get(i).setIsDel("1");
//            }
//            if(hasRole("branchAdmin") && gridSelectInfoList.get(i).getGridLevel().equals("2") && headList.isEmpty()){//支行对自己创建的二级网格有编辑与删除权限
//                gridSelectInfoList.get(i).setIsEditSec("1");
//                gridSelectInfoList.get(i).setIsDel("1");
//            }
//        }
        if(gridSelectInfo.getGridLevel().equals("1")) {//因一二级网格查询需要字段不同,根据网格级别分别查询网格
            gridSelectInfoList = gridRegionGridMapper.selectTopGrid(gridSelectInfo);
        }else if(gridSelectInfo.getGridLevel().equals("2")){
            gridSelectInfoList = gridRegionGridMapper.selectSecGrid(gridSelectInfo);
        }
        for (int i = 0; i < gridSelectInfoList.size(); i++) {

            List<Long> gridCustCount = regionGridUtilsService.getGridCustCount(gridSelectInfoList.get(i).getGridId());
            gridSelectInfoList.get(i).setCustNum(gridCustCount.get(0));//获取个人客户数
            gridSelectInfoList.get(i).setPosNum(gridCustCount.get(1));//获取商户客户数
            gridSelectInfoList.get(i).setBusiNum(gridCustCount.get(2));//获取企业客户数
//            gridSelectInfoList.get(i).setCustNum(custInfoRetailMapper.selectCustNumByAdmin(gridSelectInfoList.get(i).getGridId(),getUserDept()));//获取个人客户数
//            gridSelectInfoList.get(i).setPosNum(custInfoMerchantMapper.selectPosNumByAdmin(gridSelectInfoList.get(i).getGridId(),getUserDept()));//获取商户客户数
//            gridSelectInfoList.get(i).setBusiNum(custInfoBusinessMapper.selectBusinessNumByAdmin(gridSelectInfoList.get(i).getGridId(),getUserDept()));//获取企业客户数
            List<String> headList= gridRegionUserRelateMapper.selectHeadDeptExist(gridSelectInfoList.get(i).getGridId());
            if(headList.isEmpty()){//校验该一级网格是分配给支行还是总行以区分操作权限
                gridSelectInfoList.get(i).setRelationDeptList(gridRegionUserRelateMapper.selectGridRegionDeptByGridId(gridSelectInfoList.get(i).getGridId()));//获取一级网格关联支行
            }else{
                String headBank = headList.get(0) + "(总行)";
                headList.set(0,headBank);
                gridSelectInfoList.get(i).setRelationDeptList(headList);
            }
           if(gridSelectInfo.getGridLevel().equals("2")) {//二级网格部分字段逻辑
               gridSelectInfoList.get(i).setTopGridName(gridRegionGridMapper.selectGridName(gridSelectInfoList.get(i).getTopGridId()));
               gridSelectInfoList.get(i).setRelationOutletList(gridRegionUserRelateMapper.selectGridRegionDeptTypeByGridId(gridSelectInfoList.get(i).getGridId(),getUserDept()));//获取二级网格关联网点
               gridSelectInfoList.get(i).setRelationManagerList(gridRegionUserRelateMapper.selectGridRegionUserByGridId(gridSelectInfoList.get(i).getGridId(),getUserDept()));//获取二级网格关联客户经理
           }else{//一级网格部分字段逻辑
               gridSelectInfoList.get(i).setTopGridId(gridSelectInfoList.get(i).getGridId());
               gridSelectInfoList.get(i).setTopGridName(gridSelectInfoList.get(i).getSecGridName());
               gridSelectInfoList.get(i).setSecGridName(gridRegionGridMapper.getSecGridName(gridSelectInfoList.get(i).getGridId(),getUserDept()));//获取一级网格下属于自己机构的二级网格名称
           }
            //用户权限判断
            if(hasRole("headAdmin") && gridSelectInfoList.get(i).getGridLevel().equals("1")){//总行可以编辑与删除所有一级网格
                gridSelectInfoList.get(i).setIsEdit("1");
                gridSelectInfoList.get(i).setIsDel("1");
            }
            if(hasRole("headAdmin") && gridSelectInfoList.get(i).getGridLevel().equals("1") && !headList.isEmpty()){//总行对分配给自己的一级网格有创建二级的权限
                gridSelectInfoList.get(i).setIsAddSec("1");
            }
            if(hasRole("branchAdmin") && gridSelectInfoList.get(i).getGridLevel().equals("1") && headList.isEmpty()){//支行对分配给自己的一级网格有创建二级的权限
                gridSelectInfoList.get(i).setIsEditSec("1");
                gridSelectInfoList.get(i).setIsAddSec("1");
            }
            if(hasRole("headAdmin") && gridSelectInfoList.get(i).getGridLevel().equals("2") && !headList.isEmpty()){//总行对分配给自己的二级网格鱼油编辑与删除的权限
                gridSelectInfoList.get(i).setIsEditSec("1");
                gridSelectInfoList.get(i).setIsDel("1");
            }
            if(hasRole("branchAdmin") && gridSelectInfoList.get(i).getGridLevel().equals("2") && headList.isEmpty()){//支行对自己创建的二级网格有编辑与删除权限
                gridSelectInfoList.get(i).setIsEditSec("1");
                gridSelectInfoList.get(i).setIsDel("1");
            }
        }
        return gridSelectInfoList;
    }


    /**
     * 查询虚拟网格信息
     *
     * @param virtualGridSelectInfo 网格信息
     * @return 网格信息集合
     */
    @Override
    public List<VirtualGridSelectInfo> selectVirtualInfoList(VirtualGridSelectInfo virtualGridSelectInfo) {
        List<VirtualGridSelectInfo> virtualGridInfoList = new ArrayList<>();
        logger.info("操作员总行机构id为" + getUserDept());
        //根据登入用户权限对机构进行处理
        Long userDept = getUserDept();
        virtualGridSelectInfo.setUserDept(userDept);
        virtualGridInfoList = gridVirtualGridMapper.selectVirtualGridList(virtualGridSelectInfo);
        logger.info("查出共" + virtualGridInfoList.size() + "个虚拟网格");
        for(int i=0; i<virtualGridInfoList.size(); i++){
            StringBuilder CustTypeDes = new StringBuilder();
            //对三种客户类型分别统计客户数
            if(gridVirtualGridMapper.getVirtualType(virtualGridInfoList.get(i).getGridId(),"1") != null){
                CustTypeDes.append("个人 ");
                virtualGridInfoList.get(i).setCustNum(gridVirtualCustomerMapper.getCustNumByGridId(virtualGridInfoList.get(i).getGridId(),"0"));
            }
            if(gridVirtualGridMapper.getVirtualType(virtualGridInfoList.get(i).getGridId(),"2") != null){
                CustTypeDes.append("商户 ");
                virtualGridInfoList.get(i).setPosNum(gridVirtualCustomerMapper.getCustNumByGridId(virtualGridInfoList.get(i).getGridId(),"1"));
            }
            if(gridVirtualGridMapper.getVirtualType(virtualGridInfoList.get(i).getGridId(),"3") != null){
                CustTypeDes.append("企业 ");
                virtualGridInfoList.get(i).setBusiNum(gridVirtualCustomerMapper.getCustNumByGridId(virtualGridInfoList.get(i).getGridId(),"2"));
            }
            virtualGridInfoList.get(i).setCustTypeDes(CustTypeDes.toString());
            virtualGridInfoList.get(i).setRelationDept(gridVirtualUserMapper.selectDeptNameByGridId(virtualGridInfoList.get(i).getGridId(),"branch"));//获取虚拟网格关联支行
            virtualGridInfoList.get(i).setRelationOutlet(gridVirtualUserMapper.selectDeptNameByGridId(virtualGridInfoList.get(i).getGridId(),"outlet"));//获取虚拟网格关联网点
            virtualGridInfoList.get(i).setCustManager(gridVirtualUserMapper.selectCustManagerByGridId(virtualGridInfoList.get(i).getGridId()));//获取虚拟网格关联经理
            if(virtualGridInfoList.get(i).getCreateRole().equals("head")){//对创建层级进行转义,0-总行；1-支行
                virtualGridInfoList.get(i).setCreateRole("0");
            }else{
                virtualGridInfoList.get(i).setCreateRole("1");
            }
            //用户权限判断
            if((virtualGridInfoList.get(i).getCreateRole().equals("0") && hasRole("headAdmin")) || (virtualGridInfoList.get(i).getCreateRole().equals("1") && hasRole("branchAdmin"))){//只有网格创建层级与当前用户相同时才可进行编辑和删除
                virtualGridInfoList.get(i).setIsEdit("1");
                virtualGridInfoList.get(i).setIsDel("1");
            }
        }
        return virtualGridInfoList;
    }


    /**
     * 管户列表查询
     *
     * @param custManageInfo 管户信息
     * @return 网格信息集合
     */
    @Override
    public List<CustManageInfo> custManageSelect(CustManageInfo custManageInfo) {
        List<CustManageInfo> custManageInfoList = new ArrayList<>();
        String gridPattern = custManageInfo.getGridPattern();
        String custPattern = custManageInfo.getCustPattern();
        String gridLevel = custManageInfo.getGridLevel();
        Long gridId = custManageInfo.getGridId();
        String gridName = custManageInfo.getGridName();
        custManageInfo.setUserDept(getUserDept());
        if(gridPattern.equals("1")){//行政网格获取管护列表
            custManageInfo.setGridPattern("XZ");
            //企业商会个人要从各自信息表中获取数据
            if(custPattern == null){
                custManageInfoList = gridRegionUserRelateMapper.selectCustInfoByManage(custManageInfo);
            }else if(custPattern.equals("0")){
                custManageInfoList = custInfoRetailMapper.selectRetailInfoByManage(custManageInfo);
            }else if(custPattern.equals("1")){
                custManageInfoList = custInfoMerchantMapper.selectMerchantInfoByManage(custManageInfo);
            }else if(custPattern.equals("2")){
                custManageInfoList = custInfoBusinessMapper.selectBusinessInfoByManage(custManageInfo);
            }
            for(int i=0; i<custManageInfoList.size(); i++){
                custManageInfoList.get(i).setGridPattern("1");
                if(gridLevel.equals("1")){//一二级网格接入管护界面时因传参不同，查询一二级网格名称逻辑不同
                    custManageInfoList.get(i).setTopGridName(gridName);
                    custManageInfoList.get(i).setTopGridDutyType(gridRegionGridMapper.selectGridDutyType(gridId));
                    custManageInfoList.get(i).setSecGridName(gridRegionGridMapper.getSecGridNameByRegionCode(custManageInfoList.get(i).getRegionCode()));
                    custManageInfoList.get(i).setSecGridDutyType(gridRegionGridMapper.getSecGridDutyTypeByRegionCode(custManageInfoList.get(i).getRegionCode()));
                }else{
                    custManageInfoList.get(i).setTopGridName(gridRegionGridMapper.getParentGridName(gridId));
                    custManageInfoList.get(i).setTopGridDutyType(gridRegionGridMapper.getParentGridDutyType(gridId));
                    custManageInfoList.get(i).setSecGridName(gridName);
                    custManageInfoList.get(i).setSecGridDutyType(gridRegionGridMapper.selectGridDutyType(gridId));
                }
                List<Long> deptListByRegionCode = gridRegionGridMapper.getDeptListByRegionCode(custManageInfoList.get(i).getRegionCode());
                if (!deptListByRegionCode.isEmpty()) {
                    List<String> branch = new ArrayList<>();
                    List<String> outlet = new ArrayList<>();
                    for (Long deptId : deptListByRegionCode) {
                        SysDept sysDept = sysDeptMapper.selectDeptById(deptId);
                        if (sysDept.getDeptType().equals("branch") || sysDept.getDeptType().equals("head")) {
                            branch.add(sysDept.getDeptName());
                        } else if (sysDept.getDeptType().equals("outlet")) {
                            branch.add(sysDept.getParentName());
                            outlet.add(sysDept.getDeptName());
                        }
                    }
                    if (!branch.isEmpty()) {
                        String collect = branch.stream().collect(Collectors.joining(","));
                        custManageInfoList.get(i).setBelongBranchName(collect);
                    }
                    if (!outlet.isEmpty()) {
                        String collect = outlet.stream().collect(Collectors.joining(","));
                        custManageInfoList.get(i).setBelongOutletName(collect);
                    }
                }
                List<String> userListByRegionCode = gridRegionGridMapper.getUserListByRegionCode(custManageInfoList.get(i).getRegionCode());
                if (!userListByRegionCode.isEmpty()) {
                    String collect = userListByRegionCode.stream().collect(Collectors.joining(","));
                    custManageInfoList.get(i).setBelongUserName(collect);
                }

//                custManageInfoList.get(i).setBelongBranchName(gridRegionUserRelateMapper.selectGridRegionDeptByUserId(custManageInfoList.get(i).getBelongUserId(),gridId,"branch"));
//                custManageInfoList.get(i).setBelongOutletName(gridRegionUserRelateMapper.selectGridRegionDeptByUserId(custManageInfoList.get(i).getBelongUserId(),gridId,"outlet"));
            }
        }else if(gridPattern.equals("2")){
            custManageInfo.setGridPattern("TS");
        }else if(gridPattern.equals("3")){
            custManageInfo.setGridPattern("XN");
            custManageInfoList = gridVirtualCustUserMapper.selectVirtualCustInfoByManage(custManageInfo);
            for(int i=0; i<custManageInfoList.size(); i++){
                custManageInfoList.get(i).setGridPattern("3");
                if(custManageInfoList.get(i).getCreateRole().equals("head")){//对创建层级进行转义,0-总行；1-支行
                    custManageInfoList.get(i).setCreateRole("0");
                }else{
                    custManageInfoList.get(i).setCreateRole("1");
                }
            }
        }
        return custManageInfoList;
    }

    /**
     * 管户批量分配删除
     *
     * @param custManageUpDTO  网格信息
     * @return 网格信息集合
     */
    @Override
    public int custManageListUpdate(CustManageUpDTO custManageUpDTO){
        int result = 0;
        if(custManageUpDTO.getCustIds().length == 0){
            throw new ServiceException("请选择需要分配或解除的客户！");
        }
        logger.info("需要操作" + custManageUpDTO.getCustIds().length + "笔");
        String gridPattern = null;
        if(custManageUpDTO.getGridPattern().equals("1")){//管护列表网格类型转义
            gridPattern = "XZ";
        }else if(custManageUpDTO.getGridPattern().equals("2")){
            gridPattern = "TS";
        }else if(custManageUpDTO.getGridPattern().equals("3")){
            gridPattern = "XN";
        }
        int length = custManageUpDTO.getCustIds().length;
        for(int i=0; i<length; i++){
            result = sysGridUserCustMapper.updateRetailManage(custManageUpDTO.getOperationType() ,gridPattern ,custManageUpDTO.getCustIds()[i] ,custManageUpDTO.getBelongUserIds()[i]);
        }
        logger.info("操作结果为" + result);
        return result;
    }

    /**
     * 管护列表解除前查询
     *
     * @param gridId 管户信息
     * @return 网格信息集合
     */
    @Override
    public List<CustManageSelectDTO> selectCustManage(String gridId ,String gridPattern ,String[] custIds,String userName) {
        List<CustManageSelectDTO> custManageSelectDTOList = new ArrayList<>();
        if(custIds.length == 0){
            throw new ServiceException("请选择需要分配或解除的客户！");
        }
        custManageSelectDTOList = sysGridUserCustMapper.selectManageInfoByCust(gridId,gridPattern,custIds,userName);
        for(int i=0; i<custManageSelectDTOList.size(); i++){
            if (custManageSelectDTOList.get(i).getCustType().equals("0")) {//根据客户类型去客户对应信息表里查证件号或社会统一编码
                custManageSelectDTOList.get(i).setCustIdc(custInfoBusinessMapper.getBusiIdcByCustId(custManageSelectDTOList.get(i).getCustId()));
            } else if (custManageSelectDTOList.get(i).getCustType().equals("1")) {
                custManageSelectDTOList.get(i).setCustIdc(custInfoMerchantMapper.getMerchantIdcByCustId(custManageSelectDTOList.get(i).getCustId()));
            } else {
                custManageSelectDTOList.get(i).setCustIdc(custInfoRetailMapper.getRetailIdcByCustId(custManageSelectDTOList.get(i).getCustId()));
            }
            custManageSelectDTOList.get(i).setBranchName(sysDeptMapper.selectDeptNameById(Long.parseLong(custManageSelectDTOList.get(i).getBranchId())));
            custManageSelectDTOList.get(i).setOutletName(sysDeptMapper.selectDeptNameById(Long.parseLong(custManageSelectDTOList.get(i).getOutletId())));
        }
        return custManageSelectDTOList;
    }


    /**
     * 角色权限判断机构
     *
     * @param
     * @return 用户角色 ：admin、head、branch、manager
     */
    public static Long getUserDept()
    {
        Long userDept = getDeptId();
        if (hasRole(Constants.SUPER_ADMIN)) {
            userDept = null;
        }else if (hasRole("headAdmin")||hasRole("branchAdmin")) {
            userDept = getDeptId();
        }else if(hasRole("manager")){
            userDept = Long.parseLong(getUsername());
        }
        return userDept;
    }
}
