package com.usky.fire.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.core.util.Arith;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.fire.domain.*;
import com.usky.fire.mapper.BscEnterpriseStreetTownMapper;
import com.usky.fire.service.*;
import com.usky.fire.service.util.OnlineMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * <p>
 * 企业自主管理大屏统计（街镇） 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-10-09
 */
@Service
public class BscEnterpriseStreetTownServiceImpl extends AbstractCrudService<BscEnterpriseStreetTownMapper, BscEnterpriseStreetTown> implements BscEnterpriseStreetTownService {

    @Autowired
    private BaseCompanyService baseCompanyService;

    @Autowired
    private DemBuildScoreService demBuildScoreService;

    @Autowired
    private DemBuildScoreRecordService demBuildScoreRecordService;

    @Autowired
    private BaseDeviceService baseDeviceService;

    @Autowired
    private BaseDeviceStatusService baseDeviceStatusService;

    @Autowired
    private PatrolInspectionAreaService patrolInspectionAreaService;

    @Autowired
    private PatrolInspectionPlanService patrolInspectionPlanService;

    @Autowired
    private PatrolInspectionPlanSonService patrolInspectionPlanSonService;

    @Autowired
    private PatrolInspectionPlanSiteSonService patrolInspectionPlanSiteSonService;

    @Autowired
    private AlarmSprayWaterService alarmSprayWaterService;

    @Autowired
    private AlarmHydrantWaterService alarmHydrantWaterService;

    @Autowired
    private AlarmSmokeControlService alarmSmokeControlService;

    @Autowired
    private AlarmVideoService alarmVideoService;

    @Autowired
    private AlarmElectricalFireService alarmElectricalFireService;

    @Autowired
    private AlarmFireService alarmFireService;

    @Override
    public Object enterpriseStreetTown(String moduleType, String dataType, String streetTown) {
        LambdaQueryWrapper<BscEnterpriseStreetTown> queryWrapper = Wrappers.lambdaQuery();
        switch (moduleType) {
            case "enterpriseAutonomy":
                queryWrapper.select(BscEnterpriseStreetTown::getEnterpriseAutonomy);
                break;
            case "iotDevice":
                queryWrapper.select(BscEnterpriseStreetTown::getIotDevice);
                break;
            case "patrolInspection":
                queryWrapper.select(BscEnterpriseStreetTown::getPatrolInspection);
                break;
            case "streetCompany":
                queryWrapper.select(BscEnterpriseStreetTown::getStreetCompany);
                break;
            default:
                throw new BusinessException("参数错误");
        }
        queryWrapper.eq(BscEnterpriseStreetTown::getStreetTown, streetTown)
                .orderByDesc(BscEnterpriseStreetTown::getId)
                .last(" limit 1");
        List<BscEnterpriseStreetTown> list = this.list(queryWrapper);
        Object data = null;
        if (CollectionUtils.isNotEmpty(list)) {
            switch (moduleType) {
                case "enterpriseAutonomy":
                    data = list.get(0).getEnterpriseAutonomy();
                    break;
                case "iotDevice":
                    data = list.get(0).getIotDevice();
                    break;
                case "patrolInspection":
                    data = list.get(0).getPatrolInspection();
                    break;
                case "streetCompany":
                    data = list.get(0).getStreetCompany();
                    break;
            }
        }
        JSONObject jsonObject = JSONObject.parseObject(data.toString());
        Object disparateData = jsonObject.get(dataType);
        return disparateData;
    }

    @Transactional
    @Override
    public void enterpriseStreetTownTask() {
        List<String> streetTownList = new ArrayList<>();
        streetTownList.add("华漕镇");
        streetTownList.add("新虹街道");
        streetTownList.add("七宝镇");
        streetTownList.add("虹桥镇");
        streetTownList.add("古美路街道");
        streetTownList.add("莘庄镇");
        streetTownList.add("梅陇镇");
        streetTownList.add("颛桥镇");
        streetTownList.add("马桥镇");
        streetTownList.add("江川路街道");
        streetTownList.add("吴泾镇");
        streetTownList.add("浦锦街道");
        streetTownList.add("浦江镇");
        if (CollectionUtils.isNotEmpty(streetTownList)) {
            for (int i = 0; i < streetTownList.size(); i++) {
                Map<String, Object> enterpriseAutonomy = this.enterpriseStreetTownData(streetTownList.get(i));
                Object iotDevice = this.iotDevice(streetTownList.get(i));
                Object patrolInspection = this.patrolInspection(streetTownList.get(i));
                Object streetCompany = this.streetCompany(streetTownList.get(i));
                BscEnterpriseStreetTown bscEnterpriseStreetTown = new BscEnterpriseStreetTown();
                bscEnterpriseStreetTown.setStreetTown(streetTownList.get(i));
                bscEnterpriseStreetTown.setEnterpriseAutonomy(JSON.toJSONString(enterpriseAutonomy));
                bscEnterpriseStreetTown.setIotDevice(JSON.toJSONString(iotDevice));
                bscEnterpriseStreetTown.setPatrolInspection(JSON.toJSONString(patrolInspection));
                bscEnterpriseStreetTown.setStreetCompany(JSON.toJSONString(streetCompany));
                bscEnterpriseStreetTown.setRpn(enterpriseAutonomy.get("data4").toString());
                bscEnterpriseStreetTown.setCreateTime(LocalDateTime.now());
                this.save(bscEnterpriseStreetTown);
            }
        }
    }


    @Override
    public Map<String, Object> enterpriseStreetTownData(String streetTown) {
        String LastMonthDate = OnlineMethod.getLastMonth(new Date());
        DecimalFormat df = new DecimalFormat("0.00");
        int deviceDamageCount = 20;
        int fireCount = 0;
        int rectificationCount = 40;
        int buildTypeCount = 10;
        int highRiskCount = 30;
        int hiddenDangerCount = 0;
        int companyScoreRatio = 0;
        List<String> companyIdList = this.companyIdList(streetTown);
        if (CollectionUtils.isNotEmpty(companyIdList)) {
            List<String> alarmTypeList = new ArrayList<>();
            alarmTypeList.add("2");
            fireCount = alarmFireService.fireCount(companyIdList, 0, alarmTypeList, null, null, null);
            hiddenDangerCount = this.hiddenCount(companyIdList);
        }

        List<DemBuildScore> list1 = demBuildScoreService.buildScoreListOne();
        int companyScoreCount1 = demBuildScoreRecordService.demBuildScoreRecordCountOne(LastMonthDate, null, 85, companyIdList);
        int companyScoreCount2 = demBuildScoreRecordService.demBuildScoreRecordCountOne(LastMonthDate, 85, 60, companyIdList);
        int companyScoreCount3 = demBuildScoreRecordService.demBuildScoreRecordCountOne(LastMonthDate, 60, null, companyIdList);
        if ((companyScoreCount1 + companyScoreCount2 + companyScoreCount3) != 0 && companyScoreCount3 != 0) {
            companyScoreRatio = companyScoreCount3 / (companyScoreCount1 + companyScoreCount2 + companyScoreCount3);
        }

        Double deviceDamageRieo = 0.00;
        Double fireRieo = 0.00;
        Double rectificationRieo = 0.00;
        Double buildTypeRieo = 0.00;
        Double highRiskRieo = 0.00;
        Double hiddenDangerRieo = 0.00;

        Double deviceDamageScore1 = 0.00;
        Double fireScore1 = 0.00;
        Double rectificationScore1 = 0.00;
        Double buildTypeScore1 = 0.00;
        Double highRiskScore1 = 0.00;
        Double hiddenDangerScore1 = 0.00;

        List<Object> data1 = new ArrayList<>();
        for (int i = 0; i < list1.size(); i++) {
            Map<String, Object> map1 = new HashMap<>();
            int riskMax = Integer.parseInt(list1.get(i).getRiskMax());
            switch (list1.get(i).getRiskId()) {
                case 1:
                    deviceDamageRieo = Double.valueOf(df.format((float) deviceDamageCount / riskMax * 100));
                    if (deviceDamageRieo != 0.00 & companyIdList.size() != 0) {
                        deviceDamageScore1 = Arith.div(deviceDamageRieo, companyIdList.size(), 2);
                    }
                    map1.put("name", "设备状况指数");
                    map1.put("value", deviceDamageRieo);
                    break;
                case 2:
                    fireRieo = Double.valueOf(df.format((float) fireCount / riskMax * 100));
                    if (fireRieo != 0.00 & companyIdList.size() != 0) {
                        fireScore1 = Arith.div(fireRieo, companyIdList.size(), 2);
                    }
                    map1.put("name", "历史火灾指数");
                    map1.put("value", fireScore1);
                    break;
                case 3:
                    rectificationRieo = Double.valueOf(df.format((float) rectificationCount / riskMax * 100));
                    if (rectificationRieo != 0.00 & companyIdList.size() != 0) {
                        rectificationScore1 = Arith.div(rectificationRieo, companyIdList.size(), 2);
                    }
                    map1.put("name", "执法监管安全指数");
                    map1.put("value", rectificationScore1);
                    break;
                case 4:
                    buildTypeRieo = Double.valueOf(df.format((float) buildTypeCount / riskMax * 100));
                    if (buildTypeRieo != 0.00 & companyIdList.size() != 0) {
                        buildTypeScore1 = Arith.div(buildTypeRieo, companyIdList.size(), 2);
                    }
                    map1.put("name", "建筑整体风险指");
                    map1.put("value", buildTypeScore1);
                    break;
                case 5:
                    highRiskRieo = Double.valueOf(df.format((float) highRiskCount / riskMax * 100));
                    if (highRiskRieo != 0.00 & companyIdList.size() != 0) {
                        highRiskScore1 = Arith.div(highRiskRieo, companyIdList.size(), 2);
                    }
                    map1.put("name", "场所行业风险指数");
                    map1.put("value", highRiskScore1);
                    break;
                case 6:
                    hiddenDangerRieo = Double.valueOf(df.format((float) hiddenDangerCount / riskMax * 100));
                    if (hiddenDangerRieo != 0.00 & companyIdList.size() != 0) {
                        hiddenDangerScore1 = Arith.div(hiddenDangerRieo, companyIdList.size(), 2);
                    }
                    map1.put("name", "隐患状况指数");
                    map1.put("value", hiddenDangerScore1);
                    break;
            }
            map1.put("mark", "分");
            data1.add(map1);
        }
        List<Object> data2 = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            Map<String, Object> map2 = new HashMap<>();
            switch (i) {
                case 0:
                    map2.put("name", "85分以上");
                    map2.put("value", companyScoreCount1);
                    map2.put("mark", "家");
                    break;
                case 1:
                    map2.put("name", "60~85分");
                    map2.put("value", companyScoreCount2);
                    map2.put("mark", "家");
                    break;
                case 2:
                    map2.put("name", "60分以下");
                    map2.put("value", companyScoreCount3);
                    map2.put("mark", "家");
                    break;
                case 3:
                    map2.put("name", "综合风险率");
                    map2.put("value", companyScoreRatio);
                    map2.put("mark", "%");
                    break;
            }
            data2.add(map2);
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        List<BscEnterpriseStreetTown> list = this.enterpriseStreetTownList(streetTown);
        List<Object> data3 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map3 = new HashMap<>();
            if (!formatter.format(LocalDateTime.now()).equals(formatter.format(list.get(list.size() - 1 - i).getCreateTime()))) {
                map3.put("time", formatter.format(list.get(list.size() - 1 - i).getCreateTime()));
                map3.put("value", list.get(list.size() - 1 - i).getRpn());
                data3.add(map3);
            }
        }
        double value1 = Arith.add(deviceDamageScore1, fireScore1);
        double value2 = Arith.add(value1, rectificationScore1);
        double value3 = Arith.add(value2, buildTypeScore1);
        double value4 = Arith.add(value3, highRiskScore1);
        double value = Arith.add(value4, hiddenDangerScore1);

        Map<String, Object> map4 = new HashMap<>();
        map4.put("time", formatter.format(LocalDateTime.now()));
        map4.put("value", value);
        data3.add(map4);

        Map<String, Object> map = new HashMap<>();
        map.put("data1", data1);
        map.put("data2", data2);
        map.put("data3", data3);
        map.put("data4", value);
        return map;
    }

    @Override
    public int hiddenCount(List<String> companyIdList) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        Date date = new Date();
        int untreated = 0;
        Integer untreated1 = alarmSprayWaterService.sprayWaterCount(companyIdList, 0, null, null, null);
        Integer untreated2 = alarmHydrantWaterService.hydrantWaterCount(companyIdList, 0, null, null, null);
        Integer untreated3 = alarmSmokeControlService.smokeCount(companyIdList, 0, null, null, null);
        Integer untreated4 = alarmVideoService.videoCount(companyIdList, 0, null, null, null);
        Integer untreated5 = alarmElectricalFireService.electricalFireCount(companyIdList, 0, null, null, null);
        untreated = untreated1 + untreated2 + untreated3 + untreated4 + untreated5;
        return untreated;
    }


    @Override
    public List<BscEnterpriseStreetTown> enterpriseStreetTownList(String streetTown) {
        LambdaQueryWrapper<BscEnterpriseStreetTown> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(BscEnterpriseStreetTown::getRpn, BscEnterpriseStreetTown::getCreateTime)
                .eq(BscEnterpriseStreetTown::getStreetTown, streetTown)
                .orderByDesc(BscEnterpriseStreetTown::getId)
                .last(" limit 6");
        List<BscEnterpriseStreetTown> list = this.list(queryWrapper);
        return list;
    }


    @Override
    public List<String> companyIdList(String streetTown) {
        LambdaQueryWrapper<BaseCompany> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(BaseCompany::getCompanyId)
                .eq(BaseCompany::getEnable, 0)
                .eq(BaseCompany::getStreetTown, streetTown);
        List<BaseCompany> list = baseCompanyService.list(queryWrapper);
        List<String> companyIdList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                companyIdList.add(list.get(i).getCompanyId());
            }
        }
        return companyIdList;
    }

    @Override
    public Object iotDevice(String streetTown) {
        Integer typeCount1 = 0;
        Integer typeCount2 = 0;
        Integer typeCount6 = 0;
        Integer typeCount7 = 0;
        Integer typeCount9 = 0;
        Integer typeOffCount1 = 0;
        Integer typeOffCount2 = 0;
        Integer typeOffCount6 = 0;
        Integer typeOffCount7 = 0;
        Integer typeOffCount9 = 0;
        Integer deviceCount = 0;
        List<String> companyIdList = this.companyIdList(streetTown);
        if (CollectionUtils.isNotEmpty(companyIdList)) {
            LambdaQueryWrapper<BaseDevice> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.select(BaseDevice::getDeviceCode, BaseDevice::getDeviceType)
                    .in(BaseDevice::getCompanyId, companyIdList)
                    .eq(BaseDevice::getEnable, 1)
                    .ne(BaseDevice::getDeviceType, 16);
            List<BaseDevice> list1 = baseDeviceService.list(queryWrapper);
            deviceCount = list1.size();
            if (CollectionUtils.isNotEmpty(list1)) {
                List<String> deviceCode1 = new ArrayList<>();
                List<String> deviceCode2 = new ArrayList<>();
                List<String> deviceCode6 = new ArrayList<>();
                List<String> deviceCode7 = new ArrayList<>();
                List<String> deviceCode9 = new ArrayList<>();
                for (int i = 0; i < list1.size(); i++) {
                    switch (list1.get(i).getDeviceType()) {
                        case 1:
                        case 3:
                            deviceCode1.add(list1.get(i).getDeviceCode());
                            break;
                        case 2:
                        case 5:
                            deviceCode2.add(list1.get(i).getDeviceCode());
                            break;
                        case 6:
                            deviceCode6.add(list1.get(i).getDeviceCode());
                            break;
                        case 7:
                            deviceCode7.add(list1.get(i).getDeviceCode());
                            break;
                        default:
                            deviceCode9.add(list1.get(i).getDeviceCode());
                            break;
                    }
                }
                if (CollectionUtils.isNotEmpty(deviceCode1)) {
                    typeCount1 = deviceCode1.size();
                    typeOffCount1 = this.deviceStateCount(deviceCode1, 0);
                }

                if (CollectionUtils.isNotEmpty(deviceCode2)) {
                    typeCount2 = deviceCode2.size();
                    typeOffCount2 = this.deviceStateCount(deviceCode2, 0);
                }

                if (CollectionUtils.isNotEmpty(deviceCode6)) {
                    typeCount6 = deviceCode6.size();
                    typeOffCount6 = this.deviceStateCount(deviceCode6, 0);
                }

                if (CollectionUtils.isNotEmpty(deviceCode7)) {
                    typeCount7 = deviceCode7.size();
                    typeOffCount7 = this.deviceStateCount(deviceCode7, 0);
                }

                if (CollectionUtils.isNotEmpty(deviceCode9)) {
                    typeCount9 = deviceCode9.size();
                    typeOffCount9 = this.deviceStateCount(deviceCode9, 0);
                }
            }
        }
        List<Object> data2 = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Map<String, Object> map2 = new HashMap<>();
            switch (i) {
                case 0:
                    map2.put("name", "火系统");
                    map2.put("on_line", typeCount1 - typeOffCount1);
                    map2.put("off_line", typeOffCount1);
                    if ((typeCount1 - typeOffCount1) != 0 && typeCount1 != 0) {
                        map2.put("ratio", (typeCount1 - typeOffCount1) / typeCount1 * 100 + "%");
                    } else {
                        map2.put("ratio", "0%");
                    }
                    break;
                case 1:
                    map2.put("name", "水系统");
                    map2.put("on_line", typeCount2 - typeOffCount2);
                    map2.put("off_line", typeOffCount2);
                    if ((typeCount2 - typeOffCount2) != 0 && typeCount2 != 0) {
                        map2.put("ratio", (typeCount2 - typeOffCount2) / typeCount2 * 100 + "%");
                    } else {
                        map2.put("ratio", "0%");
                    }
                    break;
                case 2:
                    map2.put("name", "RTU");
                    map2.put("on_line", typeCount6 - typeOffCount6);
                    map2.put("off_line", typeOffCount6);
                    if ((typeCount6 - typeOffCount6) != 0 && typeCount6 != 0) {
                        map2.put("ratio", (typeCount6 - typeOffCount6) / typeCount6 * 100 + "%");
                    } else {
                        map2.put("ratio", "0%");
                    }
                    break;
                case 3:
                    map2.put("name", "电气火灾");
                    map2.put("on_line", typeCount7 - typeOffCount7);
                    map2.put("off_line", typeOffCount7);
                    if ((typeCount7 - typeOffCount7) != 0 && typeCount7 != 0) {
                        map2.put("ratio", (typeCount7 - typeOffCount7) / typeCount7 * 100 + "%");
                    } else {
                        map2.put("ratio", "0%");
                    }
                    break;
                case 4:
                    map2.put("name", "其他");
                    map2.put("on_line", typeCount9 - typeOffCount9);
                    map2.put("off_line", typeOffCount9);
                    if ((typeCount9 - typeOffCount9) != 0 && typeCount9 != 0) {
                        map2.put("ratio", (typeCount9 - typeOffCount9) / typeCount9 * 100 + "%");
                    } else {
                        map2.put("ratio", "0%");
                    }
                    break;
            }
            data2.add(map2);
        }
        List<Object> data1 = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("name", "累计接入设备");
        map1.put("value", deviceCount);
        data1.add(map1);
        Map<String, Object> map = new HashMap<>();
        map.put("data1", data1);
        map.put("data2", data2);
        return map;
    }

    @Override
    public Integer deviceStateCount(List<String> deviceCodeList, Integer state) {
        LambdaQueryWrapper<BaseDeviceStatus> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(BaseDeviceStatus::getDeviceCode, deviceCodeList)
                .eq(BaseDeviceStatus::getDeviceStatus, state);
        int count = baseDeviceStatusService.count(queryWrapper);
        return count;
    }


    @Override
    public Object patrolInspection(String streetTown) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String sameDay = format.format(new Date());
        Integer completedCount = 0;
        Integer undoneCount = 0;
        String startTime = null;
        String endTime = null;
        List<Object> data1 = new ArrayList<>();
        List<Object> data2 = new ArrayList<>();
        List<Object> data3 = new ArrayList<>();
        List<String> companyIdList = this.companyIdList(streetTown);
        for (int i = 0; i < 3; i++) {
            if (i == 0) {
                startTime = sameDay;
                endTime = sameDay;
            } else if (i == 1) {
                long timeStamp = OnlineMethod.getLastPeroid(1, new Date());
                startTime = format.format(timeStamp);
                endTime = sameDay;
            } else if (i == 2) {
                long timeStamp = OnlineMethod.getLastPeroid(2, new Date());
                startTime = format.format(timeStamp);
                endTime = sameDay;
            }
            List<Integer> planSonIdList1 = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(companyIdList)) {
                planSonIdList1 = this.planSonIdList(startTime, endTime, companyIdList);
            }
            if (CollectionUtils.isNotEmpty(planSonIdList1)) {
                undoneCount = patrolInspectionPlanSiteSonService.siteCount(planSonIdList1, 1);
                completedCount = patrolInspectionPlanSiteSonService.siteCount(planSonIdList1, 2);
            }

            for (int j = 0; j < 4; j++) {
                Map<String, Object> map1 = new HashMap<>();
                switch (j) {
                    case 0:
                        if (i == 0) {
                            map1.put("name", "今日巡检次数");
                        } else if (i == 1) {
                            map1.put("name", "近七日巡检次数");
                        } else if (i == 2) {
                            map1.put("name", "近一月巡检次数");
                        }
                        map1.put("value", undoneCount + completedCount);
                        map1.put("mark", "");
                        break;
                    case 1:
                        map1.put("name", "巡检率");
                        if (undoneCount == 0 && completedCount == 0) {
                            map1.put("value", 0);
                        } else {
                            map1.put("value", completedCount / (undoneCount + completedCount) * 100);
                        }

                        map1.put("mark", "%");
                        break;
                    case 2:
                        map1.put("name", "合格数");
                        map1.put("value", completedCount);
                        map1.put("mark", "");
                        break;
                    case 3:
                        map1.put("name", "漏检数");
                        map1.put("value", undoneCount);
                        map1.put("mark", "");
                        break;
                }
                if (i == 0) {
                    data1.add(map1);
                } else if (i == 1) {
                    data2.add(map1);
                } else if (i == 2) {
                    data3.add(map1);
                }
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("data1", data1);
        map.put("data2", data2);
        map.put("data3", data3);
        return map;
    }

    @Override
    public List<Integer> planSonIdList(String startDate, String endDate, List<String> companyIdList) {
        LambdaQueryWrapper<PatrolInspectionArea> queryWrapper2 = Wrappers.lambdaQuery();
        queryWrapper2.select(PatrolInspectionArea::getAreaFid)
                .eq(PatrolInspectionArea::getEnable, 1)
                .in(PatrolInspectionArea::getDeptId, companyIdList);
        List<PatrolInspectionArea> areaList = patrolInspectionAreaService.list(queryWrapper2);
        List<Integer> planSonIdList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(areaList)) {
            List<Integer> areaIdList = new ArrayList<>();
            for (int i = 0; i < areaList.size(); i++) {
                areaIdList.add(areaList.get(i).getId());
            }
            if (CollectionUtils.isNotEmpty(areaIdList)) {
                LambdaQueryWrapper<PatrolInspectionPlan> queryWrapper = Wrappers.lambdaQuery();
                queryWrapper.select(PatrolInspectionPlan::getId)
//                        .eq(PatrolInspectionPlan::getTenantId, SecurityUtils.getTenantId())
                        .eq(PatrolInspectionPlan::getEnable, 1)
                        .in(PatrolInspectionPlan::getAreaId, areaIdList)
                        .between(PatrolInspectionPlan::getEndDate, startDate, endDate)
                        .between(PatrolInspectionPlan::getStartDate, startDate, endDate);
                List<PatrolInspectionPlan> planList = patrolInspectionPlanService.list(queryWrapper);
                if (CollectionUtils.isNotEmpty(planList)) {
                    List<Integer> planIdList = new ArrayList<>();
                    for (int i = 0; i < planList.size(); i++) {
                        planIdList.add(planList.get(i).getId());
                    }
                    List<PatrolInspectionPlanSon> planSonList = new ArrayList<>();
                    if (CollectionUtils.isNotEmpty(planIdList)) {
                        LambdaQueryWrapper<PatrolInspectionPlanSon> queryWrapper1 = Wrappers.lambdaQuery();
                        queryWrapper1.select(PatrolInspectionPlanSon::getId)
                                .in(PatrolInspectionPlanSon::getPlanId, planIdList)
                                .between(PatrolInspectionPlanSon::getInspectionDate, startDate, endDate);
                        planSonList = patrolInspectionPlanSonService.list(queryWrapper1);
                    }
                    for (int i = 0; i < planSonList.size(); i++) {
                        planSonIdList.add(planSonList.get(i).getId());
                    }
                }
            }
        }
        return planSonIdList;
    }

    @Override
    public Object streetCompany(String streetTown) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        long timeStamp = OnlineMethod.getLastPeroid(2, new Date());
        String scoreDate = format.format(timeStamp);
        List<String> companyIdList = baseCompanyService.companyIdList(streetTown);

        QueryWrapper<BaseCompany> query = Wrappers.query();
        query.select("street_town as streetTown", "company_type as companyType", "count(id) as count")
                .eq("enable", 0)
                .eq("street_town", streetTown)
                .groupBy("street_town,company_type");
        List<Map<String, Object>> list1 = baseCompanyService.listMaps(query);

        QueryWrapper<BaseCompany> query1 = Wrappers.query();
        query1.select("street_town as streetTown")
                .eq("enable", 0)
                .eq("street_town", streetTown)
                .groupBy("street_town");
        List<Map<String, Object>> list2 = baseCompanyService.listMaps(query1);

        QueryWrapper<DemBuildScoreRecord> query2 = Wrappers.query();
        query2.select("avg(build_score) as streetTownScore")
                .in("company_id", companyIdList)
                .eq("score_date", scoreDate);
        List<Map<String, Object>> list3 = demBuildScoreRecordService.listMaps(query2);

        List<Map<String, Object>> stressList = new ArrayList<>();
        List<Map<String, Object>> generalList = new ArrayList<>();
        List<Map<String, Object>> otherList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list1)) {
            for (int i = 0; i < list1.size(); i++) {
                switch (list1.get(i).get("companyType").toString()) {
                    case "1":
                        stressList.add(list1.get(i));
                        break;
                    case "2":
                        generalList.add(list1.get(i));
                        break;
                    default:
                        otherList.add(list1.get(i));
                        break;
                }
            }
        }

        List<Object> data = new ArrayList<>();
        for (int i = 0; i < list2.size(); i++) {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("name", list2.get(i).get("streetTown"));
            map1.put("stress", 0);
            map1.put("general", 0);
            map1.put("other", 0);
            map1.put("streetTownScore", 0);
            if (CollectionUtils.isNotEmpty(list3)){
                map1.put("streetTownScore", list3.get(0).get("streetTownScore"));
            }

            for (int j = 0; j < stressList.size(); j++) {
                if (list2.get(i).get("streetTown").equals(stressList.get(j).get("streetTown"))) {
                    map1.put("stress", stressList.get(j).get("count"));
                }
            }

            for (int j = 0; j < generalList.size(); j++) {
                if (list2.get(i).get("streetTown").equals(generalList.get(j).get("streetTown"))) {
                    map1.put("general", generalList.get(j).get("count"));
                }
            }
            for (int j = 0; j < otherList.size(); j++) {
                if (list2.get(i).get("streetTown").equals(otherList.get(j).get("streetTown"))) {
                    map1.put("other", otherList.get(j).get("count"));
                }
            }
            data.add(map1);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("data", data);
        return map;
    }
}
