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

import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.log.dto.RobotWalkingDistanceDTO;
import cn.iocoder.yudao.module.system.controller.admin.log.vo.RobotTaskTimeConsumingPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.log.vo.RobotTaskTimeConsumingSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.log.RobotTaskTimeConsumingDO;
import cn.iocoder.yudao.module.system.dal.mysql.log.RobotTaskTimeConsumingMapper;
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 com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


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

/**
 * 车辆任务行程耗时 Service 实现类
 *
 * @author 陈宾顺
 */
@Service
@Validated
public class RobotTaskTimeConsumingServiceImpl extends ServiceImpl<RobotTaskTimeConsumingMapper, RobotTaskTimeConsumingDO> implements RobotTaskTimeConsumingService {

    @Resource
    private RobotTaskTimeConsumingMapper robotTaskTimeConsumingMapper;

    @Override
    public Long createRobotTaskTimeConsuming(RobotTaskTimeConsumingSaveReqVO createReqVO) {
        // 插入
        RobotTaskTimeConsumingDO robotTaskTimeConsuming = BeanUtils.toBean(createReqVO, RobotTaskTimeConsumingDO.class);
        robotTaskTimeConsumingMapper.insert(robotTaskTimeConsuming);
        // 返回
        return robotTaskTimeConsuming.getId();
    }

    @Override
    public void updateRobotTaskTimeConsuming(RobotTaskTimeConsumingSaveReqVO updateReqVO) {
        // 校验存在
        validateRobotTaskTimeConsumingExists(updateReqVO.getId());
        // 更新
        RobotTaskTimeConsumingDO updateObj = BeanUtils.toBean(updateReqVO, RobotTaskTimeConsumingDO.class);
        robotTaskTimeConsumingMapper.updateById(updateObj);
    }

    @Override
    public void deleteRobotTaskTimeConsuming(Long id) {
        // 校验存在
        validateRobotTaskTimeConsumingExists(id);
        // 删除
        robotTaskTimeConsumingMapper.deleteById(id);
    }

    private void validateRobotTaskTimeConsumingExists(Long id) {
        if (robotTaskTimeConsumingMapper.selectById(id) == null) {
            throw exception(ROBOT_TASK_TIME_CONSUMING_NOT_EXISTS);
        }
    }

    @Override
    public RobotTaskTimeConsumingDO getRobotTaskTimeConsuming(Long id) {
        return robotTaskTimeConsumingMapper.selectById(id);
    }

    @Override
    public PageResult<RobotTaskTimeConsumingDO> getRobotTaskTimeConsumingPage(RobotTaskTimeConsumingPageReqVO pageReqVO) {
        return robotTaskTimeConsumingMapper.selectPage(pageReqVO);
    }

    @Override
    public void updateTime(RobotWalkingDistanceDTO data) {
        RobotTaskTimeConsumingDO updateObj = BeanUtils.toBean(data, RobotTaskTimeConsumingDO.class);
        updateObj.setTaskDetailId(Long.valueOf(data.getOrderId()));
        updateConsuming(updateObj);
    }

    @Override
    public void updateConsuming(RobotTaskTimeConsumingDO data) {
        RobotTaskTimeConsumingDO entity = BeanUtils.toBean(data, RobotTaskTimeConsumingDO.class);
        robotTaskTimeConsumingMapper.updateEntity(entity);
    }

}
