新火炬后端单体项目初始化代码
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
package org.jeecg.modules.cms.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.micrometer.core.annotation.Timed;
import liquibase.pro.packaged.C;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.cms.entity.CuttingInventory;
import org.jeecg.modules.cms.entity.CuttingReceive;
import org.jeecg.modules.cms.entity.CuttingReceiveDetail;
import org.jeecg.modules.cms.entity.RatedLife;
import org.jeecg.modules.cms.mapper.CuttingReceiveMapper;
import org.jeecg.modules.cms.service.ICuttingInventoryService;
import org.jeecg.modules.cms.service.ICuttingReceiveDetailService;
import org.jeecg.modules.cms.service.ICuttingReceiveService;
import org.jeecg.modules.cms.service.IRatedLifeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
/**
 * @Description: 刀具领用单
 * @Author: jeecg-boot
 * @Date:   2025-07-28
 * @Version: V1.0
 */
@Slf4j
@Service
public class CuttingReceiveServiceImpl extends ServiceImpl<CuttingReceiveMapper, CuttingReceive> implements ICuttingReceiveService {
    @Autowired
    private ICuttingInventoryService cuttingInventoryService;
    @Autowired
    private ICuttingReceiveDetailService cuttingReceiveDetailService;
    @Autowired
    private IRatedLifeService ratedLifeService;
    @Override
    public IPage<Map<String, Object>> getInventoryToolList(Integer pageNo, Integer pageSize, Map<String, Object> params) {
        IPage<Map> pageData = new Page<Map>(pageNo, pageSize);
        return super.getBaseMapper().getInventoryToolList(pageData,params);
    }
    @Override
    @Timed(value = "cutting.receive.submit", description = "刀具领用提交耗时")
    public synchronized Result<?> submit(String orderId) {
        long startTime = System.currentTimeMillis();
        try {
            // 1. 更新领用单状态
            CuttingReceive cuttingReceive = this.getById(orderId);
            if (cuttingReceive == null) {
                return Result.error("未找到对应的领用单");
            }
 
            // 检查领用单状态,只允许状态为"1"的领用单提交
            if (!"1".equals(cuttingReceive.getOrderStatus())) {
                return Result.error("只有状态为未提交的领用单才能执行提交操作");
            }
 
            cuttingReceive.setOrderStatus("2"); // 设置为已领用状态
            boolean updateResult = this.updateById(cuttingReceive);
            if (!updateResult) {
                return Result.error("更新领用单状态失败,可能已被其他用户处理");
            }
 
            // 2. 获取领用明细
            List<CuttingReceiveDetail> detailList = cuttingReceiveDetailService.lambdaQuery()
                    .eq(CuttingReceiveDetail::getOrderId, orderId)
                    .list();
 
            // 3. 收集所有需要更新状态的库存ID
            List<String> inventoryIds = new ArrayList<>();
            for (CuttingReceiveDetail detail : detailList) {
                if (detail.getInventoryId() != null && !detail.getInventoryId().isEmpty()) {
                    inventoryIds.add(detail.getInventoryId());
                }
            }
 
            // 4. 批量更新库存状态为"已出库"
            if (!inventoryIds.isEmpty()) {
                cuttingInventoryService.updateStatus(inventoryIds, "已出库");
            }
 
            long endTime = System.currentTimeMillis();
            log.info("刀具领用提交完成,耗时: {} ms", (endTime - startTime));
            return Result.ok("提交成功");
 
        } catch (Exception e) {
            long endTime = System.currentTimeMillis();
            log.error("提交领用单失败,orderId: " + orderId + ",耗时: " + (endTime - startTime) + " ms", e);
            return Result.error("提交失败: " + e.getMessage());
        }
    }
 
 
    @Override
    public synchronized Result<?> handleBack(String orderId) {
        //FIXME:刀具归还 目前设计的是以领用单为维度进行还库;若是需要以刀具为维度进行归还的则需要另行设计
        long startTime = System.currentTimeMillis();
        try {
            // 1. 更新领用单状态为已归还
            CuttingReceive cuttingReceive = this.getById(orderId);
            if (cuttingReceive == null) {
                return Result.error("未找到对应的领用单");
            }
 
            // 检查领用单状态,只允许状态为"已领用"的领用单进行归还操作
            if (!"2".equals(cuttingReceive.getOrderStatus())) {
                return Result.error("只有状态为已领用的领用单才能执行归还操作");
            }
 
            cuttingReceive.setOrderStatus("3"); // 设置为已归还状态
            boolean updateResult = this.updateById(cuttingReceive);
            if (!updateResult) {
                return Result.error("更新领用单状态失败,可能已被其他用户处理");
            }
 
            // 2. 获取领用明细
            List<CuttingReceiveDetail> detailList = cuttingReceiveDetailService.lambdaQuery()
                    .eq(CuttingReceiveDetail::getOrderId, orderId)
                    .list();
 
            // 3. 收集所有需要更新状态的库存ID
            List<String> inventoryIds = new ArrayList<>();
            for (CuttingReceiveDetail detail : detailList) {
                if (detail.getInventoryId() != null && !detail.getInventoryId().isEmpty()) {
                    inventoryIds.add(detail.getInventoryId());
                }
            }
 
            // 4. 寿命扣减
            for (CuttingReceiveDetail detail : detailList) {
                if (detail.getInventoryId() != null && !detail.getInventoryId().isEmpty()) {
                    CuttingReceiveDetail cuttingReceiveDetail = cuttingReceiveDetailService.getById(detail.getInventoryId());
                    CuttingInventory inventory = cuttingInventoryService.getById(detail.getInventoryId());
 
                    RatedLife ratedLife = ratedLifeService.lambdaQuery()
                            .eq(RatedLife::getCuttingId, detail.getCuttingId())
                            .eq(RatedLife::getWorkpieceMaterial, detail.getWorkpieceMaterial())
                            .one();//额定寿命
 
                    Integer useLife = detail.getUsedLife();//使用寿命
                    //优化:(1-使用寿命/额定寿命)*100
                    BigDecimal usageRatio = BigDecimal.valueOf(useLife).divide(ratedLife.getRatedLife(), 4, RoundingMode.HALF_UP);
                    BigDecimal newLife = BigDecimal.valueOf(1)
                            .subtract(usageRatio)
                            .multiply(BigDecimal.valueOf(100))
                            .setScale(2, RoundingMode.HALF_UP);
                    // 更新库存寿命
                    inventory.setCurrentLife(newLife);
                    cuttingInventoryService.updateById(inventory);
                }
            }
 
            // 5. 批量更新库存状态为"正常"
            if (!inventoryIds.isEmpty()) {
                cuttingInventoryService.updateStatus(inventoryIds, "在库");
            }
 
            long endTime = System.currentTimeMillis();
            log.info("刀具归还处理完成,耗时: {} ms", (endTime - startTime));
            return Result.ok("归还成功");
 
        } catch (Exception e) {
            long endTime = System.currentTimeMillis();
            log.error("处理归还失败,orderId: " + orderId + ",耗时: " + (endTime - startTime) + " ms", e);
            return Result.error("归还失败: " + e.getMessage());
        }
    }
}