lxw
2023-05-25 f3c27fb78447449a950ba73c5e72ceda64ad8a12
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.whyc.dto.Response;
import com.whyc.mapper.BaoJiGroupMapper;
import com.whyc.mapper.BaoJiGroupUserMapper;
import com.whyc.mapper.BaoJiGroupBattGroupMapper;
import com.whyc.mapper.CommonMapper;
import com.whyc.pojo.BaoJiGroup;
import com.whyc.pojo.BaoJiGroupUser;
import com.whyc.pojo.BaojiGroupBattGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
 
@Service
public class BaoJiGroupService {
 
    @Resource
    private BaoJiGroupMapper mapper;
 
    @Resource
    private BaoJiGroupUserMapper baoJiGroupUserMapper;
 
    @Resource
    private BaoJiGroupBattGroupMapper BaoJiGroupBattGroupMapper;
 
    @Resource
    private CommonMapper commonMapper;
 
    @Autowired
    private BattInfService battInfService;
 
    public void add(String groupName) {
        BaoJiGroup baoJiGroup = new BaoJiGroup();
        baoJiGroup.setBaoJiGroupName(groupName);
        baoJiGroup.setDischargePlanFlag(0);
 
        mapper.insert(baoJiGroup);
    }
 
    public void update(BaoJiGroup baoJiGroup) {
        mapper.updateById(baoJiGroup);
    }
 
    public void delete(long baoJiGroupId) {
        //删除包机组
        mapper.deleteById(baoJiGroupId);
        //删除关联的用户
        UpdateWrapper<BaoJiGroupUser> userWrapper = Wrappers.update();
        userWrapper.eq("baoji_group_id",baoJiGroupId);
        baoJiGroupUserMapper.delete(userWrapper);
        //删除关联的站点/电池组
        UpdateWrapper<BaojiGroupBattGroup> battGroupWrapper = Wrappers.update();
        battGroupWrapper.eq("baoji_group_id",baoJiGroupId);
        BaoJiGroupBattGroupMapper.delete(battGroupWrapper);
    }
 
    public List<BaoJiGroup> getBaoJiGroup() {
        return mapper.selectList(null);
    }
 
    /**
     * 每次设置放电标识时,提前校验组内是否与其他标识班组存在相同站点,存在则提示,不修改
     * @param baoJiGroupId
     * @param flag
     * @return
     */
    public Response updateDischargeFlag(Long baoJiGroupId, Integer flag) {
        if(flag==1) {
            //查询要设置的包机组的站点
            QueryWrapper<BaojiGroupBattGroup> query = Wrappers.query();
            query.select("baoji_group_id", "StationId").eq("baoji_group_id", baoJiGroupId);
            List<BaojiGroupBattGroup> stationIdList = BaoJiGroupBattGroupMapper.selectList(query);
            stationIdList = stationIdList.stream().distinct().collect(Collectors.toList());
            //查询其他的组,包含的所有站点
            List<BaojiGroupBattGroup> stationIdListWithDischargePlanFlag = BaoJiGroupBattGroupMapper.getStationIdListWithDischargePlanFlag();
            stationIdListWithDischargePlanFlag = stationIdListWithDischargePlanFlag.stream().distinct().collect(Collectors.toList());
            List<BaojiGroupBattGroup> repeatedStationIdList = new LinkedList<>();
            for (int i = 0; i < stationIdList.size(); i++) {
                BaojiGroupBattGroup baojiGroupBattGroup = stationIdList.get(i);
                for (int j = 0; j < stationIdListWithDischargePlanFlag.size(); j++) {
                    BaojiGroupBattGroup baojiGroupBattGroupWithFlag = stationIdListWithDischargePlanFlag.get(j);
                    if (baojiGroupBattGroupWithFlag.getStationId().equals(baojiGroupBattGroup.getStationId())) {
                        repeatedStationIdList.add(baojiGroupBattGroupWithFlag);
                        break;
                    }
                }
            }
            //查询重复站点的信息
            if (repeatedStationIdList.size() > 0) {
                return new Response().setII(1, false, repeatedStationIdList, "设置失败");
            }
        }
        UpdateWrapper<BaoJiGroup> update = Wrappers.update();
        update.set("discharge_plan_flag",flag).eq("baoji_group_id",baoJiGroupId);
        mapper.update(null,update);
        return new Response().set(1,true,"设置完成");
    }
 
    public List<BaoJiGroup> getBaoJiGroupNameListByFlag(int dischargePlanFlag) {
        QueryWrapper<BaoJiGroup> query = Wrappers.query();
        query.eq("discharge_plan_flag",dischargePlanFlag);
        return mapper.selectList(query);
    }
}