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

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.module.system.controller.admin.log.dto.RobotTaskLineTimeConsumingDTO;
import cn.iocoder.yudao.module.system.controller.admin.log.dto.RobotTaskLineTimeConsumingDetailDTO;
import cn.iocoder.yudao.module.system.controller.admin.log.dto.RobotWalkingDistanceDTO;
import cn.iocoder.yudao.module.system.controller.admin.log.vo.RobotTaskLineTimeConsumingPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.log.vo.RobotTaskLineTimeConsumingSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.log.RobotTaskLineTimeConsumingDO;
import cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotTaskDetailDO;
import cn.iocoder.yudao.module.system.dal.mysql.log.RobotTaskLineTimeConsumingMapper;
import cn.iocoder.yudao.module.system.enums.common.ZeroOneEnum;
import cn.iocoder.yudao.module.system.service.robot.RobotTaskDetailService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
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.TASK_LINE_TIME_CONSUMING_NOT_EXISTS;

/**
 * 车辆行走路线耗时记录 Service 实现类
 *
 * @author 陈宾顺
 */
@Service
@Validated
@Slf4j
public class RobotTaskLineTimeConsumingServiceImpl extends ServiceImpl<RobotTaskLineTimeConsumingMapper, RobotTaskLineTimeConsumingDO> implements RobotTaskLineTimeConsumingService {

    @Resource
    private RobotTaskLineTimeConsumingMapper taskLineTimeConsumingMapper;

    @Resource
    private RobotTaskDetailService robotTaskDetailService;

    @Override
    public Long createTaskLineTimeConsuming(RobotTaskLineTimeConsumingSaveReqVO createReqVO) {
        // 插入
        RobotTaskLineTimeConsumingDO taskLineTimeConsuming = BeanUtils.toBean(createReqVO, RobotTaskLineTimeConsumingDO.class);
        taskLineTimeConsumingMapper.insert(taskLineTimeConsuming);
        // 返回
        return taskLineTimeConsuming.getId();
    }

    @Override
    public void updateTaskLineTimeConsuming(RobotTaskLineTimeConsumingSaveReqVO updateReqVO) {
        // 校验存在
        validateTaskLineTimeConsumingExists(updateReqVO.getId());
        // 更新
        RobotTaskLineTimeConsumingDO updateObj = BeanUtils.toBean(updateReqVO, RobotTaskLineTimeConsumingDO.class);
        taskLineTimeConsumingMapper.updateById(updateObj);
    }

    @Override
    public void deleteTaskLineTimeConsuming(Long id) {
        // 校验存在
        validateTaskLineTimeConsumingExists(id);
        // 删除
        taskLineTimeConsumingMapper.deleteById(id);
    }

    private void validateTaskLineTimeConsumingExists(Long id) {
        if (taskLineTimeConsumingMapper.selectById(id) == null) {
            throw exception(TASK_LINE_TIME_CONSUMING_NOT_EXISTS);
        }
    }

    @Override
    public RobotTaskLineTimeConsumingDO getTaskLineTimeConsuming(Long id) {
        return taskLineTimeConsumingMapper.selectById(id);
    }

    @Override
    public PageResult<RobotTaskLineTimeConsumingDO> getTaskLineTimeConsumingPage(RobotTaskLineTimeConsumingPageReqVO pageReqVO) {
        return taskLineTimeConsumingMapper.selectPage(pageReqVO);
    }

    @Override
    public void addTaskLineTimeConsuming(String message) {
        log.info("消息 :{}",message);
        List<RobotTaskLineTimeConsumingDetailDTO> list = JSONUtil.toList(message, RobotTaskLineTimeConsumingDetailDTO.class);
        Long orderId = Long.valueOf(list.get(0).getOrderId());
        String taskNo = robotTaskDetailService.getTaskNoByDetailId(orderId);
        RobotTaskDetailDO taskDetail = robotTaskDetailService.getTaskDetail(orderId);
        List<RobotTaskLineTimeConsumingDO> addList = new ArrayList<>();
        for (RobotTaskLineTimeConsumingDetailDTO v : list) {
            if ((ZeroOneEnum.ZERO.getType()+"").equals(v.getTimeConsuming())) {
                continue;
            }
            RobotTaskLineTimeConsumingDO line = BeanUtils.toBean(v, RobotTaskLineTimeConsumingDO.class);
            line.setTaskNo(taskNo);
            line.setTaskDetailId(orderId);
            line.setRobotNo(taskDetail.getRobotNo());
            line.setTotalTimeConsuming(String.format("%.2f", Double.valueOf(v.getTimeConsuming())));
            line.setFromSortNum(getSpit(line.getFromSortNum()));
            line.setToSortNum(getSpit(line.getToSortNum()));
            if (ObjectUtil.isNotEmpty(v.getTimeConsuming()) && ObjectUtil.isNotEmpty(v.getTotalDistance())) {
                BigDecimal distance = new BigDecimal(v.getTotalDistance());
                BigDecimal timeConsuming = new BigDecimal(v.getTimeConsuming());
                BigDecimal divide = distance.divide(timeConsuming,1, RoundingMode.HALF_UP);
                line.setAverageSpeed(divide.toString());
            }
            addList.add(line);
        }
        taskLineTimeConsumingMapper.insertBatch(addList);
    }

    public String getSpit(String str) {
        String[] split = str.split("-");
        return split[1];
    }




























}
