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 { @Autowired private IOiloutEventService oiloutEventService; /** * 分页列表查询 */ @GetMapping("/list") @Operation(summary = "告警分页查询") @DictMethod public Result> queryPageList(EventReqVo eventReqVo, @RequestParam(defaultValue = "1") Integer pageNo, @RequestParam(defaultValue = "10") Integer pageSize, HttpServletRequest req) { LambdaQueryWrapper wrapper = getEventQueryWrapper(eventReqVo); Page page = new Page<>(pageNo, pageSize); IPage pageList = oiloutEventService.page(page, wrapper); return Result.ok(pageList); } public static LambdaQueryWrapper getEventQueryWrapper(EventReqVo eventReqVo) { OiloutEvent event = new OiloutEvent(); BeanUtils.copyProperties(eventReqVo, event); LambdaQueryWrapper 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> listByRecord(@RequestParam Long recordId) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(OiloutEvent::getRecordId, recordId); wrapper.orderByAsc(OiloutEvent::getEventPhrase).orderByAsc(OiloutEvent::getAlgorithmCode); List 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 eventList = oiloutEventService.oilOutStatis(param); Map 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 exportZip(EventReqVo eventReqVo) throws IOException { LoginUser user = SystemContextUtil.currentLoginUser(); String fileId = SystemConstant.OILOUT_EVENT_PREFFIX + user.getUsername(); Map 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 wrapper = getEventQueryWrapper(eventReqVo); List eventList = oiloutEventService.list(wrapper); oiloutEventService.exportZip(eventList, fileId); return Result.ok(0); } }