kongdeqiang
2023-05-11 d4e6ec0389dd8abbcce4ee4f9e5cdad1633491d5
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
package com.wgcloud.controller;
 
import com.github.pagehelper.PageInfo;
import com.wgcloud.entity.InspectionTask;
import com.wgcloud.entity.TaskInfo;
import com.wgcloud.service.InspectionTaskService;
import com.wgcloud.service.LogInfoService;
import com.wgcloud.service.TaskInfoService;
import com.wgcloud.service.WorkLoggingService;
import com.wgcloud.util.DateUtil;
import com.wgcloud.util.PageUtil;
import com.wgcloud.util.staticvar.StaticKeys;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.*;
 
/**
 * @author kdq
 * @version 1.0.0
 * @ClassName InspectionTaskController.java
 * @Description TODO
 * @createTime 2023年02月16日 14:22:00
 */
@Controller
@RequestMapping("/inspectionTask")
public class InspectionTaskController {
 
    private static final Logger logger = LoggerFactory.getLogger(InspectionTaskController.class);
 
    @Resource
    private InspectionTaskService inspectionTaskService;
    @Resource
    private LogInfoService logInfoService;
    @Resource
    private WorkLoggingService workLoggingService;
    @Resource
    private TaskInfoService taskInfoService;
    /**
     * 根据条件查询设备列表
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "list")
    public String failureList(InspectionTask inspectionTask, Model model, HttpServletRequest request) {
        Map<String, Object> params = new HashMap<String, Object>();
        try {
            StringBuffer url = new StringBuffer();
//            if (!StringUtils.isEmpty(InspectionTask.getOldDeviceName())) {
//                url.append("&oldDeviceName=").append(InspectionTask.getOldDeviceName());
//            }
            PageInfo pageInfo = inspectionTaskService.selectByParams(params, inspectionTask.getPage(), inspectionTask.getPageSize());
            PageUtil.initPageNumber(pageInfo, model);
 
 
            model.addAttribute("pageUrl", "/inspectionTask/list?1=1" + url.toString());
            model.addAttribute("page", pageInfo);
            model.addAttribute("inspectionTask", inspectionTask);
        }catch (Exception e){
            logger.error("查询设备更换列表错误", e);
            logInfoService.save("查询资产信息错误", e.toString(), StaticKeys.LOG_XTCZ);
        }
        return "instask/tasklist";
    }
 
    @RequestMapping(value = "save")
    public String saveInspectionTask(InspectionTask inspectionTask, Model model, HttpServletRequest request) {
        try {
            if (StringUtils.isEmpty(inspectionTask.getId())) {
                inspectionTaskService.save(inspectionTask);
                inspectionTaskService.saveLog(request, StaticKeys.LOG_ADD, inspectionTask);
                workLoggingService.save(inspectionTask);
                long l = subDaysByDate2(inspectionTask.getStartDate(), inspectionTask.getEndDate());
                if(l>0){
                    for (long i = 0; i < l; i++) {
                        TaskInfo taskInfo = new TaskInfo();
                        taskInfo.setTaskId(inspectionTask.getId());
                        if(i == 0){
                            taskInfo.setTaskDate(inspectionTask.getStartDate());
                        }else {
                            String s = daysAgoOrAfter(inspectionTask.getStartDate(), new Long(i).intValue());
                            taskInfo.setTaskDate(s);
                        }
                        taskInfo.setIsOk(0);
                        taskInfoService.save(taskInfo);
                    }
                }
            } else {
                inspectionTaskService.updateById(inspectionTask);
                inspectionTaskService.saveLog(request, StaticKeys.LOG_UPDATE, inspectionTask);
                workLoggingService.update(inspectionTask);
            }
        } catch (Exception e) {
            logger.error("保存设备更换数据错误", e);
            logInfoService.save("保存设备更换数据错误", e.toString(), StaticKeys.LOG_XTCZ);
        }
        return "redirect:/inspectionTask/list";
    }
 
    @RequestMapping(value = "save2")
    public String save2(TaskInfo taskInfo) {
        try {
            taskInfoService.updateById(taskInfo);
        } catch (Exception e) {
            logger.error("修改任务详情错误", e);
            logInfoService.save("修改任务详情错误", e.toString(), StaticKeys.LOG_XTCZ);
        }
        return "redirect:/inspectionTask/detail?id="+taskInfo.getTaskId();
    }
 
    /**
     * 添加
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "edit")
    public String edit(Model model, HttpServletRequest request) {
        String errorMsg = "添加设备";
        String id = request.getParameter("id");
        InspectionTask inspectionTask = new InspectionTask();
        try {
            if (StringUtils.isEmpty(id)) {
                model.addAttribute("inspectionTask", inspectionTask);
                return "instask/add";
            }
            inspectionTask = inspectionTaskService.selectById(id);
            model.addAttribute("inspectionTask", inspectionTask);
        } catch (Exception e) {
            logger.error(errorMsg, e);
            logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
        }
        return "instask/add";
    }
 
    /**
     * 添加
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "edit2")
    public String edit2(Model model, HttpServletRequest request) {
        String errorMsg = "修改任务详情";
        String id = request.getParameter("id");
        TaskInfo taskInfo = new TaskInfo();
        try {
            if (StringUtils.isEmpty(id)) {
                model.addAttribute("taskInfo", taskInfo);
                return "instask/infoedit";
            }
            taskInfo = taskInfoService.selectById(id);
            model.addAttribute("taskInfo", taskInfo);
        } catch (Exception e) {
            logger.error(errorMsg, e);
            logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
        }
        return "instask/infoedit";
    }
 
    /**
     * 查看该应用统计图
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "view")
    public String viewChart(Model model, HttpServletRequest request) {
        String errorMsg = "查看设备更换错误";
        String id = request.getParameter("id");
        InspectionTask inspectionTask = new InspectionTask();
        try {
            inspectionTask = inspectionTaskService.selectById(id);
            model.addAttribute("inspectionTask", inspectionTask);
        } catch (Exception e) {
            logger.error(errorMsg, e);
            logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
        }
        return "instask/view";
    }
 
 
    /**
     * 删除设备
     *
     * @param
     * @param model
     * @param request
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "del")
    public String delete(Model model, HttpServletRequest request, RedirectAttributes redirectAttributes) {
        String errorMsg = "删除更换设备错误";
        InspectionTask inspectionTask = new InspectionTask();
        try {
            if (!StringUtils.isEmpty(request.getParameter("id"))) {
                String[] ids = request.getParameter("id").split(",");
                for (String id : ids) {
                    inspectionTask = inspectionTaskService.selectById(id);
                    inspectionTaskService.saveLog(request, StaticKeys.LOG_DEL, inspectionTask);
                }
                inspectionTaskService.deleteById(ids);
            }
        } catch (Exception e) {
            logger.error(errorMsg, e);
            logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
        }
        return "redirect:/inspectionTask/list";
    }
 
    /**
     * 查看任务详情
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "detail")
    public String detail(Model model, HttpServletRequest request) {
 
        try {
            String id = request.getParameter("id");
            List<TaskInfo> allByTaskId = taskInfoService.getAllByTaskId(id);
            model.addAttribute("taskInfos", allByTaskId);
        }catch (Exception e){
            logger.error("查询任务详情错误", e);
            logInfoService.save("查询任务详情错误", e.toString(), StaticKeys.LOG_XTCZ);
        }
        return "instask/taskinfo";
    }
 
    /**
     * 获取两个日期相差的天数  方法二
     *
     * @param startDate 开始日期的字符串
     * @param endDate   结束日期的字符串
     * @return 相差天数,如果解析失败则返回-1
     **/
    private long subDaysByDate2(String startDate, String endDate) {
        long sub;
        try {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate start = LocalDate.parse(startDate, dateTimeFormatter);
            LocalDate end = LocalDate.parse(endDate, dateTimeFormatter);
 
            sub = end.toEpochDay() - start.toEpochDay();
        } catch (DateTimeParseException e) {
            sub = -1;
        }
        return sub;
    }
 
    private String daysAgoOrAfter(String startDate,int amount){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d = new Date();
        try {
            d= sdf.parse(startDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar mon = Calendar.getInstance();
        mon.setTime(d);
        mon.add(Calendar.DATE,amount);
        Date time = mon.getTime();
        String format = sdf.format(time);
        return format;
    }
 
 
 
}