package com.ruoyi.ibs.cmpm.service;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ibs.cmpm.domain.dto.CustLevelDTO;
import com.ruoyi.ibs.cmpm.domain.dto.CustManagerDTO;
import com.ruoyi.ibs.cmpm.domain.vo.DwbRetailCustLevelManagerDetailVO;
import com.ruoyi.ibs.cmpm.mapper.GridCmpmMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @Author 吴凯程
 * @Date 2025/10/15
 **/
@Service
public class GridCmpmService {


    @Resource
    private GridCmpmMapper gridCmpmMapper;

    private final String CUST_LEVEL_KEY = "GRID_CMPM_CUST_LEVEL_KEY_";
    @Resource
    private RedisCache redisCache;

    public String getCustLevelByCust(CustLevelDTO custLevelDTO){
        if (custLevelDTO.getCustType().equals("0") || custLevelDTO.getCustType().equals("1")){
            custLevelDTO.setGridType("retail");
            return gridCmpmMapper.getCustLevel(custLevelDTO);
        }else if (Objects.nonNull(custLevelDTO.getAccountNo())){
            custLevelDTO.setGridType("corporate_account");
        }else {
            custLevelDTO.setGridType("corporate");
        }
        // todo 公司部未上
        return null;
    }

    public List<String> getAllCustLevel(String gridType){
        String custLevelKey = CUST_LEVEL_KEY + gridType + SecurityUtils.getHeadId();
        List<String> allCustLevel;
        if (redisCache.hasKey(custLevelKey)){
            allCustLevel = redisCache.getCacheObject(custLevelKey);
        }else{
            allCustLevel = gridCmpmMapper.getAllCustLevel(gridType);
            redisCache.setCacheObject(custLevelKey,allCustLevel, 1, TimeUnit.HOURS);
        }
        return allCustLevel;
    }

    public List<DwbRetailCustLevelManagerDetailVO> selectCustManagerList(CustManagerDTO custManagerDTO) {
        String userRole = SecurityUtils.userRole();
        if (userRole.equals("manager")){
            //客户经理查自己
            custManagerDTO.setManagerId(SecurityUtils.getUsername());
        }else if (userRole.equals("outlet")){
            //网点管理员查网点
            custManagerDTO.setOutletId(String.valueOf(SecurityUtils.getDeptId()));
        }else if (userRole.equals("branch")){
            //支行管理员查支行
            custManagerDTO.setBranchId(String.valueOf(SecurityUtils.getDeptId()));
        }
        //其他角色查全部
        return gridCmpmMapper.getCustManagerList(custManagerDTO);
    }

    public int custLevelCount(CustManagerDTO custManagerDTO) {
        String userRole = SecurityUtils.userRole();
        if (userRole.equals("manager")){
            //客户经理查自己
            custManagerDTO.setManagerId(SecurityUtils.getUsername());
        }else if (userRole.equals("outlet")){
            //网点管理员查网点
            custManagerDTO.setOutletId(String.valueOf(SecurityUtils.getDeptId()));
        }else if (userRole.equals("branch")){
            //支行管理员查支行
            custManagerDTO.setBranchId(String.valueOf(SecurityUtils.getDeptId()));
        }
        return gridCmpmMapper.getCustLevelCount(custManagerDTO);
    }

}
