jiangping
2023-08-16 fb65ffe3606482e15bcdf1fd5adfb1acb1b8e846
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
package doumeemes.service.ext.impl;
 
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import doumeemes.biz.system.impl.SystemDictDataBizImpl;
import doumeemes.core.exception.BusinessException;
import doumeemes.core.model.ApiResponse;
import doumeemes.core.model.LoginUserInfo;
import doumeemes.core.model.PageData;
import doumeemes.core.model.PageWrap;
import doumeemes.core.utils.Constants;
import doumeemes.core.utils.excel.EasyExcelUtil;
import doumeemes.core.utils.redis.RedisUtil;
import doumeemes.dao.business.model.*;
import doumeemes.dao.ext.AppliancesExtMapper;
import doumeemes.dao.ext.dto.QueryAppliancesByManualDTO;
import doumeemes.dao.ext.dto.QueryAppliancesExtDTO;
import doumeemes.dao.ext.vo.AppliancesExtListVO;
import doumeemes.dao.ext.vo.CategoryExtListVO;
import doumeemes.dao.system.model.SystemDictData;
import doumeemes.service.business.AppliancesService;
import doumeemes.service.business.BarcodeParamService;
import doumeemes.service.business.impl.*;
import doumeemes.service.ext.AppliancesExtService;
import doumeemes.service.system.impl.SystemDictDataServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
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.web.multipart.MultipartFile;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * 工装器具信息Service实现
 * @author 江蹄蹄
 * @date 2022/04/20 11:01
 */
@Service
public class AppliancesExtServiceImpl implements AppliancesExtService {
 
 
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private AppliancesExtMapper appliancesExtMapper;
 
 
    @Autowired
    private AppliancesService appliancesService;
 
    @Autowired
    private BarcodeParamService barcodeParamService;
    @Autowired
    private SystemDictDataBizImpl systemDictDataBizimpl;
    @Autowired
    private AppliancesServiceImpl appliancesServiceimpl;
    @Autowired
    private WorkorderServiceImpl workorderServiceimpl;
 
    @Autowired
    private WorkorderRecordServiceImpl workorderRecordServiceimpl;
    @Autowired
    private SystemDictDataServiceImpl systemDictDataServiceimpl;
    @Autowired
    private ApplianceChangeServiceImpl applianceChangeServiceimpl;
    @Autowired
    private ApplianceChangeDetialServiceImpl applianceChangeDetialServiceimpl;
 
    @Override
    public PageData<AppliancesExtListVO> findPage(PageWrap<QueryAppliancesExtDTO> pageWrap) {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        pageWrap.getModel().setDeleted(Constants.ZERO);
        pageWrap.getModel().setRootDepartId(user.getRootDepartment().getRootId());
        pageWrap.getModel().setDepartId(user.getCurComDepartment().getId());
 
        PageHelper.startPage(pageWrap.getPage(), pageWrap.getCapacity());
        List<AppliancesExtListVO> result = appliancesExtMapper.selectListByCondition(pageWrap.getModel());
        List<CategoryExtListVO> clist = RedisUtil.getObject(redisTemplate,Constants.RedisKeys.COM_CATEGORY_KEY+user.getCompany().getId(), ArrayList.class);
 
        for(int i=0;i<result.size();i++){
            AppliancesExtListVO appliancesExtListVO=result.get(i);
            for(int j=0;j<clist.size();j++){
                CategoryExtListVO categoryExtListVO=clist.get(j);
                if(StringUtils.isNotEmpty(appliancesExtListVO.getCumodelCateBigId())){
                    if(Constants.equalsInteger(Integer.valueOf(appliancesExtListVO.getCumodelCateBigId()),categoryExtListVO.getId())){
                        appliancesExtListVO.setCmodel1BigName(categoryExtListVO.getName());
                    }
                }
 
                if(StringUtils.isNotEmpty(appliancesExtListVO.getCumodelCateMiddleId())){
                    if(Constants.equalsInteger(Integer.valueOf(appliancesExtListVO.getCumodelCateMiddleId()),categoryExtListVO.getId())){
                        appliancesExtListVO.setCmodel1MiddleName(categoryExtListVO.getName());
                    }
                }
 
                if(StringUtils.isNotEmpty(appliancesExtListVO.getCumodelCateSmallId())){
                    if(Constants.equalsInteger(Integer.valueOf(appliancesExtListVO.getCumodelCateSmallId()),categoryExtListVO.getId())){
                        appliancesExtListVO.setCmodel1SmallName(categoryExtListVO.getName());
                    }
                }
 
            }
        }
 
        return PageData.from(new PageInfo<>(result));
    }
 
    @Override
    public AppliancesExtListVO findMaxSerial(QueryAppliancesExtDTO pageWrap) {
        AppliancesExtListVO result = appliancesExtMapper.findMaxSerial(pageWrap);
        return result;
    }
 
 
    @Override
    @Transactional(rollbackFor = {BusinessException.class,Exception.class})
    public ApiResponse importExcel(MultipartFile file, String categoryId) {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        //获取二维码规则
        BarcodeParam queryparam=new BarcodeParam();
        queryparam.setDepartId(user.getCurComDepartment().getId());
        queryparam.setRootDepartId(user.getRootDepartment().getId());
        queryparam.setType(Constants.BARCODEPARAM_TYPE.appliance);
        BarcodeParam barcodeParam =barcodeParamService.findOne(queryparam);
        if(barcodeParam==null){
            return ApiResponse.failed("请先完善二维码篮筐配置");
        }
 
        List<Appliances> alllist = EasyExcelUtil.importExcel(file, 1, 1, Appliances.class);
       // List<Appliances> alllist = ExcelExporter.build(Appliances.class).readExcel(null, Appliances.class, file);
        if(StringUtils.isEmpty(categoryId)){
            return ApiResponse.failed("工装类型不能为空");
        }
        //查询编码是否存在
        try{
            for(int i=0;i<alllist.size();i++){
                Appliances chriApp=alllist.get(i);
                if(StringUtils.isEmpty(chriApp.getCode())){
                   throw new RuntimeException("编码不能为空");
                }
 
                if(StringUtils.isEmpty(chriApp.getTypeName())){
                    throw new RuntimeException("工装属性不能为空");
                }
 
                List<SystemDictData> dicList=  systemDictDataBizimpl.queryDataByCode("SYSTEM_APPLIANCE_TYPE",chriApp.getTypeName());
 
                if(dicList==null||dicList.size()<=0){
                    throw new RuntimeException("工装属性不存在");
                }
 
                Appliances query1=new Appliances();
                query1.setDeleted(Constants.ZERO);
               // query1.setCategoryId(Integer.valueOf(categoryId));
                query1.setDepartId(user.getCurComDepartment().getId());
                query1.setRootDepartId(user.getRootDepartment().getId());
                query1.setCode(chriApp.getCode());
                List<Appliances> queryData1=  appliancesService.findList(query1);
                if(queryData1.size()>0){
                    throw new RuntimeException(chriApp.getCode()+"编码已存在,不允许插入");
                }
 
                //获取工装属性
                Appliances appliances=new Appliances();
                appliances.setBrcode(barcodeParam.getId());
                appliances.setDeleted(Constants.ZERO);
                appliances.setDepartId(user.getCurComDepartment().getId());
                appliances.setRootDepartId(user.getRootDepartment().getId());
                appliances.setCreateUser(user.getId());
                appliances.setCreateTime(new Date());
                appliances.setStatus(Constants.APPLIANCES_STATUS.empty);
                appliances.setCategoryId(Integer.valueOf(categoryId));
                appliances.setType(dicList.get(0).getId());
                appliances.setCode(chriApp.getCode());
              //  appliances.setCategoryId(Integer.valueOf(categoryId));
                appliancesService.insertDate(appliances);
            }
        }catch (Exception e){
            e.printStackTrace();
            return ApiResponse.failed(e.getMessage());
        }
        return ApiResponse.success(null);
    }
 
    @Override
    public List<AppliancesExtListVO> selectListByCondition(QueryAppliancesExtDTO pageWrap) {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        pageWrap.setDeleted(Constants.ZERO);
        pageWrap.setRootDepartId(user.getRootDepartment().getRootId());
        pageWrap.setDepartId(user.getCurComDepartment().getId());
        List<AppliancesExtListVO> result = appliancesExtMapper.selectListByCondition(pageWrap);
        List<CategoryExtListVO> clist = RedisUtil.getObject(redisTemplate,Constants.RedisKeys.COM_CATEGORY_KEY+user.getCompany().getId(), ArrayList.class);
 
        for(int i=0;i<result.size();i++){
            AppliancesExtListVO appliancesExtListVO=result.get(i);
            for(int j=0;j<clist.size();j++){
                CategoryExtListVO categoryExtListVO=clist.get(j);
                if(StringUtils.isNotEmpty(appliancesExtListVO.getCumodelCateBigId())){
                    if(Constants.equalsInteger(Integer.valueOf(appliancesExtListVO.getCumodelCateBigId()),categoryExtListVO.getId())){
                        appliancesExtListVO.setCmodel1BigName(categoryExtListVO.getName());
                    }
                }
 
                if(StringUtils.isNotEmpty(appliancesExtListVO.getCumodelCateMiddleId())){
                    if(Constants.equalsInteger(Integer.valueOf(appliancesExtListVO.getCumodelCateMiddleId()),categoryExtListVO.getId())){
                        appliancesExtListVO.setCmodel1MiddleName(categoryExtListVO.getName());
                    }
                }
 
                if(StringUtils.isNotEmpty(appliancesExtListVO.getCumodelCateSmallId())){
                    if(Constants.equalsInteger(Integer.valueOf(appliancesExtListVO.getCumodelCateSmallId()),categoryExtListVO.getId())){
                        appliancesExtListVO.setCmodel1SmallName(categoryExtListVO.getName());
                    }
                }
 
            }
        }
 
        return result;
    }
 
 
 
    @Override
    public ApiResponse<List<AppliancesExtListVO>> getApplianceListByCondition(QueryAppliancesExtDTO pageWrap) {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        pageWrap.setDeleted(Constants.ZERO);
        pageWrap.setRootDepartId(user.getRootDepartment().getRootId());
        pageWrap.setDepartId(user.getCurComDepartment().getId());
        List<AppliancesExtListVO> result = appliancesExtMapper.selectListByCondition(pageWrap);//第一个旧篮筐
 
        List<CategoryExtListVO> clist = RedisUtil.getObject(redisTemplate,Constants.RedisKeys.COM_CATEGORY_KEY+user.getCompany().getId(), ArrayList.class);
 
        for(int i=0;i<result.size();i++){
            AppliancesExtListVO appliancesExtListVO=result.get(i);
            for(int j=0;j<clist.size();j++){
                CategoryExtListVO categoryExtListVO=clist.get(j);
                if(StringUtils.isNotEmpty(appliancesExtListVO.getCumodelCateBigId())){
                    if(Constants.equalsInteger(Integer.valueOf(appliancesExtListVO.getCumodelCateBigId()),categoryExtListVO.getId())){
                        appliancesExtListVO.setCmodel1BigName(categoryExtListVO.getName());
                    }
                }
 
                if(StringUtils.isNotEmpty(appliancesExtListVO.getCumodelCateMiddleId())){
                    if(Constants.equalsInteger(Integer.valueOf(appliancesExtListVO.getCumodelCateMiddleId()),categoryExtListVO.getId())){
                        appliancesExtListVO.setCmodel1MiddleName(categoryExtListVO.getName());
                    }
                }
 
                if(StringUtils.isNotEmpty(appliancesExtListVO.getCumodelCateSmallId())){
                    if(Constants.equalsInteger(Integer.valueOf(appliancesExtListVO.getCumodelCateSmallId()),categoryExtListVO.getId())){
                        appliancesExtListVO.setCmodel1SmallName(categoryExtListVO.getName());
                    }
                }
 
            }
        }
 
 
 
        return ApiResponse.success(result);
    }
 
 
    @Override
    @Transactional
    public  ApiResponse chageApplicance(Integer oldId, Integer newId) {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        Appliances result = appliancesServiceimpl.findById(oldId);//旧篮筐
        if(result!=null&&result.getStatus()!=0&&result.getStatus()!=1){
            Appliances result1 = appliancesServiceimpl.findById(newId);//新篮筐
            if(result1!=null&&result1.getStatus()==1){
                if(result.getType()!=result1.getType()){
                    return ApiResponse.failed("工装属性不相同,请检查后再试");
                }
                result1.setValidTime(result.getValidTime());
                result1.setStatus(result.getStatus());
                result1.setWorkorderId(result.getWorkorderId());
                result1.setMaterialId(result.getMaterialId());
                result1.setNum(result.getNum());
                result1.setPositionType(result.getPositionType());
                result1.setWarehouseId(result.getWarehouseId());
                result1.setLocationId(result.getLocationId());
                result1.setProcedureId(result.getProcedureId());
                result1.setCurObjId(result.getCurObjId());
                result1.setCurObjType(result.getCurObjType());
                appliancesServiceimpl.updateById(result1);
 
                result1.setValidTime(null);
                result.setStatus(Constants.ONE);
                result.setWorkorderId(null);
                result.setMaterialId(null);
                result.setNum(new BigDecimal(Constants.ZERO));
                result.setPositionType(null);
                result.setWarehouseId(null);
                result.setLocationId(null);
                result.setProcedureId(null);
                result.setCurObjId(null);
                result.setCurObjType(null);
                appliancesServiceimpl.updateById(result);
            }else{
                return ApiResponse.failed("工装不为空,请从新扫描");
            }
        }else{
            return ApiResponse.failed("旧工装不可用");
        }
        return ApiResponse.success(null);
    }
 
 
 
    @Override
    @Transactional(rollbackFor = {BusinessException.class,Exception.class})
    public  ApiResponse chageApplicance(QueryAppliancesExtDTO query) {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        BigDecimal oldnum= new BigDecimal(0);
        BigDecimal newnum= new BigDecimal(0);
        Integer type=null;
        Integer workorderId=null;
        Boolean workorderBoo=true;//标识旧篮筐是否是同一工单
        if(query.getOldList()!=null&&query.getOldList().size()>0){
           for(int i=0;i<query.getOldList().size();i++){
               Appliances data=query.getOldList().get(i);
               Appliances app=   appliancesService.findById(data.getId());
               if(i==0){
                   type=app.getType();
                   workorderId=app.getWorkorderId();
               }
               if(!Constants.equalsInteger(app.getType(),type)){
                   return ApiResponse.failed(app.getCode()+":新旧篮筐篮筐属性不相同,不允许更换");
               }
               if(app.getWarehouseId()==null){
                   return ApiResponse.failed(app.getCode()+":旧工装不在仓库不允许更换");
               }
               if(Constants.equalsInteger(app.getStatus(),0)||Constants.equalsInteger(app.getStatus(),1)){
                   return ApiResponse.failed(app.getCode()+":旧篮筐报废或空时,不允许更换");
               }
               if(!Constants.equalsInteger(app.getRootDepartId(),user.getRootDepartment().getId())){
                   return ApiResponse.failed(app.getCode()+":非当前组织下篮筐不允许更换");
               }
               if(!Constants.equalsInteger(app.getDepartId(),user.getCurComDepartment().getId())){
                   return ApiResponse.failed(app.getCode()+":非当前组织下篮筐不允许更换");
               }
               oldnum=oldnum.add(app.getNum());
 
               if( !pdworkorder(app.getWorkorderId(),app.getId(),user)){
                   return ApiResponse.failed("工装["+app.getCode()+"]有生产记录,当前无法进行更换;");
               }
 
               SystemDictData systemDictData=   systemDictDataServiceimpl.findById(app.getType());
               if(StringUtils.equals(systemDictData.getLabel(),"APPLIANCE_USELESS")){
                   //报废
                   if(!oldlkBF(query.getOldList())){
                       return ApiResponse.failed("旧工装["+app.getCode()+"]属性为报废,更换时,需要旧篮筐的物料编码、最近加工工序、批次号、仓库、库位都相同,才能一起更换");
                   }
               }else{
                   //非报废
                   if(!oldlkFBF(query.getOldList())){
                       return ApiResponse.failed("旧工装["+app.getCode()+"]属性为非报废,更换时,需要旧篮筐的物料编码、批次号、质量属性、最近加工工序、返工返修工序、申请单编号(返工返修/报废申请)、客返单号、仓库、库位都相同,才能一起更换");
                   }
               }
 
               if(!Constants.equalsInteger(workorderId,app.getWorkorderId())){
                   workorderBoo=false;
               }
           }
        }else{
            return ApiResponse.failed("旧工装不能为空");
        }
 
 
        if(query.getNewList()!=null&&query.getNewList().size()>0){
            for(int j=0;j<query.getNewList().size();j++){
                Appliances data=query.getNewList().get(j);
                Appliances app1=   appliancesService.findById(data.getId());
 
 
                Boolean compareNewOrOld=true;
                //新篮筐里面可以包含旧篮筐
                for(int o=0;o<query.getOldList().size();o++) {
                    Appliances comparolddata = query.getOldList().get(o);
                    Appliances comparoldapp1=   appliancesService.findById(comparolddata.getId());
                    if(Constants.equalsInteger(comparoldapp1.getId(),app1.getId())){
                        compareNewOrOld=false;
                    }
                }
                if(compareNewOrOld){
                    if(!Constants.equalsInteger(app1.getStatus(),1)){
                        return ApiResponse.failed(app1.getCode()+":新篮筐为非空篮筐不允许更换");
                    }
                }
 
                if(!Constants.equalsInteger(app1.getRootDepartId(),user.getRootDepartment().getId())){
                    return ApiResponse.failed(app1.getCode()+":非当前组织下篮筐不允许更换");
                }
                if(!Constants.equalsInteger(app1.getDepartId(),user.getCurComDepartment().getId())){
                    return ApiResponse.failed(app1.getCode()+":非当前组织下篮筐不允许更换");
                }
                newnum=newnum.add(data.getNum());
 
               if( !pdworkorder(app1.getWorkorderId(),app1.getId(),user)){
                   return ApiResponse.failed("工装["+app1.getCode()+"]有生产记录,当前无法进行更换;");
               }
 
                if(!Constants.equalsInteger(app1.getType(),type)){
                    return ApiResponse.failed(app1.getCode()+":新旧篮筐篮筐属性不相同,不允许更换");
                }
            }
        }else{
            return ApiResponse.failed("新工装不能为空");
        }
 
        if(newnum.compareTo(oldnum)!=0){
            return ApiResponse.failed("新篮筐中物料总数量不等于旧篮筐中物料总数量");
        }
 
 
        Appliances olddata=   appliancesService.findById(query.getOldList().get(0).getId());
        //添加更换单记录表
       Integer acid= insertApplianceChange(olddata,user);
 
 
       List<Appliances> newappliancelist=new ArrayList<>();
        List<Appliances> oldappliancelist=new ArrayList<>();
 
        //查询新篮筐中的数据是否重复
        Boolean newappliancerepeat=checkRepeatNewApplianceOrNo(query.getNewList());
        if(newappliancerepeat){
            return ApiResponse.failed("新工装重复扫描,更换失败!");
        }
        for(int z=0;z<query.getNewList().size();z++){
            Appliances data=query.getNewList().get(z);
            Appliances app=   appliancesService.findById(data.getId());
            app.setStatus(Constants.APPLIANCES_STATUS.full);
            app.setUpdateTime(new Date());
            app.setUpdateUser(user.getId());
            SystemDictData systemDictData=   systemDictDataServiceimpl.findById(olddata.getType());
            if(StringUtils.equals(systemDictData.getLabel(),"APPLIANCE_USELESS")){
             //报废
 
 
            }else{
                //非报废
                app.setNextProcedureId(olddata.getNextProcedureId());
                app.setBackProcedureId(olddata.getBackProcedureId());
                app.setBackorderId(olddata.getBackorderId());
                app.setSaleorderId(olddata.getSaleorderId());
            }
 
            if(workorderBoo){
                //相同的workorderid
                app.setWorkorderId(olddata.getWorkorderId());
                app.setRelobjType(0);
            }else{
                //不相同的workorderid
                app.setWorkorderId(acid);
                app.setRelobjType(1);
 
            }
 
            if(query.getOldList()!=null&&query.getOldList().size()==1){
                app.setWorkorderId(olddata.getWorkorderId());
                app.setRelobjType(olddata.getRelobjType());
            }
 
            app.setMaterialId(olddata.getMaterialId());
            app.setNum(data.getNum());
            app.setPositionType(olddata.getPositionType());
            app.setWarehouseId(olddata.getWarehouseId());
            app.setLocationId(olddata.getLocationId());
            app.setProcedureId(olddata.getProcedureId());
            app.setBatch(olddata.getBatch());
            app.setQualityType(olddata.getQualityType());
            newappliancelist.add(app);
           // appliancesService.updateById(app);
            //更换单详情记录表
            insertApplianceChangedetail( app, user, 1, acid );
        }
 
 
        for(int h=0;h<query.getOldList().size();h++){
            Appliances data=query.getOldList().get(h);
            Appliances result=   appliancesService.findById(data.getId());
 
            Boolean cmpar=true;
            insertApplianceChangedetail( result, user, 0, acid );
            if(query.getNewList().size()>0){
                for(int newi=0;newi<query.getNewList().size();newi++){
                    Appliances newcom=query.getNewList().get(newi);
                    //新篮筐包含旧篮筐,旧篮筐中的数据不需要清空
                    if(Constants.equalsInteger(newcom.getId(),result.getId())){
                        cmpar =false;
                        break;
                    }
 
                }
            }
            if(cmpar){
                result.setStatus(Constants.ONE);
                result.setWorkorderId(null);
                result.setMaterialId(null);
                result.setNum(new BigDecimal(Constants.ZERO));
                result.setPositionType(null);
                result.setWarehouseId(null);
                result.setLocationId(null);
                result.setProcedureId(null);
                result.setCurObjId(null);
                result.setCurObjType(null);
                //  result.setCategoryId(null);
                result.setBatch(null);
                result.setQualityType(null);
                result.setBackorderId(null);
                result.setSaleorderId(null);
                result.setBackProcedureId(null);
                result.setNextProcedureId(null);
                result.setRelobjType(null);
                oldappliancelist.add(result);
               // appliancesExtMapper.editById(result);
            }
 
 
        }
 
 
 
        if(oldappliancelist.size()>0){
            for(int n=0;n<oldappliancelist.size();n++){
                Appliances app=oldappliancelist.get(n);
                appliancesExtMapper.editById(app);
            }
        }
 
        if(newappliancelist.size()>0){
            for(int n=0;n<newappliancelist.size();n++){
                Appliances app=newappliancelist.get(n);
                appliancesService.updateById(app);
            }
 
        }
 
 
 
 
 
 
 
        return ApiResponse.success(null);
    }
 
    /**
     * 如果新篮筐/旧篮筐在工单(未报工状态,包括:已创建、已备料、已检验、已完工)上有投料或报工记录,
     * 无法更换;需要提示:工装[LK0001]有生产记录,当前无法进行更换;
     * @param workorderId
     * @return
     */
    public boolean pdworkorder(Integer workorderId,Integer appliancesId,LoginUserInfo user){
 
       /* WorkorderRecord workorderRecord=new WorkorderRecord();
        workorderRecord.setDeleted(Constants.ZERO);
        workorderRecord.setWorkorderId(workorderId);
        workorderRecord.setRootDepartId(user.getRootDepartment().getId());
        workorderRecord.setDepartId(user.getCurComDepartment().getId());
        List<WorkorderRecord> workrecordList=  workorderRecordServiceimpl.findList(workorderRecord);
 
        Workorder workorder= workorderServiceimpl.findById(workorderId);
        if(workorder!=null&&(Constants.equalsInteger(workorder.getStatus(),Constants.WORKORDER_STATUS.create)
                ||Constants.equalsInteger(workorder.getStatus(),Constants.WORKORDER_STATUS.material)
                ||Constants.equalsInteger(workorder.getStatus(),Constants.WORKORDER_STATUS.done)
                ||Constants.equalsInteger(workorder.getStatus(),Constants.WORKORDER_STATUS.check))){
            if(workrecordList!=null&&workrecordList.size()>0){
                return false;
            }
        }else{
            return true ;//工单不存在直接跳过判断逻辑
        }
*/
 
        //根据appliance id 查询当前声生产记录里面包含的工单workorderid   的状态是否包含已创建、已备料、已检验、已完工
        WorkorderRecord wr=new WorkorderRecord();
        wr.setDeleted(Constants.ZERO);
        wr.setAppliancesId(appliancesId);
        wr.setRootDepartId(user.getRootDepartment().getId());
        wr.setDepartId(user.getCurComDepartment().getId());
       // wr.setType(1);//产出
        List<WorkorderRecord> worList=  workorderRecordServiceimpl.findList(wr);
        if(worList!=null&&worList.size()>0){
            for(int i=0;i<worList.size();i++){
                WorkorderRecord wrresult=worList.get(i);
                Workorder woresult= workorderServiceimpl.findById(wrresult.getWorkorderId());
                if(woresult!=null){
                    if(Constants.equalsInteger(woresult.getStatus(),Constants.WORKORDER_STATUS.create)
                            ||Constants.equalsInteger(woresult.getStatus(),Constants.WORKORDER_STATUS.material)
                            ||Constants.equalsInteger(woresult.getStatus(),Constants.WORKORDER_STATUS.done)
                            ||Constants.equalsInteger(woresult.getStatus(),Constants.WORKORDER_STATUS.check)){
                      return false;
                    }
                }
            }
        }
        return true ;
    }
 
 
    /**
     * 旧篮筐属性为报废,更换时,需要旧篮筐的物料编码、最近加工工序、批次号、仓库、库位都相同,才能一起更换;
     * @param oldlist
     * @return
     */
    public boolean oldlkBF(List<Appliances> oldlist){
        if(oldlist!=null&&oldlist.size()>0) {
            Integer materialId=null;
            Integer nextProcedureId=null;
            String batch="";
            Integer warehouseId=null;
            Integer locationId=null;
            for (int i = 0; i < oldlist.size(); i++) {
                Appliances data = oldlist.get(i);
                Appliances app = appliancesService.findById(data.getId());
                if(i==0){
                    materialId= app.getMaterialId();
                    nextProcedureId=app.getNextProcedureId();
                    batch=app.getBatch();
                    warehouseId=app.getWarehouseId();
                    locationId=app.getLocationId();
                }else{
                    if(Constants.equalsInteger(app.getMaterialId(),materialId)
                    &&Constants.equalsInteger(app.getNextProcedureId(),nextProcedureId)
                    &&StringUtils.equals(app.getBatch(),batch)
                    &&Constants.equalsInteger(app.getWarehouseId(),warehouseId)
                    &&Constants.equalsInteger(app.getLocationId(),locationId)){
 
                    }else{
                        return false;
                    }
                }
            }
        }
        return true;
    }
 
    /**
     * 如果旧篮筐属性非报废,更换时,需要旧篮筐的物料编码、批次号、质量属性、最近加工工序、返工返修工序、申请单编号(返工返修/报废申请)、客返单号、仓库、库位都相同,才能一起更换;
     * @param oldlist
     * @return
     */
    public boolean oldlkFBF(List<Appliances> oldlist){
        if(oldlist!=null&&oldlist.size()>0) {
            Integer materialId=null;
            String batch="";
            Integer qualityType=null;
            Integer nextProcedureId=null;
 
            Integer backProcedureId=null;
            Integer backorderId=null;
            Integer saleOrderId=null;
            Integer warehouseId=null;
            Integer locationId=null;
            for (int i = 0; i < oldlist.size(); i++) {
                Appliances data = oldlist.get(i);
                Appliances app = appliancesService.findById(data.getId());
                if(i==0){
                    materialId= app.getMaterialId();
                    nextProcedureId=app.getNextProcedureId();
                    batch=app.getBatch();
                    warehouseId=app.getWarehouseId();
                    locationId=app.getLocationId();
                    qualityType=app.getQualityType();
                    backProcedureId= app.getBackProcedureId();
                    backorderId=app.getBackorderId();
                    saleOrderId=app.getSaleorderId();
                }else{
                    if(Constants.equalsInteger(app.getMaterialId(),materialId)
                            &&Constants.equalsInteger(app.getNextProcedureId(),nextProcedureId)
                            &&StringUtils.equals(app.getBatch(),batch)
                            &&Constants.equalsInteger(app.getWarehouseId(),warehouseId)
                            &&Constants.equalsInteger(app.getLocationId(),locationId)
                            &&Constants.equalsInteger(app.getQualityType(),qualityType)
                            &&Constants.equalsInteger(app.getBackProcedureId(),backProcedureId)
                            &&Constants.equalsInteger(app.getBackorderId(),backorderId)
                            &&Constants.equalsInteger(app.getSaleorderId(),saleOrderId)){
 
                    }else{
                        return false;
                    }
                }
            }
        }
        return true;
    }
 
 
    //查詢新籃筐中的数据是否重复
    private Boolean checkRepeatNewApplianceOrNo(List<Appliances> list ) {
        if(list!=null&&list.size()>1){
            List<Integer> collect1 = list.stream().map(Appliances::getId).distinct().collect(Collectors.toList());
            if(collect1.size()!=list.size()){
                return true;
            }else{
                return false;
            }
        }else {
            return false;
        }
    }
 
 
 
    /**
     * 新增更换单记录
     * @param olddata
     * @param user
     * @return
     */
    public Integer insertApplianceChange(Appliances olddata,LoginUserInfo user){
        ApplianceChange applianceChange=new ApplianceChange();
        applianceChange.setDeleted(Constants.ZERO);
        applianceChange.setCreateTime(new Date());
        applianceChange.setCreateUser(user.getId());
        applianceChange.setRootDepartId(user.getRootDepartment().getId());
        applianceChange.setDepartId(user.getCurComDepartment().getId());
        applianceChange.setType(olddata.getType());
        applianceChange.setWarehouseId(olddata.getWarehouseId());
        applianceChange.setLocationId(olddata.getLocationId());
        applianceChange.setProcedureId(olddata.getProcedureId());
        applianceChange.setSaleorderId(olddata.getSaleorderId());
        applianceChange.setBackProcedureId(olddata.getBackProcedureId());
        applianceChange.setMaterialId(olddata.getMaterialId()+"");
        applianceChange.setBatch(olddata.getBatch());
        String code=applianceChangeServiceimpl.getNextCode(user.getCompany().getId());
        applianceChange.setCode(code);
        return applianceChangeServiceimpl.create(applianceChange);
    }
 
 
    /**
     * 工单记录详情添加
     * mark 标记新篮筐1,0旧篮筐
     * @param appliances
     * @param user
     * @param mark
     * @return
     */
 
    public Integer insertApplianceChangedetail(Appliances appliances,LoginUserInfo user,Integer mark,Integer changeid ){
        ApplianceChangeDetial applianceChangedetail=new ApplianceChangeDetial();
        applianceChangedetail.setDeleted(Constants.ZERO);
        applianceChangedetail.setCreateTime(new Date());
        applianceChangedetail.setCreateUser(user.getId());
        applianceChangedetail.setRootDepartId(user.getRootDepartment().getId());
        applianceChangedetail.setDepartId(user.getCurComDepartment().getId());
        applianceChangedetail.setApplianceId(appliances.getId());
        applianceChangedetail.setType(mark);
        applianceChangedetail.setMaterialId(appliances.getMaterialId()+"");
        applianceChangedetail.setBatch(appliances.getBatch());
        applianceChangedetail.setNum(appliances.getNum().intValue());
        applianceChangedetail.setWorkorderId(appliances.getWorkorderId());
        applianceChangedetail.setRelobjType(appliances.getRelobjType());
        applianceChangedetail.setChaneId(changeid);
        return applianceChangeDetialServiceimpl.create(applianceChangedetail);
    }
 
 
 
    @Override
    public PageData<AppliancesExtListVO> findListPageForManual(PageWrap<QueryAppliancesByManualDTO> pageWrap) {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        pageWrap.getModel().setRootDepartId(user.getRootDepartment().getRootId());
        pageWrap.getModel().setDepartId(user.getCurComDepartment().getId());
        PageHelper.startPage(pageWrap.getPage(), pageWrap.getCapacity());
        List<AppliancesExtListVO> result = appliancesExtMapper.findListPageForManual(pageWrap.getModel());
        return PageData.from(new PageInfo<>(result));
    }
 
 
}