zhongrj
2023-09-08 a5264ae79165ac3db0878896f7995c7c92259ecb
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
package cn.gistack.sm.sjztmd.service.impl;
 
import cn.gistack.sm.sjztmd.entity.TbAttResWaterBlock;
import cn.gistack.sm.sjztmd.entity.TbAttResWaterBlock;
import cn.gistack.sm.sjztmd.mapper.TbAttResWaterBlockMapper;
import cn.gistack.sm.sjztmd.service.ITbAttResBaseService;
import cn.gistack.sm.sjztmd.service.ITbAttResWaterBlockService;
import cn.gistack.system.user.feign.IUserClient;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.core.secure.utils.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @ClassName TbAttResWaterBlockServiceImpl
 * @Description TODO
 * @author zhongrj
 * @date 2023/8/14
 * @Version 1.0
 */
@Service
@DS("zt")
public class TbAttResWaterBlockServiceImpl extends ServiceImpl<TbAttResWaterBlockMapper, TbAttResWaterBlock> implements ITbAttResWaterBlockService {
 
    @Autowired
    private IUserClient userClient;
 
    @Autowired
    private ITbAttResBaseService tbAttResBaseService;
 
    /**
     * 查询水库填报详情信息
     * @param guid
     * @return
     */
    @Override
    public List<TbAttResWaterBlock> getDetailByGuid(String guid) {
        return baseMapper.getDetailByResGuid(guid);
    }
 
    /**
     * 编辑
     * @param newList
     * @return
     */
    @Override
    public boolean updateByGuid(List<TbAttResWaterBlock> newList) {
        boolean edit = false;
        // 编辑标记
        if (newList.get(0).getCheckState()==4){
            edit = true;
        }
        // 查询是否存在
        List<TbAttResWaterBlock> oldList = baseMapper.getDetailByResGuid(newList.get(0).getResGuid());
        // 更新日志
        setUpdateLog(oldList,newList);
        // 创建或更新数据
        boolean flag = addOrUpdate(oldList,newList);
        if (flag){
            if (!edit) {
                tbAttResBaseService.updateFinalState(newList.get(0).getResGuid(), 0);
            }
            return true;
        }
        // 返回
        return false;
    }
 
    /**
     * 新增或更新数据
     * @param oldList
     * @param newList
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean addOrUpdate(List<TbAttResWaterBlock> oldList,List<TbAttResWaterBlock> newList) {
        // 取旧数据和新提交数据差集--删除
        // 去除前一天已填报的人员手机号集合
        List<TbAttResWaterBlock> removeList = oldList.stream().filter(vo -> !newList.stream().map(e ->
            e.getGuid()).collect(Collectors.toList()).contains(vo.getGuid())).collect(Collectors.toList());
        // 取旧数据和新提交数据交集--更新
        List<TbAttResWaterBlock> updateList = newList.stream().filter(vo -> oldList.stream().map(e ->
            e.getGuid()).collect(Collectors.toList()).contains(vo.getGuid())).collect(Collectors.toList());
        // 取新数提交据和旧数据交集--新增
        List<TbAttResWaterBlock> addList = newList.stream().filter(vo -> !oldList.stream().map(e ->
            e.getGuid()).collect(Collectors.toList()).contains(vo.getGuid())).collect(Collectors.toList());
        boolean addFlag = true;
        boolean updateFlag = true;
        boolean removeFlag = true;
        if (addList.size()>0){
            // 批量新增
            addFlag = saveBatch(addList);
        }
        if (updateList.size()>0){
            // 批量更新
            addFlag = updateBatchById(updateList);
        }
        if (removeList.size()>0){
            // 批量删除
            removeFlag = removeBatchByIds(removeList);
        }
        if (addFlag && updateFlag && removeFlag){
            return true;
        }
        // 返回
        return false;
    }
 
    /**
     * 审核
     * @param newList
     * @return
     */
    @Override
    public boolean auditByGuid(List<TbAttResWaterBlock> newList) {
        boolean edit = false;
        // 编辑标记
        if (newList.get(0).getCheckState()==4){
            edit = true;
        }
        // 查询是否存在
        List<TbAttResWaterBlock> oldList = baseMapper.getDetailByResGuid(newList.get(0).getResGuid());
        // 更新操作日志
        setUpdateLog(oldList,newList);
        // 更新审核日志
        setUpdateCheckLog(oldList,newList);
        // 更新数据
        boolean batch = updateBatchById(newList);
        if (batch){
            if (!edit) {
                tbAttResBaseService.updateFinalState(newList.get(0).getResGuid(), newList.get(0).getCheckState());
            }
            return true;
        }
        // 返回
        return false;
    }
 
    /**
     * @param oldList
     * @param newList
     * 更新审核记录
     */
    private void setUpdateCheckLog(List<TbAttResWaterBlock> oldList,List<TbAttResWaterBlock> newList) {
        // 遍历
        for (TbAttResWaterBlock newTbAttResWaterBlock : newList) {
            String fillName = "";
            if (newTbAttResWaterBlock.getCheckState() == 2) {
                fillName = "审核";
            }
            if (newTbAttResWaterBlock.getCheckState() == 3) {
                fillName = "退回" + (null == newTbAttResWaterBlock.getCheckLog() ? "" : ":" + newTbAttResWaterBlock.getCheckLog());
                for (TbAttResWaterBlock tbAttResWaterBlock : oldList) {
                    if (tbAttResWaterBlock.getGuid().equals(newTbAttResWaterBlock.getGuid())){
                        // 退回时进行设置状态为填写中
                        newTbAttResWaterBlock.setCheckState(0);
                    }
                }
            }
 
            // 拼接
            String time = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());
            String userName = AuthUtil.getUser().getUserName();
            String phone = userClient.userInfoById(AuthUtil.getUserId()).getData().getPhone();
            StringBuilder builder = new StringBuilder();
            builder.append(time)
                .append(",")
                .append(fillName)
                .append(",")
                .append(userName)
                .append(",")
                .append(phone)
                .append(";");
            // 判断是否存在
            if (null==newTbAttResWaterBlock.getGuid() || newTbAttResWaterBlock.getGuid().equals("")){
                // 填报
                newTbAttResWaterBlock.setCheckLog(builder.toString());
            } else {
                for (TbAttResWaterBlock tbAttResWaterBlock : oldList) {
                    if (tbAttResWaterBlock.getGuid().equals(newTbAttResWaterBlock.getGuid())){
                        StringBuilder stringBuilder = new StringBuilder(null == tbAttResWaterBlock.getCheckLog() ? "" : tbAttResWaterBlock.getCheckLog());
                        stringBuilder.append(builder);
                        // 填报
                        newTbAttResWaterBlock.setCheckLog(stringBuilder.toString());
                    }
                }
            }
        }
    }
 
    /**
     * @param oldList
     * @param newList
     * 更新操作记录
     */
    private void setUpdateLog(List<TbAttResWaterBlock> oldList,List<TbAttResWaterBlock> newList) {
        // 遍历
        for (TbAttResWaterBlock newTbAttResWaterBlock : newList){
            String fillName = "";
            if (newTbAttResWaterBlock.getCheckState()==0){
                fillName = "编辑";
            }
            if (newTbAttResWaterBlock.getCheckState()==1){
                fillName = "提交";
            }
            if (newTbAttResWaterBlock.getCheckState()==2){
                fillName = "审核";
            }
            if (newTbAttResWaterBlock.getCheckState()==3){
                fillName = "退回" + (null==newTbAttResWaterBlock.getCheckLog()?"":":" + newTbAttResWaterBlock.getCheckLog());
            }
            // 管理员编辑
            if (newTbAttResWaterBlock.getCheckState()==4){
                fillName = "编辑";
                // 找到相关的 guid 数据
                if (oldList.size()>0){
                    newTbAttResWaterBlock.setCheckState(oldList.get(0).getCheckState());
                }
            }
 
            // 拼接
            String time = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());
            String userName = AuthUtil.getUser().getUserName();
            String phone = userClient.userInfoById(AuthUtil.getUserId()).getData().getPhone();
            StringBuilder builder = new StringBuilder();
            builder.append(time)
                .append(",")
                .append(fillName)
                .append(",")
                .append(userName)
                .append(",")
                .append(phone)
                .append(";");
            // 判断是否存在
            if (null==newTbAttResWaterBlock.getGuid() || newTbAttResWaterBlock.getGuid().equals("")){
                // 填报
                newTbAttResWaterBlock.setLog(builder.toString());
            }else {
                for (TbAttResWaterBlock tbAttResWaterBlock : oldList) {
                    if (tbAttResWaterBlock.getGuid().equals(newTbAttResWaterBlock.getGuid())){
                        StringBuilder stringBuilder = new StringBuilder(null==tbAttResWaterBlock.getLog()?"":tbAttResWaterBlock.getLog());
                        stringBuilder.append(builder);
                        // 填报
                        newTbAttResWaterBlock.setLog(stringBuilder.toString());
                    }
                }
            }
        }
    }
}