Fancy
2024-08-22 4a36a12d40453f7686e5f2664f19601b9f6b9ae2
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
package com.dy.pmsProduct.order;
 
import com.dy.common.webFilter.UserTokenContext;
import com.dy.common.webUtil.QueryResultVo;
import com.dy.pmsGlobal.daoPr.*;
import com.dy.pmsGlobal.pojoBa.BaUser;
import com.dy.pmsGlobal.pojoPr.*;
import com.dy.pmsGlobal.util.UserUtil;
import com.dy.pmsProduct.taskPlan.PlanStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.PojoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
 
@Slf4j
@Service
public class OrderSv {
    private PrOrderMapper orderDao;
    private PrOrderItemMapper orderItemDao;
    private UserUtil userUtil;
 
    @Autowired
    public void setOrderDao(PrOrderMapper orderDao) {
        this.orderDao = orderDao;
    }
 
    @Autowired
    public void setOrderItemDao(PrOrderItemMapper orderItemDao) {
        this.orderItemDao = orderItemDao;
    }
 
    @Autowired
    public void setUserUtil(UserUtil userUtil) {
        this.userUtil = userUtil;
    }
    @Transactional
    public int save(PrOrder p) {
        p.id = null;
        //判断产品不能重名
        if (orderDao.exists(p.name, p.id)) {
            throw new RuntimeException("订单名称不能重复");
        }
        p.setDeleted(false);
        BaUser loginUser = userUtil.getUser(UserTokenContext.get());
        if (loginUser != null) {
            p.creator = loginUser.id;
        }
        int count = orderDao.insertSelective(p);
        saveOrderItems(p);
        return count;
    }
 
    @Transactional
    public int update(PrOrder p) {
        if (orderDao.exists(p.name, p.id)) {
            throw new RuntimeException("订单名称不能重复");
        }
        int count = orderDao.updateByPrimaryKeySelective(p);
        if (count > 0) {
            saveOrderItems(p);
        }
        return count;
    }
 
    private void saveOrderItems(PrOrder p) {
        p.items.forEach(param -> {
            param.orderId = p.id;
            if (param.id != null) {
                orderItemDao.updateByPrimaryKeySelective(param);
            } else {
                param.deleted = false;
                orderItemDao.insert(param);
            }
        });
    }
 
    /**
     * 逻辑删除实体
     *
     * @param id 实体ID
     * @return 影响记录数量
     */
    @Transactional
    public int delete(Long id) {
        return orderDao.deleteLogicById(id);
    }
 
    public PrOrder selectById(String proId) {
        PrOrder pro = orderDao.selectByPrimaryKey(Long.valueOf(proId));
        changeRate(pro);
        return pro;
    }
 
    /**
     * 获取订单列表
     */
    public QueryResultVo<List<PrOrder>> selectSome(QueryVo queryVo) {
        Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo);
 
        //查询符合条件的记录总数
        Long itemTotal = orderDao.selectSomeCount(params);
 
        QueryResultVo<List<PrOrder>> rsVo = new QueryResultVo<>(queryVo.pageSize, queryVo.pageCurr);
        //计算分页等信息
        rsVo.calculateAndSet(itemTotal, params);
 
        List<PrOrder> orderList = orderDao.selectSome(params);
        for (PrOrder prOrder : orderList) {
            changeRate(prOrder);
        }
        //查询符合条件的记录
        rsVo.obj = orderList;
        return rsVo;
    }
 
    private void changeRate(PrOrder prOrder) {
        List<PrOrderItem> items = prOrder.items;
        for (int i = 1; i < items.size(); i++) {
            for (int j = i - 1; j >= 0; j--) {
                //拿着i依次跟上一个比较,如果产品相同,则上一个记录complete_number - number 如果 > 0 分给i ,如果 < 0 则将 complete_number 置为0
                if (items.get(j).getProId().intValue() == items.get(i).getProId().intValue()) {
                    int remainNumber = items.get(j).getCompleteNumber() - items.get(j).getNumber();
                    if (remainNumber > 0) {
                        items.get(j).setCompleteNumber(items.get(j).getNumber());
                        items.get(j).setCompleteRate("100.00%");
                        items.get(i).setCompleteNumber(remainNumber);
                        BigDecimal remainBig = new BigDecimal(remainNumber * 100);
                        BigDecimal iNumberBig = new BigDecimal(items.get(i).getNumber());
                        BigDecimal result = remainBig.divide(iNumberBig, 2, RoundingMode.HALF_UP);
                        items.get(i).setCompleteRate(result.toString() +"%");
                    }else{
                        items.get(i).setCompleteNumber(0);
                        items.get(i).setCompleteRate("0.00%");
                    }
                    break;
                }
            }
        }
    }
 
    public List<PrOrder> selectAll(QueryVo queryVo) {
        Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo);
        List<PrOrder> orderList = orderDao.selectAll(params);
        for (PrOrder prOrder : orderList) {
            changeRate(prOrder);
        }
        return orderList;
    }
    /**
     * 只更新状态,不更新其他字段
     * @param plan
     * 更新状态  如果有在执行中的任务,不让暂停
     * @return
     */
  /*  @Transactional
    public int updateStatus(PrOrder plan) {
        if(plan.status == PlanStatusEnum.NORMAL.getCode()){
            PrAssemblyPlan assemblyPlan = assemblyDao.selectByPrimaryKey(plan.id);
            assemblyPlan.status = plan.status;
            extractedCheck(assemblyPlan);
        }
        PrAssemblyPlan param = new PrAssemblyPlan();
        param.id =plan.id;
        param.status = plan.status;
        return assemblyDao.updateByPrimaryKeySelective(param);
    }
    private void extractedCheck(PrAssemblyPlan plan) {
        PrProductionProcess process = processDao.selectByPrimaryKey(plan.processId);
        if(process == null || !process.proId.equals(plan.proId)){
            throw new RuntimeException("产品与生产流程不匹配");
        }
        //开始日期要小于结束日期
        if(plan.startDate.compareTo(plan.endDate) > 0){
            throw new RuntimeException("开始日期不能大于结束日期");
        }
        //Fancy add 2024/07/09   如果状态为执行 ,则结束日期必须大于等于当前日期
        if(plan.status == PlanStatusEnum.NORMAL.getCode()){
            String endDateStr = plan.getEndDate();
            LocalDate endDate = LocalDate.parse(endDateStr, DateTimeFormatter.ISO_LOCAL_DATE);
            LocalDate nextDay = endDate.plusDays(1);
            LocalDate today = LocalDate.now(); // 获取当前日期
            if (nextDay.isBefore(today)) {
                throw new RuntimeException("执行状态结束日期必须大于等于当前日期,请修改结束日期");
            }
        }
    }*/
}