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

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ibs.customerselect.mapper.CustInfoBusinessMapper;
import com.ruoyi.ibs.draw.domain.dto.grid.CustCountDTO;
import com.ruoyi.ibs.draw.mapper.DrawGridMapper;
import com.ruoyi.ibs.grid.domain.vo.GridInfoVO;
import com.ruoyi.ibs.grid.mapper.*;
import com.ruoyi.ibs.grid.service.GridInfoService;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

import static com.ruoyi.common.utils.SecurityUtils.getDeptId;
import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * @Author 吴凯程
 * @Date 2025/2/6
 **/
@Service
public class GridInfoServiceImpl implements GridInfoService {

    @Autowired
    VirtualUserMapper gridVirtualUserMapper;
    @Autowired
    VirtualCustMapper gridVirtualCustomerMapper;
    @Autowired
    VirtualCustUserMapper gridVirtualCustUserMapper;
    @Autowired
    SysGridUserCustMapper sysGridUserCustMapper;
    @Autowired
    SysDeptMapper sysDeptMapper;
    @Autowired
    private CustInfoRetailMapper custInfoRetailMapper;
    @Autowired
    private CustInfoMerchantMapper custInfoMerchantMapper;
    @Autowired
    private CustInfoBusinessMapper custInfoBusinessMapper;
    @Resource
    private RegionGridMapper regionGridMapper;
    @Resource
    private VirtualGridMapper virtualGridMapper;
    @Resource
    private RegionGridUtilsService regionGridUtilsService;

    @Resource
    private DrawGridMapper drawGridMapper;

    /**
     * @param tabGridType
     * @return
     */
    @Override
    public GridInfoVO getGirdInfoVo(String tabGridType) {
        String opsDept = null;
        if (SecurityUtils.isHead()) {
            opsDept = SecurityUtils.getOpsDept();
        }
        GridInfoVO gridInfoVO = new GridInfoVO();
        SysDept sysDept = sysDeptMapper.selectDeptById(getDeptId());
        gridInfoVO.setDeptName(sysDept.getDeptName());
        gridInfoVO.setOutletNum(sysDeptMapper.getDeptOutletById(getDeptId()));
        if (!sysDept.getDeptType().equals("outlet")) {
            gridInfoVO.setBranchNum(sysDeptMapper.getDeptBranchById(getDeptId()));
        }
        if (tabGridType.equals("1")) {
            gridInfoVO.setTopGridNum(regionGridMapper.getGridCountByDeptId(getDeptId(), opsDept, "1"));
            gridInfoVO.setSecGridNum(regionGridMapper.getGridCountByDeptId(getDeptId(), opsDept, "2"));
            CustCountDTO custByDeptId = regionGridUtilsService.getCustByDeptId(getDeptId(), opsDept);
            if (Objects.nonNull(opsDept) && opsDept.equals("0")) {
                gridInfoVO.setRetailNum(custByDeptId.getRetailCount());
                gridInfoVO.setMerchantNum(custByDeptId.getMerchantCount());
                gridInfoVO.setBusinessNum(0l);
            } else if (Objects.nonNull(opsDept) && opsDept.equals("1")) {
                gridInfoVO.setRetailNum(0l);
                gridInfoVO.setMerchantNum(0l);
                gridInfoVO.setBusinessNum(custByDeptId.getBusinessCount());
            } else {
                gridInfoVO.setRetailNum(custByDeptId.getRetailCount());
                gridInfoVO.setMerchantNum(custByDeptId.getMerchantCount());
                gridInfoVO.setBusinessNum(custByDeptId.getBusinessCount());
            }

        } else if (tabGridType.equals("2")) {
            gridInfoVO.setDrawGridNum(drawGridMapper.getGridCount(getDeptId(),opsDept));
            gridInfoVO.setRetailNum(drawGridMapper.getCustCount(getDeptId(), "0",opsDept));
            gridInfoVO.setMerchantNum(drawGridMapper.getCustCount(getDeptId(), "1",opsDept));
            gridInfoVO.setBusinessNum(drawGridMapper.getCustCount(getDeptId(), "2",opsDept));
        } else if (tabGridType.equals("3")) {
            gridInfoVO.setVirtualGridNum(virtualGridMapper.getVirtualGridNumByDeptId(getDeptId(), opsDept));
            gridInfoVO.setRetailNum(gridVirtualCustomerMapper.getCustNumByDeptId(getDeptId(), opsDept, "0"));
            gridInfoVO.setMerchantNum(gridVirtualCustomerMapper.getCustNumByDeptId(getDeptId(), opsDept, "1"));
            gridInfoVO.setBusinessNum(gridVirtualCustomerMapper.getCustNumByDeptId(getDeptId(), opsDept, "2"));
        }
        return gridInfoVO;
    }


    /**
     * @param tabGridType
     * @return
     */
    @Override
    public GridInfoVO getGirdInfoVoByManager(String tabGridType) {
        GridInfoVO gridInfoVO = new GridInfoVO();
        String username = getUsername();
        if (tabGridType.equals("1")) {
            gridInfoVO.setSecGridNum(regionGridMapper.getSecGridCountByUserName(username));
            gridInfoVO.setRetailNum(custInfoRetailMapper.getRetailNumByUserId(username));
            gridInfoVO.setMerchantNum(custInfoMerchantMapper.getMerchantNumByUserId(username));
            gridInfoVO.setBusinessNum(custInfoBusinessMapper.getBusinessNumByUserId(username));
        } else if (tabGridType.equals("2")) {
            gridInfoVO.setDrawGridNum(drawGridMapper.getGridCountByManager(username));
            gridInfoVO.setRetailNum(drawGridMapper.getCustCountByManager(username, "0"));
            gridInfoVO.setMerchantNum(drawGridMapper.getCustCountByManager(username, "1"));
            gridInfoVO.setBusinessNum(drawGridMapper.getCustCountByManager(username, "2"));
        } else if (tabGridType.equals("3")) {
            gridInfoVO.setVirtualGridNum(virtualGridMapper.getVirtualGridNumByUserId(username));
            gridInfoVO.setRetailNum(gridVirtualCustomerMapper.getVirtualCustNumByUserName(username, "0"));
            gridInfoVO.setMerchantNum(gridVirtualCustomerMapper.getVirtualCustNumByUserName(username, "1"));
            gridInfoVO.setBusinessNum(gridVirtualCustomerMapper.getVirtualCustNumByUserName(username, "2"));
        }
        return gridInfoVO;
    }
}
