yangys
2024-10-30 25db770e621f1259b8d5b7fd514207f7481c2d0f
smart-man-boot/src/main/java/com/qianwen/smartman/modules/system/service/impl/ParamServiceImpl.java
@@ -1,25 +1,11 @@
package com.qianwen.smartman.modules.system.service.impl;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qianwen.core.log.exception.ServiceException;
import com.qianwen.core.mp.base.BaseServiceImpl;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.smartman.common.constant.FmsConstant;
import com.qianwen.smartman.common.utils.MessageUtils;
import com.qianwen.smartman.modules.fms.dto.FmsInputDto;
import com.qianwen.smartman.modules.fms.dto.SwitchDTO;
import com.qianwen.smartman.modules.fms.entity.FmsInstructionRecord;
import com.qianwen.smartman.modules.fms.enums.InstructionEnum;
import com.qianwen.smartman.modules.fms.forestClient.FmsForestClient;
import com.qianwen.smartman.modules.fms.service.IFmsInstructionRecordService;
import com.qianwen.smartman.modules.fms.util.FmsForestUtil;
import com.qianwen.smartman.modules.system.entity.Param;
import com.qianwen.smartman.modules.system.mapper.ParamMapper;
import com.qianwen.smartman.modules.system.service.IParamService;
@@ -27,15 +13,7 @@
@Service
public class ParamServiceImpl extends BaseServiceImpl<ParamMapper, Param> implements IParamService {
    private static final Logger log = LoggerFactory.getLogger(ParamServiceImpl.class);
    private final IFmsInstructionRecordService instructionRecordService;
    private final FmsForestClient fmsForestClient;
    public ParamServiceImpl(final IFmsInstructionRecordService instructionRecordService, final FmsForestClient fmsForestClient) {
        this.instructionRecordService = instructionRecordService;
        this.fmsForestClient = fmsForestClient;
    }
    @Override
    public String getValue(String paramKey) {
       Param param =  getOne(Wrappers.<Param>query().lambda().eq(Param::getParamKey, paramKey));
@@ -46,87 +24,6 @@
        return param.getParamValue();
    }
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void startSwitching() {
       Param fmsParam = getOne(Wrappers.<Param>lambdaQuery().eq(Param::getParamName, FmsConstant.PARAM_NAME));
       /*
        Param fmsParam = (Param) getOne((Wrapper) Wrappers.lambdaQuery().eq((v0) -> {
            return v0.getParamName();
        }, FmsConstant.PARAM_NAME));*/
        if (fmsParam != null) {
            fmsParam.setParamValue(FmsConstant.MANUAL);
            updateById(fmsParam);
        } else {
            Param fmsParam2 = new Param();
            fmsParam2.setParamName(FmsConstant.PARAM_NAME);
            fmsParam2.setParamKey(FmsConstant.PARAM_KEY);
            fmsParam2.setParamValue(FmsConstant.MANUAL);
            save(fmsParam2);
        }
        log.info("==============调用dmp接口,调度系统自动切换开始执行====================");
        FmsInputDto fms = new FmsInputDto();
        fms.setAutomaticHandType(0);
        changeToDoDmp(fms);
    }
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void switchState(SwitchDTO switchDTO) {
        String paramValue = switchDTO.getParamValue();
        List<FmsInstructionRecord> list = this.instructionRecordService.list(Wrappers.<FmsInstructionRecord>lambdaQuery().orderByDesc(FmsInstructionRecord::getCreateTime));
        /*
        List<FmsInstructionRecord> list = this.instructionRecordService.list((Wrapper) Wrappers.lambdaQuery().orderByDesc((v0) -> {
            return v0.getCreateTime();
        }));*/
        if (Func.isNotEmpty(list)) {
            FmsInstructionRecord fmsInstructionRecord = list.get(0);
            Integer state = fmsInstructionRecord.getState();
            if (InstructionEnum.ZERO.getType() == state.intValue() || InstructionEnum.ONE.getType() == state.intValue()) {
                throw new ServiceException(MessageUtils.message("cps.fms.order.last.instruction.status.fail.can.not.automatic", new Object[0]));
            }
        }
        Param fmsParam = getOne(Wrappers.<Param>lambdaQuery().eq(Param::getParamName,FmsConstant.PARAM_NAME));
        /*
        Param fmsParam = (Param) getOne((Wrapper) Wrappers.lambdaQuery().eq((v0) -> {
            return v0.getParamName();
        }, ));*/
        log.info("==============调用dmp接口,调度系统自动切换开始执行====================");
        FmsInputDto fms = new FmsInputDto();
        if (FmsConstant.MANUAL.equals(paramValue)) {
            fms.setAutomaticHandType(0);
        }
        if (FmsConstant.AUTOMATIC.equals(paramValue)) {
            fms.setAutomaticHandType(1);
        }
        changeToDoDmp(fms);
        if (Func.isNotEmpty(fmsParam)) {
            fmsParam.setParamValue(switchDTO.getParamValue());
            updateById(fmsParam);
        }
    }
    @Override
    public SwitchDTO orderStartStatus() {
       Param fmsParam = getOne(Wrappers.<Param>lambdaQuery().eq(Param::getParamName, FmsConstant.PARAM_NAME));
       /*
        Param fmsParam = (Param) getOne((Wrapper) Wrappers.lambdaQuery().eq((v0) -> {
            return v0.getParamName();
        }, FmsConstant.PARAM_NAME));*/
        if (fmsParam != null) {
            SwitchDTO switchDTO = new SwitchDTO();
            switchDTO.setParamValue(fmsParam.getParamValue());
            return switchDTO;
        }
        return null;
    }
    private void changeToDoDmp(FmsInputDto fms) {
        try {
            String returnMessage = this.fmsForestClient.automaticHandSwitch(fms);
            FmsForestUtil.checkMessage(returnMessage, "调度系统自动切换");
        } catch (Exception e) {
            throw new ServiceException("调度系统自动切换异常");
        }
    }
}