package com.sckw.mine.service;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.core.model.page.PageRes;
import com.sckw.core.utils.IdWorker;
import com.sckw.core.utils.TenantUtil;
import com.sckw.core.web.response.HttpResult;
import com.sckw.mine.common.OrderServerCommon;
import com.sckw.mine.entity.KwBusinessActionTrack;
import com.sckw.mine.entity.KwBusinessMineOrder;
import com.sckw.mine.entity.req.CompleteTaskParam;
import com.sckw.mine.entity.req.TaskCardPageListParam;
import com.sckw.mine.entity.res.MineOrderPageListRes;
import com.sckw.mine.entity.res.TaskCardPageListRes;
import com.sckw.mine.enums.MineStatusEnum;
import com.sckw.mine.mapper.KwBusinessActionTrackMapper;
import com.sckw.mine.mapper.MineMapper;
import com.sckw.mine.mapper.MineOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @desc:
 * @author: Lt
 * @date: 2024-05-29
 */
@Service
public class TaskCardService {

    @Autowired
    KwBusinessActionTrackMapper kwBusinessActionTrackMapper;

    @Autowired
    MineMapper mineMapper;

    @Autowired
    MineOrderMapper mineOrderMapper;

    @Autowired
    OrderServerCommon orderServerCommon;


    /**
    * @Description: 任务卡片分页列表
    * @Author: Lt
    * @Date: 2024/5/29 15:38
    */
    public HttpResult taskCardPageList(TaskCardPageListParam param)
    {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<TaskCardPageListRes> businessTruckPageListRes = kwBusinessActionTrackMapper.taskCardPageList(param);
        businessTruckPageListRes.forEach(e -> {
            e.setStatusStr(MineStatusEnum.getNameByCode(e.getStatus()));
        });
        return HttpResult.ok(new PageRes<>(new PageInfo<>(businessTruckPageListRes)));
    }

    /**
    * @Description: 任务卡片操作
    * @Author: Lt
    * @Date: 2024/5/29 15:44
    */
    public HttpResult taskAction(CompleteTaskParam completeTaskParam)
    {
        KwBusinessActionTrack kwBusinessActionTrack = kwBusinessActionTrackMapper.selectById(completeTaskParam.getId());
        if (ObjectUtils.isNull(kwBusinessActionTrack))
        {
            throw new RuntimeException("当前任务卡片不存在");
        }
        //根据订单号查找订单
        Long orderId = kwBusinessActionTrack.getMineOrderId();
        KwBusinessMineOrder kwBusinessMineOrder = mineOrderMapper.selectById(orderId);
        if (ObjectUtils.isNull(kwBusinessMineOrder))
        {
            throw new RuntimeException("未找到当前订单");
        }
        if (kwBusinessMineOrder.getStatus().equals(MineStatusEnum.EIGHT.getCode())) {
            throw new RuntimeException("当前订单已执行"    + MineStatusEnum.getNameByCode(kwBusinessMineOrder.getStatus()) +  ",请勿重复操作");
        }
        if(kwBusinessMineOrder.getStatus().equals(completeTaskParam.getStatus())){
            throw new RuntimeException("参数有误,不能和当前状态相同");
        }
        if(completeTaskParam.getStatus() > kwBusinessMineOrder.getStatus()) {
            Integer status = completeTaskParam.getStatus();
            kwBusinessMineOrder.setStatus(status);
            mineOrderMapper.updateById(kwBusinessMineOrder);
            orderServerCommon.saveActionTrack(kwBusinessMineOrder.getId(), status, completeTaskParam.getRemark(), "T");
            return HttpResult.ok();
        }else{
            throw new RuntimeException("参数有误");
        }

    }


}
