shenyijian
2024-06-18 402ff1aeac9bb33e6d8ff04913cbf1b0364df2a6
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
package cn.gistack.sm.sg.service.impl;
 
import cn.gistack.sm.sg.DO.SgDeviceDO;
import cn.gistack.sm.sg.DO.SgGxDO;
import cn.gistack.sm.sg.DO.SgGxStepDO;
import cn.gistack.sm.sg.DO.SgProjectInfoDO;
import cn.gistack.sm.sg.DTO.ProjectSearchDTO;
import cn.gistack.sm.sg.VO.SgProjectInfoVO;
import cn.gistack.sm.sg.VO.SgTypeVO;
import cn.gistack.sm.sg.mapper.SgProjectInfoMapper;
import cn.gistack.sm.sg.service.SgProjectInfoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 施工项目表 服务实现类
 * </p>
 *
 * @author shenyijian
 * @since 2024-06-13 11:51:20
 */
@Service
public class SgProjectInfoServiceImpl extends ServiceImpl<SgProjectInfoMapper, SgProjectInfoDO> implements SgProjectInfoService {
    @Autowired
    private SgProjectInfoMapper sgProjectInfoMapper;
    @Override
    public IPage<SgProjectInfoDO> queryPageList(IPage<SgProjectInfoDO> page, ProjectSearchDTO searchDTO) {
        List<SgProjectInfoDO> sgProjectInfoDOS = baseMapper.queryPageList(page,searchDTO);
        return page.setRecords(sgProjectInfoDOS);
    }
 
    @Override
    public List<SgProjectInfoDO> queryStepByProject(ProjectSearchDTO searchDTO) {
        List<SgProjectInfoVO> list = sgProjectInfoMapper.queryStepByProject( searchDTO);
        //项目分组
        Map<Long, List<SgProjectInfoVO>> projectMap = list.stream().collect(Collectors.groupingBy(SgProjectInfoVO::getId));
        List<SgProjectInfoDO> result = new ArrayList<>();
        for (Map.Entry<Long, List<SgProjectInfoVO>> entry : projectMap.entrySet()) {
            Long porId = entry.getKey();
            List<SgProjectInfoVO> value = entry.getValue();
            if (CollectionUtils.isEmpty(value)){
                continue;
            }
            SgProjectInfoDO sgProjectInfoDO = SgProjectInfoDO.builder().id(porId).projectName(value.get(0).getProjectName()).build();
            List<SgTypeVO> sgGxDOS = new ArrayList<>();
            //类型分组
            Map<String, List<SgProjectInfoVO>> GxMap = value.stream().filter(s->s.getDeviceType()!=null).collect(Collectors.groupingBy(SgProjectInfoVO::getDeviceType));
            for (Map.Entry<String, List<SgProjectInfoVO>> ty : GxMap.entrySet()) {
                String type = ty.getKey();
                List<SgProjectInfoVO> gxList = ty.getValue();
                if (CollectionUtils.isEmpty(gxList)){
                    continue;
                }
                SgTypeVO typeVO = SgTypeVO.builder().typeName(gxList.get(0).getTypeName()).build();
                List<SgDeviceDO> deviceDOS = new ArrayList<>();
                //设备分组
                Map<Long, List<SgProjectInfoVO>> dMap = gxList.stream().collect(Collectors.groupingBy(SgProjectInfoVO::getDId));
                for (Map.Entry<Long, List<SgProjectInfoVO>> device : dMap.entrySet()) {
                    Long dId = device.getKey();
                    List<SgProjectInfoVO> deList = device.getValue();
                    SgDeviceDO sgDeviceDO = new SgDeviceDO();
                    sgDeviceDO.setId(dId);
                    sgDeviceDO.setDeviceName(deList.get(0).getDeviceName());
                    sgDeviceDO.setStepDOList(deList.stream().map(s->SgGxStepDO.builder().gxStepName(s.getGxStepName()).approvalStatus(s.getSTATUS()).build()).collect(Collectors.toList()));
                    deviceDOS.add(sgDeviceDO);
                }
                typeVO.setDeviceDOS(deviceDOS);
                sgGxDOS.add(typeVO);
            }
            sgProjectInfoDO.setSgGxDOS(sgGxDOS);
            result.add(sgProjectInfoDO);
        }
        return result;
    }
 
    @Override
    public List<SgProjectInfoVO> progressList(ProjectSearchDTO searchDTO) {
        List<SgProjectInfoVO> list = sgProjectInfoMapper.progressList(searchDTO);
        Map<Long, List<SgProjectInfoVO>> map = list.stream().collect(Collectors.groupingBy(SgProjectInfoVO::getId));
        List<SgProjectInfoVO> result = new ArrayList<>();
        for (Map.Entry<Long, List<SgProjectInfoVO>> entry : map.entrySet()) {
            SgProjectInfoVO sgProjectInfoVO = new SgProjectInfoVO();
            List<SgProjectInfoVO> deviceList = entry.getValue();
            if (CollectionUtils.isEmpty(deviceList)){
                continue;
            }
            sgProjectInfoVO.setId(entry.getKey());
            sgProjectInfoVO.setProjectName(deviceList.get(0).getProjectName());
            List<SgProjectInfoVO> overList = deviceList.stream().filter(s->s.getDeviceStatus()!=null).filter(s -> s.getDeviceStatus() == 2).collect(Collectors.toList());
            BigDecimal v1 =BigDecimal.ZERO;
            if (CollectionUtils.isNotEmpty(overList)){
                v1 = BigDecimal.valueOf(overList.size());
            }
            BigDecimal v2 = BigDecimal.valueOf(deviceList.size());
            BigDecimal percentage = v1.divide(v2).multiply(new BigDecimal("100"));
            // 如果不需要保留小数位,可以设置一个scale为0
            BigDecimal percentageRounded = percentage.setScale(0, RoundingMode.HALF_UP);
            sgProjectInfoVO.setProgressName(percentageRounded+"%");
            result.add(sgProjectInfoVO);
        }
        return result;
    }
 
    @Override
    public Map<String, Long> projectCount(ProjectSearchDTO searchDTO) {
        Map<String, Long> map  = new HashMap<>();
        List<SgProjectInfoDO> list = new LambdaQueryChainWrapper<>(sgProjectInfoMapper).list();
        List<SgProjectInfoDO> pending = list.stream().filter(s -> s.getStatus() == 0).collect(Collectors.toList());
        List<SgProjectInfoDO> progress = list.stream().filter(s -> s.getStatus() == 1).collect(Collectors.toList());
        List<SgProjectInfoDO> over = list.stream().filter(s -> s.getStatus() == 2).collect(Collectors.toList());
        map.put("all", (long) list.size());
        map.put("pending", (long) pending.size());
        map.put("progress", (long) progress.size());
        map.put("over", (long) over.size());
        return map;
    }
}