maven
2 天以前 0ef7a6b7f63a1f7b866bccef4eabf64041d5bbad
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
package com.ruoyi.production.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.production.dto.ProductionReportDto;
import com.ruoyi.production.dto.SalesLedgerWorkDto;
import com.ruoyi.production.mapper.SalesLedgerProductionAccountingMapper;
import com.ruoyi.production.mapper.SalesLedgerWorkMapper;
import com.ruoyi.production.pojo.SalesLedgerProductionAccounting;
import com.ruoyi.production.pojo.SalesLedgerWork;
import com.ruoyi.production.service.SalesLedgerProductionAccountingService;
import com.ruoyi.production.service.SalesLedgerWorkService;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.mapper.SysUserMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @author :yys
 * @date : 2025/7/21 14:40
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class SalesLedgerWorkServiceImpl extends ServiceImpl<SalesLedgerWorkMapper, SalesLedgerWork> implements SalesLedgerWorkService {
 
    private final SalesLedgerWorkMapper salesLedgerWorkMapper;
 
    private final SysUserMapper sysUserMapper;
 
    private final SalesLedgerProductionAccountingMapper salesLedgerProductionAccountingMapper;
 
    @Override
    public IPage<SalesLedgerWorkDto> listPage(Page page, SalesLedgerWorkDto salesLedgerWorkDto) {
        IPage<SalesLedgerWorkDto> iPage = salesLedgerWorkMapper.listPage(page, salesLedgerWorkDto);
        return iPage;
    }
 
    @Override
    public int productionReport(ProductionReportDto productionReportDto) {
        SalesLedgerWork salesLedgerWork = salesLedgerWorkMapper.selectById(productionReportDto.getId());
        SysUser sysUser = sysUserMapper.selectUserById(productionReportDto.getSchedulingUserId());
        if (salesLedgerWork == null) throw new RuntimeException("报工数据不存在");
        if (salesLedgerWork.getStatus() == 3) throw new RuntimeException("报工已完成");
        if (sysUser == null) throw new RuntimeException("生产人不存在");
        salesLedgerWork.setFinishedNum(salesLedgerWork.getFinishedNum().add(productionReportDto.getFinishedNum()));
        if(salesLedgerWork.getSchedulingNum().compareTo(salesLedgerWork.getFinishedNum()) <= 0){
            salesLedgerWork.setStatus(3);
        }else{
            salesLedgerWork.setStatus(2);
        }
        salesLedgerWorkMapper.updateById(salesLedgerWork);
        // 新增报工数据
        SalesLedgerProductionAccounting.SalesLedgerProductionAccountingBuilder builder = SalesLedgerProductionAccounting.builder()
                .salesLedgerWorkId(salesLedgerWork.getId())
                .salesLedgerSchedulingId(salesLedgerWork.getSalesLedgerSchedulingId())
                .salesLedgerId(salesLedgerWork.getSalesLedgerId())
                .salesLedgerProductId(salesLedgerWork.getSalesLedgerProductId())
                .schedulingUserId(sysUser.getUserId())
                .schedulingUserName(sysUser.getNickName())
                .finishedNum(productionReportDto.getFinishedNum())
                .workHours(salesLedgerWork.getWorkHours())
                .process(salesLedgerWork.getProcess())
                .schedulingDate(LocalDate.parse(productionReportDto.getSchedulingDate(), DateTimeFormatter.ISO_LOCAL_DATE));
        salesLedgerProductionAccountingMapper.insert(builder.build());
        return 0;
    }
 
    @Override
    public int productionReportUpdate(ProductionReportDto productionReportDto) {
        SalesLedgerWork salesLedgerWork = salesLedgerWorkMapper.selectById(productionReportDto.getId());
        if(salesLedgerWork == null) throw new RuntimeException("报工数据不存在");
        SysUser sysUser = sysUserMapper.selectUserById(productionReportDto.getSchedulingUserId());
        if(sysUser == null) throw new RuntimeException("生产人不存在");
        salesLedgerWork.setFinishedNum(productionReportDto.getFinishedNum());
        if(salesLedgerWork.getSchedulingNum().compareTo(salesLedgerWork.getFinishedNum()) <= 0){
            salesLedgerWork.setStatus(3);
        }else{
            salesLedgerWork.setStatus(2);
        }
        salesLedgerWork.setSchedulingUserId(sysUser.getUserId());
        salesLedgerWork.setSchedulingUserName(sysUser.getNickName());
        salesLedgerWork.setSchedulingDate(LocalDate.parse(productionReportDto.getSchedulingDate(), DateTimeFormatter.ISO_LOCAL_DATE));
        salesLedgerWorkMapper.updateById(salesLedgerWork);
 
        // 更新核算数据
        LambdaQueryWrapper<SalesLedgerProductionAccounting> salesLedgerProductionAccountingLambdaQueryWrapper = new LambdaQueryWrapper<>();
        salesLedgerProductionAccountingLambdaQueryWrapper.eq(SalesLedgerProductionAccounting::getSalesLedgerWorkId, salesLedgerWork.getId())
                .orderByDesc(SalesLedgerProductionAccounting::getCreateTime)
                .last("limit 1");
        SalesLedgerProductionAccounting salesLedgerProductionAccounting = salesLedgerProductionAccountingMapper.selectOne(salesLedgerProductionAccountingLambdaQueryWrapper);
        if(salesLedgerProductionAccounting != null){
            salesLedgerProductionAccounting.setFinishedNum(productionReportDto.getFinishedNum());
            salesLedgerProductionAccounting.setSchedulingUserId(sysUser.getUserId());
            salesLedgerProductionAccounting.setSchedulingUserName(sysUser.getNickName());
            salesLedgerProductionAccounting.setSchedulingDate(LocalDate.parse(productionReportDto.getSchedulingDate(), DateTimeFormatter.ISO_LOCAL_DATE));
            salesLedgerProductionAccountingMapper.updateById(salesLedgerProductionAccounting);
        }
        return 0;
    }
 
    @Override
    public List<ProductionReportDto> getList(Long id) {
        SalesLedgerWork salesLedgerWork = salesLedgerWorkMapper.selectById(id);
        if(salesLedgerWork == null) throw new RuntimeException("报工数据不存在");
        LambdaQueryWrapper<SalesLedgerProductionAccounting> salesLedgerProductionAccountingLambdaQueryWrapper = new LambdaQueryWrapper<>();
        salesLedgerProductionAccountingLambdaQueryWrapper.eq(SalesLedgerProductionAccounting::getSalesLedgerWorkId, id);
        List<SalesLedgerProductionAccounting> salesLedgerProductionAccountingList = salesLedgerProductionAccountingMapper.selectList(salesLedgerProductionAccountingLambdaQueryWrapper);
        if(CollectionUtils.isEmpty(salesLedgerProductionAccountingList)) throw new RuntimeException("没有生产记录数据");
        return salesLedgerProductionAccountingList.stream().map(salesLedgerProductionAccounting -> {
            ProductionReportDto productionReportDto = new ProductionReportDto();
            BeanUtils.copyProperties(salesLedgerProductionAccounting, productionReportDto);
            productionReportDto.setSchedulingDate(salesLedgerProductionAccounting.getSchedulingDate().format(DateTimeFormatter.ISO_LOCAL_DATE));
            productionReportDto.setSchedulingNum(salesLedgerWork.getSchedulingNum());
            return productionReportDto;
        }).collect(Collectors.toList());
    }
}