package cn.iocoder.yudao.module.system.service.tool;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.module.mqtt.api.path.PathPlanningApi;
import cn.iocoder.yudao.module.system.constant.path.PathPlanningTopicConstant;
import cn.iocoder.yudao.module.system.controller.admin.config.dto.TaskOrderConfigDTO;
import cn.iocoder.yudao.module.system.controller.admin.config.vo.CommonConfigVO;
import cn.iocoder.yudao.module.system.dal.dataobject.config.CommonConfigDO;
import cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotWarnMsgDO;
import cn.iocoder.yudao.module.system.dal.mysql.config.CommonConfigMapper;
import cn.iocoder.yudao.module.system.dal.mysql.robot.RobotWarnMsgMapper;
import cn.iocoder.yudao.module.system.enums.config.CommandConfigTypeEnum;
import cn.iocoder.yudao.module.system.enums.robot.RobotWarnType;
import cn.iocoder.yudao.module.system.service.robot.RobotWarnMsgService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Arrays;

@Slf4j
@Service
@Validated
public class ToolsServiceImpl implements ToolsService {

    @Resource
    private PathPlanningApi pathPlanningApi;

    @Resource
    private CommonConfigMapper configMapper;

    @Resource
    private RobotWarnMsgService warnMsgService;

    @Resource
    private RobotWarnMsgMapper warnMsgMapper;

    /**
     * 发送时间、优先级、距离的权重给PP
     *
     * @return
     */
    @Override
    public String sendPPsortConfig() {

        CommonConfigDO commonConfigDO = configMapper.selectOne(new LambdaQueryWrapper<CommonConfigDO>()
                .eq(CommonConfigDO::getConfigType, CommandConfigTypeEnum.CHARG_CONFIG.getType()));

        if (ObjectUtil.isEmpty(commonConfigDO) || ObjectUtil.isEmpty(commonConfigDO.getConfigStr())) {
            log.info("没有配置路径规划权重");
            addWarnMsg();
            return "没有配置路径规划权重";
        }

        CommonConfigVO chargeConfig = JSONUtil.toBean(commonConfigDO.getConfigStr(), CommonConfigVO.class);
        if (ObjectUtil.isEmpty(chargeConfig.getPathSortConfig())) {
            addWarnMsg();
            return "没有配置路径规划权重";
        }

        CommonConfigDO config = configMapper.selectOne(new LambdaQueryWrapper<CommonConfigDO>()
                .eq(CommonConfigDO::getConfigType, chargeConfig.getPathSortConfig()));
        if (ObjectUtil.isEmpty(config)) {
            addWarnMsg();
            return "没有配置路径规划权重";
        }

        TaskOrderConfigDTO taskOrderConfigDTO = JSONUtil.toBean(config.getConfigStr(), TaskOrderConfigDTO.class);
        pathPlanningApi.synchronousLineObject(taskOrderConfigDTO,
                PathPlanningTopicConstant.SEND_SORT_CONFIG_TO_PP);

        return "发送数据库权重配置成功";
    }


    public void addWarnMsg() {
        RobotWarnMsgDO warnMsg = RobotWarnMsgDO.builder().warnLevel(4)
                .warnCode("NOT_PP_CONFIG")
                .robotNo("PP")
                .warnType(RobotWarnType.ROBOT_WARN.getType())
                .warnMsg("没有配置路径规划权重")
                .warnSolve("配置页面设置")
                .build();
        warnMsgService.sendWarnMsgToWebsocket(warnMsg.getWarnMsg());
        warnMsgMapper.insertBatch(Arrays.asList(warnMsg));
    }





}
