renkang
2025-01-24 d3767d594de66cb5f9d1294931acefea1866f783
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
package com.doumee.service.business.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.model.PageData;
import com.doumee.core.model.PageWrap;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.DateUtil;
import com.doumee.core.utils.Utils;
import com.doumee.core.utils.redis.RedisUtil;
import com.doumee.dao.business.YwStockMapper;
import com.doumee.dao.business.YwStocktakingMapper;
import com.doumee.dao.business.YwStocktakingRecordMapper;
import com.doumee.dao.business.YwWarehouseMapper;
import com.doumee.dao.business.model.*;
import com.doumee.dao.system.SystemUserMapper;
import com.doumee.dao.system.join.NoticesJoinMapper;
import com.doumee.dao.system.model.Notices;
import com.doumee.dao.system.model.SystemUser;
import com.doumee.service.business.YwStocktakingService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 运维盘点信息表Service实现
 * @author 江蹄蹄
 * @date 2025/01/06 11:05
 */
@Service
public class YwStocktakingServiceImpl implements YwStocktakingService {
 
    @Autowired
    private YwStocktakingMapper ywStocktakingMapper;
 
    @Autowired
    private YwStockMapper ywStockMapper;
 
 
    @Autowired
    private YwStocktakingRecordMapper ywStocktakingRecordMapper;
    
    @Autowired
    private YwWarehouseMapper ywWarehouseMapper;
 
    @Autowired
    private NoticesJoinMapper noticesJoinMapper;
 
    @Autowired
    private SystemUserMapper systemUserMapper;
 
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public Integer create(YwStocktaking ywStocktaking) {
        if(Objects.isNull(ywStocktaking)
                || StringUtils.isBlank(ywStocktaking.getName())
                || Objects.isNull(ywStocktaking.getPlanDate())
                || Objects.isNull(ywStocktaking.getWarehouseId())
                || Objects.isNull(ywStocktaking.getUserId())
        ){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        YwWarehouse ywWarehouse = ywWarehouseMapper.selectById(ywStocktaking.getWarehouseId());
        if(Objects.isNull(ywWarehouse)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到仓库数据");
        }
        //判断当前仓库当前日期是否存在盘点单
        if(ywStocktakingMapper.selectCount(new QueryWrapper<YwStocktaking>().lambda()
                .eq(YwStocktaking::getIsdeleted,Constants.ZERO)
                .in(YwStocktaking::getStatus,Constants.ZERO,Constants.ONE)
                .eq(YwStocktaking::getWarehouseId,ywStocktaking.getWarehouseId())
                .apply(" DATE(PLAN_DATE) = '" + DateUtil.formatDate(ywStocktaking.getPlanDate(),"yyyy-MM-dd") +"'" ))>0){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"["+DateUtil.formatDate(ywStocktaking.getPlanDate(),"yyyy-MM-dd")+"]["+ywWarehouse.getName()+"]已有盘点单,不可重复发起盘点");
        }
        SystemUser systemUser = systemUserMapper.selectById(ywStocktaking.getUserId());
        if(Objects.isNull(systemUser)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到盘点人信息");
        }
        LoginUserInfo loginUserInfo = ywStocktaking.getLoginUserInfo();
        ywStocktaking.setCreator(loginUserInfo.getId());
        ywStocktaking.setCreateDate(new Date());
        ywStocktaking.setIsdeleted(Constants.ZERO);
        ywStocktaking.setStatus(Constants.ZERO);
        ywStocktaking.setCode(getNextCode());
        ywStocktakingMapper.insert(ywStocktaking);
        //添加待办信息
        Notices notices = new Notices();
        notices.setCreateDate(new Date());
        notices.setCreator(ywStocktaking.getCreator());
        notices.setIsdeleted(Constants.ZERO);
        notices.setParam1(DateUtil.formatDate(ywStocktaking.getPlanDate(),"yyyy-MM-dd"));
        notices.setTitle("盘点待办");
        notices.setContent("["+ywStocktaking.getName()+"]将在["+notices.getParam1()+"]进行盘点,请按时执行");
        notices.setObjId(ywStocktaking.getId());
        notices.setObjType(Constants.ZERO);
        notices.setType(notices.getObjType());
        notices.setUserId(ywStocktaking.getUserId());
        notices.setStatus(Constants.ZERO);
        //整理json数据
        Map<String,Object> jsonMap = new HashMap<>();
        jsonMap.put("key1","盘点单"+ywStocktaking.getCode());
        jsonMap.put("key2", DateUtil.getFomartDate(ywStocktaking.getPlanDate(),"yyyy-MM-dd"));
        jsonMap.put("key3", ywWarehouse.getName());
        jsonMap.put("key4", systemUser.getRealname());
        notices.setParam2(JSONObject.toJSONString(jsonMap));
        notices.setParam3(Constants.ZERO+"");
        noticesJoinMapper.insert(notices);
        return ywStocktaking.getId();
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public void deleteById(Integer id, LoginUserInfo loginUserInfo) {
        YwStocktaking model = ywStocktakingMapper.selectById(id);
        if(Objects.isNull(model)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到盘点单数据");
        }
        if(Constants.equalsInteger(model.getStatus(),Constants.ZERO)||Constants.equalsInteger(model.getStatus(),Constants.THREE)){
            ywStocktakingMapper.update(new UpdateWrapper<YwStocktaking>().lambda()
                    .set(YwStocktaking::getIsdeleted,Constants.ONE)
                    .set(YwStocktaking::getEditDate,DateUtil.getCurrDateTime())
                    .set(YwStocktaking::getEditor,loginUserInfo.getId())
                    .eq(YwStocktaking::getId,id)
            );
            //标记待办删除
            noticesJoinMapper.update(null,new UpdateWrapper<Notices>().lambda()
                    .set(Notices::getIsdeleted,Constants.ONE)
                    .eq(Notices::getIsdeleted,Constants.ZERO)
                    .eq(Notices::getObjType,Constants.ZERO)
                    .eq(Notices::getObjId,id)
            );
        }else{
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"盘点单状态已流转,无法进行该操作");
        }
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public void cancelById(Integer id, LoginUserInfo loginUserInfo) {
        YwStocktaking model = ywStocktakingMapper.selectById(id);
        if(Objects.isNull(model)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到盘点单数据");
        }
        if(Constants.equalsInteger(model.getStatus(),Constants.ZERO)||Constants.equalsInteger(model.getStatus(),Constants.ONE)){
            ywStocktakingMapper.update(new UpdateWrapper<YwStocktaking>().lambda()
                    .set(YwStocktaking::getEditDate,DateUtil.getCurrDateTime())
                    .set(YwStocktaking::getCancelDate,DateUtil.getCurrDateTime())
                    .set(YwStocktaking::getCancelUserId,loginUserInfo.getId())
                    .set(YwStocktaking::getEditor,loginUserInfo.getId())
                    .set(YwStocktaking::getStatus,Constants.THREE).eq(YwStocktaking::getId,id));
            //标记待办删除
            noticesJoinMapper.update(null,new UpdateWrapper<Notices>().lambda()
                    .set(Notices::getIsdeleted,Constants.ONE)
                    .eq(Notices::getIsdeleted,Constants.ZERO)
                    .eq(Notices::getObjType,Constants.ZERO)
                    .eq(Notices::getObjId,id)
            );
        }else{
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"盘点单状态已流转,无法进行该操作");
        }
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public void beginById(Integer id, LoginUserInfo loginUserInfo) {
        YwStocktaking model = ywStocktakingMapper.selectById(id);
        if(Objects.isNull(model)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到盘点单数据");
        }
        if(model.getPlanDate().getTime()>System.currentTimeMillis()){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"未到开始时间,无法进行该操作");
        }
        if(Constants.equalsInteger(model.getStatus(),Constants.ZERO)&&Constants.equalsInteger(model.getIsdeleted(),Constants.ZERO)){
            ywStocktakingMapper.update(new UpdateWrapper<YwStocktaking>().lambda()
                    .set(YwStocktaking::getEditDate,DateUtil.getCurrDateTime())
                    .set(YwStocktaking::getDealDate,DateUtil.getCurrDateTime())
                    .set(YwStocktaking::getDealUserId,loginUserInfo.getId())
                    .set(YwStocktaking::getEditor,loginUserInfo.getId())
                    .set(YwStocktaking::getStatus,Constants.ONE)
                    .eq(YwStocktaking::getId,model.getId()));
 
            //标记待办已处理
            noticesJoinMapper.update(null,new UpdateWrapper<Notices>().lambda()
                    .set(Notices::getParam3,Constants.ONE)
                    .eq(Notices::getStatus,Constants.ZERO)
                    .eq(Notices::getIsdeleted,Constants.ZERO)
                    .eq(Notices::getObjType,Constants.ZERO)
                    .eq(Notices::getObjId,id)
            );
 
            //生成当前仓库下所有的资产信息
            List<YwStock> stockList = ywStockMapper.selectList(new QueryWrapper<YwStock>().lambda().eq(YwStock::getIsdeleted,Constants.ZERO).eq(YwStock::getWarehouseId,model.getWarehouseId()));
            if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(stockList)){
                List<YwStocktakingRecord> ywStocktakingRecordList = new ArrayList<>();
                for (YwStock ywStock:stockList) {
                    YwStocktakingRecord ywStocktakingRecord = new YwStocktakingRecord();
                    ywStocktakingRecord.setCreator(loginUserInfo.getId());
                    ywStocktakingRecord.setCreateDate(new Date());
                    ywStocktakingRecord.setIsdeleted(Constants.ZERO);
                    ywStocktakingRecord.setStatus(Constants.ZERO);
                    ywStocktakingRecord.setMaterialId(ywStock.getMaterialId());
                    ywStocktakingRecord.setUserId(model.getUserId()+"");
                    ywStocktakingRecord.setStock(ywStock.getStock());
                    ywStocktakingRecord.setStocktakingId(model.getId());
                    ywStocktakingRecordList.add(ywStocktakingRecord);
                }
                ywStocktakingRecordMapper.insert(ywStocktakingRecordList);
            }
        }else{
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"盘点单状态已流转,无法进行该操作");
        }
    }
 
 
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public void finishById(Integer id, YwOutinboundServiceImpl ywOutinboundService, LoginUserInfo loginUserInfo) {
        YwStocktaking model = ywStocktakingMapper.selectById(id);
        if(Objects.isNull(model)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到盘点单数据");
        }
        if(!Constants.equalsInteger(model.getStatus(),Constants.ONE)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"盘点单状态已流转,无法进行该操作!");
        }
        if(!Constants.equalsInteger(loginUserInfo.getId(),model.getUserId())){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"非您的盘点单无法进行操作");
        }
        List<YwStocktakingRecord> recordList = ywStocktakingRecordMapper.selectList(new QueryWrapper<YwStocktakingRecord>().lambda()
                .eq(YwStocktakingRecord::getStocktakingId,id).eq(YwStocktakingRecord::getIsdeleted,Constants.ZERO));
        //盘盈数据列表
        List<YwOutinboundRecord> inBoundRecordList = new ArrayList<>();
        //盘亏数据列表
        List<YwOutinboundRecord> outBoundRecordList = new ArrayList<>();
        for (YwStocktakingRecord ywStocktakingRecord:recordList) {
            //提交时 未进行盘点的明细数据
            if(Constants.equalsInteger(ywStocktakingRecord.getStatus(),Constants.ZERO)){
                ywStocktakingRecordMapper.update(new UpdateWrapper<YwStocktakingRecord>().lambda()
                        .set(YwStocktakingRecord::getStatus,Constants.TWO)
                        .set(YwStocktakingRecord::getUserId,loginUserInfo.getId())
                        .eq(YwStocktakingRecord::getId,ywStocktakingRecord.getId())
                );
                continue;
            }
            //查询库存信息 进行库存更新
            YwStock ywStock = ywStockMapper.selectOne(new QueryWrapper<YwStock>().lambda().eq(YwStock::getMaterialId,ywStocktakingRecord.getMaterialId())
                    .eq(YwStock::getWarehouseId,model.getWarehouseId()).eq(YwStock::getStock,ywStocktakingRecord.getStock()));
            if(ywStock.getStock().compareTo(ywStocktakingRecord.getActStock())<Constants.ZERO ){
                YwOutinboundRecord ywOutinboundRecord = new YwOutinboundRecord();
                ywOutinboundRecord.setMaterialId(ywStock.getMaterialId());
                ywOutinboundRecord.setStock(ywStocktakingRecord.getActStock().subtract(ywStock.getStock()));
                inBoundRecordList.add(ywOutinboundRecord);
            }else if(ywStock.getStock().compareTo(ywStocktakingRecord.getActStock())>Constants.ZERO ){
                YwOutinboundRecord ywOutinboundRecord = new YwOutinboundRecord();
                ywOutinboundRecord.setMaterialId(ywStock.getMaterialId());
                ywOutinboundRecord.setStock(ywStock.getStock().subtract(ywStocktakingRecord.getActStock()));
                outBoundRecordList.add(ywOutinboundRecord);
            }
//            if(Objects.isNull(ywStock)){
//                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"库存信息异常,请联系管理员");
//            }
//            if(ywStocktakingRecord.getActStock().compareTo(BigDecimal.ZERO)==Constants.ZERO){
//                ywStockMapper.deleteById(ywStock.getId());
//            }else {
//                ywStockMapper.update(new UpdateWrapper<YwStock>().lambda().set(YwStock::getStock,ywStocktakingRecord.getActStock()).eq(YwStock::getId,ywStock.getId()));
//            }
        }
        ywStocktakingMapper.update(new UpdateWrapper<YwStocktaking>().lambda()
                .set(YwStocktaking::getEditDate,DateUtil.getCurrDateTime())
                .set(YwStocktaking::getDoneDate,DateUtil.getCurrDateTime())
                .set(YwStocktaking::getDoneUserId,loginUserInfo.getId())
                .set(YwStocktaking::getEditor,loginUserInfo.getId())
                .set(YwStocktaking::getStatus,Constants.TWO)
                .eq(YwStocktaking::getId,model.getId()));
        //生产盘盈的入库单
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(inBoundRecordList)){
            YwOutinbound ywOutinbound = new YwOutinbound();
            ywOutinbound.setDoneDate(new Date());
            ywOutinbound.setWarehouseId(model.getWarehouseId());
            ywOutinbound.setType(4);
            ywOutinbound.setOrigin(1);
            ywOutinbound.setContent("盘点单【"+model.getCode() +"】处理生成盘盈数据");
            ywOutinbound.setStatus(Constants.ZERO);
            ywOutinbound.setRecordList(inBoundRecordList);
            ywOutinbound.setLoginUserInfo(loginUserInfo);
            ywOutinboundService.create(ywOutinbound);
        }
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(outBoundRecordList)){
            YwOutinbound ywOutinbound = new YwOutinbound();
            ywOutinbound.setDoneDate(new Date());
            ywOutinbound.setWarehouseId(model.getWarehouseId());
            ywOutinbound.setType(10);
            ywOutinbound.setOrigin(1);
            ywOutinbound.setContent("盘点单【"+model.getCode() +"】处理生成盘亏数据");
            ywOutinbound.setStatus(Constants.ZERO);
            ywOutinbound.setRecordList(outBoundRecordList);
            ywOutinbound.setLoginUserInfo(loginUserInfo);
            ywOutinboundService.create(ywOutinbound);
        }
 
        //标记待办已完成
        noticesJoinMapper.update(null,new UpdateWrapper<Notices>().lambda()
                .set(Notices::getStatus,Constants.ONE)
                .set(Notices::getParam3,Constants.TWO)
                .eq(Notices::getIsdeleted,Constants.ZERO)
                .eq(Notices::getObjType,Constants.ZERO)
                .eq(Notices::getObjId,id)
        );
 
    }
 
    @Override
    public void delete(YwStocktaking ywStocktaking) {
        UpdateWrapper<YwStocktaking> deleteWrapper = new UpdateWrapper<>(ywStocktaking);
        ywStocktakingMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        ywStocktakingMapper.deleteBatchIds(ids);
    }
 
    @Override
    public void updateById(YwStocktaking ywStocktaking) {
        if(Objects.isNull(ywStocktaking)
                || StringUtils.isBlank(ywStocktaking.getName())
                || Objects.isNull(ywStocktaking.getId())
                || Objects.isNull(ywStocktaking.getPlanDate())
                || Objects.isNull(ywStocktaking.getWarehouseId())
                || Objects.isNull(ywStocktaking.getUserId())
        ){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        YwStocktaking model = ywStocktakingMapper.selectById(ywStocktaking.getId());
        if(Objects.isNull(model)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到盘点单数据");
        }
 
        SystemUser systemUser = systemUserMapper.selectById(ywStocktaking.getUserId());
        if(Objects.isNull(systemUser)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到盘点人信息");
        }
 
        YwWarehouse ywWarehouse = ywWarehouseMapper.selectById(ywStocktaking.getWarehouseId());
        if(Objects.isNull(ywWarehouse)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到仓库数据");
        }
        //判断当前仓库当前日期是否存在盘点单
        if(ywStocktakingMapper.selectCount(new QueryWrapper<YwStocktaking>().lambda().ne(YwStocktaking::getId,ywStocktaking.getId()).eq(YwStocktaking::getIsdeleted,Constants.ZERO).eq(YwStocktaking::getStatus,Constants.ZERO)
                .eq(YwStocktaking::getWarehouseId,ywStocktaking.getWarehouseId()).apply(" DATE(PLAN_DATE) = '" + DateUtil.formatDate(ywStocktaking.getPlanDate(),"yyyy-MM-dd") +"'" ))>0){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"["+DateUtil.formatDate(ywStocktaking.getPlanDate(),"yyyy-MM-dd")+"]["+ywWarehouse.getName()+"]已有盘点单,不可重复发起盘点");
        }
        LoginUserInfo loginUserInfo = ywStocktaking.getLoginUserInfo();
        ywStocktaking.setEditor(loginUserInfo.getId());
        ywStocktaking.setEditDate(new Date());
        ywStocktaking.setCode(null);
        ywStocktakingMapper.updateById(ywStocktaking);
 
 
        //标记待办已处理
        noticesJoinMapper.update(null,new UpdateWrapper<Notices>().lambda()
                .set(Notices::getStatus,Constants.ONE)
                .eq(Notices::getIsdeleted,Constants.ONE)
                .eq(Notices::getObjType,Constants.ZERO)
                .eq(Notices::getObjId,ywStocktaking.getId())
        );
 
        //添加待办信息
        Notices notices = new Notices();
        notices.setCreateDate(new Date());
        notices.setCreator(ywStocktaking.getCreator());
        notices.setIsdeleted(Constants.ZERO);
        notices.setParam1(DateUtil.formatDate(ywStocktaking.getPlanDate(),"yyyy-MM-dd"));
        notices.setTitle("盘点待办");
        notices.setContent("["+ywStocktaking.getName()+"]将在["+notices.getParam1()+"]进行盘点,请按时执行");
        notices.setObjId(ywStocktaking.getId());
        notices.setObjType(Constants.ZERO);
        notices.setType(notices.getObjType());
        notices.setUserId(ywStocktaking.getUserId());
        notices.setStatus(Constants.ZERO);
        //整理json数据
        Map<String,Object> jsonMap = new HashMap<>();
        jsonMap.put("key1","盘点单"+ywStocktaking.getCode());
        jsonMap.put("key2", DateUtil.getFomartDate(ywStocktaking.getPlanDate(),"yyyy-MM-dd"));
        jsonMap.put("key3", ywWarehouse.getName());
        jsonMap.put("key4", systemUser.getRealname());
        notices.setParam2(JSONObject.toJSONString(jsonMap));
        notices.setParam3(Constants.ZERO+"");
        noticesJoinMapper.insert(notices);
 
 
    }
 
    @Override
    public void updateByIdInBatch(List<YwStocktaking> ywStocktakings) {
        if (CollectionUtils.isEmpty(ywStocktakings)) {
            return;
        }
        for (YwStocktaking ywStocktaking: ywStocktakings) {
            this.updateById(ywStocktaking);
        }
    }
 
    @Override
    public YwStocktaking findById(Integer id) {
        return ywStocktakingMapper.selectById(id);
    }
 
    @Override
    public YwStocktaking getDetail(Integer id) {
        MPJLambdaWrapper<YwStocktaking> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(YwStocktaking.class)
                .select(" s.realname ", YwStocktaking::getUserName)
                .select(" s1.realname ",YwStocktaking::getCreateName)
                .selectAs(YwWarehouse::getName,YwStocktaking::getWarehouseName)
                .leftJoin(YwWarehouse.class,YwWarehouse::getId,YwStocktaking::getWarehouseId)
                .leftJoin("system_user s on t.user_id = s.id")
                .leftJoin("system_user s1 on t.creator = s1.id")
                .eq(YwStocktaking::getId,id)
                .last("limit 1");
        YwStocktaking ywStocktaking = ywStocktakingMapper.selectOne(queryWrapper);
        List<YwStocktakingRecord> recordList = ywStocktakingRecordMapper.selectList(new QueryWrapper<YwStocktakingRecord>().lambda().eq(YwStocktakingRecord::getStocktakingId,id).eq(YwStocktakingRecord::getIsdeleted,Constants.ZERO));
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(recordList)){
            //合计盘点数据
            ywStocktaking.setFinishAmount(recordList.stream().filter(i->Constants.equalsInteger(i.getStatus(),Constants.ONE)).collect(Collectors.toList()).size());
            ywStocktaking.setUnFinishAmount(recordList.stream().filter(i->Constants.equalsInteger(i.getStatus(),Constants.ZERO)).collect(Collectors.toList()).size());
            ywStocktaking.setEqualAmount(recordList.stream().filter(i->Constants.equalsInteger(i.getStatus(),Constants.ONE)&&Constants.equalsInteger(i.getType(),Constants.ZERO)).collect(Collectors.toList()).size());
            ywStocktaking.setLossAmount(recordList.stream().filter(i->Constants.equalsInteger(i.getStatus(),Constants.ONE)&&Constants.equalsInteger(i.getType(),Constants.ONE)).collect(Collectors.toList()).size());
            ywStocktaking.setProfitAmount(recordList.stream().filter(i->Constants.equalsInteger(i.getStatus(),Constants.ONE)&&Constants.equalsInteger(i.getType(),Constants.TWO)).collect(Collectors.toList()).size());
        }
        return ywStocktaking;
    }
 
    @Override
    public YwStocktaking findOne(YwStocktaking ywStocktaking) {
        QueryWrapper<YwStocktaking> wrapper = new QueryWrapper<>(ywStocktaking);
        return ywStocktakingMapper.selectOne(wrapper);
    }
 
    @Override
    public List<YwStocktaking> findList(YwStocktaking ywStocktaking) {
        QueryWrapper<YwStocktaking> wrapper = new QueryWrapper<>(ywStocktaking);
        return ywStocktakingMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<YwStocktaking> findPage(PageWrap<YwStocktaking> pageWrap) {
        IPage<YwStocktaking> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        MPJLambdaWrapper<YwStocktaking> queryWrapper = new MPJLambdaWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        YwStocktaking model = pageWrap.getModel();
        queryWrapper.selectAll(YwStocktaking.class)
                .select(" s.realname ", YwStocktaking::getUserName)
                .select(" s1.realname ",YwStocktaking::getCreateName)
                .selectAs(YwWarehouse::getName,YwStocktaking::getWarehouseName)
                .leftJoin(YwWarehouse.class,YwWarehouse::getId,YwStocktaking::getWarehouseId)
                .leftJoin("system_user s on t.user_id = s.id")
                .leftJoin("system_user s1 on t.creator = s1.id")
                .eq(YwStocktaking::getIsdeleted,Constants.ZERO)
                .like(StringUtils.isNotBlank(model.getName()),YwStocktaking::getName,model.getName())
                .eq(Objects.nonNull(model.getStatus()),YwStocktaking::getStatus,model.getStatus())
                .eq(Objects.nonNull(model.getUserId()),YwStocktaking::getUserId,model.getUserId())
                .orderByAsc(YwStocktaking::getStatus)
                .orderByDesc(YwStocktaking::getPlanDate)
        ;
        IPage iPage = ywStocktakingMapper.selectJoinPage(page, YwStocktaking.class,queryWrapper);
        return PageData.from(iPage);
    }
 
    @Override
    public long count(YwStocktaking ywStocktaking) {
        QueryWrapper<YwStocktaking> wrapper = new QueryWrapper<>(ywStocktaking);
        return ywStocktakingMapper.selectCount(wrapper);
    }
 
 
    public synchronized String  getNextCode(){
        String prefix =  "PD-" + DateUtil.getDate(new Date(),"yyyyMMdd") +"-";
        Integer countNum  = RedisUtil.getObject(redisTemplate,
                Constants.RedisKeys.COM_OUTBOUND_TAKING_KEY,
                Integer.class);
        countNum = Constants.formatIntegerNum(countNum)+1;
        //更新缓存
        RedisUtil.addObject(redisTemplate, Constants.RedisKeys.COM_OUTBOUND_TAKING_KEY,countNum);
        String nextIndex =Integer.toString( countNum );
        return prefix + org.apache.commons.lang3.StringUtils.leftPad(nextIndex,4,"0");
    }
 
 
}