邱宇豪
2023-11-06 a13c11ad715c82debac45c38a92d0e0b9f4f09ef
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
package com.thhy.materials.modules.biz.pipeoutplan.service.impl;
 
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.thhy.general.common.BasicResult;
import com.thhy.general.config.SysUserInfo;
import com.thhy.general.utils.UUIDUtils;
import com.thhy.general.utils.UserInfoUtils;
import com.thhy.materials.modules.biz.pipeoutplan.entity.PipeOutPlanEntity;
import com.thhy.materials.modules.biz.pipeoutplan.entity.PipeOutPlanMothEntity;
import com.thhy.materials.modules.biz.pipeoutplan.mapper.PipeOutPlanMapper;
import com.thhy.materials.modules.biz.pipeoutplan.service.PipeOutPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
 
/**
 * @Author QiuYuHao
 * @CreateDate 2023-11-03 9:42:41
 * 发运管理逻辑实现层
 */
@Service
public class PipeOutPlanServiceImpl implements PipeOutPlanService {
 
    @Autowired
    private PipeOutPlanMapper pipeOutPlanMapper;
 
    public static final Object LOCK = new Object();
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BasicResult insert(PipeOutPlanEntity pipeOutPlanEntity) {
        PipeOutPlanEntity isExit = pipeOutPlanMapper.selectInfoByProIdAndYear(pipeOutPlanEntity.getProId(), pipeOutPlanEntity.getPlanYear());
        if (isExit != null){
            return BasicResult.faild("500","同一项目、年份有且只有一个计划",null);
        }
        String planOutId = UUIDUtils.create();
        pipeOutPlanEntity.setPlanOutId(planOutId);
        pipeOutPlanMapper.insert(pipeOutPlanEntity);
        List<PipeOutPlanMothEntity> monthList = pipeOutPlanEntity.getMonthList();
        for (PipeOutPlanMothEntity vo : monthList) {
            vo.setPipeOutPlanId(planOutId);
            vo.setId(UUIDUtils.create());
            pipeOutPlanMapper.insertMoth(vo);
        }
        return BasicResult.success();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BasicResult delete(String pipeNeedId) {
        pipeOutPlanMapper.delete(pipeNeedId);
        pipeOutPlanMapper.deleteMoth(pipeNeedId);
        return BasicResult.success();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BasicResult update(PipeOutPlanEntity pipeOutPlanEntity) {
        PipeOutPlanEntity isExit = pipeOutPlanMapper.selectInfoByProIdAndYear(pipeOutPlanEntity.getProId(), pipeOutPlanEntity.getPlanYear());
        if (isExit != null && !pipeOutPlanEntity.getPlanOutId().equals(isExit.getPlanOutId()) ){
            return BasicResult.faild("500","同一项目、年份有且只有一个计划",null);
        }
        pipeOutPlanMapper.update(pipeOutPlanEntity);
        String planOutId = pipeOutPlanEntity.getPlanOutId();
        pipeOutPlanMapper.deleteMoth(planOutId);
        List<PipeOutPlanMothEntity> monthList = pipeOutPlanEntity.getMonthList();
        for (PipeOutPlanMothEntity vo : monthList) {
            vo.setPipeOutPlanId(planOutId);
            vo.setId(UUIDUtils.create());
            pipeOutPlanMapper.insertMoth(vo);
        }
        return BasicResult.success();
    }
 
    @Override
    public BasicResult selectInfo(String pipeNeedId) {
        PipeOutPlanEntity pipeOutPlanEntity = pipeOutPlanMapper.selectInfo(pipeNeedId);
        List<PipeOutPlanMothEntity> mothList = pipeOutPlanMapper.findMothList(pipeNeedId);
        pipeOutPlanEntity.setMonthList(mothList);
        return BasicResult.success(pipeOutPlanEntity);
    }
 
    @Override
    public BasicResult findAll(Map<String, Object> map) {
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        PageHelper.startPage(pageNum,pageSize);
        SysUserInfo userInfo = UserInfoUtils.getInstance().getUserInfo();
        map.put("companyId",userInfo.getCompanyId());
        List<PipeOutPlanEntity> all = pipeOutPlanMapper.findAll(map);
        all.forEach(obj->{
            String proId = obj.getProId();
            Integer planYear = obj.getPlanYear();
            String planOutId = obj.getPlanOutId();
            List<PipeOutPlanMothEntity> mothListByProIdAndYear = pipeOutPlanMapper.findMothListByProIdAndYear(proId, planYear.toString(), planOutId);
            Integer completePlanProductNum = 0;
            synchronized (this.LOCK){
                for (PipeOutPlanMothEntity pipeOutPlanMothEntity : mothListByProIdAndYear) {
                    Integer completePlanProduct = pipeOutPlanMothEntity.getCompletePlanProduct();
                    completePlanProductNum+=completePlanProduct;
                }
            }
            obj.setMonthList(mothListByProIdAndYear);
            obj.setCompletePipeNum(completePlanProductNum);
        });
        return BasicResult.success(new PageInfo<>(all));
    }
 
 
 
}