jiaosong
2023-08-11 8fc384afe9941fffee38e69ff0e6b0d6cb2e0487
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
package doumeemes.service.ext.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import doumeemes.core.constants.ResponseStatus;
import doumeemes.core.exception.BusinessException;
import doumeemes.core.model.LoginUserInfo;
import doumeemes.core.model.PageData;
import doumeemes.core.model.PageWrap;
import doumeemes.core.utils.Constants;
import doumeemes.core.utils.DateUtil;
import doumeemes.core.utils.excel.EasyExcelUtil;
import doumeemes.core.utils.redis.RedisUtil;
import doumeemes.dao.business.model.*;
import doumeemes.dao.business.vo.WorkPlansVO;
import doumeemes.dao.ext.*;
import doumeemes.dao.ext.dto.*;
import doumeemes.dao.ext.vo.*;
import doumeemes.service.ext.DepartmentExtService;
import doumeemes.service.ext.PlansExtService;
import doumeemes.service.ext.ProceduresExtService;
import doumeemes.service.ext.WorkPlansExtService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
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.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
 
/**
 * Service实现
 * @author 江蹄蹄
 * @since 2023/07/27 13:53
 */
@Service
public class WorkPlansExtServiceImpl implements WorkPlansExtService {
 
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private WorkPlansExtMapper workPlansExtMapper;
 
    @Autowired
    private PlansExtService plansExtService;
 
    @Autowired
    private PlanImportExtMapper planImportExtMapper;
 
    @Autowired
    private PlanHistoryExtMapper planHistoryExtMapper;
 
    @Autowired
    private MaterialDistributeExtMapper materialDistributeExtMapper;
 
    @Autowired
    private BomExtMapper bomExtMapper;
 
    @Autowired
    private RouteExtMapper routeExtMapper;
 
    @Autowired
    private ProceduresExtService proceduresExtService;
 
    @Autowired
    private DepartmentExtService departmentExtService;
 
    @Override
    public PageData<WorkPlansExtListVO> findPage(PageWrap<QueryWorkPlansExtDTO> pageWrap) {
        PageHelper.startPage(pageWrap.getPage(), pageWrap.getCapacity());
        List<WorkPlansExtListVO> result = workPlansExtMapper.selectList(pageWrap.getModel());
        return PageData.from(new PageInfo<>(result));
    }
 
 
    /****************************************************************************************************************************************************************************/
 
    @Override
    public Integer saveOrUpdate(WorkPlansSaveDTO workPlansSaveDTO) {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!");
        }
        if(Objects.isNull(workPlansSaveDTO)
                || StringUtils.isBlank(workPlansSaveDTO.getBatch())
                || Objects.isNull(workPlansSaveDTO.getPlanDate())
                || Objects.isNull(workPlansSaveDTO.getNum())
                || Objects.isNull(workPlansSaveDTO.getFactoryId())
                || Objects.isNull(workPlansSaveDTO.getUnitId())
                || Objects.isNull(workPlansSaveDTO.getMaterialId())
                || Objects.isNull(workPlansSaveDTO.getStartDate())
                || workPlansSaveDTO.getStartDate().getTime() > workPlansSaveDTO.getPlanDate().getTime()){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        WorkPlans workPlans = new WorkPlans();
        if(!Objects.isNull(workPlansSaveDTO.getWorkPlansId())){
            workPlans = workPlansExtMapper.selectById(workPlansSaveDTO.getWorkPlansId());
            if(Objects.isNull(workPlans)){
                throw new BusinessException(ResponseStatus.DATA_EMPTY);
            }
            if (!workPlans.getStatus().equals(Constants.WORKORDER_HISTORY_STATUS.create)){
                throw new BusinessException(ResponseStatus.ERR_STATUS);
            }
            BeanUtils.copyProperties(workPlansSaveDTO,workPlans);
            workPlans.setUpdateTime(DateUtil.getCurrentDate());
            workPlans.setUpdateUser(user.getId());
            workPlansExtMapper.updateById(workPlans);
        }else{
            BeanUtils.copyProperties(workPlansSaveDTO,workPlans);
            workPlans.setCreateTime(DateUtil.getCurrentDate());
            workPlans.setCreateUser(user.getId());
            workPlans.setDeleted(Constants.ZERO);
            workPlans.setDepartId(user.getCurComDepartment().getId());
            workPlans.setRootDepartId(user.getRootDepartment().getId());
            workPlans.setUserId(user.getId());
            workPlans.setOrigin(Constants.PLAS_ORIGIN.user);
            workPlans.setStatus(Constants.PLAN_STATUS.create);
            workPlans.setPlanCode(this.getNextCode(user.getCompany().getId()));
            workPlansExtMapper.insert(workPlans);
        }
 
        return workPlans.getId();
    }
 
    @Override
    public PageData<WorkPlansVO> findWorkPlansVOPage(PageWrap<QueryWorkPlansDTO> pageWrap) {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!");
        }
        //只能查看当前根组织的数据
        pageWrap.getModel().setRootDepartId(user.getRootDepartment().getId());
//        if(!Constants.equalsInteger(user.getCurComDepartment().getId(),user.getRootDepartment().getId())){
        //如果当前选择的公司级组织非根组织信息,只能查看当前选择公司级组织数据
        pageWrap.getModel().setDepartId(user.getCurComDepartment().getId());
//        }
        //数据权限
        List<Integer> dataPermission = user.getDepartPermissionList();
        if(dataPermission!=null){
            //只能看自己的
//            pageWrap.getModel().setCreateUser(user.getId());
//            //否则走数据权限
//            pageWrap.getModel().setDepartIds(dataPermission);
         /*   if(dataPermission.size() == 0){
                //只能看自己的
                pageWrap.getModel().setCreateUser(user.getId());
            }else{
 
            }*/
        }
//        if(user.getProcedureIds()!=null){
//            pageWrap.getModel().setProcedureIds(user.getProcedureIds());
//        }else{
//            // pageWrap.getModel().setUserId(user.getId());
//            pageWrap.getModel().setCreateUser(user.getId());
//        }
        PageHelper.startPage(pageWrap.getPage(), pageWrap.getCapacity());
        List<WorkPlansVO> result = workPlansExtMapper.getWorkPlansVOPage(pageWrap.getModel());
        if(!Objects.isNull(result)&&result.size()>Constants.ZERO){
            result.forEach(i->{
                List<PlansExtListVO> plansExtListVOList = plansExtService.getListByWorkPlan(i.getId());
                if(!Objects.isNull(plansExtListVOList)&&plansExtListVOList.size()>Constants.ZERO){
                    PlansExtListVO plansExtListVO = plansExtListVOList.get(plansExtListVOList.size()-Constants.ONE);
                    i.setFinishQualifiedNum(Objects.isNull(plansExtListVO.getQulifiedNum())?Constants.ZERO:plansExtListVO.getQulifiedNum());
                    i.setFinishUnQualifiedNum(Objects.isNull(plansExtListVO.getUnqulifiedNum())?Constants.ZERO:plansExtListVO.getUnqulifiedNum());
                    i.setFinishNum(Objects.isNull(plansExtListVO.getDoneNum())?Constants.ZERO:plansExtListVO.getDoneNum());
                }
                if (Objects.nonNull(i.getPlanDate())){
                    i.setHasExpire(DateUtil.toDateLocalDateTime(i.getPlanDate()).toLocalDate().isBefore(LocalDate.now()));
                }else {
                    i.setHasExpire(false);
                }
 
            });
        }
        return PageData.from(new PageInfo<>(result));
    }
 
    @Transactional(rollbackFor = {BusinessException.class,Exception.class})
    @Override
    public Integer deleteById(Integer id) {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        WorkPlans workPlans = workPlansExtMapper.selectById(id);
        if(Objects.isNull(workPlans)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(!workPlans.getStatus().equals(Constants.WORKPLANHISTORY_TYPE.create)){
            throw new BusinessException(ResponseStatus.ERR_STATUS);
        }
        workPlans.setDeleted(Constants.ONE);
        workPlans.setUpdateUser(user.getId());
        workPlans.setUpdateTime(new Date());
        return workPlansExtMapper.updateById(workPlans);
    }
 
 
    @Transactional(rollbackFor = {BusinessException.class,Exception.class})
    @Override
    public void paused(Integer id){
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!");
        }
        WorkPlans workPlans = workPlansExtMapper.selectById(id);
        if(Objects.isNull(workPlans)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        //已暂停的计划不需重复操作
        if(Constants.equalsInteger(workPlans.getPaused(),Constants.ONE) ){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,该计划已暂停,无需重复操作!");
        }
        workPlans.setUpdateTime(DateUtil.getCurrentDate());
        workPlans.setUpdateUser(user.getId());
        workPlans.setPaused(Constants.ONE);
        workPlansExtMapper.updateById(workPlans);
        //历史数据,关闭
        planHistoryExtMapper.insert(initHistoryByModel(workPlans,user.getId(),Constants.PLANHISTORY_TYPE.pause));
        List<Plans> plansList = plansExtService.getPlansList(new QueryWrapper<Plans>()
                .in("STATUS",0,1,4)
                .eq("PAUSED",Constants.ZERO)
                .eq("DELETED",Constants.ZERO)
                .eq("WORK_PLANS_ID",workPlans.getId()));
 
        plansList.forEach(i->{
            plansExtService.pauseByIdForStandard(i,user);
        });
    }
 
    @Transactional(rollbackFor = {BusinessException.class,Exception.class})
    @Override
    public void regain(Integer id){
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!");
        }
        WorkPlans workPlans = workPlansExtMapper.selectById(id);
        if(Objects.isNull(workPlans)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        //已暂停的计划不需重复操作
        if(Constants.equalsInteger(workPlans.getPaused(),Constants.ZERO) ){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,该计划已恢复,无需重复操作!");
        }
        workPlans.setUpdateTime(DateUtil.getCurrentDate());
        workPlans.setUpdateUser(user.getId());
        workPlans.setPaused(Constants.ZERO);
        workPlansExtMapper.updateById(workPlans);
        //历史数据,关闭
        planHistoryExtMapper.insert(initHistoryByModel(workPlans,user.getId(),Constants.PLANHISTORY_TYPE.pause));
        List<Plans> plansList = plansExtService.getPlansList(new QueryWrapper<Plans>()
                .in("STATUS",0,1,4)
                .eq("PAUSED",Constants.ONE)
                .eq("DELETED",Constants.ZERO)
                .eq("WORK_PLANS_ID",workPlans.getId()));
        plansList.forEach(i->{
            plansExtService.regainByIdForStandard(i,user);
        });
    }
 
    @Transactional(rollbackFor = {BusinessException.class,Exception.class})
    @Override
    public void close(Integer id){
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!");
        }
        WorkPlans workPlans = workPlansExtMapper.selectById(id);
        if(Objects.isNull(workPlans)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(Constants.equalsInteger(workPlans.getStatus(),Constants.PLAN_STATUS.close) ){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,该计划已关闭,无需重复操作!");
        }
        workPlans.setUpdateTime(DateUtil.getCurrentDate());
        workPlans.setUpdateUser(user.getId());
        workPlans.setStatus(Constants.PLAN_STATUS.close);
        workPlansExtMapper.updateById(workPlans);
        List<Plans> plansList = plansExtService.getPlansList(new QueryWrapper<Plans>()
                .in("STATUS",0,1,4)
                .eq("DELETED",Constants.ZERO)
                .eq("WORK_PLANS_ID",workPlans.getId()));
        plansList.forEach(i->{
            i.setUpdateTime(DateUtil.getCurrentDate());
            i.setUpdateUser(user.getId());
            i.setStatus(Constants.PLAN_STATUS.close);
            plansExtService.updateForPlan(i);
        });
    }
 
 
    public static PlanHistory initHistoryByModel(WorkPlans p, Integer userId, int status ) {
        PlanHistory h = new PlanHistory();
        h.setDeleted(Constants.ZERO);
        h.setCreateTime(DateUtil.getCurrentDate());
        h.setCreateUser(userId);
        h.setDepartId(p.getDepartId());
        h.setRootDepartId(p.getRootDepartId());
        h.setPlanId(p.getId());
        h.setRootDepartId(p.getRootDepartId());
        h.setDepartId(p.getDepartId());
        h.setTitle(Constants.WORKPLANHISTORY_TYPE.getTitleByStatus(p,status));
        h.setType(status);
        h.setInfo(Constants.WORKPLANHISTORY_TYPE.getInfoByStatus(p,status));
        h.setType(status);
 
        return h;
    }
 
 
    @Transactional(rollbackFor = {BusinessException.class,Exception.class})
    @Override
    public void importPlans(MultipartFile file)  {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!");
        }
        //解析excel
        List<WorkPlans> plansList = EasyExcelUtil.importExcel(file, 1, 1, WorkPlans.class);
        if(plansList == null || plansList.size()==0){
            throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "导入数据内容有误!");
        }
        PlanImport ip = new PlanImport();
        ip.setDeleted(Constants.ZERO);
        ip.setCreateTime(DateUtil.getCurrentDate());
        ip.setCreateUser(user.getId());
        ip.setFileName(file.getOriginalFilename());
        ip.setRootDepartId(user.getRootDepartment().getId());
        ip.setDepartId(user.getComDepartment().getId());
        //计划文件上传记录
        planImportExtMapper.insert(ip);
        int index = 1;
        List<PlanHistory> historyList = new ArrayList<>();
        for(WorkPlans p : plansList){
            p.setCreateTime(DateUtil.getCurrentDate());
            p.setCreateUser(user.getId());
            p.setDeleted(Constants.ZERO);
            p.setDepartId(user.getCurComDepartment().getId());
            p.setRootDepartId(ip.getRootDepartId());
            p.setImportId(ip.getId());
            p.setStatus(Constants.PLAN_STATUS.create);
            p.setOrigin(Constants.PLAS_ORIGIN.imports);
            p.setUserId(user.getId());
            p.setPlanCode(this.getNextCode(user.getCompany().getId()));
//            p.setFactoryId(user.getDepartment().getId());
            //检查数据有效性
            checkData(p,index,user);
            workPlansExtMapper.insert(p);
            historyList.add(initHistoryByModel(p,user.getId(),Constants.WORKPLANHISTORY_TYPE.create));
            index++;
        }
        //批量导入计划数据
        planHistoryExtMapper.insertBatch(historyList);
    }
 
 
    /**
     * 数据有效性检查
     * @param p
     * @param index
     * @param user
     * @throws BusinessException
     */
    private void checkData(WorkPlans p, int index,LoginUserInfo user) throws BusinessException{
        if(Constants.formatIntegerNum(p.getNum())<=0){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【计划生产数量】数据错误!");
        }
        if(p.getStartDate() == null){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【计划开始日期】数据错误,正确格式为:yyyy-MM-dd(如2022-06-07)!");
        }
        if(p.getPlanDate() == null){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【计划结束日期】数据错误,正确格式为:yyyy-MM-dd(如2022-06-07)!");
        }
        if(p.getPlanDate().getTime() <= p.getStartDate().getTime()){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【计划开始日期】要早于【计划结束日期】!");
        }
        if(StringUtils.isBlank(p.getMaterialCode())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【产品编码】数据错误!");
        }
        if(p.getFactoryName() == null){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【生产工厂】数据错误!");
        }
        if(p.getBatch() == null){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【生产批次】数据错误!");
        }
 
        Date pDate = DateUtil.getDateFromString(DateUtil.getShortTime(p.getPlanDate()) +" 00:00:00");
        Date nDate =  DateUtil.getDateFromString(DateUtil.getShortTime(DateUtil.getCurrentDate()) +" 00:00:00");
        if( nDate.getTime() > pDate.getTime()){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,计划生产日期必须为今天以后的日期!");
        }
        p.setMaterialCode(p.getMaterialCode().trim());
        p.setFactoryName(p.getFactoryName().trim());
        p.setBatch(p.getBatch().trim());
        QueryMaterialDistributeExtDTO d = new QueryMaterialDistributeExtDTO();
        d.setDeleted(Constants.ZERO);
        d.setMmodelCode(p.getMaterialCode());
        d.setRootDepartId(p.getRootDepartId());
        d.setDepartId(p.getDepartId());
        //查询产品信息
        MaterialDistributeExtListVO mm = materialDistributeExtMapper.selectByModel(d);
        if(mm == null){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【产品编码】数据不存在!");
        }
        if(Constants.equalsInteger(mm.getStatus(),Constants.ZERO)){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【产品】已失效,无法进行生产!");
        }
        //存储物料分配表主键
        p.setMaterialId(mm.getId());
        //单位编码
        p.setUnitId(mm.getUnitId());
        if(Constants.equalsInteger(mm.getStatus(),Constants.ZERO)){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【产品编码】不能安排生产计划!");
        }
        Bom bom = new Bom();
        bom.setDeleted(Constants.ZERO);
        bom.setMaterialId(mm.getId());
        bom.setRootDepartId(mm.getRootDepartId());
        bom = bomExtMapper.selectOne(new QueryWrapper<>(bom).last(" limit 1"));
        if(bom == null){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【产品】BOM未配置数据!");
        }
        if(!Constants.equalsInteger(bom.getStatus(),Constants.ONE)){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【产品】BOM已失效,无法进行生产!");
        }
        DepartmentExtListVO f = departmentExtService.getModelByName(user.getCompany().getId(),p.getFactoryName());
        if(f==null || !Constants.equalsInteger(f.getType(),Constants.DEPART_TYPE.factory) ){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【生产工厂】数据非工厂组织,请检查!");
        }
        if(Constants.equalsInteger(f.getStatus(),Constants.ONE) ){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【生产工厂】当前状态不正确!");
        }
        if(!Constants.equalsInteger(p.getDepartId(),departmentExtService.getComDepartId(f))){
            //如果工厂与公司级组织不对应
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【生产工厂】数据非当前公司组织,请检查!");
        }
        p.setFactoryId(f.getId());
    }
 
 
    @Override
    public WorkPlansVO getWorkPlansDetail(Integer id){
        if(Objects.isNull(id)){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        QueryWorkPlansDTO dto = new QueryWorkPlansDTO();
        dto.setWorkPlanId(id);
        WorkPlansVO workPlansVO = workPlansExtMapper.getWorkPlansVODetail(dto);
        if(Objects.isNull(workPlansVO)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        List<PlansExtListVO> plansExtListVOList = plansExtService.getListByWorkPlan(id);
        if(!Objects.isNull(plansExtListVOList)&&plansExtListVOList.size()>Constants.ZERO){
            workPlansVO.setPlansExtListVOList(plansExtListVOList);
            PlansExtListVO plansExtListVO = plansExtListVOList.get(plansExtListVOList.size()-Constants.ONE);
            workPlansVO.setFinishQualifiedNum(Objects.isNull(plansExtListVO.getQulifiedNum())?Constants.ZERO:plansExtListVO.getQulifiedNum());
            workPlansVO.setFinishUnQualifiedNum(Objects.isNull(plansExtListVO.getUnqulifiedNum())?Constants.ZERO:plansExtListVO.getUnqulifiedNum());
            workPlansVO.setFinishNum(Objects.isNull(plansExtListVO.getDoneNum())?Constants.ZERO:plansExtListVO.getDoneNum());
        }
        return workPlansVO;
    }
 
 
    public synchronized  String  getNextCode(Integer comId ){
        String prefix =  "WP-" + DateUtil.getDate(new Date(),"yyyyMMdd") +"-";
        Integer countNum  = RedisUtil.getObject(redisTemplate,Constants.RedisKeys.COM_WORK_PLAN_KEY+comId,Integer.class);
        countNum = Constants.formatIntegerNum(countNum)+1;
        //更新缓存
        RedisUtil.addObject(redisTemplate,Constants.RedisKeys.COM_WORK_PLAN_KEY+comId,countNum);
        String nextIndex =Integer.toString( countNum);
        return prefix + StringUtils.leftPad(nextIndex,4,"0");
    }
 
 
    @Transactional(rollbackFor = {BusinessException.class,Exception.class})
    @Override
    public void release(Integer id){
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!");
        }
        WorkPlans workPlans = workPlansExtMapper.selectById(id);
        if(Objects.isNull(workPlans)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(!Constants.equalsInteger(workPlans.getStatus(),Constants.ZERO) ){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,计划状态已流转,无法进行发布!");
        }
        workPlans.setUpdateTime(DateUtil.getCurrentDate());
        workPlans.setUpdateUser(user.getId());
        workPlans.setPublishDate(new Date());
        workPlans.setStatus(Constants.ONE);
        workPlansExtMapper.updateById(workPlans);
        planHistoryExtMapper.insert(initHistoryByModel(workPlans,user.getId(),Constants.WORKPLANHISTORY_TYPE.publish));
        //查询物料BOM工序信息
        Bom bom = bomExtMapper.selectOne(new QueryWrapper<Bom>().eq("MATERIAL_ID",workPlans.getMaterialId()).eq("DELETED",Constants.ZERO).last(" limit 1 "));
        if(Objects.isNull(bom)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,未查询到BOM信息,无法进行发布!");
        }
        List<ProceduresExtListVO> list= proceduresExtService.getGYListByCondition(bom.getRouteId());
        if(Objects.isNull(list)||list.size()==Constants.ZERO){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,未查询到BOM工序数据信息!");
        }
        list.forEach(i->{
            Plans plans = new Plans();
            BeanUtils.copyProperties(workPlans,plans);
            plans.setId(null);
            plans.setCreateTime(new Date());
            plans.setCreateUser(user.getId());
            plans.setOrigin(Constants.TWO);
            plans.setProcedureId(i.getId());
            plans.setUnitId(workPlans.getUnitId());
            plans.setStatus(Constants.PLAN_STATUS.publish);
            plans.setPublishDate(new Date());
            plans.setWorkPlansId(workPlans.getId());
            plansExtService.insertForWorkPlan(plans);
        });
    }
 
}