zf
2023-08-30 464fe41610a39c3a06f070d5a01c930134ff0163
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package com.gk.hotwork.Service.ServiceImpl;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gk.hotwork.Domain.*;
import com.gk.hotwork.Domain.Do.CompanyInspectionCountDO;
import com.gk.hotwork.Domain.Do.CompanyStatisticInspectionDO;
import com.gk.hotwork.Domain.Do.SubordinateOrganizationInfoDO;
import com.gk.hotwork.Domain.Utils.BeanUtils;
import com.gk.hotwork.Domain.Utils.Msg;
import com.gk.hotwork.Domain.Utils.StringUtils;
import com.gk.hotwork.Domain.dto.resp.AreaInspectionCountRespDTO;
import com.gk.hotwork.Domain.dto.resp.CompanyInspectionCountRespDTO;
import com.gk.hotwork.Domain.query.CountQuery;
import com.gk.hotwork.Service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * @email 1603559716@qq.com
 * @author: zf
 * @date: 2023/8/22
 * @time: 15:41
 */
@Service
public class SafetyInspectionStatisticServiceImpl implements SafetyInspectionStatisticService {
 
    private final static String province = "新疆维吾尔自治区";
    @Autowired
    private DistrictService districtService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private InspectionHiddenDangerService hiddenDangerService;
    @Autowired
    private SafetySelfInspectionService selfInspectionService;
    @Override
    public Msg getCountByArea(CountQuery query, UserInfo user) {
        List<SubordinateOrganizationInfoDO> organizationInfos = null;
 
        Integer type = user.getType();
        //如果传来的区县级不为空,则表明是到县级
        if(StringUtils.isNotBlank(query.getArea())){
            return getAreaDataCount(query);
        }
        //如果传来市级不为空
        if(StringUtils.isNotBlank(query.getCity())){
            List<DistrictInfo>  districtInfos = districtService.selectDistrictByName("3", query.getCity(), "2");
            organizationInfos = districtInfos.stream().map((districtInfo)->{
                SubordinateOrganizationInfoDO organizationInfoDO = new SubordinateOrganizationInfoDO();
                organizationInfoDO.setProvince(province);
                organizationInfoDO.setCity(query.getCity());
                organizationInfoDO.setArea(districtInfo.getName());
                return organizationInfoDO;
            }).collect(Collectors.toList());
        }else {
            //管理员
            if(type.equals(1)){
                List<DistrictInfo> districtInfos = districtService.selectDistrictByName("2", province, "1");
                organizationInfos = districtInfos.stream().map((districtInfo)->{
                    SubordinateOrganizationInfoDO organizationInfoDO = new SubordinateOrganizationInfoDO();
                    organizationInfoDO.setProvince(province);
                    organizationInfoDO.setCity(districtInfo.getName());
                    return organizationInfoDO;
                }).collect(Collectors.toList());
            }
            //监护人员
            if(type.equals(2)){
                //属于县级
                if(StringUtils.isNotBlank(user.getCounty())){
                    query.setArea(user.getCounty());
                    return getAreaDataCount(query);
                }
                //属于市级
                if(StringUtils.isNotBlank(user.getCity())){
                    List<DistrictInfo> districtInfos = districtService.selectDistrictByName("3", user.getCity(), "2");
                    organizationInfos = districtInfos.stream().map((districtInfo)->{
                        SubordinateOrganizationInfoDO organizationInfoDO = new SubordinateOrganizationInfoDO();
                        organizationInfoDO.setProvince(user.getProvince());
                        organizationInfoDO.setCity(user.getCity());
                        organizationInfoDO.setArea(districtInfo.getName());
                        return organizationInfoDO;
                    }).collect(Collectors.toList());
                }
                //属于省级
                if(StringUtils.isNotBlank(user.getProvince())){
                    List<DistrictInfo> districtInfos = districtService.selectDistrictByName("2", user.getProvince(), "1");
                    organizationInfos = districtInfos.stream().map((districtInfo)->{
                        SubordinateOrganizationInfoDO organizationInfoDO = new SubordinateOrganizationInfoDO();
                        organizationInfoDO.setProvince(user.getProvince());
                        organizationInfoDO.setCity(districtInfo.getName());
                        return organizationInfoDO;
                    }).collect(Collectors.toList());
                }
            }
        }
        List<AreaInspectionCountRespDTO> list = getDataCountSelfCheckInfo(query,organizationInfos);
        Msg msg = new Msg();
        msg.setCode("200");
        msg.setResult("查询成功");
        msg.setResult(list);
        return msg;
    }
 
    @Override
    public Msg getUnCheckCompany(CountQuery query) {
        Page<CompanyInfo> uncheckCompanyByArea = companyService.getUncheckCompanyByArea(query);
        Msg msg = new Msg();
        msg.setCode("200");
        msg.setResult("查询成功");
        msg.setResult(uncheckCompanyByArea);
        return msg;
    }
 
    private List<AreaInspectionCountRespDTO> getDataCountSelfCheckInfo(CountQuery query, List<SubordinateOrganizationInfoDO> organizationInfos) {
 
        List<AreaInspectionCountRespDTO> respDTOList = new ArrayList<>();
        for (SubordinateOrganizationInfoDO organizationInfo : organizationInfos) {
            AreaInspectionCountRespDTO areaInspectionCountRespDTO = new AreaInspectionCountRespDTO();
            //统计检查企业数量以及清单数量等
            Map<String,Object> map = new HashMap<>();
            map.put("startTime",query.getStartTime());
            map.put("endTime",query.getEndTime());
            map.put("province",organizationInfo.getProvince());
            map.put("city",organizationInfo.getCity());
            map.put("area",organizationInfo.getArea());
            CompanyStatisticInspectionDO companyStatisticInspection = selfInspectionService.companyStatisticInspection(map);
            BeanUtils.copyProperties(companyStatisticInspection,areaInspectionCountRespDTO);
            areaInspectionCountRespDTO.setProvince(organizationInfo.getProvince());
            areaInspectionCountRespDTO.setCity(organizationInfo.getCity());
            areaInspectionCountRespDTO.setArea(organizationInfo.getArea());
            areaInspectionCountRespDTO.setRectifyRate(areaInspectionCountRespDTO.getHdTotal() == 0 ? new BigDecimal(0).setScale(2,BigDecimal.ROUND_HALF_UP) : new BigDecimal(areaInspectionCountRespDTO.getRectifyTotal()).divide(new BigDecimal(areaInspectionCountRespDTO.getHdTotal()), 2, BigDecimal.ROUND_HALF_UP));
            //统计公司总数量
            int companyCount = companyService.getCountByArea(map);
            areaInspectionCountRespDTO.setCompanyTotal(companyService.getCountByArea(map));
            areaInspectionCountRespDTO.setUnCheckCompanyCount(companyCount-areaInspectionCountRespDTO.getCheckCompanyCount());
            respDTOList.add(areaInspectionCountRespDTO);
        }
        return respDTOList;
    }
 
    private Msg getAreaDataCount(CountQuery query) {
        if(query.getPageIndex() == null){
            query.setPageIndex(1);
        }
        if(query.getPageSize() == null){
            query.setPageSize(10);
        }
        //获取该地区所有企业隐患统计
        List<CompanyInspectionCountDO> statisticHiddenDangerList = hiddenDangerService.getStatisticHiddenDanger(query);
        //获取相关企业
        Page<CompanyInfo> companyInfoPage = companyService.getByArea(query);
        List<CompanyInspectionCountRespDTO> list = new ArrayList<>();
        for (CompanyInfo companyInfo : companyInfoPage.getRecords()) {
            CompanyInspectionCountRespDTO inspectionCountRespDTO = new CompanyInspectionCountRespDTO();
            List<CompanyInspectionCountDO> collect = statisticHiddenDangerList
                    .stream()
                    .filter(statistic -> statistic.getCompanyId().equals(companyInfo.getId()))
                    .collect(Collectors.toList());
            if(collect.size() > 0){
                CompanyInspectionCountDO companyInspectionCountDO = collect.get(0);
                BeanUtils.copyProperties(companyInspectionCountDO,inspectionCountRespDTO);
                inspectionCountRespDTO.setRectifyRate(companyInspectionCountDO.getSHdTotal() == 0 ? new BigDecimal(0).setScale(2,BigDecimal.ROUND_HALF_UP) : new BigDecimal(companyInspectionCountDO.getSReTotal()).divide(new BigDecimal(companyInspectionCountDO.getSHdTotal()), 2, BigDecimal.ROUND_HALF_UP));
            }else {
 
                inspectionCountRespDTO.setCheckTotal(0);
                inspectionCountRespDTO.setSHdTotal(0);
                inspectionCountRespDTO.setSMaTotal(0);
                inspectionCountRespDTO.setSMaReTotal(0);
                inspectionCountRespDTO.setSReTotal(0);
                inspectionCountRespDTO.setSUnReTotal(0);
                inspectionCountRespDTO.setSSaTotal(0);
                inspectionCountRespDTO.setSSaReTotal(0);
                inspectionCountRespDTO.setRectifyRate(new BigDecimal(0).setScale(2,BigDecimal.ROUND_HALF_UP));
                inspectionCountRespDTO.setCompanyId(companyInfo.getId());
            }
            inspectionCountRespDTO.setCompanyName(companyInfo.getCompany());
            inspectionCountRespDTO.setLastTime(selfInspectionService.selectLastTimeByCompanyId(companyInfo.getId(),query.getStartTime(),query.getEndTime()));
            list.add(inspectionCountRespDTO);
        }
        Page<CompanyInspectionCountRespDTO> page = new Page<>();
        BeanUtils.copyProperties(companyInfoPage,page);
        page.setRecords(list);
 
        Msg msg = new Msg();
        msg.setCode("200");
        msg.setMessage("查询成功");
        msg.setResult(page);
        return msg;
    }
}