package com.bohuikeji.delivery.backstage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.delivery.backstage.entity.TLogistics;
import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.delivery.backstage.mapper.TLogisticsMapper;
import com.bohuikeji.delivery.backstage.service.ITLogisticsRecordService;
import com.bohuikeji.delivery.backstage.service.ITLogisticsService;
import com.bohuikeji.delivery.backstage.service.ITOrderService;
import com.bohuikeji.frame.module.admin.base.ServiceImplBase;
import com.bohuikeji.frame.module.admin.utils.Constants;
import com.bohuikeji.frame.module.admin.utils.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 物流信息表 服务实现类
 * </p>
 *
 * @author 10552
 * @since 2019-09-10
 */
@Service
public class TLogisticsServiceImpl extends ServiceImplBase<TLogisticsMapper, TLogistics> implements ITLogisticsService {

    @Autowired
    @Lazy
    ITOrderService itOrderService;
    @Autowired
    ITLogisticsService itLogisticsService;
    @Autowired
    ITLogisticsRecordService logisticsRecordService;

    /**
     * 添加物流信息表，也添加与之对应的物流流转记录表
     * @param tLogistics
     */
    @Override
    public void addTLogistics(TLogistics tLogistics) {
        tLogistics.setLogisticsStatus(Constants.WAITING_FOR_DELIVERY);
        tLogistics.setCreateTime(LocalDateTime.now());
        if(itLogisticsService.save(tLogistics)){
            logisticsRecordService.createRecord(tLogistics);
        }
    }


    @Transactional
    @Override
    public void updateOrderAndTLogistics(List<TLogistics> logisticsList, String orderStatus) {
        if(logisticsList==null||logisticsList.size()<1){
            return;
        }
        logisticsList.forEach(logistics->{
            logistics.setLogisticsStatus(orderStatus);
            logisticsRecordService.createRecord(logistics);
        });
        super.updateBatchById(logisticsList);
    }

    @Transactional
    @Override
    public boolean createLogistics(TOrder order) {
        TLogistics logistics=new TLogistics();
        logistics.setLogisticsNum(order.getOrderNum()+"-1");
        logistics.setOrderId(order.getOrderId());
        logistics.setOrderNum(order.getOrderNum());
        logistics.setPlayerId(order.getPlayerId());
        logistics.setCourierId(order.getCourierId());
        logistics.setCourierRemakrs("系统支付成功自动取件");
        logistics.setLogisticsStatus(order.getOrderStatus());
        if(super.save(logistics)){
            logisticsRecordService.createRecord(logistics);
            return true;
        }
        return false;

    }

    @Override
    public void saveTLogistics(Integer orderId, Integer courierId, String cargoPhotos) {
        TLogistics logistics = itLogisticsService.getOne(new QueryWrapper<TLogistics>().eq("order_id",orderId));
        if(logistics != null){
            logistics.setLogisticsStatus(Constants.WAITING_FOR_DELIVERY);
            logistics.setCreateTime(LocalDateTime.now());
            logistics.setCargoPhotos(cargoPhotos);
            itLogisticsService.updateById(logistics);
            logisticsRecordService.createRecord(logistics);
            return;
        }else {
            logistics = new TLogistics();
            TOrder tOrder = itOrderService.getById(orderId);
            logistics.setCargoPhotos(cargoPhotos);
            logistics.setOrderId(tOrder.getOrderId());
            logistics.setOrderNum(tOrder.getOrderNum());
            logistics.setPlayerId(tOrder.getPlayerId());
            logistics.setCourierId(courierId);
            logistics.setVehicleId(tOrder.getVehicleConfigId());
            logistics.setLogisticsNum(NumberUtils.get("L"));
            this.addTLogistics(logistics);
        }
    }
}
