From 9364cf6824389a3264dc2cdb2bc3b5857ce76ad5 Mon Sep 17 00:00:00 2001
From: xuhy <3313886187@qq.com>
Date: 星期四, 03 七月 2025 23:38:54 +0800
Subject: [PATCH] bug修改

---
 ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TTemplateController.java |  239 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 237 insertions(+), 2 deletions(-)

diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TTemplateController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TTemplateController.java
index 5465308..a2ff1c3 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TTemplateController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TTemplateController.java
@@ -1,9 +1,34 @@
 package com.ruoyi.web.controller.api;
 
 
+import com.alibaba.fastjson2.JSONArray;
+import com.alibaba.fastjson2.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.google.common.collect.ImmutableMap;
+import com.ruoyi.common.annotation.Log;
+import com.ruoyi.common.basic.PageInfo;
+import com.ruoyi.common.core.domain.R;
+import com.ruoyi.common.core.domain.entity.SysRole;
+import com.ruoyi.common.enums.BusinessType;
+import com.ruoyi.common.utils.bean.BeanUtils;
+import com.ruoyi.system.dto.TTemplateDTO;
+import com.ruoyi.system.model.TLocationType;
+import com.ruoyi.system.model.TTemplate;
+import com.ruoyi.system.model.TTemplateDetail;
+import com.ruoyi.system.query.TemplateListQuery;
+import com.ruoyi.system.service.*;
+import com.ruoyi.system.task.base.QuartzManager;
+import com.ruoyi.system.task.base.TimeJobType;
+import com.ruoyi.system.task.jobs.CreateInspectionJob;
+import com.ruoyi.system.vo.system.TemplateDetailVO;
+import com.ruoyi.system.vo.system.TemplateListVO;
 import io.swagger.annotations.Api;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.web.bind.annotation.*;
+
+import javax.annotation.Resource;
+import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * <p>
@@ -17,6 +42,216 @@
 @RestController
 @RequestMapping("/t-template")
 public class TTemplateController {
+    @Resource
+    private TTemplateService templateService;
+    @Resource
+    private TTemplateDetailService templateDetailService;
+    @Resource
+    private TLocationTypeService tLocationTypeService;
+    @Resource
+    private ISysRoleService sysRoleService;
 
+
+
+
+    @ApiOperation(value = "任务模板分页列表")
+    @PostMapping(value = "/pageList")
+    public R<PageInfo<TemplateListVO>> pageList(@RequestBody TemplateListQuery query) {
+        return R.ok(templateService.pageList(query));
+    }
+    @ApiOperation(value = "任务模板不分页列表")
+    @PostMapping(value = "/listAllTemplate")
+    public R<List<TTemplate>> listAllTemplate() {
+        return R.ok(templateService.list());
+    }
+    @ApiOperation(value = "点位类型不分页列表")
+    @PostMapping(value = "/listAll")
+    public R<List<TLocationType>> pageList() {
+        return R.ok(tLocationTypeService.list());
+    }
+    @Log(title = "新增任务模板", businessType = BusinessType.INSERT)
+    @ApiOperation(value = "新增任务模板")
+    @PostMapping(value = "/add")
+    public R<Boolean> add(@RequestBody TTemplateDTO dto) {
+        templateService.save(dto);
+        List<TTemplateDetail> list = dto.getList();
+        for (TTemplateDetail tTemplateDetail : list) {
+            tTemplateDetail.setTemplateId(dto.getId());
+        }
+        templateDetailService.saveBatch(list);
+
+        // 计算定时任务时间周期
+
+        // 添加定时任务
+//        Map<String, ? extends Object> maps =
+//                new ImmutableMap.Builder<String, String>().
+//                        put("id", dto.getId())
+//                        .build();
+//        QuartzManager.addJob(
+//                CreateInspectionJob.class,
+//                (CreateInspectionJob.name+dto.getId()).toUpperCase(),
+//                TimeJobType.CREATE_INSPECTION,
+//                new Date(new Date().getTime()+48*60*60*1000L),
+//                maps
+//        );
+
+        return R.ok();
+    }
+    @Log(title = "编辑任务模板", businessType = BusinessType.UPDATE)
+    @ApiOperation(value = "编辑任务模板")
+    @PostMapping(value = "/edit")
+    public R<Boolean> edit(@RequestBody TTemplateDTO dto) {
+        templateService.updateById(dto);
+        templateDetailService.remove(new LambdaQueryWrapper<TTemplateDetail>()
+                .eq(TTemplateDetail::getTemplateId,  dto.getId()));
+        for (TTemplateDetail tTemplateDetail : dto.getList()) {
+            tTemplateDetail.setTemplateId(dto.getId());
+            tTemplateDetail.setId(null);
+        }
+        templateDetailService.saveBatch(dto.getList());
+
+        // 先删除定时任务
+//        QuartzManager.removeJob((CreateInspectionJob.name+dto.getId()).toUpperCase(),TimeJobType.CREATE_INSPECTION);
+
+        // 添加定时任务
+//        Map<String, ? extends Object> maps =
+//                new ImmutableMap.Builder<String, String>().
+//                        put("id", dto.getId())
+//                        .build();
+//        QuartzManager.addJob(
+//                CreateInspectionJob.class,
+//                (CreateInspectionJob.name+dto.getId()).toUpperCase(),
+//                TimeJobType.CREATE_INSPECTION,
+//                new Date(new Date().getTime()+48*60*60*1000L),
+//                maps
+//        );
+
+        return R.ok();
+    }
+    @Log(title = "批量删除任务模板", businessType = BusinessType.DELETE)
+    @ApiOperation(value = "批量删除任务模板")
+    @DeleteMapping(value = "/deleteByIds")
+    public R<Boolean> deleteByIds(@RequestParam String ids) {
+        String[] split = ids.split(",");
+        templateService.removeBatchByIds(Arrays.asList(split));
+        return R.ok();
+    }
+    @ApiOperation(value = "根据模板id查询角色")
+    @DeleteMapping(value = "/getRoleByTemplateId")
+    public R<SysRole> getRoleByTemplateId(@RequestParam String id) {
+        TTemplate template = templateService.getById(id);
+        SysRole sysRole = sysRoleService.selectRoleById(template.getRoleId());
+        return R.ok(sysRole);
+    }
+    @ApiOperation(value = "详情任务模板")
+    @GetMapping(value = "/detail")
+    public R<TemplateDetailVO> detail(@RequestParam String id) {
+        TemplateDetailVO templateDetailVO = new TemplateDetailVO();
+        TTemplate byId = templateService.getById(id);
+        List<TTemplateDetail> list = templateDetailService.lambdaQuery().eq(TTemplateDetail::getTemplateId, id)
+                .list();
+        BeanUtils.copyProperties(byId,templateDetailVO);
+        List<String> collect = tLocationTypeService.list().stream().map(TLocationType::getId).collect(Collectors.toList());
+        JSONArray res = new JSONArray();
+        for (String s : collect) {
+            JSONObject jsonObject = new JSONObject();
+            jsonObject.put("id",s);
+            jsonObject.put("value","");
+            res.add(jsonObject);
+        }
+        for (TTemplateDetail tTemplateDetail : list) {
+            JSONArray temp = new JSONArray();
+            String num4 = tTemplateDetail.getNum4();
+            JSONArray num5Array = JSONArray.parseArray(num4);
+            for (Object re : res) {
+                JSONObject jsonObject2 = (JSONObject) re;
+                int i = 0;
+                for (Object o : num5Array) {
+                    JSONObject jsonObject1 = (JSONObject) o;
+                    String string = jsonObject1.getString("id");
+                    // 如果res中有这个id,则设置num
+                    if (jsonObject2.getString("id").equals(string)) {
+                        JSONObject jsonObject = new JSONObject();
+                        jsonObject.put("id",string);
+                        jsonObject.put("value", jsonObject1.getString("value"));
+                        temp.add(jsonObject);
+                        break;
+                    }
+                    i++;
+                    if (i==num5Array.size()){
+                        JSONObject jsonObject = new JSONObject();
+                        jsonObject.put("id",string);
+                        jsonObject.put("value", "");
+                        temp.add(jsonObject);
+                    }
+                }
+
+            }
+
+            tTemplateDetail.setNum4(temp.toJSONString());
+        }
+        if (byId.getRoleId()!=null){
+            SysRole sysRole = sysRoleService.selectRoleById(byId.getRoleId());
+            templateDetailVO.setRoleName(sysRole.getRoleName());
+        }
+        templateDetailVO.setList(list);
+        return R.ok(templateDetailVO);
+    }
+
+    public static void main(String[] args) {
+        List<String> collect = new ArrayList<>();
+        collect.add("987");
+        collect.add("1234");
+        collect.add("4321");
+        collect.add("9654");
+        collect.add("4567");
+        collect.add("288");
+        List<TTemplateDetail> list = new ArrayList<>();
+        TTemplateDetail tTemplateDetail = new TTemplateDetail();
+        tTemplateDetail.setId("6666");
+        tTemplateDetail.setNum4("{\"num5\":[{\"id\":\"1234\",\"value\":\"18\"},{\"id\":\"4321\",\"value\":\"18\"}]}");
+        TTemplateDetail tTemplateDetail1 = new TTemplateDetail();
+        tTemplateDetail1.setNum4("{\"num5\":[{\"id\":\"4567\",\"value\":\"18\"},{\"id\":\"7654\",\"value\":\"18\"}]}");
+        tTemplateDetail1.setId("1111");
+        list.add(tTemplateDetail);
+        list.add(tTemplateDetail1);
+        for (TTemplateDetail templateDetail : list) {
+            JSONArray res = new JSONArray();
+            for (String s : collect) {
+                JSONObject jsonObject = new JSONObject();
+                jsonObject.put("id",s);
+                jsonObject.put("value","");
+                res.add(jsonObject);
+            }
+            String num4 = templateDetail.getNum4();
+            JSONObject jsonObject = JSONObject.parseObject(num4);
+            JSONArray num5Array = jsonObject.getJSONArray("num5");
+            for (Object o : num5Array) {
+                JSONObject jsonObject1 = (JSONObject) o;
+                String string = jsonObject1.getString("id");
+                // 如果res中有这个id,则设置num
+                for (int i = 0; i < res.size(); i++) {
+                    JSONObject jsonObject2 = (JSONObject) res.get(i);
+                    if (jsonObject2.getString("id").equals(string)) {
+                        jsonObject2.put("value",jsonObject1.getString("value"));
+                        break;
+                    }
+                }
+            }
+            templateDetail.setNum4(res.toJSONString());
+        }
+        System.err.println(list);
+//        String num4 = "{\"num5\":[{\"id\":\"18678093453\",\"num\":\"18\"},{\"id\":\"18678093453\",\"num\":\"18\"}]}";
+//        JSONObject jsonObject = JSONObject.parseObject(num4);
+//        JSONArray num5Array = jsonObject.getJSONArray("num5");
+//        System.err.println(num5Array);
+//        JSONArray res = new JSONArray();
+//        for (Object o : num5Array) {
+//            JSONObject jsonObject1 = (JSONObject) o;
+//            String string = jsonObject1.getString("id");
+//            System.err.println(string);
+//        }
+    }
 }
 
+

--
Gitblit v1.7.1