qushaowei
2023-09-21 1f95d9a630f4333710d92b6563a8cceed2b199f4
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package org.jeecg.modules.eam.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.base.entity.DataVersion;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
 
import org.jeecg.modules.eam.entity.*;
import org.jeecg.modules.eam.mapper.InspectionCycleMapper;
import org.jeecg.modules.eam.service.IInspectionCycleService;
import org.jeecg.modules.eam.service.IInspectionProjectService;
 
import org.jeecg.modules.eam.service.IdentityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @Description: mom_eam_inspection_cycle
 * @Author: cj
 * @Date: 2023-04-06
 * @Version: V1.0
 */
@Api(tags = "点检周期规则")
@RestController
@RequestMapping("/eam/inspectionCycle")
@Slf4j
public class InspectionCycleController extends JeecgController<InspectionCycle, IInspectionCycleService> {
 
 
    @Autowired
    private IInspectionCycleService inspectionCycleService;
 
 
    @Autowired
    private ISysBaseAPI sysBaseApi;
 
    @Autowired
    private InspectionCycleMapper inspectionCycleMapper;
 
    @Autowired
    @Lazy
    private IdentityService sysIdentityService;
 
 
//
//
//    @Autowired
//    private IDataVersionService dataVersionService;
//
//    @Autowired
//    private IEnterpriseService enterpriseService;
//
//    @Autowired
//    private IQuartzJobService quartzJobService;
//
//    @Autowired
//    private ISysDictService dictService;
 
 
    /**
     * 分页列表查询
     *
     * @param inspectionCycle
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "点检周期规则-分页列表查询", notes = "点检周期规则-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<Map<String, Object>>> queryPageList(InspectionCycle inspectionCycle, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        Page<Map<String, Object>> page = new Page(pageNo, pageSize);
        IPage<Map<String, Object>> pageList = inspectionCycleService.page(page, inspectionCycle);
        return Result.OK(pageList);
    }
 
 
    /**
     * 添加
     *
     * @param map
     * @return
     */
    @ApiOperation(value = "点检周期规则-添加", notes = "点检周期规则-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody Map<String, Object> map) {
        InspectionCycle inspectionCycle = new InspectionCycle();
//        String cycleUnit = dictService.queryDictTextByKey("cycle_unit", String.valueOf(map.get("cycleUnit")));
        String cycleUnit = sysBaseApi.queryDictTextByKey("cycle_unit", String.valueOf(map.get("cycleUnit")));
        inspectionCycle.setCode(String.valueOf(map.get("code"))).setName(String.valueOf(map.get("cycle")).concat(cycleUnit)).setCycle(new BigDecimal(String.valueOf(map.get("cycle")))).setCycleUnit(String.valueOf(map.get("cycleUnit"))).setArrangeWay(String.valueOf(map.get("arrangeWay"))).setAuditStatus(String.valueOf(map.get("auditStatus"))).setCalendar(String.valueOf(map.get("calendar"))).setEffectiveTime(new BigDecimal(String.valueOf(map.get("effectiveTime")))).setEnterpriseId(String.valueOf(map.get("enterpriseId"))).setUnit(String.valueOf(map.get("unit"))).setStartCondition(String.valueOf(map.get("startCondition"))).setLeadTime(new BigDecimal(String.valueOf(map.get("leadTime")))).setVersion(String.valueOf(map.get("version"))).setFirstInspectionTime(DateUtils.str2Date(String.valueOf(map.get("firstInspectionTime")), DateUtils.datetimeFormat.get())).setDelFlag(0);
        inspectionCycleService.save(inspectionCycle);
        QueryWrapper<InspectionCycle> queryWrapper = new QueryWrapper<InspectionCycle>().eq("code", inspectionCycle.getCode()).eq("version", inspectionCycle.getVersion()).eq("del_flag", 0);
        InspectionCycle selectInspectionCycle = inspectionCycleService.getOne(queryWrapper, true);
        DataVersion dataVersion = new DataVersion();
        dataVersion.setBusinessId(selectInspectionCycle.getId())
                .setBusinessType("点检周期").setErterpriseId(String.valueOf(map.get("enterpriseId")))
                .setVersion(Integer.parseInt(selectInspectionCycle.getVersion()))
                .setVersionStatus(String.valueOf(map.get("versionStatus"))).setDelFlag(0).setEffectiveType("0").setIsLastUsable("0");
        sysBaseApi.saveDataVersion(dataVersion);
       /* QueryWrapper<DataVersion> queryWrapper1 = new QueryWrapper<DataVersion>()
                .eq("business_id",selectInspectionCycle.getId()).eq("del_flag",0);
        DataVersion selectDataVersion = dataVersionService.getOne(queryWrapper1,true);
        selectInspectionCycle.setDataVersionId(selectDataVersion.getId());
        inspectionCycleService.saveOrUpdate(selectInspectionCycle);*/
        return Result.OK("添加成功!");
    }
 
    /**
     * 添加
     */
    @PostMapping(value = "/addNew")
    public Result<String> addNew(@RequestBody InspectionCycle inspectionCycle) {
        String cycleUnit = sysBaseApi.queryDictTextByKey("cycle_unit", String.valueOf(inspectionCycle.getCycleUnit()));
        inspectionCycle.setName(String.valueOf(inspectionCycle.getCycle()).concat(cycleUnit));
//        String num = "DJ" + DateUtils.date2Str(DateUtils.yyyyMMdd.get()) + inspectionCycleMapper.getInspectionCycleNum();
        String num = sysIdentityService.getNumByTypeAndLength("InspectionCycle", 4);
        inspectionCycle.setCode(num);
        inspectionCycleService.save(inspectionCycle);
        return Result.OK("添加成功!");
    }
 
    /**
     * 编辑
     *
     * @param inspectionCycle
     * @return
     */
    @ApiOperation(value = "点检周期规则-编辑", notes = "点检周期规则-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody InspectionCycle inspectionCycle) {
        inspectionCycleService.updateById(inspectionCycle);
        return Result.OK("编辑成功!");
    }
 
    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "点检周期规则-通过id删除", notes = "点检周期规则-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
//        dataVersionService.update(new UpdateWrapper<DataVersion>().set("del_flag", 1).eq("business_id", id));
        sysBaseApi.removeDataVersionById(id);
//        inspectionCycleService.update(new UpdateWrapper<InspectionCycle>().set("del_flag", 1).eq("id", id));
        inspectionCycleService.removeById(id);
        return Result.OK("删除成功!");
    }
 
    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "点检周期规则-批量删除", notes = "点检周期规则-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
//        dataVersionService.update(new UpdateWrapper<DataVersion>().set("del_flag", 1).in("business_id", Arrays.asList(ids.split(","))));
//        inspectionCycleService.update(new UpdateWrapper<InspectionCycle>().set("del_flag", 1).in("id", Arrays.asList(ids.split(","))));
        sysBaseApi.removeDataVersionByIds(ids);
        return Result.OK("批量删除成功!");
    }
 
    /**
     * 获取初始信息
     */
    @GetMapping("/getInspectionCycleInfo")
    public Result<?> getInspectionCycleInfo() {
        Map<String, Object> infoMap = new HashMap<>(3);
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//        String enterpriseName = enterpriseService.lambdaQuery().eq(Enterprise::getId, loginUser.getEnterpriseId()).eq(Enterprise::getDelFlag, CommonConstant.DEL_FLAG_0).one().getName();
        String enterpriseName = sysBaseApi.getEnterpriseNameById(loginUser.getEnterpriseId());
        infoMap.put("enterpriseId", loginUser.getEnterpriseId());
        infoMap.put("enterpriseName", enterpriseName);
        if (CollectionUtils.isNotEmpty(inspectionCycleService.getUsableVersion())) {
            infoMap.put("version", inspectionCycleService.getUsableVersion().get(0));
//            String versionStatus = dataVersionService.lambdaQuery().eq(DataVersion::getVersion, inspectionCycleService.getUsableVersion().get(0)).eq(DataVersion::getBusinessType, "点检周期")
//                    .eq(DataVersion::getDelFlag, CommonConstant.DEL_FLAG_0).list().get(0).getVersionStatus();
            String versionStatus = sysBaseApi.getVersionStatusByVersionAndBusinessType(inspectionCycleService.getUsableVersion().get(0), "点检周期");
            infoMap.put("versionStatus", versionStatus);
        } else {
            infoMap.put("version", 1);
            infoMap.put("versionStatus", "0");
        }
        return Result.OK(infoMap);
    }
 
    /**
     * 获取所有版本号
     */
    @GetMapping("/getVersionList")
    public Result<?> getAllVersion() {
        return Result.OK(inspectionCycleService.getVersionList());
    }
 
    /**
     * 根据版本号获取版本状态
     */
    @PostMapping("/getVersionStatus")
    public Result<?> getVersionStatus(@RequestBody Map<String, Object> map) {
        Integer version = Integer.parseInt((String) map.get("version"));
//        String versionStatus = dataVersionService.lambdaQuery().eq(DataVersion::getVersion, version).eq(DataVersion::getBusinessType, "点检周期")
//                .eq(DataVersion::getDelFlag, CommonConstant.DEL_FLAG_0).list().get(0).getVersionStatus();
        String versionStatus = sysBaseApi.getVersionStatusByVersionAndBusinessType(version, "点检周期");
        return Result.OK(versionStatus);
    }
 
    /**
     * 升版
     *
     * @param map
     * @return
     */
    @PostMapping("/updateVersion")
    @Transactional
    public Result<?> updateVersion(@RequestBody Map<String, Object> map) {
        Integer version = Integer.parseInt((String) map.get("version"));
        String enterpriseId = String.valueOf(map.get("enterpriseId"));
        List<InspectionCycle> inspectionCycleList = inspectionCycleService.lambdaQuery().eq(InspectionCycle::getEnterpriseId, enterpriseId).eq(InspectionCycle::getVersion, version).eq(InspectionCycle::getDelFlag, CommonConstant.DEL_FLAG_0).list();
        Integer newVersion = inspectionCycleService.getVersionList().stream().findFirst().get() + 1;
        for (InspectionCycle inspectionCycle : inspectionCycleList) {
//            DataVersion oldDataVersion = dataVersionService.lambdaQuery().eq(DataVersion::getBusinessId, inspectionCycle.getId())
//                    .eq(DataVersion::getDelFlag, CommonConstant.DEL_FLAG_0).one();
            DataVersion oldDataVersion = sysBaseApi.getDataVersionByBusinessId(inspectionCycle.getId());
            inspectionCycle.setId(null);
            inspectionCycle.setVersion(newVersion.toString());
            inspectionCycleService.save(inspectionCycle);
            DataVersion dataVersion = new DataVersion();
            dataVersion.setBusinessId(inspectionCycle.getId()).setBusinessType("点检周期").setVersion(newVersion).setErterpriseId(enterpriseId).setVersionStatus("1").setDelFlag(0).setEffectiveType("0").setSourceVersionId(oldDataVersion.getId()).setIsLastUsable("0");
//            dataVersionService.save(dataVersion);
            sysBaseApi.saveDataVersion(dataVersion);
        }
        return Result.OK("升版成功", newVersion);
    }
 
    /**
     * 立即生效
     *
     * @param map
     * @param
     * @return
     */
    @PostMapping("/updateVersionStatusToUsable")
    @Transactional
    public Result<?> updateVersionStatusToUsable(@RequestBody Map<String, Object> map) {
        //如果有定时生生效,则将定时生效任务关闭
//        QuartzJob quartzJob = quartzJobService.getOne(new QueryWrapper<QuartzJob>().eq("job_class_name", "org.jeecg.modules.quartz.job.InspectionCycleSetUsableJob"), true);
//        if (ObjectUtils.isNotNull(quartzJob)) {
//            quartzJobService.deleteAndStopJob(quartzJob);
//        }
        sysBaseApi.closeJobByClassName("org.jeecg.modules.quartz.job.InspectionCycleSetUsableJob");
        //获取版本号
        Integer version = Integer.parseInt((String) map.get("version"));
        String enterpriseId = String.valueOf(map.get("enterpriseId"));
        //将上次生效的是否上次生效置为否
//        List<DataVersion> lastDataVersions = dataVersionService.lambdaQuery().eq(DataVersion::getBusinessType, "点检周期").eq(DataVersion::getErterpriseId, enterpriseId).eq(DataVersion::getIsLastUsable, "1").list();
        List<DataVersion> lastDataVersions = sysBaseApi.getLastDataVersion("点检周期", enterpriseId, "1");
        for (DataVersion dataVersion : lastDataVersions) {
            dataVersion.setIsLastUsable("0");
        }
//        dataVersionService.updateBatchById(lastDataVersions);
        sysBaseApi.updateBatchDataVersion(lastDataVersions);
        //将状态为生效的置为失效
//        List<DataVersion> dataVersions = dataVersionService.lambdaQuery().eq(DataVersion::getBusinessType, "点检周期").eq(DataVersion::getErterpriseId, enterpriseId).eq(DataVersion::getVersionStatus, "2").list();
        List<DataVersion> dataVersions = sysBaseApi.getDataVersionList("点检周期",enterpriseId,"2",null);
        for (DataVersion dataVersion : dataVersions) {
            dataVersion.setVersionStatus("3");
            dataVersion.setIsLastUsable("1");
            dataVersion.setExpiredTime(new Date(System.currentTimeMillis()));
        }
//        dataVersionService.updateBatchById(dataVersions);
        sysBaseApi.updateBatchDataVersion(dataVersions);
        //获取待生效版本及版本信息并将其置为生效
//        List<DataVersion> dataVersionList = dataVersionService.lambdaQuery().eq(DataVersion::getBusinessType, "点检周期").eq(DataVersion::getVersion, version).eq(DataVersion::getErterpriseId, enterpriseId).list();
        List<DataVersion> dataVersionList = sysBaseApi.getDataVersionList("点检周期",enterpriseId,null,version.toString());
        for (DataVersion dataVersion : dataVersionList) {
            dataVersion.setVersionStatus("2");
            dataVersion.setEffectiveType("2");
            dataVersion.setEffectiveTime(new Date(System.currentTimeMillis()));
            dataVersion.setIsLastUsable("0");
        }
//        dataVersionService.updateBatchById(dataVersionList);
        sysBaseApi.updateBatchDataVersion(dataVersionList);
        return Result.OK("生效成功");
    }
//
//    /**
//     * 定时生效
//     *
//     * @param quartzJob
//     * @param
//     * @return
//     */
//    @PostMapping("/updateVersionStatusToUsableBySetTime")
//    @Transactional
//    public Result<?> updateVersionStatusToUsableBySetTime(@RequestBody QuartzJob quartzJob) throws ParseException {
//        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("ss mm HH dd MM ? yyyy");
//        String jobClassName = quartzJob.getJobClassName();
//        String time = quartzJob.getCronExpression();
//        System.out.println(time);
//        LocalDateTime localDateTime = LocalDateTime.parse(time, dateTimeFormatter);
//        String cronStr = dateTimeFormatter1.format(localDateTime);
//        quartzJob.setCronExpression(cronStr);
//        QueryWrapper<QuartzJob> queryWrapper = new QueryWrapper();
//        queryWrapper.eq("JOB_CLASS_NAME", jobClassName);
//        List<QuartzJob> quartzJobs = quartzJobService.list(queryWrapper);
//        if (CollectionUtils.isEmpty(quartzJobs)) {
//            quartzJobService.saveAndScheduleJob(quartzJob);
//            quartzJobs = quartzJobService.list(queryWrapper);
//            quartzJob.setId(quartzJobs.get(0).getId());
//            quartzJobService.resumeJob(quartzJob);
//            return Result.OK(quartzJob);
//        } else {
//            quartzJob.setId(quartzJobs.get(0).getId());
//            quartzJobService.resumeJob(quartzJob);
//            return Result.OK(quartzJob);
//        }
//    }
 
    /**
     * 版本升级
     * qsw 2023-7-26
     */
    @PostMapping(value = "/revise")
    public Result<String> revise(@RequestBody InspectionCycle inspectionCycle) {
        String cycleUnit = sysBaseApi.queryDictTextByKey("cycle_unit", String.valueOf(inspectionCycle.getCycleUnit()));
        inspectionCycle.setId("");
        inspectionCycle.setName(String.valueOf(inspectionCycle.getCycle()).concat(cycleUnit));
        inspectionCycle.setCreateTime(new Date());
        inspectionCycle.setUpdateBy(null);
        inspectionCycle.setUpdateTime(null);
        inspectionCycle.setLoseEfficacyTime(null);
        inspectionCycle.setTakeEffectTime(null);
        boolean b = inspectionCycleService.save(inspectionCycle);
        if(b){
            return Result.OK("版本升级成功!");
        }else{
            return Result.error("版本升级失败!");
        }
 
    }
 
    /**
     * 升版
     * qsw 2023-7-26
     */
    @RequestMapping("/getReviseVersion")
    public Result<?> getReviseVersion(@RequestBody InspectionCycle inspectionCycle) {
        List<InspectionCycle> inspectionCycles = inspectionCycleService.lambdaQuery()
                .eq(InspectionCycle::getCode, inspectionCycle.getCode())
                .orderByDesc(InspectionCycle::getVersion).list();
        String version = inspectionCycles.get(0).getVersion();
        BigDecimal versionB = new BigDecimal(version);
        BigDecimal versionCode = versionB.add(new BigDecimal(1));
        return Result.ok(versionCode.toString());
    }
 
    /**
     * 版本生效
     * qsw 2023-7-26
     */
    @RequestMapping(value = "/versionTakeEffect", method = {RequestMethod.PUT,RequestMethod.POST})
    @Transactional(rollbackFor = { Exception.class })
    public Result<String> versionTakeEffect(@RequestBody InspectionCycle inspectionCycle) {
        List<InspectionCycle> list = inspectionCycleService.lambdaQuery().eq(InspectionCycle::getCode, inspectionCycle.getCode()).eq(InspectionCycle::getVersionStatus, "2").list();
        for (InspectionCycle cycle : list) {
            cycle.setVersionStatus("3");
            cycle.setLoseEfficacyTime(new Date());
            inspectionCycleService.updateById(cycle);
        }
        inspectionCycle.setTakeEffectTime(new Date());
        boolean b = inspectionCycleService.updateById(inspectionCycle);
        if (b){
            return Result.OK("编辑成功!");
        }else{
            return Result.error("编辑失败!");
        }
    }
}