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

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.infra.api.websocket.WebSocketSenderApi;
import cn.iocoder.yudao.module.system.constant.webSocket.WebSocketConstant;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotWarnMsgPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotWarnMsgSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.positionmap.PositionMapDO;
import cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotWarnMsgDO;
import cn.iocoder.yudao.module.system.dal.mysql.positionmap.PositionMapMapper;
import cn.iocoder.yudao.module.system.dal.mysql.robot.RobotWarnMsgMapper;
import cn.iocoder.yudao.module.system.enums.robot.RobotWarnType;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.Arrays;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.WARN_MSG_NOT_EXISTS;

/**
 * 机器人告警信息 Service 实现类
 *
 * @author 陈宾顺
 */
@Service
@Validated
@Slf4j
public class RobotWarnMsgServiceImpl extends ServiceImpl<RobotWarnMsgMapper, RobotWarnMsgDO> implements RobotWarnMsgService {

    @Resource
    private RobotWarnMsgMapper warnMsgMapper;

    @Resource
    private PositionMapMapper positionMapMapper;

    @Resource
    public WebSocketSenderApi webSocketSenderApi;

    @Override
    public Long createWarnMsg(RobotWarnMsgSaveReqVO createReqVO) {
        // 插入
        RobotWarnMsgDO warnMsg = BeanUtils.toBean(createReqVO, RobotWarnMsgDO.class);
        warnMsgMapper.insert(warnMsg);
        // 返回
        return warnMsg.getId();
    }

    @Override
    public void updateWarnMsg(RobotWarnMsgSaveReqVO updateReqVO) {
        // 校验存在
        validateWarnMsgExists(updateReqVO.getId());
        // 更新
        RobotWarnMsgDO updateObj = BeanUtils.toBean(updateReqVO, RobotWarnMsgDO.class);
        warnMsgMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarnMsg(Long id) {
        // 校验存在
        validateWarnMsgExists(id);
        // 删除
        warnMsgMapper.deleteById(id);
    }

    private void validateWarnMsgExists(Long id) {
        if (warnMsgMapper.selectById(id) == null) {
            throw exception(WARN_MSG_NOT_EXISTS);
        }
    }

    @Override
    public RobotWarnMsgDO getWarnMsg(Long id) {
        return warnMsgMapper.selectById(id);
    }

    @Override
    public PageResult<RobotWarnMsgDO> getWarnMsgPage(RobotWarnMsgPageReqVO pageReqVO) {
        return warnMsgMapper.selectPage(pageReqVO);
    }

    @Override
    public void sendWarnMsgToWebsocket(Object errorMsg) {
        List<PositionMapDO> positionMapDOS = positionMapMapper.selectList(new LambdaQueryWrapperX<PositionMapDO>());
        if (ObjectUtil.isEmpty(positionMapDOS)){
            log.info("暂无楼层/区域信息，无法推送数据给前端");
            return;
        }
        for (PositionMapDO positionMapDO : positionMapDOS) {
            webSocketSenderApi.sendObject(positionMapDO.getFloor() + "_" + positionMapDO.getArea(),
                    WebSocketConstant.AGV_WARN, JSONUtil.toJsonStr(errorMsg));
        }
    }

    @Override
    public void addWarnMsg(String message) {
        RobotWarnMsgDO warnMsg = RobotWarnMsgDO.builder().warnLevel(4)
                .warnCode("PP")
                .robotNo("")
                .warnType(RobotWarnType.PATH.getType())
                .warnMsg(message)
                .warnSolve(message)
                .build();
        warnMsgMapper.insert(warnMsg);
    }

}
