package com.dy.pipIrrProject.controller; 
 | 
  
 | 
import com.alibaba.excel.EasyExcel; 
 | 
import com.alibaba.fastjson2.JSONArray; 
 | 
import com.dy.common.aop.SsoAop; 
 | 
import com.dy.common.webUtil.BaseResponse; 
 | 
import com.dy.common.webUtil.BaseResponseUtils; 
 | 
import com.dy.common.webUtil.QueryResultVo; 
 | 
import com.dy.common.webUtil.ResultCodeMsg; 
 | 
import com.dy.pipIrrGlobal.excel.CellWriteHandler; 
 | 
import com.dy.pipIrrGlobal.excel.ExcelUtil; 
 | 
import com.dy.pipIrrGlobal.pojoBa.BaClient; 
 | 
import com.dy.pipIrrGlobal.pojoPr.PrController; 
 | 
import com.dy.pipIrrGlobal.voPr.VoController; 
 | 
import com.dy.pipIrrGlobal.voPr.VoControllerSimple; 
 | 
import com.dy.pipIrrGlobal.voSe.VoActiveCard; 
 | 
import com.dy.pipIrrProject.result.ProjectResultCode; 
 | 
import io.swagger.v3.oas.annotations.Operation; 
 | 
import io.swagger.v3.oas.annotations.media.Content; 
 | 
import io.swagger.v3.oas.annotations.media.Schema; 
 | 
import io.swagger.v3.oas.annotations.responses.ApiResponse; 
 | 
import io.swagger.v3.oas.annotations.responses.ApiResponses; 
 | 
import io.swagger.v3.oas.annotations.tags.Tag; 
 | 
import jakarta.servlet.http.HttpServletResponse; 
 | 
import jakarta.validation.Valid; 
 | 
import lombok.RequiredArgsConstructor; 
 | 
import lombok.SneakyThrows; 
 | 
import lombok.extern.slf4j.Slf4j; 
 | 
import org.springframework.http.MediaType; 
 | 
import org.springframework.transaction.annotation.Transactional; 
 | 
import org.springframework.validation.BindingResult; 
 | 
import org.springframework.web.bind.annotation.*; 
 | 
  
 | 
import java.io.IOException; 
 | 
import java.io.UnsupportedEncodingException; 
 | 
import java.net.URLEncoder; 
 | 
import java.time.LocalDate; 
 | 
import java.util.*; 
 | 
  
 | 
/** 
 | 
 * @author ZhuBaoMin 
 | 
 * @date 2023-12-29 10:06 
 | 
 * @LastEditTime 2023-12-29 10:06 
 | 
 * @Description 
 | 
 */ 
 | 
  
 | 
@Slf4j 
 | 
@Tag(name = "控制器管理", description = "控制器操作") 
 | 
@RestController 
 | 
@RequestMapping(path = "controller") 
 | 
@RequiredArgsConstructor 
 | 
public class ControllerCtrl { 
 | 
    private final ControllerSv controllerSv; 
 | 
  
 | 
    /** 
 | 
     * 根据指定条件获取控制器列表 
 | 
     * 
 | 
     * @param vo 查询条件 
 | 
     * @return 符合条件的控制器列表 
 | 
     */ 
 | 
    @Operation(summary = "获得一页控制器记录", description = "返回一页控制器数据") 
 | 
    @ApiResponses(value = { 
 | 
            @ApiResponse( 
 | 
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, 
 | 
                    description = "返回一页控制器数据(BaseResponse.content:QueryResultVo[{}])", 
 | 
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, 
 | 
                            schema = @Schema(implementation = VoActiveCard.class))} 
 | 
            ) 
 | 
    }) 
 | 
    @GetMapping(path = "/getControllers") 
 | 
    @SsoAop() 
 | 
    public BaseResponse<QueryResultVo<List<VoController>>> getControllers(QueryVo vo) { 
 | 
        try { 
 | 
            QueryResultVo<List<VoController>> res = controllerSv.getControllers(vo); 
 | 
            if (res.itemTotal != null && res.itemTotal > 0) { 
 | 
                return BaseResponseUtils.buildSuccess(res); 
 | 
            } else { 
 | 
                return BaseResponseUtils.buildSuccess(ProjectResultCode.NO_RECORDS.getMessage()); 
 | 
            } 
 | 
            //return BaseResponseUtils.buildSuccess(res); 
 | 
        } catch (Exception e) { 
 | 
            log.error("获取控制器记录异常", e); 
 | 
            return BaseResponseUtils.buildException(e.getMessage()); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 根据控制器编号获取控制器列表 
 | 
     * 
 | 
     * @param rtuAddr 控制器地址 
 | 
     * @return 符合条件的控制器列表 
 | 
     */ 
 | 
    @Operation(summary = "获得控制器记录", description = "返回取控制器数据") 
 | 
    @ApiResponses(value = { 
 | 
            @ApiResponse( 
 | 
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, 
 | 
                    description = "返回控制器数据(BaseResponse.content:QueryResultVo[{}])", 
 | 
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, 
 | 
                            schema = @Schema(implementation = BaClient.class))} 
 | 
            ) 
 | 
    }) 
 | 
    @GetMapping(path = "controller_list") 
 | 
    @SsoAop() 
 | 
    public BaseResponse<List<Map<String, Object>>> getControllersByCode(String rtuAddr) { 
 | 
        try { 
 | 
            List<Map<String, Object>> list = Optional.ofNullable(controllerSv.getControllersByAddr(rtuAddr)).orElse(new ArrayList<>()); 
 | 
            if (list.size() <= 0) { 
 | 
                return BaseResponseUtils.buildErrorMsg(ProjectResultCode.NO_RECORDS.getMessage()); 
 | 
            } 
 | 
            return BaseResponseUtils.buildSuccess(list); 
 | 
        } catch (Exception e) { 
 | 
            log.error("查询控制器异常", e); 
 | 
            return BaseResponseUtils.buildException(e.getMessage()); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 添加控制器 
 | 
     * 
 | 
     * @param po            控制器实体对象 
 | 
     * @param bindingResult 
 | 
     * @return 添加是否成功 
 | 
     */ 
 | 
    @Operation(summary = "添加控制器记录", description = "添加控制器记录") 
 | 
    @ApiResponses(value = { 
 | 
            @ApiResponse( 
 | 
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, 
 | 
                    description = "操作结果:true:成功,false:失败(BaseResponse.content)", 
 | 
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, 
 | 
                            schema = @Schema(implementation = Boolean.class))} 
 | 
            ) 
 | 
    }) 
 | 
    @PostMapping(path = "add", consumes = MediaType.APPLICATION_JSON_VALUE) 
 | 
    @Transactional(rollbackFor = Exception.class) 
 | 
    @SsoAop() 
 | 
    //public BaseResponse<Boolean> add(@RequestBody @Valid DtoController po, BindingResult bindingResult){ 
 | 
    public BaseResponse<Boolean> add(@RequestBody @Valid PrController po, BindingResult bindingResult) { 
 | 
        if (bindingResult != null && bindingResult.hasErrors()) { 
 | 
            return BaseResponseUtils.buildErrorMsg(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); 
 | 
        } 
 | 
  
 | 
        //PrController prController = DtoToPojo.INSTANCT.po2vo(po); 
 | 
        //Date operateTime = new Date(); 
 | 
        //prController.setOperatedt(operateTime); 
 | 
        //prController.setDeleted((byte)0); 
 | 
        //prController.setReporttime(operateTime); 
 | 
  
 | 
        Date operateTime = new Date(); 
 | 
        po.setOperateDt(operateTime); 
 | 
        po.setDeleted((byte) 0); 
 | 
        po.setFindDt(operateTime); 
 | 
        if (po.getAddWays() == null) { 
 | 
            po.setAddWays((byte) 1); 
 | 
        } 
 | 
        Integer rec = Optional.ofNullable(controllerSv.addController(po)).orElse(0); 
 | 
        if (rec == 0) { 
 | 
            return BaseResponseUtils.buildErrorMsg(ProjectResultCode.CONTROLLER_FAIL.getMessage()); 
 | 
        } 
 | 
        return BaseResponseUtils.buildSuccess(true); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 根据控制器编号删除控制器 
 | 
     * 
 | 
     * @param map 
 | 
     * @return 
 | 
     */ 
 | 
    @Operation(summary = "删除控制器记录", description = "删除控制器记录") 
 | 
    @ApiResponses(value = { 
 | 
            @ApiResponse( 
 | 
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, 
 | 
                    description = "操作结果:true:成功,false:失败(BaseResponse.content)", 
 | 
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, 
 | 
                            schema = @Schema(implementation = Boolean.class))} 
 | 
            ) 
 | 
    }) 
 | 
    @PostMapping(path = "delete") 
 | 
    @SsoAop() 
 | 
    public BaseResponse<Boolean> delete(@RequestBody Map map) { 
 | 
        if (map == null || map.size() <= 0) { 
 | 
            return BaseResponseUtils.buildErrorMsg(ProjectResultCode.PLEASE_INPUT_CONTROLLER_ID.getMessage()); 
 | 
        } 
 | 
  
 | 
        Long controllerId = Long.parseLong(map.get("controllerId").toString()); 
 | 
        Integer recordCount = Optional.ofNullable(controllerSv.deleteControllerById(controllerId)).orElse(0); 
 | 
        if (recordCount == 0) { 
 | 
            return BaseResponseUtils.buildErrorMsg(ProjectResultCode.DELETE_CONTROLLER_FAIL.getMessage()); 
 | 
        } 
 | 
        return BaseResponseUtils.buildSuccess(true); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 获取未绑控制器的取水口列表 
 | 
     * 
 | 
     * @return 
 | 
     */ 
 | 
    @Operation(summary = "获得未绑控制器的取水口记录", description = "返回未绑控制器的取水口数据") 
 | 
    @ApiResponses(value = { 
 | 
            @ApiResponse( 
 | 
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, 
 | 
                    description = "返回控制器数据(BaseResponse.content:QueryResultVo[{}])", 
 | 
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, 
 | 
                            schema = @Schema(implementation = BaClient.class))} 
 | 
            ) 
 | 
    }) 
 | 
    @GetMapping(path = "nobinding_intakes") 
 | 
    @SsoAop() 
 | 
    public BaseResponse<JSONArray> getNoBindingIntakes() { 
 | 
        try { 
 | 
            JSONArray array = controllerSv.getNoBindingIntakes(); 
 | 
//            if (array.size() <= 0) { 
 | 
//                return BaseResponseUtils.buildFail(ProjectResultCode.NO_BINDING_INTAKE.getMessage()); 
 | 
//            } 
 | 
            return BaseResponseUtils.buildSuccess(array); 
 | 
        } catch (Exception e) { 
 | 
            log.error("查询控制器异常", e); 
 | 
            return BaseResponseUtils.buildException(e.getMessage()); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 导出控制器列表 
 | 
     * 
 | 
     * @param response 
 | 
     * @param vo 
 | 
     */ 
 | 
    @SneakyThrows(IOException.class) 
 | 
    @RequestMapping(value = "/export", method = RequestMethod.GET) 
 | 
    public void export(HttpServletResponse response, QueryVo vo) { 
 | 
        List<VoController> controllerList = controllerSv.export(vo); 
 | 
        ExcelUtil.setExcelRespProp(response, "控制器列表" + LocalDate.now()); 
 | 
        EasyExcel.write(response.getOutputStream(), VoController.class) 
 | 
                .registerWriteHandler(new CellWriteHandler("控制器列表")) 
 | 
                .sheet("控制器") 
 | 
                .doWrite(controllerList); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 设置excel下载响应头属性 
 | 
     */ 
 | 
    private void setExcelRespProp(HttpServletResponse response, String rawFileName) throws UnsupportedEncodingException { 
 | 
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); 
 | 
        response.setCharacterEncoding("utf-8"); 
 | 
        String fileName = URLEncoder.encode(rawFileName, "UTF-8").replaceAll("\\+", "%20"); 
 | 
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 不分页获取全部未删除的阀控器列表 
 | 
     * @return 
 | 
     */ 
 | 
    @GetMapping(path = "/getSimpleControllers") 
 | 
    @SsoAop() 
 | 
    public BaseResponse<List<VoControllerSimple>> getSimpleControllers() { 
 | 
        try { 
 | 
            List<VoControllerSimple> res = controllerSv.getSimpleControllers(); 
 | 
            return BaseResponseUtils.buildSuccess(res); 
 | 
        } catch (Exception e) { 
 | 
            log.error("获取未完的计划异常", e); 
 | 
            return BaseResponseUtils.buildException(e.getMessage()); 
 | 
        } 
 | 
    } 
 | 
} 
 |