package com.njzscloud.supervisory.order.service;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.core.ex.Exceptions;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.supervisory.biz.pojo.entity.BizTruckEntity;
import com.njzscloud.supervisory.biz.service.BizTruckService;
import com.njzscloud.supervisory.order.contant.OrderCategory;
import com.njzscloud.supervisory.order.mapper.OrderCarInOutMapper;
import com.njzscloud.supervisory.order.pojo.entity.OrderCarInOutEntity;
import com.njzscloud.supervisory.order.pojo.param.TruckComingOrderParam;
import com.njzscloud.supervisory.order.pojo.param.TruckLeavingOrderParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 订单车辆进出场信息
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OrderCarInOutService extends ServiceImpl<OrderCarInOutMapper, OrderCarInOutEntity> implements IService<OrderCarInOutEntity> {
    private final BizTruckService bizTruckService;

    /**
     * 新增
     */
    public void add(OrderCarInOutEntity orderCarInOutEntity) {
        this.save(orderCarInOutEntity);
    }

    /**
     * 修改
     */
    public void modify(OrderCarInOutEntity orderCarInOutEntity) {
        this.updateById(orderCarInOutEntity);
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 详情
     */
    public OrderCarInOutEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     */
    public PageResult<OrderCarInOutEntity> paging(PageParam pageParam, OrderCarInOutEntity orderCarInOutEntity) {
        return PageResult.of(this.page(pageParam.toPage(), Wrappers.<OrderCarInOutEntity>query(orderCarInOutEntity)));
    }

    /**
     * 车辆进场
     */
    @Transactional(rollbackFor = Exception.class)
    public long truckComing(TruckComingOrderParam truckComingOrderParam, OrderCategory orderCategory) {
        Integer weight = truckComingOrderParam.getWeight();
        Assert.isTrue(weight != null && weight > 0, () -> Exceptions.clierr("磅重错误"));
        Integer roughWeight = null;
        Integer tareWeight = null;
        if (orderCategory == OrderCategory.DuanBoChu) {
            tareWeight = truckComingOrderParam.getWeight();
        } else {
            roughWeight = truckComingOrderParam.getWeight();
        }
        OrderCarInOutEntity entity = new OrderCarInOutEntity()
                .setRoughWeight(roughWeight)
                .setTareWeight(tareWeight)
                .setInFrontPhoto(truckComingOrderParam.getFrontPhoto())
                .setInBodyPhoto(truckComingOrderParam.getBodyPhoto())
                .setInTime(LocalDateTime.now());
        this.save(entity);
        return entity.getId();
    }

    /**
     * 车辆出场
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer truckOut(Long carInOutId, TruckLeavingOrderParam truckLeavingOrderParam, OrderCategory orderCategory, Long truckId) {
        Integer weight = truckLeavingOrderParam.getWeight();
        Assert.isTrue(weight != null && weight > 0, () -> Exceptions.clierr("磅重错误"));

        OrderCarInOutEntity orderCarInOutEntity = this.getById(carInOutId);
        Integer roughWeight = null;
        Integer tareWeight = null;
        Integer settleWeight = null;
        /*Integer roughWeight__ = orderCarInOutEntity.getRoughWeight();
         if (roughWeight__ == null) {
            BizTruckEntity bizTruckEntity = bizTruckService.getById(truckId);
            Integer truckTareWeight = bizTruckEntity.getTareWeight();
            Assert.notNull(truckTareWeight, () -> Exceptions.exception("{} 车辆暂无皮重", bizTruckEntity.getLicensePlate()));
            orderCarInOutEntity.setRoughWeight(truckTareWeight);
            roughWeight__ = truckTareWeight;
        } */
        if (orderCategory == OrderCategory.DuanBoChu) {
            roughWeight = weight;
            settleWeight = roughWeight - orderCarInOutEntity.getTareWeight();
        } else {
            tareWeight = weight;
            settleWeight = orderCarInOutEntity.getRoughWeight() - tareWeight;
        }
        Integer roughWeight_ = roughWeight;
        Integer tareWeight_ = tareWeight;
        Assert.isTrue(settleWeight >= 0, () -> Exceptions.exception("磅重错误，毛重：{}、皮重：{}", roughWeight_, tareWeight_));
        bizTruckService.updateById(new BizTruckEntity()
                .setId(truckId)
                .setTareWeight(settleWeight));
        this.updateById(new OrderCarInOutEntity()
                .setId(carInOutId)
                .setRoughWeight(roughWeight)
                .setTareWeight(tareWeight)
                .setOutFrontPhoto(truckLeavingOrderParam.getFrontPhoto())
                .setOutBodyPhoto(truckLeavingOrderParam.getBodyPhoto())
                .setSettleWeight(settleWeight)
                .setOutTime(LocalDateTime.now())
        );
        return settleWeight;
    }
}
