hyingbo
2025-06-06 a3cb768d958e64625786857e0db69335875f5d8e
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
package org.jeecg.modules.spare.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.modules.spare.entity.*;
import org.jeecg.modules.spare.mapper.SparePartCancellingStocksDetailMapper;
import org.jeecg.modules.spare.service.*;
import org.jeecg.modules.system.entity.SysRole;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDictService;
import org.jeecg.modules.system.service.ISysRoleService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
/**
 * @Description: 备件退库详情
 * @Author: jeecg-boot
 * @Date: 2023-07-04
 * @Version: V1.0
 */
@Service
public class SparePartCancellingStocksDetailServiceImpl extends ServiceImpl<SparePartCancellingStocksDetailMapper, SparePartCancellingStocksDetail> implements ISparePartCancellingStocksDetailService {
 
    @Autowired
    private SparePartCancellingStocksDetailMapper sparePartCancellingStocksDetailMapper;
 
    @Autowired
    private ISparePartCancellingStocksService sparePartCancellingStocksService;
 
 
    @Autowired
    private ISparesPartInventoryService sparesPartInventoryService;
 
    @Autowired
    private ISparePartService sparePartService;
 
    @Autowired
    @Lazy
    private ISparePartOutboundService sparePartOutboundService;
 
    @Autowired
    @Lazy
    private ISysDictService sysDictService;
 
    @Autowired
    @Lazy
    private ISysRoleService sysRoleService;
 
    @Autowired
    @Lazy
    private ISysUserService sysUserService;
 
    @Autowired
    @Lazy
    private ISysBaseAPI sysBaseAPI;
 
 
    @Override
    public List<Map<String, Object>> getSparePartCancellingStocksDeatilList(String sparePartCancellingId) {
        return super.getBaseMapper().getSparePartCancellingStocksDeatilList(sparePartCancellingId);
    }
 
    @Override
    public IPage<Map<String, Object>> getSparePartList(Integer pageNo, Integer pageSize, Map<String, Object> params) {
        IPage<Map> pageData = new Page<Map>(pageNo, pageSize);
        return super.getBaseMapper().getSparePartList(pageData, params);
    }
 
    @Override
    public IPage<Map<String, Object>> getSparePartCanxellingStoksDetailsById(Integer pageNo, Integer pageSize, Map<String, Object> params) {
        IPage<Map> pageData = new Page<Map>(pageNo, pageSize);
        return super.getBaseMapper().getSparePartCanxellingStoksDetailsById(pageData, params);
    }
 
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean sparePartGround(SparePartCancellingStocksDetail stocksDetail) {
        //1.获取库存信息及库存总数量
        String sparePartCancellingId = stocksDetail.getSparePartCancellingId();
        SparePartCancellingStocks sparePartCancellingStocks = sparePartCancellingStocksService.getById(sparePartCancellingId);
        String sparesPartInventoryId = stocksDetail.getSparePartInventoryId();
        SparesPartInventory sparesPartInventory = sparesPartInventoryService.getById(sparesPartInventoryId);
        //2.获取出库数量
        Double outboundMainQuantity = stocksDetail.getOutboundMainQuantity();
        BigDecimal outboundMainQuantityB = new BigDecimal(outboundMainQuantity);
        String sparePartId = stocksDetail.getSparePartId();
 
        //3.获取主 辅单位转换比例
        SparePart sparePart = sparePartService.getById(sparePartId);
  /*      String conversionRatio = sparePart.getConversionRatio();
        double conversionRatioD = 0;
        if (StringUtils.isBlank(conversionRatio)) {
            conversionRatioD = Double.parseDouble("0");
        } else {
            conversionRatioD = Double.parseDouble(conversionRatio);
        }
        BigDecimal conversionRatioB = new BigDecimal(conversionRatioD);*/
 
        //4.计算出库数量更新库存数量(库存)
        Double mainQuantity = sparesPartInventory.getMainQuantity();
        BigDecimal mainQuantityB = new BigDecimal(mainQuantity);
        /*仓库辅数量*/
        Double auxiliaryQuantity = sparesPartInventory.getAuxiliaryQuantity();
     /*   BigDecimal auxiliaryQuantityB = new BigDecimal(auxiliaryQuantity);*/
 
        BigDecimal mainQuantitySubtract = mainQuantityB.add(outboundMainQuantityB);//加后的库存主数量
       /* BigDecimal outboundAuxiliaryQuantity = outboundMainQuantityB.multiply();*///出库辅数量
 
        /*辅数量=仓库辅数量+出库的辅数量(出库数量*计算比例)*/
       /* BigDecimal auxiliaryQuantitySubtract = auxiliaryQuantityB.add(outboundAuxiliaryQuantity);//加后的库存辅数量*/
        sparesPartInventory.setMainQuantity(mainQuantitySubtract.doubleValue());
    /*    sparesPartInventory.setAuxiliaryQuantity(auxiliaryQuantitySubtract.doubleValue());*/
 
        boolean b = sparesPartInventoryService.updateById(sparesPartInventory);
        if (!b) {
            return b;
        }
 
        stocksDetail.setStatus("1");
        super.updateById(stocksDetail);
 
        //5.更新出库单状态
        List<SparePartCancellingStocksDetail> sparePartCancellingStocksDetails = super.lambdaQuery()
                .eq(SparePartCancellingStocksDetail::getSparePartCancellingId, sparePartCancellingId)
                .eq(SparePartCancellingStocksDetail::getStatus, "0")
                .eq(SparePartCancellingStocksDetail::getDelFlag, "0").list();
        if (sparePartCancellingStocksDetails.size() == 0) {
            sparePartCancellingStocks.setStatus("4");
            sparePartCancellingStocksService.updateById(sparePartCancellingStocks);
        }
 
        //6.判断出库后库存是否低于安全库存或出现积压情况
        List roleList = new ArrayList<>();
        List<DictModel> dictModelList = sysDictService.getDictItems("warehouse_message");
        for(DictModel dictModel : dictModelList){
            roleList.add(dictModel.getText());
        }
        QueryWrapper<SysRole> queryWrapper = Wrappers.query();
        queryWrapper.in("role_code", roleList);
        List<SysRole> sysRoleList = sysRoleService.list(queryWrapper);
        List<SysUser> sysUsers = new ArrayList<>();
        for (SysRole sysRole : sysRoleList) {
            sysUsers.addAll(sysUserService.getAllUsersByRoleId(sysRole.getId()));
        }
        double quantity = 0.0;
        List<SparesPartInventory> sparesPartInventoryList = sparesPartInventoryService.lambdaQuery().eq(SparesPartInventory::getSparePartId,sparesPartInventory.getSparePartId()).eq(SparesPartInventory::getDelFlag, CommonConstant.DEL_FLAG_0).list();
        for(SparesPartInventory sparesPartInventory1 : sparesPartInventoryList){
            quantity = quantity + sparesPartInventory1.getMainQuantity();
        }
        if(quantity < sparePart.getInventoryLowerLimit()){
            String messageTitle;
            String messageContent;
            String sysTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            messageTitle = "系统消息:备件编码为 " + sparePart.getNum() + " 的备件库存量不足,请及时处理!";
            messageContent = "系统消息:备件编码:" + sparePart.getNum()  + " 的备件库存量已低于安全库存!" + "\n" + "消息时间:" + sysTime + "\n";
            for (SysUser sysUser : sysUsers) {
                sysBaseAPI.sendAnnouncement("admin", sysUser.getUsername(), messageTitle, messageContent, "备件库存量不足");
            }
        }else if(quantity > sparePart.getInventoryUpperLimit()){
            String messageTitle;
            String messageContent;
            String sysTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            messageTitle = "系统消息:备件编码为 " + sparePart.getNum() + " 的备件出现积压情况,请及时处理!";
            messageContent = "系统消息:备件编码为 " + sparePart.getNum() + " 的备件库存量已高于库存上限值,出现积压情况!" + "\n" + "消息时间:" + sysTime + "\n";
            for (SysUser sysUser : sysUsers) {
                sysBaseAPI.sendAnnouncement("admin", sysUser.getUsername(), messageTitle, messageContent, "备件积压");
            }
        }
        return true;
    }
 
 
    @Override
    public List<SparePartCancellingStocksDetail> selectByMainId(String mainId) {
        return sparePartCancellingStocksDetailMapper.selectByMainId(mainId);
    }
 
}