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
package com.by4cloud.platformx.business.service.impl;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.by4cloud.platformx.business.dto.ZBJTStatisticDTO;
import com.by4cloud.platformx.business.entity.AnnualPlanKey;
import com.by4cloud.platformx.business.entity.ZbStatisticZdgz;
import com.by4cloud.platformx.business.mapper.AnnualPlanKeyMapper;
import com.by4cloud.platformx.business.mapper.ContractSubjectMatterMapper;
import com.by4cloud.platformx.business.mapper.ZbStatisticZdgzMapper;
import com.by4cloud.platformx.business.service.ZbStatisticZdgzService;
import com.by4cloud.platformx.business.vo.GcpbkwcqkItemVo;
import com.by4cloud.platformx.business.vo.SckfzdgzwcqkVo;
import com.by4cloud.platformx.common.core.util.R;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * 重点工作
 *
 * @author syt
 * @date 2026-11-12 10:28:35
 */
@Service
@RequiredArgsConstructor
public class ZbStatisticZdgzServiceImpl extends ServiceImpl<ZbStatisticZdgzMapper, ZbStatisticZdgz> implements ZbStatisticZdgzService {
 
    private final ContractSubjectMatterMapper contractSubjectMatterMapper;
    private final AnnualPlanKeyMapper annualPlanKeyMapper;
 
 
    @Override
    public R genZdgzwcqk(ZBJTStatisticDTO queryDTO) {
        if (StrUtil.isBlank(queryDTO.getYearMonth())) {
            queryDTO.setYearMonth(DateUtil.format(new Date(), DatePattern.NORM_MONTH_PATTERN));
        }
        List<SckfzdgzwcqkVo> list = new ArrayList<>();
        Date monstart = DateUtil.parseDateTime(queryDTO.getYearMonth() + "-01 00:00:00");
        Date monend = DateUtil.parseDateTime(queryDTO.getYearMonth() + "-31 23:59:59");
        Date monstartLastYear = DateUtil.offsetYear(DateUtil.parseDateTime(queryDTO.getYearMonth() + "-01 00:00:00"), -1);
        Date monendLastYear = DateUtil.offsetYear(DateUtil.parseDateTime(queryDTO.getYearMonth() + "-31 23:59:59"), -1);
        Date yearstart = DateUtil.parseDateTime(queryDTO.getYearMonth().substring(0, 4) + "-01-01 00:00:00");
        Date yearend = DateUtil.parseDateTime(queryDTO.getYearMonth() + "-31 23:59:59");
        Date yearstartLastYear = DateUtil.offsetYear(DateUtil.parseDateTime(queryDTO.getYearMonth().substring(0, 4) + "-01-01 00:00:00"), -1);
        Date yearendLastYear = DateUtil.offsetYear(DateUtil.parseDateTime(queryDTO.getYearMonth() + "-31 23:59:59"), -1);
        String curYear = queryDTO.getYearMonth().substring(0, 4);
        String curMonth = queryDTO.getYearMonth().substring(5);
        YearMonth qndy = YearMonth.of(Integer.valueOf(curYear), Integer.valueOf(curMonth)).minusYears(1);
        YearMonth sy = YearMonth.of(Integer.valueOf(curYear), Integer.valueOf(curMonth)).minusMonths(1);
        //新增合同
        List<GcpbkwcqkItemVo> itemVoListMon = contractSubjectMatterMapper.sckfzdgzwcqk(monstart, monend);
        if (CollUtil.isNotEmpty(itemVoListMon)) {
            itemVoListMon.stream().forEach(item -> {
                ZbStatisticZdgz vo = new ZbStatisticZdgz();
                vo.setDw(item.getDw());
                vo.setXm(item.getXm());
                vo.setStatYm(queryDTO.getYearMonth());
                Long compId = null;
                if (StrUtil.equals(item.getDw(), "石煤机")) {
                    compId = 2089995177814495234L;
                    vo.setCompId(2089995177814495234L);
                }
                if (StrUtil.equals(item.getDw(), "石工泵")) {
                    compId = 2056556196124250113L;
                    vo.setCompId(2056556196124250113L);
                }
                if (StrUtil.equals(item.getDw(), "通方厂")) {
                    compId = 2056556317461270529L;
                    vo.setCompId(2056556317461270529L);
                }
                if (StrUtil.equals(item.getDw(), "邢台厂")) {
                    compId = 2056556421857497090L;
                    vo.setCompId(2056556421857497090L);
                }
                AnnualPlanKey plan = annualPlanKeyMapper.selectOne(Wrappers.<AnnualPlanKey>lambdaQuery()
                        .eq(AnnualPlanKey::getPlanYear, queryDTO.getYearMonth().substring(0, 4)).eq(AnnualPlanKey::getPlanType, item.getCode())
                        .eq(AnnualPlanKey::getCompId, compId).last("limit 1"));
                ZbStatisticZdgz vo1 = baseMapper.selectOne(Wrappers.<ZbStatisticZdgz>lambdaQuery().eq(ZbStatisticZdgz::getDw, vo.getDw())
                        .eq(ZbStatisticZdgz::getXm, vo.getXm()).eq(ZbStatisticZdgz::getStatYm, vo.getStatYm()).last("limit 1"));
                if (ObjUtil.isNotNull(vo1)) {
                    if (ObjUtil.isNotNull(plan)) {
                        vo1.setDywc(item.getWc());
                        vo1.setDyjh(plan.getMonthAmount(queryDTO.getYearMonth().substring(5)));
                        vo1.setLjjh(plan.getToMonTotalAmount(queryDTO.getYearMonth().substring(5)));
                        vo1.setNdjh(plan.getTotalAmount());
                        if (vo1.getDyjh().compareTo(new BigDecimal("0")) > 0) {
                            vo1.setYwcl(vo1.getDywc().divide(vo1.getDyjh(), 10, RoundingMode.HALF_UP).multiply(new BigDecimal("100"))
                                    .setScale(2, RoundingMode.HALF_UP) + "%");
                        }
                    }
                    ZbStatisticZdgz voQndy = baseMapper.selectOne(Wrappers.<ZbStatisticZdgz>lambdaQuery().eq(ZbStatisticZdgz::getDw, vo.getDw())
                            .eq(ZbStatisticZdgz::getXm, vo.getXm()).eq(ZbStatisticZdgz::getStatYm, qndy).last("limit 1"));
                    ZbStatisticZdgz voSy = baseMapper.selectOne(Wrappers.<ZbStatisticZdgz>lambdaQuery().eq(ZbStatisticZdgz::getDw, vo.getDw())
                            .eq(ZbStatisticZdgz::getXm, vo.getXm()).eq(ZbStatisticZdgz::getStatYm, sy).last("limit 1"));
                    if (ObjUtil.isNotNull(voSy)) {
                        vo1.setLjwc(voSy.getLjwc().add(vo1.getDywc()));
                        if (ObjUtil.isNotNull(vo1.getLjjh()) && vo1.getLjjh().compareTo(new BigDecimal("0")) > 0) {
                            vo1.setLjwcl(vo1.getLjwc().divide(vo1.getLjjh(), 10, RoundingMode.HALF_UP).multiply(new BigDecimal("100"))
                                    .setScale(2, RoundingMode.HALF_UP) + "%");
                        }
                    }
                    if (ObjUtil.isNotNull(voQndy) && voQndy.getLjwc().compareTo(new BigDecimal("0")) > 0) {
                        BigDecimal percent = vo1.getLjwc().subtract(voQndy.getLjwc()).divide(voQndy.getLjwc(), 10, RoundingMode.HALF_UP).multiply(new BigDecimal("100"));
                        vo1.setLjtb(percent.setScale(2, RoundingMode.HALF_UP) + "%");
                    }
                    baseMapper.updateById(vo1);
                } else {
                    if (ObjUtil.isNotNull(plan)) {
                        vo.setDywc(item.getWc());
                        vo.setDyjh(plan.getMonthAmount(queryDTO.getYearMonth().substring(5)));
                        vo.setLjjh(plan.getToMonTotalAmount(queryDTO.getYearMonth().substring(5)));
                        vo.setNdjh(plan.getTotalAmount());
                        if (vo.getDyjh().compareTo(new BigDecimal("0")) > 0) {
                            vo.setYwcl(vo.getDywc().divide(vo.getDyjh(), 10, RoundingMode.HALF_UP).multiply(new BigDecimal("100"))
                                    .setScale(2, RoundingMode.HALF_UP) + "%");
                        }
                    }
                    ZbStatisticZdgz voQndy = baseMapper.selectOne(Wrappers.<ZbStatisticZdgz>lambdaQuery().eq(ZbStatisticZdgz::getDw, vo.getDw())
                            .eq(ZbStatisticZdgz::getXm, vo.getXm()).eq(ZbStatisticZdgz::getStatYm, qndy).last("limit 1"));
                    ZbStatisticZdgz voSy = baseMapper.selectOne(Wrappers.<ZbStatisticZdgz>lambdaQuery().eq(ZbStatisticZdgz::getDw, vo.getDw())
                            .eq(ZbStatisticZdgz::getXm, vo.getXm()).eq(ZbStatisticZdgz::getStatYm, sy).last("limit 1"));
                    if (ObjUtil.isNotNull(voSy)) {
                        vo.setLjwc(voSy.getLjwc().add(vo.getDywc()== null ? BigDecimal.ZERO :vo.getDywc()));
                        if (ObjUtil.isNotNull(vo.getLjjh()) && vo.getLjjh().compareTo(new BigDecimal("0")) > 0) {
                            vo.setLjwcl(vo.getLjwc().divide(vo.getLjjh(), 10, RoundingMode.HALF_UP).multiply(new BigDecimal("100"))
                                    .setScale(2, RoundingMode.HALF_UP) + "%");
                        }
                    }
                    if (ObjUtil.isNotNull(voQndy) && voQndy.getLjwc().compareTo(new BigDecimal("0")) > 0) {
                        BigDecimal percent = vo.getLjwc().subtract(voQndy.getLjwc()).divide(voQndy.getLjwc(), 10, RoundingMode.HALF_UP).multiply(new BigDecimal("100"));
                        vo.setLjtb(percent.setScale(2, RoundingMode.HALF_UP) + "%");
                    }
                    baseMapper.insert(vo);
                }
 
            });
        }
//        List<GcpbkwcqkItemVo> itemVoListYear = contractSubjectMatterMapper.sckfzdgzwcqk(yearstart, yearend);
//        List<GcpbkwcqkItemVo> itemVoListYearLastYear = contractSubjectMatterMapper.sckfzdgzwcqk(yearstartLastYear, yearendLastYear);
//        if (CollUtil.isNotEmpty(list) && CollUtil.isNotEmpty(itemVoListYear)) {
//            for (GcpbkwcqkItemVo itemVo : itemVoListYear
//            ) {
//                ZbStatisticZdgz vo = baseMapper.selectOne(Wrappers.<ZbStatisticZdgz>lambdaQuery().eq(ZbStatisticZdgz::getDw, itemVo.getDw())
//                        .eq(ZbStatisticZdgz::getXm, itemVo.getXm()).eq(ZbStatisticZdgz::getStatYm, queryDTO.getYearMonth()).last("limit 1"));
//                if (StrUtil.equals(vo.getDw(), itemVo.getDw()) && StrUtil.equals(vo.getXm(), itemVo.getXm())) {
//                    vo.setLjwc(itemVo.getWc());
//                    if (ObjUtil.isNotNull(vo.getLjjh()) && vo.getLjjh().compareTo(new BigDecimal("0")) > 0) {
//                        vo.setLjwcl(vo.getLjwc().divide(vo.getLjjh(), 10, RoundingMode.HALF_UP).multiply(new BigDecimal("100"))
//                                .setScale(2, RoundingMode.HALF_UP) + "%");
//                    }
//                }
//                if (CollUtil.isNotEmpty(itemVoListYearLastYear)) {
//                    itemVoListYearLastYear.stream().forEach(item1 -> {
//                        if (StrUtil.equals(vo.getDw(), item1.getDw()) && StrUtil.equals(vo.getXm(), itemVo.getXm()) &&
//                                item1.getWc().compareTo(new BigDecimal("0")) > 0) {
//                            BigDecimal percent = vo.getLjwc().subtract(item1.getWc()).divide(item1.getWc(), 10, RoundingMode.HALF_UP).multiply(new BigDecimal("100"));
//                            vo.setLjtb(percent.setScale(2, RoundingMode.HALF_UP) + "%");
//                        }
//                    });
//                }
//                baseMapper.updateById(vo);
//            }
//        }
        return R.ok();
    }
}