package com.ruoyi.web.project.service.impl;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.DeptTypeEnum;
import com.ruoyi.common.enums.SysRoleEnum;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.web.project.mapper.WxProjectInfoMapper;
import com.ruoyi.web.project.service.WxIProjectInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wxw
 * @version : V1.0
 * @ClassName: ProjectInfoServiceImpl
 * @Description: TODO
 * @Auther: Makalu
 * @Date: 2020/7/7 16:45
 */
@Service
public class WxProjectInfoServiceImpl implements WxIProjectInfoService {

    @Autowired
    private WxProjectInfoMapper ProjectInfoMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public Map<String, Object> selectDeptSubordinate(Map<String,Object> param) {
        List<Map<String,Object>> deptList = ProjectInfoMapper.getDeptSubordinate(param);
        Map<String,Object> dataMap = new HashMap<>(16);
        for(Map<String,Object> map:deptList){
            map.put("userId",param.get("userId"));
            dataMap.put(map.get("text").toString(),ProjectInfoMapper.getQUDeptSubordinate(map));
        }
        return dataMap;
    }

    @Override
    public List<Map<String, Object>> getProjectByDeptIdAndCompanyId(Map<String, Object> map) {
        List<Map<String,Object>> list = new ArrayList<>();
        String nowRole = null;
        if(map.get("minRoleId")!=null && !"undefined".equals(map.get("minRoleId"))){
            nowRole = map.get("minRoleId").toString();
        }else{
            nowRole = Convert.toStr(sysRoleMapper.getUserMinRoleId(Convert.toLong(map.get("userId"))));
        }
        if(SysRoleEnum.ZGS.getCode().equals(nowRole)){
            Long deptId = this.getZgsDept(Convert.toLong(map.get("deptId"))).getDeptId();
            map.put("nowDeptId",Convert.toStr(deptId));
        }else{
            map.put("nowDeptId",Convert.toStr(map.get("deptId")));
        }
        //当前角色时管理员时，修改角色为集团公司
        if(Convert.toInt(SysRoleEnum.JTGS.getCode())>Convert.toInt(nowRole)){
            nowRole = SysRoleEnum.JTGS.getCode();
        }
        map.put("nowRole",nowRole);
        List<Map<String, Object>> getProjec  = ProjectInfoMapper.getProjectByDeptIdAndCompanyId(map);
        for(int i=0;i<getProjec.size();i++){
            //视频接入数
            String videoNum = "0";
            //结构体监测点位数
            Integer monitoringCount = 0;
            //结构体监测预警个数
            Integer monitoringErrorCount = 0;
            getProjec.get(i).put("id",i);
            getProjec.get(i).put("videoNum",StringUtils.isNotBlank(videoNum)?videoNum:"0");
            getProjec.get(i).put("monitoringCount",monitoringCount);
            getProjec.get(i).put("projectCount",Integer.valueOf(getProjec.get(i).get("projectCount").toString())+monitoringErrorCount);
            list.add(getProjec.get(i));
        }
        return list;
    }

    @Override
    public Map<String, Object> getProjectBasicInfo(String id) {
        //根据项目的id查基本信息
        return ProjectInfoMapper.selectProjectOverview(id);
    }

    @Override
    public List<Map<String, Object>> getProjectUnitList(String projectId) {
        return ProjectInfoMapper.getProjectUnitList(projectId);
    }

    /**
     * 查询子公司部门ID
     *
     * @param deptId 部门ID
     * @return 结果
     */
    private SysDept getZgsDept(Long deptId){
        SysDept sysDept = sysDeptMapper.selectDeptById(deptId);
        if(DeptTypeEnum.ZGS.getCode().equals(sysDept.getTypeFlag())){
            //comFlag标识为1是子公司
            if(DeptTypeEnum.COMFLAG.getCode().equals(sysDept.getComFlag())){
                return sysDept;
            }else{
                return getZgsDept(sysDept.getParentId());
            }
        }else{
            return sysDept;
        }
    }
}
