刘小明
2024-08-21 14b0417bca9050a11792248f5c5c7e7c86e4744f
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
package com.dy.pmsProduct.schedule;
 
import com.dy.common.webUtil.QueryResultVo;
import com.dy.pmsGlobal.daoPlt.PltStationMapper;
import com.dy.pmsGlobal.daoPr.PrAssemblyPlanMapper;
import com.dy.pmsGlobal.daoPr.PrScheduleMapper;
import com.dy.pmsGlobal.daoPr.PrScheduleRelMapper;
import com.dy.pmsGlobal.pojoPlt.PltStation;
import com.dy.pmsGlobal.pojoPr.PrSchedule;
import com.dy.pmsGlobal.pojoPr.PrScheduleRel;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.PojoUtils;
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.Optional;
 
@Slf4j
@Service
public class ScheduleSv {
    private PrScheduleMapper scheduleDao;
    private PrScheduleRelMapper scheduleRelDao;
    private PrAssemblyPlanMapper assemblyPlanDao;
    private PltStationMapper stationDao;
    @Autowired
    public void setStationDao(PltStationMapper stationDao) {
        this.stationDao = stationDao;
    }
    @Autowired
    public void setScheduleRelDao(PrScheduleRelMapper scheduleRelDao) {
        this.scheduleRelDao = scheduleRelDao;
    }
    @Autowired
    public void setAssemblyPlanDao(PrAssemblyPlanMapper assemblyPlanDao) {
        this.assemblyPlanDao = assemblyPlanDao;
    }
    @Autowired
    public void setScheduleDao(PrScheduleMapper scheduleDao) {
        this.scheduleDao = scheduleDao;
    }
    @Transactional
    public int save(PrSchedule schedule) {
        int count = scheduleDao.insertSelective(schedule);
        saveRel(schedule);
        return count;
    }
    @Transactional
    public int update(PrSchedule schedule) {
        scheduleRelDao.deleteByScheduleId(schedule.id);
        saveRel(schedule);
        return scheduleDao.updateByPrimaryKeySelective(schedule);
    }
    private void saveRel(PrSchedule schedule) {
        schedule.relList.forEach(rel ->validateRelData(rel, schedule));
        schedule.relList.forEach(rel -> scheduleRelDao.insertSelective(rel));
    }
    private void validateRelData(PrScheduleRel rel,PrSchedule schedule) {
        validatePlan(rel);
        validateStation(rel);
        rel.scheduleId = schedule.id;
    }
 
    private void validatePlan(PrScheduleRel rel) {
        if (rel.planId != null && rel.nodeId != null) {
            long countPlan = assemblyPlanDao.countByPlanIdAndNodeId(rel.planId, rel.nodeId);
            if (countPlan == 0) {
                log.error("排班数据不匹配, planId:{}, nodeId:{}", rel.planId, rel.nodeId);
                throw new RuntimeException("排班数据不匹配, planId:" + rel.planId + ", nodeId:" + rel.nodeId);
            }
        }
    }
 
    private void validateStation(PrScheduleRel rel) {
        if (rel.stationId != null) {
            Optional<PltStation> stationOpt = Optional.ofNullable(stationDao.selectByPrimaryKey(rel.stationId));
            stationOpt.orElseThrow(() -> {
                log.error("工站信息不存在, stationId:{}", rel.stationId);
                return new RuntimeException("工站信息不存在, stationId:" + rel.stationId);
            });
        }
    }
 
    public PrSchedule selectById(Long id) {
        return scheduleDao.selectByPrimaryKey(id);
    }
 
    public QueryResultVo<List<PrSchedule>> selectSome(QueryVo vo) {
        Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(vo);
        //查询符合条件的记录总数
        Long itemTotal = scheduleDao.selectSomeCount(params);
        QueryResultVo<List<PrSchedule>> rsVo = new QueryResultVo<>(vo.pageSize, vo.pageCurr) ;
        //计算分页等信息
        rsVo.calculateAndSet(itemTotal, params);
        //查询符合条件的记录
        rsVo.obj = scheduleDao.selectSome(params);
        return rsVo;
    }
 
    public List<Map<String, Object>> selectPlan(QueryVo vo) {
        List<Map<String,Object>> list = assemblyPlanDao.selectByPlanName(vo.planName);
 
        return list;
    }
 
    public List<PrSchedule> selectAll(QueryVo queryVo) {
        return scheduleDao.selectAll(queryVo.scheduleDate, queryVo.userId);
    }
}