李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package com.chinaztt.mes.production.service.impl;
 
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaztt.mes.production.dto.ProductOutputDTO;
import com.chinaztt.mes.production.dto.ShiftWageDTO;
import com.chinaztt.mes.production.entity.PersonBoard;
import com.chinaztt.mes.production.entity.ShiftWage;
import com.chinaztt.mes.production.entity.UnitWork;
import com.chinaztt.mes.production.mapper.PersonBoardMapper;
import com.chinaztt.mes.production.mapper.SegmentationTaskRecordMapper;
import com.chinaztt.mes.production.mapper.ShiftWageMapper;
import com.chinaztt.mes.production.mapper.UnitWorkMapper;
import com.chinaztt.mes.production.service.DutyRecordService;
import com.chinaztt.mes.production.service.ShiftWageService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
 
/**
 * @Description : 班次工资
 * @ClassName : ShiftWageServiceImpl
 * @Author : user
 * @Date: 2022-12-03  20:05
 */
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class ShiftWageServiceImpl extends ServiceImpl<ShiftWageMapper, ShiftWage> implements ShiftWageService{
    private DutyRecordService dutyRecordService;
    private PersonBoardMapper personBoardMapper;
    private SegmentationTaskRecordMapper segmentationTaskRecordMapper;
    private UnitWorkMapper unitWorkMapper;
 
    @Override
    public List<ShiftWageDTO> qryShiftWageByDutyRecordId(Long dutyRecordId){
        List<ShiftWageDTO> shiftWageDTOList = baseMapper.qryShiftWageByDutyRecordId(dutyRecordId);
        if(CollectionUtils.isNotEmpty(shiftWageDTOList)){
            return shiftWageDTOList;//有则直接返回
        }else{
            //无则创建班次工资记录
 
            //1.获取班次杂工工资汇总
            List<ShiftWageDTO.HandymanWageBean> handymanWageBeanList = baseMapper.qryHandymanWageByDutyRecordId(dutyRecordId);
 
            //2.获取产量工资汇总
            List<ProductOutputDTO> productOutputDTOListSalary = dutyRecordService.getOutputByDutyRecordId(dutyRecordId);//用于取产量工资
            List<ProductOutputDTO> productOutputDTOListQty = dutyRecordService.getStaffProductionWage(dutyRecordId);//用于按零件分组取对应的零件产量
            List<ProductOutputDTO> segmentationOutputDTOListQty = segmentationTaskRecordMapper.getStaffProductionWage(dutyRecordId);//取分割任务产量
            if (CollectionUtils.isNotEmpty(segmentationOutputDTOListQty)) {
                for (ProductOutputDTO productOutputDTO : segmentationOutputDTOListQty) {
                    Integer count = unitWorkMapper.selectCount(Wrappers.<UnitWork>lambdaQuery()
                            .eq(UnitWork::getPartId, productOutputDTO.getPartId())
                            .eq(UnitWork::getWorkstationId, productOutputDTO.getWorkstationId())
                            .isNull(UnitWork::getOperationId));
                    if (count != 1) {
                        throw new RuntimeException("工作站【" + productOutputDTO.getWorkstationName() + "】,零件号【" + productOutputDTO.getPartNo() + "】,没有匹配到单位工时系数或者匹配到多条单位工时系数,无法计算");
                    }
                }
                productOutputDTOListQty.addAll(segmentationOutputDTOListQty);
            }
 
            BigDecimal productionWage = BigDecimal.ZERO;//初始化产量工资
            if(CollectionUtils.isNotEmpty(productOutputDTOListSalary)){
                for(ProductOutputDTO productOutputDTO : productOutputDTOListSalary){
                    productionWage = productionWage.add(null == productOutputDTO.getQtySalary() ? BigDecimal.ZERO : productOutputDTO.getQtySalary());
                }
            }
 
            //3.获取班次人员
            List<PersonBoard> personBoardList = personBoardMapper.selectList(Wrappers.<PersonBoard>lambdaQuery().eq(PersonBoard :: getDutyRecordId,dutyRecordId));
            for(PersonBoard personBoard : personBoardList){
                ShiftWage shiftWage = new ShiftWage();
 
                shiftWage.setPartDesc("");//初始化零件描述
                shiftWage.setPartQuantity("");//初始化零件对应的产量
 
                shiftWage.setDutyRecordId(dutyRecordId);//班次id
                shiftWage.setStaffId(personBoard.getStaffId());//员工id
                shiftWage.setOperationCoeff(new BigDecimal("1"));//工序系数
 
                //产量工资
                shiftWage.setProductionWage(BigDecimal.ZERO);
 
                // 辅助机台数默认1
                shiftWage.setAuxiliaryWorkstationNum(1);
 
                //按零件名称进行分组
                List<ProductOutputDTO> productOutputDTOListStaff = productOutputDTOListQty.stream().filter(o -> o.getStaffId().equals(personBoard.getStaffId().toString())).collect(Collectors.toList());//过滤出对应人员的产出对应的产量工资
                Set<String> partNames =  productOutputDTOListStaff.stream().map(o -> o.getPartName()).collect(Collectors.toSet());//去重
 
                //汇总对应零件的产量
                for(String partName : partNames){
                    List<ProductOutputDTO> productOutputDTOS = productOutputDTOListStaff.stream().filter(o -> o.getPartName().equals(partName)).collect(Collectors.toList());
                    // 该零件按人报工的所有产量
                    List<ProductOutputDTO> productOutputDTOSPerson = productOutputDTOListQty.stream().filter(o -> o.getPartName().equals(partName) && o.getArtificialType()).collect(Collectors.toList());
                    // 该零件按组报工的所有产量
                    List<ProductOutputDTO> productOutputDTOSGroup = productOutputDTOListStaff.stream().filter(o -> o.getPartName().equals(partName) && !o.getArtificialType()).collect(Collectors.toList());
 
                    BigDecimal qtySum = BigDecimal.ZERO;
                    for(ProductOutputDTO productOutputDTO : productOutputDTOS){
                        qtySum = qtySum.add(productOutputDTO.getProductQty());
                    }
 
                    BigDecimal qtySumGroup = BigDecimal.ZERO;
                    BigDecimal qtySumGroupWage = BigDecimal.ZERO;
                    // 组产量 = 该组所有人该零件按人报工的产量 + 按组报工的产量
                    if (CollectionUtils.isNotEmpty(productOutputDTOSPerson)) {
                        for(ProductOutputDTO productOutputDTO : productOutputDTOSPerson){
                            if (productOutputDTO.getUnitWorkFactor() == null) {
                                throw new RuntimeException("工作站【" + productOutputDTO.getWorkstationName() + "】,零件号【" + productOutputDTO.getPartNo() + "】,工序【"+ productOutputDTO.getOperationName() +"】,没有设置单位工时系数,无法计算");
                            }
                            qtySumGroup = qtySumGroup.add(productOutputDTO.getProductQty());
                            qtySumGroupWage = qtySumGroupWage.add((productOutputDTO.getProductQty().multiply(productOutputDTO.getUnitWorkFactor()).multiply(new BigDecimal("88"))));
                        }
                    }
                    if (CollectionUtils.isNotEmpty(productOutputDTOSGroup)) {
                        for (ProductOutputDTO productOutputDTO : productOutputDTOSGroup) {
                            if (productOutputDTO.getUnitWorkFactor() == null) {
                                throw new RuntimeException("工作站【" + productOutputDTO.getWorkstationName() + "】,零件号【" + productOutputDTO.getPartNo() + "】,工序【"+ productOutputDTO.getOperationName() +"】,没有设置单位工时系数,无法计算");
                            }
                            qtySumGroup = qtySumGroup.add(productOutputDTO.getProductQty());
                            qtySumGroupWage = qtySumGroupWage.add((productOutputDTO.getProductQty().multiply(productOutputDTO.getUnitWorkFactor()).multiply(new BigDecimal("88"))));
                        }
                    }
 
                    if(shiftWage.getPartDesc().equals("")){
                        shiftWage.setPartDesc(partName);
                        shiftWage.setPartQuantity(qtySum.toString());
                        shiftWage.setPartQuantityGroup(qtySumGroup.toString());
                    }else{
                        shiftWage.setPartDesc(shiftWage.getPartDesc() + "$" + partName);
                        shiftWage.setPartQuantity(shiftWage.getPartQuantity() + "$" + qtySum.toString());
                        shiftWage.setPartQuantityGroup(shiftWage.getPartQuantityGroup() + "$" + qtySumGroup.toString());
                    }
 
                    // 产量工资 = 组产量 * 单位工时系数 * 88
                    shiftWage.setProductionWage(shiftWage.getProductionWage().add(qtySumGroupWage));
                }
 
                //初始化杂工工资
                shiftWage.setHandymanWage(BigDecimal.ZERO);
                for(ShiftWageDTO.HandymanWageBean handymanWageBean : handymanWageBeanList){
                    if(handymanWageBean.getStaffId().equals(shiftWage.getStaffId())){
                        shiftWage.setHandymanWage(handymanWageBean.getHandymanWage());//杂工工资(已按人员汇总过)
                    }
                }
                //工资合计
                shiftWage.setSummaryWage(shiftWage.getProductionWage().multiply(shiftWage.getOperationCoeff()).divide(new BigDecimal(shiftWage.getAuxiliaryWorkstationNum()), 2, RoundingMode.HALF_UP));
                shiftWage.setSummaryWage(shiftWage.getSummaryWage().add(shiftWage.getHandymanWage()));
 
                baseMapper.insert(shiftWage);//保存
            }
 
            return baseMapper.qryShiftWageByDutyRecordId(dutyRecordId);
        }
    }
 
 
    @Override
    public List<ShiftWageDTO> qryShiftWageByDutyRecordIdList(List<Long> dutyRecordIdList) {
        List<Long> newDutyRecordIdList = new ArrayList<>(dutyRecordIdList);
        List<ShiftWageDTO> shiftWageDTOList = baseMapper.qryShiftWageByDutyRecordIdList(dutyRecordIdList);
        List<Long> filterDutyRecordIdList = shiftWageDTOList.stream().map(ShiftWageDTO::getDutyRecordId).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(filterDutyRecordIdList)){
            dutyRecordIdList.removeAll(filterDutyRecordIdList);
        }
        //无则创建班次工资记录
        //1.获取班次杂工工资汇总
        for (Long dutyRecordId:dutyRecordIdList) {
            List<ShiftWageDTO.HandymanWageBean> handymanWageBeanList = baseMapper.qryHandymanWageByDutyRecordId(dutyRecordId);
 
            //2.获取产量工资汇总
            List<ProductOutputDTO> productOutputDTOListSalary = dutyRecordService.getOutputByDutyRecordId(dutyRecordId);//用于取产量工资
            List<ProductOutputDTO> productOutputDTOListQty = dutyRecordService.getStaffProductionWage(dutyRecordId);//用于按零件分组取对应的零件产量
            List<ProductOutputDTO> segmentationOutputDTOListQty = segmentationTaskRecordMapper.getStaffProductionWage(dutyRecordId);//取分割任务产量
            if (CollectionUtils.isNotEmpty(segmentationOutputDTOListQty)) {
                for (ProductOutputDTO productOutputDTO : segmentationOutputDTOListQty) {
                    Integer count = unitWorkMapper.selectCount(Wrappers.<UnitWork>lambdaQuery()
                            .eq(UnitWork::getPartId, productOutputDTO.getPartId())
                            .eq(UnitWork::getWorkstationId, productOutputDTO.getWorkstationId())
                            .isNull(UnitWork::getOperationId));
                    if (count != 1) {
                        throw new RuntimeException("工作站【" + productOutputDTO.getWorkstationName() + "】,零件号【" + productOutputDTO.getPartNo() + "】,没有匹配到单位工时系数或者匹配到多条单位工时系数,无法计算");
                    }
                }
                productOutputDTOListQty.addAll(segmentationOutputDTOListQty);
            }
 
            BigDecimal productionWage = BigDecimal.ZERO;//初始化产量工资
            if(CollectionUtils.isNotEmpty(productOutputDTOListSalary)){
                for(ProductOutputDTO productOutputDTO : productOutputDTOListSalary){
                    productionWage = productionWage.add(null == productOutputDTO.getQtySalary() ? BigDecimal.ZERO : productOutputDTO.getQtySalary());
                }
            }
 
            //3.获取班次人员
            List<PersonBoard> personBoardList = personBoardMapper.selectList(Wrappers.<PersonBoard>lambdaQuery().eq(PersonBoard :: getDutyRecordId,dutyRecordId));
 
            for(PersonBoard personBoard : personBoardList){
                ShiftWage shiftWage = new ShiftWage();
 
                shiftWage.setPartDesc("");//初始化零件描述
                shiftWage.setPartQuantity("");//初始化零件对应的产量
 
                shiftWage.setDutyRecordId(dutyRecordId);//班次id
                shiftWage.setStaffId(personBoard.getStaffId());//员工id
                shiftWage.setOperationCoeff(new BigDecimal("1"));//工序系数
 
                //产量工资
                shiftWage.setProductionWage(BigDecimal.ZERO);
 
                // 辅助机台数默认1
                shiftWage.setAuxiliaryWorkstationNum(1);
 
                //按零件名称进行分组
                List<ProductOutputDTO> productOutputDTOListStaff = productOutputDTOListQty.stream().filter(o -> o.getStaffId().equals(personBoard.getStaffId().toString())).collect(Collectors.toList());//过滤出对应人员的产出对应的产量工资
                Set<String> partNames =  productOutputDTOListStaff.stream().map(o -> o.getPartName()).collect(Collectors.toSet());//去重
 
                //汇总对应零件的产量
                for(String partName : partNames){
                    List<ProductOutputDTO> productOutputDTOS = productOutputDTOListStaff.stream().filter(o -> o.getPartName().equals(partName)).collect(Collectors.toList());
                    // 该零件按人报工的所有产量
                    List<ProductOutputDTO> productOutputDTOSPerson = productOutputDTOListQty.stream().filter(o -> o.getPartName().equals(partName) && o.getArtificialType()).collect(Collectors.toList());
                    // 该零件按组报工的所有产量
                    List<ProductOutputDTO> productOutputDTOSGroup = productOutputDTOListStaff.stream().filter(o -> o.getPartName().equals(partName) && !o.getArtificialType()).collect(Collectors.toList());
 
                    // 单位工时系数
                    // BigDecimal unitWorkFactor = productOutputDTOS.get(0).getUnitWorkFactor();
                    // if (unitWorkFactor == null) {
                    //     throw new RuntimeException("工作站【" + productOutputDTOS.get(0).getWorkstationName() + "】,零件【" + partName + "】,工序【"+ productOutputDTOS.get(0).getOperationName() +"】,没有设置单位工时系数,无法计算");
                    // }
 
                    BigDecimal qtySum = BigDecimal.ZERO;
                    for(ProductOutputDTO productOutputDTO : productOutputDTOS){
                        qtySum = qtySum.add(productOutputDTO.getProductQty());
                    }
 
                    BigDecimal qtySumGroup = BigDecimal.ZERO;
                    BigDecimal qtySumGroupWage = BigDecimal.ZERO;
                    // 组产量 = 该组所有人该零件按人报工的产量 + 按组报工的产量
                    if (CollectionUtils.isNotEmpty(productOutputDTOSPerson)) {
                        for(ProductOutputDTO productOutputDTO : productOutputDTOSPerson){
                            if (productOutputDTO.getUnitWorkFactor() == null) {
                                throw new RuntimeException("工作站【" + productOutputDTO.getWorkstationName() + "】,零件号【" + productOutputDTO.getPartNo() + "】,工序【"+ productOutputDTO.getOperationName() +"】,没有设置单位工时系数,无法计算");
                            }
                            qtySumGroup = qtySumGroup.add(productOutputDTO.getProductQty());
                            qtySumGroupWage = qtySumGroupWage.add((productOutputDTO.getProductQty().multiply(productOutputDTO.getUnitWorkFactor()).multiply(new BigDecimal("88"))));
                        }
                    }
                    if (CollectionUtils.isNotEmpty(productOutputDTOSGroup)) {
                        for (ProductOutputDTO productOutputDTO : productOutputDTOSGroup) {
                            if (productOutputDTO.getUnitWorkFactor() == null) {
                                throw new RuntimeException("工作站【" + productOutputDTO.getWorkstationName() + "】,零件号【" + productOutputDTO.getPartNo() + "】,工序【"+ productOutputDTO.getOperationName() +"】,没有设置单位工时系数,无法计算");
                            }
                            qtySumGroup = qtySumGroup.add(productOutputDTO.getProductQty());
                            qtySumGroupWage = qtySumGroupWage.add((productOutputDTO.getProductQty().multiply(productOutputDTO.getUnitWorkFactor()).multiply(new BigDecimal("88"))));
                        }
                    }
 
                    if(shiftWage.getPartDesc().equals("")){
                        shiftWage.setPartDesc(partName);
                        shiftWage.setPartQuantity(qtySum.toString());
                        shiftWage.setPartQuantityGroup(qtySumGroup.toString());
                    }else{
                        shiftWage.setPartDesc(shiftWage.getPartDesc() + "$" + partName);
                        shiftWage.setPartQuantity(shiftWage.getPartQuantity() + "$" + qtySum.toString());
                        shiftWage.setPartQuantityGroup(shiftWage.getPartQuantityGroup() + "$" + qtySumGroup.toString());
                    }
 
                    // 产量工资 = 组产量 * 单位工时系数 * 88
                    shiftWage.setProductionWage(shiftWage.getProductionWage().add(qtySumGroupWage).setScale(2, RoundingMode.HALF_UP));
                }
 
                //初始化杂工工资
                shiftWage.setHandymanWage(BigDecimal.ZERO);
                for(ShiftWageDTO.HandymanWageBean handymanWageBean : handymanWageBeanList){
                    if(handymanWageBean.getStaffId().equals(shiftWage.getStaffId())){
                        shiftWage.setHandymanWage(handymanWageBean.getHandymanWage());//杂工工资(已按人员汇总过)
                    }
                }
                //工资合计
                shiftWage.setSummaryWage(shiftWage.getProductionWage().multiply(shiftWage.getOperationCoeff()).divide(new BigDecimal(shiftWage.getAuxiliaryWorkstationNum()), 2, RoundingMode.HALF_UP));
                shiftWage.setSummaryWage(shiftWage.getSummaryWage().add(shiftWage.getHandymanWage()));
 
                baseMapper.insert(shiftWage);//保存
            }
        }
        return baseMapper.qryShiftWageByDutyRecordIdList(newDutyRecordIdList);
 
    }
 
    @Override
    public List<ShiftWageDTO> saveShiftWage(List<ShiftWageDTO> shiftWageDTOList){
        if(CollectionUtils.isEmpty(shiftWageDTOList)){
            throw new RuntimeException("请求参数集合为空");
        }
 
        for(ShiftWageDTO shiftWageDTO : shiftWageDTOList){
            //班次工资 = 产量工资 * 工序系数 + 杂工工资
            if(null == shiftWageDTO.getProductionWage()){
                shiftWageDTO.setProductionWage(BigDecimal.ZERO);
            }
            if(null == shiftWageDTO.getOperationCoeff()){
                shiftWageDTO.setOperationCoeff(BigDecimal.ZERO);
            }
            if(null == shiftWageDTO.getHandymanWage()){
                shiftWageDTO.setHandymanWage(BigDecimal.ZERO);
            }
            if (null == shiftWageDTO.getAuxiliaryWorkstationNum()) {
                shiftWageDTO.setAuxiliaryWorkstationNum(0);
            }
 
            shiftWageDTO.setSummaryWage(shiftWageDTO.getProductionWage().multiply(shiftWageDTO.getOperationCoeff()).divide(new BigDecimal(shiftWageDTO.getAuxiliaryWorkstationNum()), 2, RoundingMode.HALF_UP).add(shiftWageDTO.getHandymanWage()));
            //更新
            baseMapper.updateById(shiftWageDTO);
        }
 
        //更新班次记录为状态为“已审核”
        if(CollectionUtils.isNotEmpty(shiftWageDTOList)){
            List<Long> collect = shiftWageDTOList.stream().map(ShiftWageDTO::getDutyRecordId).collect(Collectors.toList());
            baseMapper.updShiftWageIsAuditValueByList(collect,true);
        }
        return shiftWageDTOList;
    }
 
    @Override
    public ShiftWageDTO getCountResult(ShiftWageDTO shiftWageDTO){
        //班次工资 = 产量工资 * 工序系数 + 杂工工资
        if(null == shiftWageDTO.getProductionWage()){
            shiftWageDTO.setProductionWage(BigDecimal.ZERO);
        }
        if(null == shiftWageDTO.getOperationCoeff()){
            shiftWageDTO.setOperationCoeff(BigDecimal.ZERO);
        }
        if(null == shiftWageDTO.getHandymanWage()){
            shiftWageDTO.setHandymanWage(BigDecimal.ZERO);
        }
        if (null == shiftWageDTO.getAuxiliaryWorkstationNum()) {
            shiftWageDTO.setAuxiliaryWorkstationNum(0);
        }
 
        shiftWageDTO.setSummaryWage(shiftWageDTO.getProductionWage().multiply(shiftWageDTO.getOperationCoeff()).divide(new BigDecimal(shiftWageDTO.getAuxiliaryWorkstationNum()), 2, RoundingMode.HALF_UP).add(shiftWageDTO.getHandymanWage()));
        return shiftWageDTO;
    }
 
    @Override
    public List<ShiftWageDTO> summaryAgain(Long dutyRecordId){
        //清除班次工资
        baseMapper.delShiftWageByDutyRecordId(dutyRecordId);
 
        //更新班次记录的状态为“未审核”
        baseMapper.updShiftWageIsAuditValue(dutyRecordId,false);
 
        //重新汇总班次工资
        return qryShiftWageByDutyRecordId(dutyRecordId);
    }
 
    @Override
    public List<ShiftWageDTO> summaryAll(List<Long> dutyRecordIdList){
        //清除班次工资
        baseMapper.delShiftWageByDutyRecordIdList(dutyRecordIdList);
 
        //更新班次记录的状态为“未审核”
        baseMapper.updShiftWageIsAuditValueByList(dutyRecordIdList,false);
 
        //重新汇总班次工资
        return qryShiftWageByDutyRecordIdList(dutyRecordIdList);
    }
}