月球大数据地理空间分析展示平台-【后端】-月球后台服务
13693261870
2023-06-02 c31e03f0e51214a524d3fc34d30f3459698ff625
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
package com.moon.server.service.show;
 
import com.moon.server.entity.all.StaticData;
import com.moon.server.entity.ctrl.DownloadReqEntity;
import com.moon.server.entity.data.MetaEntity;
import com.moon.server.entity.show.ApplyEntity;
import com.moon.server.entity.show.FlowEntity;
import com.moon.server.entity.sys.UserEntity;
import com.moon.server.helper.AesHelper;
import com.moon.server.helper.StringHelper;
import com.moon.server.mapper.show.ApplyMapper;
import com.moon.server.service.data.MetaService;
import com.moon.server.service.sys.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
/**
 * 数据申请
 * @author WWW
 */
@Service
public class ApplyService implements ApplyMapper {
    @Autowired
    ApplyMapper applyMapper;
 
    @Autowired
    FlowService flowService;
 
    @Autowired
    UserService userService;
 
    @Autowired
    MetaService metaService;
 
    @Autowired
    DataLibService dataLibService;
 
    private final static Log log = LogFactory.getLog(ApplyService.class);
 
    @Override
    public Integer selectCount(String uname, Integer status, Timestamp start, Timestamp end) {
        uname = StringHelper.getLikeUpperStr(uname);
 
        return applyMapper.selectCount(uname, status, start, end);
    }
 
    @Override
    public List<ApplyEntity> selectByPage(Integer userid, String uname, Integer status, Timestamp start, Timestamp end, Integer limit, Integer offset) {
        uname = StringHelper.getLikeUpperStr(uname);
 
        return applyMapper.selectByPage(userid, uname, status, start, end, limit, offset);
    }
 
    @Override
    public List<ApplyEntity> selectAll() {
        return applyMapper.selectAll();
    }
 
    @Override
    public ApplyEntity selectById(int id) {
        return applyMapper.selectById(id);
    }
 
    @Override
    public UserEntity selectUserByDepcode(String depcode) {
        return applyMapper.selectUserByDepcode(depcode);
    }
 
    @Override
    public Integer selectSubmits(Integer userid) {
        return applyMapper.selectSubmits(userid);
    }
 
    @Override
    public List<FlowEntity> selectFlows(Integer applyid) {
        return applyMapper.selectFlows(applyid);
    }
 
    @Override
    public Integer insert(ApplyEntity entity) {
        return applyMapper.insert(entity);
    }
 
    @Override
    public Integer inserts(List<ApplyEntity> list) {
        return applyMapper.inserts(list);
    }
 
    @Override
    public Integer delete(int id) {
        return applyMapper.delete(id);
    }
 
    @Override
    public Integer deletes(List<Integer> ids) {
        return applyMapper.deletes(ids);
    }
 
    @Override
    public Integer update(ApplyEntity entity) {
        return applyMapper.update(entity);
    }
 
    @Override
    public Integer updates(List<ApplyEntity> list) {
        return applyMapper.updates(list);
    }
 
    @Override
    public Integer updateForDiscard(Integer userid, Integer id) {
        return applyMapper.updateForDiscard(userid, id);
    }
 
    @Override
    public Integer updateForResubmit(Integer userid, Integer id) {
        return applyMapper.updateForResubmit(userid, id);
    }
 
    @Override
    public Integer updateForSubmit(Integer userid, Integer applyid, Integer flowId) {
        return applyMapper.updateForSubmit(userid, applyid, flowId);
    }
 
    @Override
    public Integer updateForReject(Integer userid, Integer applyid, Integer flowId) {
        return applyMapper.updateForReject(userid, applyid, flowId);
    }
 
    /**
     * 插入数据申请
     */
    public Integer insertApply(UserEntity ue, DownloadReqEntity dr) throws Exception {
        ApplyEntity apply = getApplyEntity(ue, dr);
        int rows = applyMapper.insert(apply);
        if (0 == rows) {
            return 0;
        }
 
        List<FlowEntity> list = getFlowEntities(apply.getId(), ue, dr);
        if (list.isEmpty()) {
            applyMapper.delete(apply.getId());
            throw new Exception("找不到待审核人");
        }
 
        return flowService.inserts(list);
    }
 
    /**
     * 获取数据申请实体类
     */
    private ApplyEntity getApplyEntity(UserEntity ue, DownloadReqEntity dr) throws Exception {
        // String dbPwd = Md5Helper.reverse(Md5Helper.generate(dr.getPwd()))
        String aesPwd = AesHelper.encrypt(dr.getPwd());
        String gids = null == dr.getIds() || dr.getIds().isEmpty() ? null : StringHelper.join(dr.getIds(), ",");
 
        ApplyEntity apply = new ApplyEntity();
        apply.setUserid(ue.getId());
        apply.setDepids(StringHelper.join(dr.getDepcodes(), ","));
        apply.setTabs(getTabs(dr));
        apply.setEntities(StringHelper.join(dr.getEntities(), ","));
        apply.setWkt(dr.getWkt());
        apply.setPwd(aesPwd);
        apply.setStatus(0);
        apply.setCount(dr.getDepcodes().size());
        apply.setDescr(dr.getDescr());
        apply.setCreateUser(ue.getId());
        apply.setDepcode(dr.getDepcode());
        apply.setDircodes(dr.getDirs());
        apply.setGids(gids);
        apply.setFilters(dr.getFilter());
 
        return apply;
    }
 
    /**
     * 获取表名
     */
    private String getTabs(DownloadReqEntity dr) {
        if (!StaticData.SYS_META.equals(dr.getEntities().get(0))) {
            return StringHelper.join(dr.getTabs(), ",");
        }
 
        List<MetaEntity> list = metaService.selectMetaFiles(dr.getIds());
        if (null == list || list.isEmpty()) {
            return "元数据表";
        }
 
        StringBuilder sb = new StringBuilder();
        for (MetaEntity me : list) {
            sb.append(me.getName()).append(",");
            if (sb.length() > StaticData.I1000) {
                break;
            }
        }
        if (sb.length() > 1) {
            sb.deleteCharAt(sb.length() - 1);
        }
 
        return sb.toString();
    }
 
    /**
     * 获取申请流程实体类集合
     */
    private List<FlowEntity> getFlowEntities(Integer applyId, UserEntity ue, DownloadReqEntity dr) {
        List<FlowEntity> list = new ArrayList<>();
        for (String depcode : dr.getDepcodes()) {
            UserEntity user = selectUserByDepcode(depcode);
            if (null == user) {
                user = userService.selectByUid(StaticData.ADMIN);
            }
 
            FlowEntity flow = new FlowEntity();
            flow.setApplyid(applyId);
            flow.setDepcode(depcode);
            flow.setUserid(user.getId());
            flow.setStatus(0);
            flow.setDescr(null);
            flow.setCreateUser(ue.getId());
 
            list.add(flow);
        }
 
        return list;
    }
 
    /**
     * 打包DB数据
     */
    public void zipDbData(UserEntity ue, ApplyEntity entity) {
        try {
            List<String> entities = Arrays.asList(entity.getEntities().split(StaticData.COMMA));
            String pwd = AesHelper.decrypt(entity.getPwd());
 
            DownloadReqEntity dr = new DownloadReqEntity();
            dr.setEntities(entities);
            dr.setWkt(dr.getWkt());
            dr.setPwd(pwd);
            dr.setDepcode(entity.getDepcode());
            dr.setDirs(entity.getDircodes());
            dr.setIds(getIds(entity.getGids()));
            dr.setFilter(entity.getFilters());
 
            String guid = StaticData.SYS_META.equals(entity.getEntities()) ? metaService.downloadMeteReq(ue, dr) : dataLibService.downloadDbReq(ue, dr);
            if (!StringHelper.isEmpty(guid)) {
                entity.setGuid(guid);
                update(entity);
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }
 
    /**
     * 获取ID集合
     */
    private List<Integer> getIds(String gids) {
        if (StringHelper.isEmpty(gids)) {
            return null;
        }
 
        List<Integer> list = new ArrayList<>();
        for (String str : gids.split(StaticData.COMMA)) {
            list.add(Integer.parseInt(str));
        }
 
        return list;
    }
}