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

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotTaskDetailAddVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotTaskDetailPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotTaskDetailRespVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotTaskDetailSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotTaskDO;
import cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotTaskDetailDO;
import cn.iocoder.yudao.module.system.dal.mysql.robot.RobotTaskDetailMapper;
import cn.iocoder.yudao.module.system.dal.mysql.robot.RobotTaskMapper;
import cn.iocoder.yudao.module.system.enums.robot.RobotTaskDetailStatusEnum;
import cn.iocoder.yudao.module.system.enums.robot.RobotTaskStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


import java.util.List;

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


/**
 * 机器人任务明细 Service 实现类
 *
 * @author 陈宾顺
 */
@Service
@Validated
@Slf4j
public class RobotTaskDetailServiceImpl implements RobotTaskDetailService {

    @Resource
    private RobotTaskDetailMapper taskDetailMapper;

    @Resource
    private RobotTaskMapper robotTaskMapper;

    @Override
    public Long createTaskDetail(RobotTaskDetailSaveReqVO createReqVO) {
        // 插入
        RobotTaskDetailDO taskDetail = BeanUtils.toBean(createReqVO, RobotTaskDetailDO.class);
        taskDetailMapper.insert(taskDetail);
        // 返回
        return taskDetail.getId();
    }

    @Override
    public void updateTaskDetail(RobotTaskDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateTaskDetailExists(updateReqVO.getId());
        RobotTaskDetailDO robotTaskDetailDO = taskDetailMapper.selectById(updateReqVO.getId());
        if (ObjectUtil.isNotEmpty(updateReqVO.getRobotNo()) &&
                !RobotTaskDetailStatusEnum.NEW.getType().equals(robotTaskDetailDO.getTaskStatus())) {
            throw exception(TASK_DETAIL_CHANGE_ROBOT);
        }
        // 更新
        RobotTaskDetailDO updateObj = BeanUtils.toBean(updateReqVO, RobotTaskDetailDO.class);
        taskDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteTaskDetail(Long id) {
        // 校验存在
        validateTaskDetailExists(id);
        // 删除
        taskDetailMapper.deleteById(id);
    }

    private void validateTaskDetailExists(Long id) {
        if (taskDetailMapper.selectById(id) == null) {
            throw exception(TASK_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public RobotTaskDetailDO getTaskDetail(Long id) {
        return taskDetailMapper.selectById(id);
    }

    @Override
    public PageResult<RobotTaskDetailDO> getTaskDetailPage(RobotTaskDetailPageReqVO pageReqVO) {
        return taskDetailMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertRobotDetailList(List<RobotTaskDetailAddVO> taskDetailList) {
        List<RobotTaskDetailDO> bean = BeanUtils.toBean(taskDetailList, RobotTaskDetailDO.class);
        taskDetailMapper.insertBatch(bean);
//        taskDetailMapper.insertBatchList(taskDetailList);
    }

    @Override
    public RobotTaskDetailDO closeTaskDetail(Long taskDetailId) {
        RobotTaskDetailDO robotTaskDetailDO = taskDetailMapper.selectById(taskDetailId);
        robotTaskDetailDO.setTaskStatus(RobotTaskDetailStatusEnum.ABNORMAL.getType());
        taskDetailMapper.updateById(robotTaskDetailDO);

        List<RobotTaskDetailDO> taskDetailDOS = taskDetailMapper.selectList(new LambdaQueryWrapperX<RobotTaskDetailDO>()
                .eq(RobotTaskDetailDO::getRobotTaskId, robotTaskDetailDO.getRobotTaskId())
                .in(RobotTaskDetailDO::getTaskStatus, RobotTaskStatusEnum.NEW.getType(), RobotTaskStatusEnum.DOING.getType()));
        if (ObjectUtil.isEmpty(taskDetailDOS)) {
            RobotTaskDO robotTask = RobotTaskDO.builder()
                    .id(robotTaskDetailDO.getRobotTaskId())
                    .taskStatus(RobotTaskStatusEnum.Exc.getType()).build();
            robotTaskMapper.updateRobot(robotTask);
        }
        log.info("车机上报异常， 把任务取消 :{}", taskDetailId);
        return robotTaskDetailDO;
    }
}