package com.clx.performance.service.impl.vehiclewarn;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.clx.performance.dao.OrderChildDao;
import com.clx.performance.dao.vehiclewarn.VehicleWarnInfoDao;
import com.clx.performance.dao.vehiclewarn.VehicleWarnLogDao;
import com.clx.performance.enums.ResultEnum;
import com.clx.performance.enums.vehiclewarn.VehicleWarnInfoEnum;
import com.clx.performance.enums.vehiclewarn.VehicleWarnLogEnum;
import com.clx.performance.model.OrderChild;
import com.clx.performance.model.vehiclewarn.VehicleWarnInfo;
import com.clx.performance.model.vehiclewarn.VehicleWarnLog;
import com.clx.performance.param.pc.vehiclewarn.PageVehicleWarnInfoParam;
import com.clx.performance.param.pc.vehiclewarn.VehicleWarnInfoResolveUpdateParam;
import com.clx.performance.param.pc.vehiclewarn.VehicleWarnInfoSuspendUpdateParam;
import com.clx.performance.service.trace.TruckTraceService;
import com.clx.performance.service.vehiclewarn.VehicleWarnInfoService;
import com.clx.performance.struct.vehiclewarn.VehicleWarnInfoStruct;
import com.clx.performance.utils.LocalDateTimeUtils;
import com.clx.performance.vo.pc.trace.DriverTraceVO;
import com.clx.performance.vo.pc.trace.DriverTruckTraceVO;
import com.clx.performance.vo.pc.vehiclewarn.VehicleTraceVO;
import com.clx.performance.vo.pc.vehiclewarn.VehicleWarnInfoVO;
import com.msl.user.data.UserSessionData;
import com.msl.user.utils.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class VehicleWarnInfoServiceImpl implements VehicleWarnInfoService {

    @Autowired
    private VehicleWarnInfoDao vehicleWarnInfoDao;
    @Autowired
    private VehicleWarnLogDao vehicleWarnLogDao;
    @Autowired
    private OrderChildDao orderChildDao;

    @Autowired
    private VehicleWarnInfoStruct vehicleWarnInfoStruct;

    @Autowired
    private TruckTraceService truckTraceService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSuspend(VehicleWarnInfoSuspendUpdateParam param) {
        UserSessionData loginUserInfo = TokenUtil.getLoginUserInfo();

        VehicleWarnInfo vehicleWarnInfo = vehicleWarnInfoDao.findById(param.getId()).orElseThrow(ResultEnum.DATA_NOT_FIND);
        vehicleWarnInfo.setStatus(VehicleWarnInfoEnum.Status.SUSPEND.getCode());
        vehicleWarnInfoDao.updateStatus(vehicleWarnInfo);

        VehicleWarnLog vehicleWarnLog = new VehicleWarnLog();
        vehicleWarnLog.setInfoId(vehicleWarnInfo.getId());
        vehicleWarnLog.setType(VehicleWarnLogEnum.Type.SUSPEND.getCode());
        vehicleWarnLog.setContent(VehicleWarnLogEnum.Type.SUSPEND.getMsg());
        vehicleWarnLog.setChildNo(vehicleWarnInfo.getChildNo());
        vehicleWarnLog.setWarnName(vehicleWarnInfo.getWarnName());
        vehicleWarnLog.setWarnRang(param.getWarnRang());
        vehicleWarnLog.setWarnEndTime(LocalDateTimeUtils.parseTime(param.getWarnEndTime()));
        vehicleWarnLog.setCreateBy(loginUserInfo.getUserName());

        vehicleWarnLogDao.saveEntity(vehicleWarnLog);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateResolve(VehicleWarnInfoResolveUpdateParam param) {
        UserSessionData loginUserInfo = TokenUtil.getLoginUserInfo();

        VehicleWarnInfo vehicleWarnInfo = vehicleWarnInfoDao.findById(param.getId()).orElseThrow(ResultEnum.DATA_NOT_FIND);
        vehicleWarnInfo.setStatus(VehicleWarnInfoEnum.Status.RESOLVE.getCode());
        vehicleWarnInfoDao.updateStatus(vehicleWarnInfo);

        VehicleWarnLog vehicleWarnLog = new VehicleWarnLog();
        vehicleWarnLog.setInfoId(vehicleWarnInfo.getId());
        vehicleWarnLog.setType(VehicleWarnLogEnum.Type.RESOLVE.getCode());
        vehicleWarnLog.setContent(VehicleWarnLogEnum.Type.RESOLVE.getMsg());
        vehicleWarnLog.setChildNo(vehicleWarnInfo.getChildNo());
        vehicleWarnLog.setWarnName(vehicleWarnInfo.getWarnName());
        vehicleWarnLog.setCreateBy(loginUserInfo.getUserName());

        vehicleWarnLogDao.saveEntity(vehicleWarnLog);
    }

    @Override
    public VehicleTraceVO getTrace(Integer id) {
        VehicleWarnInfo vehicleWarnInfo = vehicleWarnInfoDao.findById(id).orElseThrow(ResultEnum.DATA_NOT_FIND);
        OrderChild orderChild = orderChildDao.getByChildNo(vehicleWarnInfo.getChildNo()).orElseThrow(ResultEnum.DATA_NOT_FIND);

        LocalDateTime beginTime = vehicleWarnInfo.getTakeTime();
        LocalDateTime endTime = vehicleWarnInfo.getTakeTime();
        if (orderChild.getArriveSendTime() != null) {endTime = orderChild.getArriveSendTime();}
        if (orderChild.getLoadTime() != null) {endTime = orderChild.getLoadTime();}
        if (orderChild.getArriveReceiveTime() != null) {endTime = orderChild.getArriveReceiveTime();}
        if (orderChild.getUnloadTime() != null) {endTime = orderChild.getUnloadTime();}
        if (orderChild.getConfirmTime() != null) {endTime = orderChild.getConfirmTime();}
        if (orderChild.getSettleTime() != null) {endTime = orderChild.getSettleTime();}
        if (orderChild.getFinishTime() != null) {endTime = orderChild.getFinishTime();}
        if (endTime.isAfter(beginTime.plusDays(3))) {endTime = beginTime.plusDays(3);}      //最多查询3天

        List<DriverTruckTraceVO> truckTraceList = truckTraceService.listTruckTraceByTime(vehicleWarnInfo.getTruckNo(), LocalDateTimeUtils.formatTime(beginTime), LocalDateTimeUtils.formatTime(endTime));
        List<DriverTraceVO> driverTraceList = truckTraceService.listDriverTraceByTime(vehicleWarnInfo.getDriverUserNo(), LocalDateTimeUtils.formatTime(beginTime), LocalDateTimeUtils.formatTime(endTime));

        VehicleTraceVO result = new VehicleTraceVO();
        result.setDriverTraceList(driverTraceList);
        result.setTruckTraceList(truckTraceList);

        return result;
    }

    @Override
    public VehicleWarnInfoVO getInfo(Integer id) {
        return vehicleWarnInfoStruct.convert(vehicleWarnInfoDao.findById(id).orNull());

    }

    @Override
    public IPage<VehicleWarnInfoVO> pageInfo(PageVehicleWarnInfoParam param) {
        return vehicleWarnInfoStruct.convertPage(vehicleWarnInfoDao.pageByParam(param));

    }
}
