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

import cn.iocoder.yudao.module.system.constant.robot.RobotTaskChcheConstant;
import cn.iocoder.yudao.module.system.controller.admin.actionlog.vo.RobotTaskDetailActionLogPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.actionlog.vo.RobotTaskDetailActionLogSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.actionlog.RobotTaskDetailActionLogDO;
import cn.iocoder.yudao.module.system.dal.mysql.actionlog.RobotTaskDetailActionLogMapper;
import cn.iocoder.yudao.module.system.util.redis.RedisUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

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


import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.config.SystemJobConfiguration.NOTIFY_THREAD_POOL_TASK_EXECUTOR;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.TASK_DETAIL_ACTION_LOG_NOT_EXISTS;

/**
 * 车辆动作记录 Service 实现类
 *
 * @author 陈宾顺
 */
@Service
@Validated
public class RobotTaskDetailActionLogServiceImpl implements RobotTaskDetailActionLogService {

    @Resource
    private RobotTaskDetailActionLogMapper taskDetailActionLogMapper;

    @Resource
    private RedisUtil redisUtil;

    @Value("${zn.robot_doing_action.action_entity_cache_time:3600*8}")
    private Long robotPositionCacheTime;

    @Override
    public Long createTaskDetailActionLog(RobotTaskDetailActionLogSaveReqVO createReqVO) {
        // 插入
        RobotTaskDetailActionLogDO taskDetailActionLog = BeanUtils.toBean(createReqVO, RobotTaskDetailActionLogDO.class);
        taskDetailActionLogMapper.insert(taskDetailActionLog);
        // 返回
        return taskDetailActionLog.getId();
    }

    @Override
    public void updateTaskDetailActionLog(RobotTaskDetailActionLogSaveReqVO updateReqVO) {
        // 校验存在
        validateTaskDetailActionLogExists(updateReqVO.getId());
        // 更新
        RobotTaskDetailActionLogDO updateObj = BeanUtils.toBean(updateReqVO, RobotTaskDetailActionLogDO.class);
        taskDetailActionLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteTaskDetailActionLog(Long id) {
        // 校验存在
        validateTaskDetailActionLogExists(id);
        // 删除
        taskDetailActionLogMapper.deleteById(id);
    }

    private void validateTaskDetailActionLogExists(Long id) {
        if (taskDetailActionLogMapper.selectById(id) == null) {
            throw exception(TASK_DETAIL_ACTION_LOG_NOT_EXISTS);
        }
    }

    @Override
    public RobotTaskDetailActionLogDO getTaskDetailActionLog(Long id) {
        return taskDetailActionLogMapper.selectById(id);
    }

    @Override
    public PageResult<RobotTaskDetailActionLogDO> getTaskDetailActionLogPage(RobotTaskDetailActionLogPageReqVO pageReqVO) {
        return taskDetailActionLogMapper.selectPage(pageReqVO);
    }

    @Override
    @Async(NOTIFY_THREAD_POOL_TASK_EXECUTOR)
    public void addLogInCache(List<RobotTaskDetailActionLogDO> logs) {
        for (RobotTaskDetailActionLogDO v : logs) {
            redisUtil.set(RobotTaskChcheConstant.ROBOT_ACTION_LOG_ENTITY+v.getCommandId(),
                    JSON.toJSONString(v),robotPositionCacheTime);
        }
    }

}