package com.pj.project.tb_logistics;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.pj.api.dto.TbLogisticsDto;
import com.pj.common.core.utils.StringUtils;
import com.pj.current.config.SystemObject;
import com.pj.current.dto.APPLoginUserInfo;
import com.pj.current.satoken.StpAPPUserUtil;
import com.pj.enummj.DeleteStatus;
import com.pj.enummj.OrderStatus;
import com.pj.utils.so.SoMap;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service: tb_logistics -- 物流表
 * @author qzy
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TbLogisticsService extends ServiceImpl<TbLogisticsMapper, TbLogistics> implements IService<TbLogistics>{

	/** 底层 Mapper 对象 */
	@Autowired
	TbLogisticsMapper tbLogisticsMapper;

	/** 增 */
	void add(TbLogistics t){
		save(t);
	}

	/** 删 */
	void delete(Long id){
		removeById(id);
	}

	/** 改 */
	void update(TbLogistics t){
		updateById(t);

	}

	/** 查 */
	TbLogistics getById(Long id){
		return super.getById(id);
	}

	/** 查集合 - 根据条件（参数为空时代表忽略指定条件） */
	List<TbLogistics> getList(SoMap so) {
		return tbLogisticsMapper.getList(so);
	}

	/**
	 * 通过订单ID表查询物流
	 */
	TbLogisticsDto selectByOrderId(Long orderId){
		List<TbLogistics> tbLogistics = tbLogisticsMapper.selectList(new LambdaQueryWrapper<TbLogistics>().eq(TbLogistics::getId, orderId).eq(TbLogistics::getDeleteStatus, DeleteStatus.DELETE_STATUS_ON.getCode()));
		if(tbLogistics.size() == 0)return null;
		if(tbLogistics.size() > 1)throw new RuntimeException("当前订单信息存在异常!");
		//创建返回值对象
		TbLogisticsDto tbLogisticsDto = new TbLogisticsDto();
		BeanUtils.copyProperties(tbLogistics.get(0),tbLogisticsDto);
		return tbLogisticsDto;
	}

	/** 增 二级市场订单选择车辆时触发,物流订单在此传入并绑定二级市场订单号*/
	boolean saveLogisticOrder(TbLogisticsDto logisticsDto) {
		if(null == logisticsDto) return false;

		//获取当前登陆人信息
		APPLoginUserInfo appLoginInfo = StpAPPUserUtil.getAPPLoginInfo();

		TbLogistics logistics = new TbLogistics();
		BeanUtils.copyProperties(logisticsDto, logistics);
		//订单编号:WLDD=物流订单
		logistics.setOrderNo(SystemObject.getREFcode("WLDD",new Date()));
		//订单待司机确认(0=待确认,1=订单已确认,2=订单确认失败)
		logistics.setPrepare(0);
		//待装货(0=待装货,1=已装货)
		logistics.setLoading(0);
		//发货(0=未发,1=已发)
		logistics.setDelivery(0);
		//已出海关(0=禁用,1=启用)
		logistics.setClearedCustom(0);
		//接货人确定接货(0=禁用,1=启用)
		logistics.setMakeCargo(0);
		//收购商确认(0=禁用,1=启用)
		logistics.setConfirmReceipt(0);
		//订单完成状态 (0=未完成,1=已完成)
		logistics.setOrderFinish(OrderStatus.ORDER_STATUS_ONE.getCode());
		//创建时间
		logistics.setCreateTime(new Date());
		//创建人编号
		logistics.setCreateBy(appLoginInfo.getLoginId());
		//创建人名称
		logistics.setCreateName(appLoginInfo.getLoginName());
		//删除状态
		logistics.setDeleteStatus(DeleteStatus.DELETE_STATUS_ON.getCode());

		return this.save(logistics);
	}

	/** 改 根据物流订单Id,修改物流状态*/
	boolean updateLogisticStatus(TbLogisticsDto logisticsDto) {
		if(null == logisticsDto) return false;

		TbLogistics logistics = new TbLogistics();
		BeanUtils.copyProperties(logisticsDto, logistics);

		LambdaUpdateWrapper<TbLogistics> wrapper = new LambdaUpdateWrapper<>();
		Date nowTime = new Date();

		//订单待司机确认(0=待确认,1=订单已确认,2=订单确认失败)
		wrapper.set(TbLogistics::getPrepare ,logisticsDto.getPrepare());
		//订单确认时间
		if(logisticsDto.getPrepare() != 0)
			wrapper.set(TbLogistics::getPrepareTime ,nowTime);

		//待装货(0=待装货,1=已装货)
		wrapper.set(TbLogistics::getLoading ,logisticsDto.getLoading());
		//已装货时间
		if(logisticsDto.getLoading() == 1)
			wrapper.set(TbLogistics::getLoadingTime ,nowTime);

		//发货(0=未发,1=已发)
		wrapper.set(TbLogistics::getDelivery ,logisticsDto.getDelivery());
		//发货时间
		if(logisticsDto.getDelivery() == 1)
			wrapper.set(TbLogistics::getDeliveryTime ,nowTime);

		//已出海关(0=禁用,1=启用)
		wrapper.set(TbLogistics::getClearedCustom ,logisticsDto.getClearedCustom());
		//出海关时间
		if(logisticsDto.getClearedCustom() == 1)
			wrapper.set(TbLogistics::getClearedCustomTime ,nowTime);

		//接货人确定接货(0=禁用,1=启用)
		wrapper.set(TbLogistics::getMakeCargo ,logisticsDto.getMakeCargo());
		//接货完成时间
		if(logisticsDto.getMakeCargo() == 1)
			wrapper.set(TbLogistics::getMakeCargoTime ,nowTime);

		//收购商确认(0=禁用,1=启用)
		wrapper.set(TbLogistics::getConfirmReceipt ,logisticsDto.getConfirmReceipt());
		//收货方确认时间(相当于订单完成时间)
		if(logisticsDto.getConfirmReceipt() == 1)
			wrapper.set(TbLogistics::getConfirmReceiptTime ,nowTime);

		//根据物流订单Id修改
		wrapper.eq(TbLogistics::getId ,logisticsDto.getId());


		return this.update(wrapper);
	}

	/** 查 通过司机Id查物流订单 */
	List<TbLogistics> selectByDriverId(SoMap soMap) {
		APPLoginUserInfo appLoginInfo = StpAPPUserUtil.getAPPLoginInfo();
		if(appLoginInfo == null || appLoginInfo.getLoginId() == null)
			throw new SecurityException("当前登录账号信息已失效,请检查!");
		//查询条件
		soMap.put("driverId",appLoginInfo.getFk());
		soMap.put("deleteStatus",DeleteStatus.DELETE_STATUS_ON.getCode());
		soMap.set("sortType",17);
		return tbLogisticsMapper.getList(soMap);
	}
}
