package com.ruoyi.ibs.customerselect.service.Impl;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ibs.customerselect.domain.vo.GridRelateVO;
import com.ruoyi.ibs.customerselect.domain.vo.GridSelectVO;
import com.ruoyi.ibs.customerselect.mapper.GridSelectMapper;
import com.ruoyi.ibs.customerselect.service.MyCustomerGridSelectService;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.springframework.stereotype.Service;

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

/**
 * @Author 吴凯程
 * @Date 2025/5/8
 **/
@Service
public class MyCustomerGridSelectServiceImpl implements MyCustomerGridSelectService {

    @Resource
    private GridSelectMapper gridSelectMapper;

    @Resource
    private SysDeptMapper sysDeptMapper;

    /**
     * @param gridLevel
     * @return
     */
    @Override
    public List<GridSelectVO> getRegionGridList(String gridLevel, String opsDept) {
        if (SecurityUtils.userRole().equals("manager")){
            return gridSelectMapper.getRegionGridList(null, SecurityUtils.getUsername(), gridLevel, opsDept);
        }
        return gridSelectMapper.getRegionGridList(SecurityUtils.getDeptId(), null, gridLevel, opsDept);
    }

    /**
     * @return
     */
    @Override
    public List<GridSelectVO> getVirtualGridList(String opsDept) {
        if (SecurityUtils.userRole().equals("manager")){
            return gridSelectMapper.getVirtualGridList(null, SecurityUtils.getUsername(), opsDept);
        }
        return gridSelectMapper.getVirtualGridList(SecurityUtils.getDeptId(), null, opsDept);
    }

    /**
     * @return
     */
    @Override
    public List<GridSelectVO> getDrawGridList(String opsDept) {
        if (SecurityUtils.userRole().equals("manager")){
            return gridSelectMapper.getDrawGridList(null, SecurityUtils.getUsername(), opsDept);
        }
        return gridSelectMapper.getDrawGridList(SecurityUtils.getDeptId(), null, opsDept);
    }

    /**
     * @param custIds 
     * @return
     */
    @Override
    public List<GridRelateVO> getVirtualGridRelate(List<String> custIds) {
        List<GridRelateVO> virtualGridRelate = new ArrayList<>();
        if (SecurityUtils.userRole().equals("manager")){
            virtualGridRelate = gridSelectMapper.getVirtualGridRelate(custIds, SecurityUtils.getUsername());
        }else{
            virtualGridRelate = gridSelectMapper.getVirtualGridRelate(custIds, null);
        }
        for (GridRelateVO gridRelateVO : virtualGridRelate) {
            if(Objects.isNull(gridRelateVO.getDeptIds())){
                continue;
            }
            String[] deptIds = gridRelateVO.getDeptIds().split(",");
            for(String deptId : deptIds) {
                List<String> branchList = new ArrayList<>();
                List<String> outletsList = new ArrayList<>();
                SysDept sysDept = sysDeptMapper.selectDeptById(Long.valueOf(deptId));
                if (sysDept.getDeptType().equals("branch")) {
                    branchList.add(sysDept.getDeptName());
                } else if (sysDept.getDeptType().equals("outlet")) {
                    branchList.add(sysDept.getParentName());
                    outletsList.add(sysDept.getDeptName());
                }
                if (branchList.size() > 0){
                    gridRelateVO.setBranchNames(branchList.stream().collect(Collectors.joining(",")));
                }
                if (outletsList.size() > 0){
                    gridRelateVO.setOutletNames(outletsList.stream().collect(Collectors.joining(",")));
                }
            }
        }
        return virtualGridRelate;
    }
}
