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

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
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.RobotTaskStatusEnum;
import cn.iocoder.yudao.module.system.service.robot.RobotInformationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
public class InitTaskServiceImpl implements InitTaskService{

    @Resource
    private RobotTaskDetailMapper taskDetailMapper;

    @Resource
    private RobotTaskMapper robotTaskMapper;

    @Resource
    private RobotInformationService informationService;

    @Override
    public void initTask() {
        log.info("将任务清除,上线时候，禁止使用");
        List<RobotTaskDetailDO> taskDetailDOS = taskDetailMapper.selectList(new LambdaQueryWrapperX<RobotTaskDetailDO>()
                .eq(RobotTaskDetailDO::getTaskStatus, RobotTaskStatusEnum.DOING.getType()));
        if (ObjectUtil.isNotEmpty(taskDetailDOS)) {
            for (RobotTaskDetailDO taskDetail : taskDetailDOS) {
                String mac = informationService.getMacByRobotNo(taskDetail.getRobotNo());
                informationService.robotCloseTaskDetail(String.valueOf(taskDetail.getId()), mac, "");
            }
        }

        taskDetailMapper.truncate();
        robotTaskMapper.truncate();
        informationService.initRobotInformation();
    }
}
