叶松
2023-11-27 111a401f033749c965f02f58c586a8feafd9afd1
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package com.thhy.materials.modules.biz.mixingstationstorage.service.impl;
 
import com.alibaba.nacos.common.utils.UuidUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.thhy.general.common.BasicResult;
import com.thhy.general.common.BasicStatus;
import com.thhy.general.config.SysUserInfo;
import com.thhy.general.exception.BasicException;
import com.thhy.general.utils.ExcelUtils;
import com.thhy.general.utils.UUIDUtils;
import com.thhy.general.utils.UserInfoUtils;
import com.thhy.materials.modules.biz.materialsplan.entity.RsbStatDto;
import com.thhy.materials.modules.biz.materialsplan.entity.StatResultVo;
import com.thhy.materials.modules.biz.materialsplan.entity.StatVo;
import com.thhy.materials.modules.biz.mixingstationstorage.dto.ConsumptionRecordDto;
import com.thhy.materials.modules.biz.mixingstationstorage.dto.MinXingPlantDto;
import com.thhy.materials.modules.biz.mixingstationstorage.dto.NumberOfMaterialsDto;
import com.thhy.materials.modules.biz.mixingstationstorage.dto.TMaterialStockRecordDto;
import com.thhy.materials.modules.biz.mixingstationstorage.entity.MXConsumptionRecordsVo;
import com.thhy.materials.modules.biz.mixingstationstorage.entity.MinXingPlantVo;
import com.thhy.materials.modules.biz.mixingstationstorage.entity.TMaterialStockRecordVo;
import com.thhy.materials.modules.biz.mixingstationstorage.mapper.MinXingPlantMapper;
import com.thhy.materials.modules.biz.mixingstationstorage.service.MinXingPlantService;
import com.thhy.materials.modules.biz.rsbmaterials.mapper.RsbMaterialsMapper;
import com.thhy.materials.modules.biz.utils.ExcelExtendUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.TimeUnit;
 
/**
 * @Author QiuYuHao
 * @CreateDate 2023-09-04 14:31:59
 * 搅拌站实现
 */
@Service
public class MinXingPlantServiceImpl implements MinXingPlantService{
 
    @Autowired
    private MinXingPlantMapper minXingPlantMapper;
 
    @Autowired
    private RedissonClient redissonClient;
 
    @Autowired
    private RsbMaterialsMapper rsbMaterialsMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BasicResult insert(MinXingPlantVo minXingPlantVo) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String companyId = sysUserInfo.getCompanyId();
        minXingPlantVo.setCompanyId(companyId);
        minXingPlantVo.setCreateUser(sysUserInfo.getUserId());
        minXingPlantVo.setId(UUIDUtils.create());
        minXingPlantVo.setOpType(1);
        minXingPlantVo.setMaterialValue(minXingPlantVo.getChangeStock());
        minXingPlantVo.setType(4);
        minXingPlantMapper.insert(minXingPlantVo);
        RLock lock = redissonClient.getLock("materlock");
        try {
            if (lock.tryLock(5, 10, TimeUnit.SECONDS)) {
                //增加库存
                minXingPlantMapper.updateInventory(minXingPlantVo.getChangeStock(),null,minXingPlantVo.getDictId());
            } else {
                throw new BasicException(BasicStatus.OP_IS_TIMEOUT);
            }
        } catch (InterruptedException e) {
            throw new BasicException(BasicStatus.OP_IS_REPEAT);
        } finally {
            if (lock.isLocked()) {
                lock.unlock();
            }
        }
        return BasicResult.success();
    }
 
    @Override
    public BasicResult selectAllList(MinXingPlantDto minXingPlantDto) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String companyId = sysUserInfo.getCompanyId();
        minXingPlantDto.setCompanyId(companyId);
        PageHelper.startPage(minXingPlantDto.getPageNum(),minXingPlantDto.getPageSize());
        List<MinXingPlantVo> minXingPlantVos = minXingPlantMapper.selectAllList(minXingPlantDto);
        PageInfo<MinXingPlantVo> pageInfo =new PageInfo<>(minXingPlantVos);
        return BasicResult.success(pageInfo);
    }
 
    /**
     * 入库记录导出
     * @param minXingPlantDto
     * @return
     */
    @Override
    public void export(MinXingPlantDto minXingPlantDto, HttpServletResponse rsp) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String companyId = sysUserInfo.getCompanyId();
        minXingPlantDto.setCompanyId(companyId);
        List<MinXingPlantVo> minXingPlantVos = minXingPlantMapper.selectAllList(minXingPlantDto);
        ExcelUtils.downExcel(minXingPlantVos,MinXingPlantVo.class,rsp,"搅拌机入库记录");
    }
 
 
    /**
     * 搅拌站库存管理列表
     */
    @Override
    public BasicResult selectInventoryList(Map<String, String> map) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String companyId = sysUserInfo.getCompanyId();
        map.put("companyId",companyId);
        PageHelper.startPage(Integer.valueOf(map.get("pageNum")),Integer.valueOf(map.get("pageSize")));
        List<Map<String, Object>> list = minXingPlantMapper.selectInventoryList(map);
        PageInfo<Map<String,Object>> info = new PageInfo<>(list);
        return BasicResult.success(info);
    }
 
    /**
     * 校正记录列表
     * @param tMaterialStockRecordDto
     * @return
     */
    @Override
    public BasicResult selectInventoryRecordList(TMaterialStockRecordDto tMaterialStockRecordDto) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String companyId = sysUserInfo.getCompanyId();
        tMaterialStockRecordDto.setCompanyId(companyId);
        PageHelper.startPage(tMaterialStockRecordDto.getPageNum(),tMaterialStockRecordDto.getPageSize());
        List<TMaterialStockRecordVo> tMaterialStockRecordVos = minXingPlantMapper.selectInventoryRecordList(tMaterialStockRecordDto);
        PageInfo<TMaterialStockRecordVo> pageInfo =new PageInfo<>(tMaterialStockRecordVos);
        return BasicResult.success(pageInfo);
    }
 
    /**
     * 校正记录列表导出
     * @param tMaterialStockRecordDto
     * @return
     */
    @Override
    public void exportInventoryRecord(TMaterialStockRecordDto tMaterialStockRecordDto, HttpServletResponse rsp) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String companyId = sysUserInfo.getCompanyId();
        tMaterialStockRecordDto.setCompanyId(companyId);
        List<TMaterialStockRecordVo> tMaterialStockRecordVos = minXingPlantMapper.selectInventoryRecordList(tMaterialStockRecordDto);
        ExcelUtils.downExcel(tMaterialStockRecordVos,TMaterialStockRecordVo.class,rsp,"校正记录");
    }
 
    /**
     * 库存管理-校正接口
     * @param tMaterialStockRecordVo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void inventoryCorrection(TMaterialStockRecordVo tMaterialStockRecordVo) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String companyId = sysUserInfo.getCompanyId();
 
        RLock lock = redissonClient.getLock("materlock");
        try {
            if (lock.tryLock(5, 10, TimeUnit.SECONDS)) {
                //此时参数实体获得的id是库存id 更新库存
                minXingPlantMapper.updateInventory(tMaterialStockRecordVo.getAfterStock(),tMaterialStockRecordVo.getId(),null);
            } else {
                throw new BasicException(BasicStatus.OP_IS_TIMEOUT);
            }
        } catch (InterruptedException e) {
            throw new BasicException(BasicStatus.OP_IS_REPEAT);
        } finally {
            if (lock.isLocked()) {
                lock.unlock();
            }
        }
 
        Map<String,String> param = new HashMap<>();
        param.put("companyId",companyId);
        param.put("id",tMaterialStockRecordVo.getId());
        List<Map<String, Object>> list = minXingPlantMapper.selectInventoryList(param);
        String dictId = (String) list.get(0).get("dictId");
        Double beforeStock = tMaterialStockRecordVo.getBeforeStock();
        Double afterStock = tMaterialStockRecordVo.getAfterStock();
        //添加校正记录
        if(beforeStock > afterStock){
            tMaterialStockRecordVo.setOpType(2);
        }else {
            tMaterialStockRecordVo.setOpType(1);
        }
        tMaterialStockRecordVo.setCreateUser(sysUserInfo.getRealName());
        tMaterialStockRecordVo.setCompanyId(companyId);
        tMaterialStockRecordVo.setMaterialName(dictId);
        tMaterialStockRecordVo.setCreateDate(new Date());
        tMaterialStockRecordVo.setType(3);//校正状态
        tMaterialStockRecordVo.setId(UuidUtils.generateUuid());
        minXingPlantMapper.insertInventoryRecord(tMaterialStockRecordVo);
    }
 
    /**
     * 消耗记录列表
     * @param consumptionRecordDto
     * @return
     */
    @Override
    public BasicResult selectAllConsumptionList(ConsumptionRecordDto consumptionRecordDto) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String companyId = sysUserInfo.getCompanyId();
        consumptionRecordDto.setCompanyId(companyId);
        PageHelper.startPage(consumptionRecordDto.getPageNum(),consumptionRecordDto.getPageSize());
        PageInfo<MXConsumptionRecordsVo> mxConsumptionRecordsVoPageInfo = new PageInfo<>(minXingPlantMapper.selectAllConsumptionList(consumptionRecordDto));
        return BasicResult.success(mxConsumptionRecordsVoPageInfo);
    }
 
    /**
     * 消耗记录导出
     * @param consumptionRecordDto
     * @return
     */
    @Override
    public void exportAllConsumptionList(ConsumptionRecordDto consumptionRecordDto, HttpServletResponse rsp) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String companyId = sysUserInfo.getCompanyId();
        consumptionRecordDto.setCompanyId(companyId);
        List<MXConsumptionRecordsVo> mxConsumptionRecordsVos = minXingPlantMapper.selectAllConsumptionList(consumptionRecordDto);
        ExcelUtils.downExcel(mxConsumptionRecordsVos,MXConsumptionRecordsVo.class,rsp,"消耗记录");
    }
 
    /**
     * 消耗记录
     * @param numberOfMaterialsDto
     * @return
     */
    @Override
    public BasicResult RsbStat(NumberOfMaterialsDto numberOfMaterialsDto) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        numberOfMaterialsDto.setCompanyId(sysUserInfo.getCompanyId());
        Map<String,Object> map = new HashMap<>();
        /**
         *个数
         */
        if(numberOfMaterialsDto.getDateType() == 2){
            String endDay =getEndDay(numberOfMaterialsDto.getEndDay()) ;
            numberOfMaterialsDto.setEndDay(endDay);
        }
        List<Map<String, Object>> list = minXingPlantMapper.numberOfMaterials(numberOfMaterialsDto);
        List<StatResultVo> statResultVos = new ArrayList<>();
        String dictId = numberOfMaterialsDto.getDictId();
        if("both".equals(dictId)){
            //获取全部
            statResultVos = rsbMaterialsMapper.queryAllMaterial(null);
        }else{
            statResultVos = rsbMaterialsMapper.queryAllMaterial(dictId);
        }
        statResultVos.forEach(s->{
            numberOfMaterialsDto.setDictId(s.getMaterialName());
            List<StatVo> statVos = null;
            if(numberOfMaterialsDto.getDateType() == 1){
 
                statVos = minXingPlantMapper.queryValueDay(numberOfMaterialsDto);
            }else {
                statVos = minXingPlantMapper.queryValueMonth(numberOfMaterialsDto);
            }
            s.setStatVoList(statVos);
            s.setMaterialCName(s.getMaterialCName());
        });
        map.put("statResult",statResultVos);
        map.put("materialTotal",list);
        return BasicResult.success(map);
    }
 
    public String getEndDay(String endDay) {
        int year = Integer.valueOf(endDay.substring(0, 4));
        int month = Integer.valueOf(endDay.substring(5, 7)); // 设置月份,注意月份从1开始,即1表示1月,12表示12月
        LocalDate lastDayOfMonth = LocalDate.of(year, month, 1).withDayOfMonth(LocalDate.of(year, month, 1).lengthOfMonth());
        System.out.println("最后一天是:" + lastDayOfMonth.getDayOfMonth());
        return endDay.substring(0,7) + "-"+lastDayOfMonth.getDayOfMonth();
    }
 
    @Override
    public void statExport(NumberOfMaterialsDto numberOfMaterialsDto, String token, HttpServletResponse response) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        numberOfMaterialsDto.setCompanyId(sysUserInfo.getCompanyId());
        List<StatResultVo> statResultVos = new ArrayList<>();
        String dictId = numberOfMaterialsDto.getDictId();
        numberOfMaterialsDto.setMoth(numberOfMaterialsDto.getStartDay().substring(0,7));
        numberOfMaterialsDto.setStartMoth(numberOfMaterialsDto.getStartDay().substring(0,7));
        numberOfMaterialsDto.setEndMoth(numberOfMaterialsDto.getEndDay().substring(0,7));
        numberOfMaterialsDto.setYear(numberOfMaterialsDto.getStartDay().substring(0, 4));
        if("".equals(dictId)){
            //获取全部
            statResultVos = rsbMaterialsMapper.queryAllMaterial(null);
        }else{
            statResultVos = rsbMaterialsMapper.queryAllMaterial(dictId);
        }
        statResultVos.forEach(s->{
            numberOfMaterialsDto.setDictId(s.getMaterialName());
            List<StatVo> statVos = null;
            if(numberOfMaterialsDto.getDateType() == 1){
                statVos = minXingPlantMapper.queryValueDay(numberOfMaterialsDto);
            }else {
                statVos = minXingPlantMapper.queryValueMonth(numberOfMaterialsDto);
            }
            s.setStatVoList(statVos);
            s.setMaterialCName(s.getMaterialCName());
        });
        ExcelExtendUtils.create(statResultVos,"原材料消耗",response);
    }
 
}