package com.dy.pipIrrRemote.monitor.p202404V201.cd15; 
 | 
  
 | 
import com.alibaba.fastjson2.JSON; 
 | 
import com.alibaba.fastjson2.JSONObject; 
 | 
import com.dy.common.aop.SsoAop; 
 | 
import com.dy.common.mw.protocol.Command; 
 | 
import com.dy.common.mw.protocol.p206V202404.upVos.DataCdXyVo; 
 | 
import com.dy.common.util.Callback; 
 | 
import com.dy.common.util.NumUtil; 
 | 
import com.dy.common.webUtil.BaseResponse; 
 | 
import com.dy.common.webUtil.BaseResponseUtils; 
 | 
import com.dy.pipIrrGlobal.pojoSe.SeClientCard; 
 | 
import com.dy.pipIrrRemote.common.dto.DtoBase; 
 | 
import com.dy.pipIrrRemote.monitor.common.ComCtrl; 
 | 
import io.swagger.v3.oas.annotations.tags.Tag; 
 | 
import jakarta.validation.Valid; 
 | 
import lombok.RequiredArgsConstructor; 
 | 
import lombok.extern.slf4j.Slf4j; 
 | 
import org.apache.commons.lang3.RandomStringUtils; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.context.annotation.Scope; 
 | 
import org.springframework.http.MediaType; 
 | 
import org.springframework.validation.BindingResult; 
 | 
import org.springframework.web.bind.annotation.PostMapping; 
 | 
import org.springframework.web.bind.annotation.RequestBody; 
 | 
import org.springframework.web.bind.annotation.RequestMapping; 
 | 
import org.springframework.web.bind.annotation.RestController; 
 | 
  
 | 
/** 
 | 
 * @Author: liurunyu 
 | 
 * @Date: 2025/5/12 11:10 
 | 
 * @Description 
 | 
 */ 
 | 
@Slf4j 
 | 
@Tag(name = "远程命令", description = "远程用户充值") 
 | 
@RestController("p202404V201Cd15Ctrl") 
 | 
@RequestMapping(path = "p202404V201/cd15") 
 | 
@RequiredArgsConstructor 
 | 
@Scope("prototype") //因为有对象类属性,所以采用原型模式,每次请求新建一个实例对象 
 | 
public class CdCtrl extends ComCtrl { 
 | 
  
 | 
    private static final String RtuSuccessMsg = "控制器接收并执行命令成功,无返回数据"; 
 | 
  
 | 
    private static final String ComCode = "15" ; 
 | 
  
 | 
    @Autowired 
 | 
    private CdSv sv ; 
 | 
    /** 
 | 
     * 向设备(控制器)发送命令 
 | 
     * @param dto 前端发来的值对象 
 | 
     * @param bindingResult 对dto验证的结果 
 | 
     * @return 返回前端 
 | 
     */ 
 | 
    @PostMapping(path = "send", consumes = MediaType.APPLICATION_JSON_VALUE) 
 | 
    @SsoAop() 
 | 
    public BaseResponse<Object> send(@RequestBody @Valid CdDto dto, BindingResult bindingResult) { 
 | 
        BaseResponse<Object> res ; 
 | 
        //发送命令前-1:验证 
 | 
        res = super.pre1(sv, ComCode, dto, bindingResult); 
 | 
        if(res == null) { 
 | 
            //发送命令前-2:获得数据 
 | 
            res = super.pre2(sv, ComCode, dto, bindingResult); 
 | 
            if (res == null) { 
 | 
                //发送命令前-3:保存命令日志 
 | 
                SeClientCard icPo = sv.selectClientIcCardById(Long.parseLong(dto.icCardId.trim())) ; 
 | 
                if(icPo == null){ 
 | 
                    return BaseResponseUtils.buildErrorMsg("服务端出错,未得到农户IC卡") ; 
 | 
                } 
 | 
                CdParam comParam = CdParam.builder().commandCode(ComCode).projectNo(projectNo).controllerType(controllerType) 
 | 
                        .icCardAddr(icPo.getCardaddr())//IC卡地址(8位数字或字母) 
 | 
                        .icCardNo(icPo.getCardnum())//用户卡序列号(17位数字)(6字节BCD,2字节HEX) 
 | 
                        .chargeMoney(dto.chargeMoney)//单位:元  2位小数点(最大值 99999999.99) 
 | 
                        .chargeWater(null)//当前应用只能远程充钱,所以chargeWater赋空值 
 | 
                        .flowNo(RandomStringUtils.randomNumeric(12))//流水号(12位数字) 
 | 
                        .build(); 
 | 
                res = super.pre3(sv, dto.getIntakeId(), dto.getOperator(), ComCode, comParam); 
 | 
                if (res == null) { 
 | 
                    //发送命令前-4:准备Feature 
 | 
                    super.pre4(); 
 | 
                    try { 
 | 
                        //创建外部命令(发给控制器) 
 | 
                        Command com = sv.createOuterCommand(ctrlPo.getRtuAddr(), "" + comId, ComCode); 
 | 
                        com.rtuResultSendWebUrl = rtuResultSendWebUrl; 
 | 
                        com.param = comParam ; 
 | 
                        //发送命令 
 | 
                        res = super.doSend(sv, com); 
 | 
                        if (res == null) { 
 | 
                            //发送命令后 
 | 
                            res = super.after(ComCode, null); 
 | 
                        } 
 | 
                    } catch (Exception e) { 
 | 
                        res = BaseResponseUtils.buildFail("服务端构造并向通信中间件发送请求时异常" + (e.getMessage() == null ? "" : e.getMessage())); 
 | 
                    } finally { 
 | 
                        //最终 
 | 
                        super.end(); 
 | 
                    } 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
        return res ; 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    protected String checkDto(DtoBase dto) { 
 | 
        if(dto != null){ 
 | 
            CdDto myo = (CdDto) dto ; 
 | 
            if(myo.icCardId == null || myo.icCardId.trim().equals("")){ 
 | 
                return "请选择农户IC卡"; 
 | 
            } 
 | 
            if(!NumUtil.isPlusIntNumber(myo.icCardId.trim())){ 
 | 
                return "请选择农户IC卡"; 
 | 
            } 
 | 
            if(myo.chargeMoney == null){ 
 | 
                return "充值金额(元)不能为空"; 
 | 
            } 
 | 
            if(myo.chargeMoney > 99999999.99 || myo.chargeMoney <= 0.0){ 
 | 
                return "请正确填写充值金额(元)"; 
 | 
            } 
 | 
        } 
 | 
        return null; 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    protected String dealComResult(String code, JSONObject resultData, Callback callback){ 
 | 
        String msg; 
 | 
        if(resultData != null){ 
 | 
            JSONObject codeData = resultData.getJSONObject("data") ; 
 | 
            if(codeData == null){ 
 | 
                msg = RtuSuccessMsg ; 
 | 
            }else { 
 | 
                String json = codeData.toJSONString(); 
 | 
                DataCdXyVo cvo = JSON.parseObject(json, DataCdXyVo.class) ; 
 | 
                if(cvo != null){ 
 | 
                    msg = cvo.toStr(false) ; 
 | 
                }else{ 
 | 
                    msg = RtuSuccessMsg ; 
 | 
                } 
 | 
            } 
 | 
        }else{ 
 | 
            msg = RtuSuccessMsg ; 
 | 
        } 
 | 
        return msg; 
 | 
    } 
 | 
} 
 |