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);
|
}
|
}
|