package com.dy.pipIrrBase.rtuUpgrade.program; 
 | 
  
 | 
import cn.hutool.core.util.HexUtil; 
 | 
import com.dy.common.aop.SsoAop; 
 | 
import com.dy.common.softUpgrade.parse.HexFileParse; 
 | 
import com.dy.common.softUpgrade.parse.HexFileVo; 
 | 
import com.dy.common.util.ByteUtil; 
 | 
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.pojoUg.UgRtuProgram; 
 | 
import com.dy.pipIrrGlobal.voUg.VoRtuProgram; 
 | 
import io.swagger.v3.oas.annotations.Operation; 
 | 
import io.swagger.v3.oas.annotations.Parameter; 
 | 
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.validation.Valid; 
 | 
import lombok.extern.slf4j.Slf4j; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.http.MediaType; 
 | 
import org.springframework.validation.BindingResult; 
 | 
import org.springframework.web.bind.annotation.*; 
 | 
  
 | 
import java.io.UnsupportedEncodingException; 
 | 
import java.util.Date; 
 | 
import java.util.List; 
 | 
import java.util.Objects; 
 | 
  
 | 
/** 
 | 
 * @Author: liurunyu 
 | 
 * @Date: 2024/11/5 8:32 
 | 
 * @Description 
 | 
 */ 
 | 
@Slf4j 
 | 
@Tag(name = "RTU升级程序管理", description = "RTU升级程序增删改查等操作") 
 | 
@RestController 
 | 
@RequestMapping(path = "ugRtuProgram") 
 | 
@SuppressWarnings("unchecked") 
 | 
//java版本越高,对泛型约束越严,所以配置SuppressWarnings("unchecked") 
 | 
public class UgRtuProgramCtrl { 
 | 
  
 | 
    private UgRtuProgramSv sv; 
 | 
  
 | 
    @Autowired 
 | 
    private void setSv(UgRtuProgramSv sv) { this.sv = sv; } 
 | 
  
 | 
    /** 
 | 
     * 得到一个RTU升级程序数据 
 | 
     * @return 一个RTU升级程序数据 
 | 
     */ 
 | 
    @Operation(summary = "一个RTU升级程序", description = "得到一个RTU升级程序数据") 
 | 
    @ApiResponses(value = { 
 | 
            @ApiResponse( 
 | 
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, 
 | 
                    description = "返回一个RTU升级程序数据(BaseResponse.content:{})", 
 | 
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, 
 | 
                            schema = @Schema(implementation = UgRtuProgram.class))} 
 | 
            ) 
 | 
    }) 
 | 
    @GetMapping(path = "one") 
 | 
    @SsoAop() 
 | 
    public BaseResponse<UgRtuProgram> one(Long id) throws UnsupportedEncodingException { 
 | 
        UgRtuProgram po = this.sv.selectById(id) ; 
 | 
        if(po != null){ 
 | 
            if(po.programBytes != null && po.programBytes.length > 0){ 
 | 
                po.programString = HexUtil.encodeHexStr(po.programBytes) ; 
 | 
            } 
 | 
            if(po.fileBytes != null && po.fileBytes.length > 0){ 
 | 
                po.fileString = new String(po.fileBytes) ; 
 | 
                po.fileString = po.fileString.replaceAll("\\r\\n", "<br/>") ; 
 | 
            } 
 | 
            return BaseResponseUtils.buildSuccess(po); 
 | 
        }else{ 
 | 
            return BaseResponseUtils.buildErrorMsg("未查询到对应的记录") ; 
 | 
        } 
 | 
    } 
 | 
    /** 
 | 
     * 客户端请求得到一些RTU升级程序数据 
 | 
     * @return 一些RTU升级程序数据 
 | 
     */ 
 | 
    @Operation(summary = "获得一些RTU升级程序", description = "返回一些分页RTU升级程序数据") 
 | 
    @ApiResponses(value = { 
 | 
            @ApiResponse( 
 | 
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, 
 | 
                    description = "返回一些RTU升级程序数据(BaseResponse.content:QueryResultVo[{}])", 
 | 
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, 
 | 
                            schema = @Schema(implementation = UgRtuProgram.class))} 
 | 
            ) 
 | 
    }) 
 | 
    @GetMapping(path = "/some") 
 | 
    @SsoAop() 
 | 
    public BaseResponse<QueryResultVo<List<UgRtuProgram>>> some(QueryVo vo){ 
 | 
        try { 
 | 
            QueryResultVo<List<UgRtuProgram>> res = this.sv.selectSome(vo) ; 
 | 
            return BaseResponseUtils.buildSuccess(res); 
 | 
        } catch (Exception e) { 
 | 
            log.error("查询RTU升级程序异常", e); 
 | 
            return BaseResponseUtils.buildErrorMsg(e.getMessage()) ; 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 客户端请求得到所有升级程序记录ID和文件名称 ,以供其他应用下拉选择 
 | 
     * @return 所RTU升级程序文件名数据 
 | 
     */ 
 | 
    @Operation(summary = "获得一些RTU升级程序", description = "返回一些分页RTU升级程序数据") 
 | 
    @ApiResponses(value = { 
 | 
            @ApiResponse( 
 | 
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, 
 | 
                    description = "返回一些RTU升级程序数据(BaseResponse.content:QueryResultVo[{}])", 
 | 
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, 
 | 
                            schema = @Schema(implementation = UgRtuProgram.class))} 
 | 
            ) 
 | 
    }) 
 | 
    @GetMapping(path = "/all") 
 | 
    @SsoAop() 
 | 
    public BaseResponse<QueryResultVo<List<UgRtuProgram>>> all(){ 
 | 
        try { 
 | 
            QueryResultVo<List<UgRtuProgram>> res = this.sv.selectAll() ; 
 | 
            return BaseResponseUtils.buildSuccess(res); 
 | 
        } catch (Exception e) { 
 | 
            log.error("查询RTU升级程序异常", e); 
 | 
            return BaseResponseUtils.buildErrorMsg(e.getMessage()) ; 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 新增保存RTU升级程序 
 | 
     * @param vo 新增保存RTU升级程序form表单对象 
 | 
     * @return 是否成功 
 | 
     */ 
 | 
    @Operation(summary = "保存RTU升级程序", description = "提交RTU升级程序数据(form表单),进行保存") 
 | 
    @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 = "save", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) 
 | 
    @SsoAop() 
 | 
    public BaseResponse<Boolean> save(@Parameter(description = "form表单数据", required = true) @Valid VoRtuProgram vo,  @Parameter(hidden = true) BindingResult bindingResult){ 
 | 
        if(bindingResult != null && bindingResult.hasErrors()){ 
 | 
            return BaseResponseUtils.buildErrorMsg(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); 
 | 
        } 
 | 
        if(vo.storeRamAddr == null 
 | 
                || vo.storeRamAddr.trim().equals("") 
 | 
                || vo.storeRamAddr.length() != 8 
 | 
                || !HexUtil.isHexNumber(vo.storeRamAddr)){ 
 | 
            return BaseResponseUtils.buildErrorMsg("升级程序存放地址(4字节,8位十六进制数)不正确") ; 
 | 
        } 
 | 
        if(vo.startRamAddr == null 
 | 
                || vo.startRamAddr.trim().equals("") 
 | 
                || vo.startRamAddr.length() != 8 
 | 
                || !HexUtil.isHexNumber(vo.startRamAddr)){ 
 | 
            return BaseResponseUtils.buildErrorMsg("程序覆盖起始地址(4字节,8位十六进制数)不正确") ; 
 | 
        } 
 | 
        if(vo.file == null){ 
 | 
            return BaseResponseUtils.buildErrorMsg("RTU程序文件必须上传") ; 
 | 
        } 
 | 
        String fileName = vo.file.getOriginalFilename() ; 
 | 
        if(this.sv.existFileName(fileName)){ 
 | 
            return BaseResponseUtils.buildErrorMsg("RTU程序文件名已存在") ; 
 | 
        } 
 | 
  
 | 
        HexFileVo fileVo = null ; 
 | 
        byte[] fileBytes = null ; 
 | 
        try{ 
 | 
            fileBytes = vo.file.getBytes() ; 
 | 
            if(fileBytes != null) { 
 | 
                HexFileParse fileParse = new HexFileParse(); 
 | 
                fileVo = fileParse.doParse(fileBytes); 
 | 
            } 
 | 
        }catch (Exception e){ 
 | 
            fileVo = null ; 
 | 
            return BaseResponseUtils.buildErrorMsg("解析RTU程序文件异常:" + e.getMessage()) ; 
 | 
        } 
 | 
        if(fileVo != null){ 
 | 
            UgRtuProgram po = new UgRtuProgram(); 
 | 
            po.hexFileName = fileName ; 
 | 
            po.programCalculateBytes = (int)fileVo.calculateBytes ; 
 | 
            po.programCrc16 = fileVo.bytesCrc16 ; 
 | 
            po.programBytes = fileVo.bytes; 
 | 
            po.fileBytes = fileBytes; 
 | 
            po.remark = vo.remark ; 
 | 
            po.dt = new Date(); 
 | 
            po.deleted = 0 ; 
 | 
            vo.toPo(po); 
 | 
            po.id = null ; 
 | 
            int count; 
 | 
            try { 
 | 
                count = this.sv.save(po); 
 | 
            } catch (Exception e) { 
 | 
                log.error("保存RTU升级程序异常", e); 
 | 
                return BaseResponseUtils.buildErrorMsg(e.getMessage()) ; 
 | 
            } 
 | 
            if(count <= 0){ 
 | 
                return BaseResponseUtils.buildErrorMsg("数据库存储失败") ; 
 | 
            }else{ 
 | 
                return BaseResponseUtils.buildSuccess(true) ; 
 | 
            } 
 | 
        }else{ 
 | 
            return BaseResponseUtils.buildErrorMsg("解析RTU程序文件异常") ; 
 | 
        } 
 | 
    } 
 | 
    /** 
 | 
     * 为了保证升级程序的正确,不提供修改功能,如果上传错了,只能删除后重新上传 
 | 
     * @param vo 保存RTU升级程序form表单对象 
 | 
     * @return 是否成功 
 | 
    @Operation(summary = "编辑修改片区", description = "提交片区数据(form表单),进行修改") 
 | 
    @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 = "update", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) 
 | 
    @SsoAop() 
 | 
    public BaseResponse<Boolean> update(@Parameter(description = "form表单数据", required = true) @Valid VoRtuProgram vo, @Parameter(hidden = true) BindingResult bindingResult){ 
 | 
    } 
 | 
    */ 
 | 
  
 | 
    /** 
 | 
     * 删除RTU升级程序 
 | 
     * @param id RTU升级程序ID 
 | 
     * @return 是否成功 
 | 
     */ 
 | 
    @Operation(summary = "删除RTU升级程序", description = "提交RTU升级程序ID,进行逻辑删除") 
 | 
    @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))} 
 | 
            ) 
 | 
    }) 
 | 
    @GetMapping(path = "delete") 
 | 
    @SsoAop() 
 | 
    public BaseResponse<Boolean> delete(Long id){ 
 | 
        if(id == null){ 
 | 
            return BaseResponseUtils.buildErrorMsg("id不能为空") ; 
 | 
        } 
 | 
        int count; 
 | 
        try { 
 | 
            count = this.sv.delete(id); 
 | 
        } catch (Exception e) { 
 | 
            log.error("删除RTU升级程序异常", e); 
 | 
            return BaseResponseUtils.buildErrorMsg(e.getMessage()) ; 
 | 
        } 
 | 
        if(count <= 0){ 
 | 
            return BaseResponseUtils.buildErrorMsg("数据库存储失败") ; 
 | 
        }else{ 
 | 
            return BaseResponseUtils.buildSuccess(true) ; 
 | 
        } 
 | 
    } 
 | 
  
 | 
} 
 |