zxl
2026-03-25 0b39edb68acc67ed01fbfe5d31bfa776a1b17de1
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
package com.tievd.jyz.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tievd.cube.commons.annotations.AutoLog;
import com.tievd.cube.commons.annotations.DictMethod;
import com.tievd.cube.commons.base.CubeController;
import com.tievd.cube.commons.base.Result;
import com.tievd.cube.commons.utils.SystemContextUtil;
import com.tievd.cube.modules.system.model.LoginUser;
import com.tievd.jyz.cache.AlgorithmCache;
import com.tievd.jyz.constants.SystemConstant;
import com.tievd.jyz.entity.OiloutEvent;
import com.tievd.jyz.entity.SysAlgorithmItem;
import com.tievd.jyz.entity.vo.EventReqVo;
import com.tievd.jyz.service.IOiloutEventService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
 
/**
 * OiloutEvent
 *
 * @author cube
 * @version V2.0.0
 * @since 2023-02-27
 */
@Slf4j
@RestController
@RequestMapping("/jyz/oiloutEvent")
@Tag(name = "卸油记录详情、卸油规范、告警列表")
public class OiloutEventController extends CubeController<OiloutEvent, IOiloutEventService> {
    
    @Autowired
    private IOiloutEventService oiloutEventService;
    
    /**
     * 分页列表查询
     */
    @GetMapping("/list")
    @Operation(summary = "告警分页查询")
    @DictMethod
    public Result<IPage<OiloutEvent>> queryPageList(EventReqVo eventReqVo,
                                   @RequestParam(defaultValue = "1") Integer pageNo,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        LambdaQueryWrapper<OiloutEvent> wrapper = getEventQueryWrapper(eventReqVo);
        Page<OiloutEvent> page = new Page<>(pageNo, pageSize);
        IPage<OiloutEvent> pageList = oiloutEventService.page(page, wrapper);
        return Result.ok(pageList);
    }
    
    public static LambdaQueryWrapper getEventQueryWrapper(EventReqVo eventReqVo) {
        OiloutEvent event = new OiloutEvent();
        BeanUtils.copyProperties(eventReqVo, event);
        LambdaQueryWrapper<OiloutEvent> wrapper = new LambdaQueryWrapper(event);
        if (eventReqVo.getAudited()!=null) {
            if (eventReqVo.getAudited() == 1) {
                wrapper.ne(OiloutEvent::getAuditResult, 0);
            } else {
                wrapper.eq(OiloutEvent::getAuditResult, 0);
            }
        }
        if (eventReqVo.getStartTime() != null) {
            wrapper.ge(OiloutEvent::getEventTime, eventReqVo.getStartTime());
        }
        if (eventReqVo.getEndTime() != null) {
            wrapper.le(OiloutEvent::getEventTime, eventReqVo.getEndTime());
        }
        wrapper.eq(OiloutEvent::getEventType, SystemConstant.STANDARD_ERROR);
        wrapper.orderByDesc(OiloutEvent::getEventTime);
        return wrapper;
    }
    
    @GetMapping("/listByRecord")
    @Operation(summary = "卸油记录详情")
    public Result<List<OiloutEvent>> listByRecord(@RequestParam Long recordId) {
        LambdaQueryWrapper<OiloutEvent> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OiloutEvent::getRecordId, recordId);
        wrapper.orderByAsc(OiloutEvent::getEventPhrase).orderByAsc(OiloutEvent::getAlgorithmCode);
        List<OiloutEvent> eventList = oiloutEventService.list(wrapper);
        eventList.stream().forEach(e -> {
            String algorithmName = AlgorithmCache.algorithmMap().getOrDefault(e.getAlgorithmCode(), new SysAlgorithmItem()).getAlgorithmName();
            e.setAlgorithmName(algorithmName);
        });
        try {
            eventList.sort(Comparator.comparingInt(o -> AlgorithmCache.algorithmMap().get(o.getAlgorithmCode()).getSort()));
        } catch (Exception e) {
            log.error("", e);
        }
        return Result.ok(eventList);
    }
    
    @GetMapping("/oilOutStatis")
    @Operation(summary = "卸油规范统计")
    @Parameter(name = "orgCode", in = ParameterIn.QUERY, description = "机构代码")
    @Parameter(name = "startTime", in = ParameterIn.QUERY, description = "开始时间")
    @Parameter(name = "endTime", in = ParameterIn.QUERY, description = "结束时间")
    @ApiResponse(description = "{'phrase3': [{'phrase': 3,'eventCount': '异常次数','algorithmName':'算法名称','percent': 100},]}")
    public Result<?> oilOutStatis(@RequestParam Map param) {
        List<Map> eventList = oiloutEventService.oilOutStatis(param);
        Map<Object, List> result = new HashMap();
        for (Map m : eventList) {
            Object key = "phrase" + m.get("phrase");
            result.putIfAbsent(key, new ArrayList<>());
            result.get(key).add(m);
        }
        
        return Result.ok(result);
    }
    
    /**
     * 添加
     */
    @AutoLog("OiloutEvent-添加")
    @PostMapping("/add")
    public Result<?> add(@RequestBody OiloutEvent oiloutEvent) {
        oiloutEventService.save(oiloutEvent);
        return Result.ok();
    }
    
    /**
     * 编辑
     */
    @AutoLog("OiloutEvent-编辑")
    @PutMapping("/edit")
    public Result<?> edit(@RequestBody OiloutEvent oiloutEvent) {
        oiloutEventService.updateById(oiloutEvent);
        return Result.ok();
    }
    
    @AutoLog("OilEvent-编辑")
    @PostMapping("/audit")
    @Operation(summary = "事件审核")
    public Result<?> audit(@RequestBody OiloutEvent oiloutEvent) {
        LoginUser sysUser = SystemContextUtil.currentLoginUser();
        oiloutEvent.setAuditUser(sysUser.getUsername());
        oiloutEvent.setAuditTime(new Date());
        oiloutEventService.updateById(oiloutEvent);
        return Result.ok();
    }
    /**
     * 通过id删除
     */
    @AutoLog("OiloutEvent-通过id删除")
    @DeleteMapping("/delete")
    public Result<?> delete(@RequestParam String id) {
        oiloutEventService.removeById(id);
        return Result.ok();
    }
    
    /**
     * 批量删除
     */
    @AutoLog("OiloutEvent-批量删除")
    @DeleteMapping("/deleteBatch")
    public Result<?> deleteBatch(@RequestParam String ids) {
        this.oiloutEventService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok();
    }
    
    /**
     * 通过id查询
     */
    @GetMapping("/queryById")
    public Result<?> queryById(@RequestParam String id) {
        OiloutEvent oiloutEvent = oiloutEventService.getById(id);
        return Result.ok(oiloutEvent);
    }
    
    /**
     * 导出excel
     */
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletRequest request, HttpServletResponse response, OiloutEvent oiloutEvent) throws IOException {
        super.exportXls(request, response, oiloutEvent, "OiloutEvent");
    }
    
    /**
     * 通过excel导入数据
     */
    @PostMapping("/importExcel")
    public Result<?> importExcel(HttpServletRequest request) throws Exception {
        return super.importExcel(request, OiloutEvent.class);
    }
    
    
    @GetMapping("/exportZip")
    @Operation(summary = "导出zip文件(异步)")
    @ApiResponse(description = "0导出中, 1导出成功")
    public Result<Integer> exportZip(EventReqVo eventReqVo) throws IOException {
        LoginUser user = SystemContextUtil.currentLoginUser();
        String fileId = SystemConstant.OILOUT_EVENT_PREFFIX + user.getUsername();
        Map<String, String> fileMap = oiloutEventService.getFileMap();
        if (fileMap.containsKey(fileId)) {
            String filePath = fileMap.get(fileId);
            if (StringUtils.isNotBlank(filePath)) {
                return Result.ok(1);
            } else {
                return Result.ok(0);
            }
        }
        LambdaQueryWrapper<OiloutEvent> wrapper = getEventQueryWrapper(eventReqVo);
        List eventList = oiloutEventService.list(wrapper);
        oiloutEventService.exportZip(eventList, fileId);
        return Result.ok(0);
    }
    
}