package com.ruoyi.warehouseBusiness.component.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.warehouseBusiness.component.CalculationWarehouseService;
import com.ruoyi.warehouseBusiness.domain.TWarehouseBills;
import com.ruoyi.warehouseBusiness.domain.TWarehousebillsfees;
import com.ruoyi.warehouseBusiness.domain.TWarehousebillsitems;
import com.ruoyi.warehouseBusiness.domain.enums.WarehouseTypeEnum;
import com.ruoyi.warehouseBusiness.mapper.TWarehousebillsfeesMapper;
import com.ruoyi.warehouseBusiness.mapper.TWarehousebillsitemsMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @author caifc
 * @date 2021-11-23 18:52
 */
@Slf4j
@Component
@AllArgsConstructor
public class CalculationWarehouseServiceImpl implements CalculationWarehouseService {

    private final TWarehousebillsfeesMapper warehousebillsfeesMapper;

    private final TWarehousebillsitemsMapper warehousebillsitemsMapper;


    /**
     * 仓库查询库存总帐
     *
     * @param warehouseBills 主表数据
     * @return 状态
     */
    @Override
    public void calculationBillStatus(TWarehouseBills warehouseBills) {

        List<TWarehousebillsitems> warehousebillsitems = warehousebillsitemsMapper.selectItemsStatusByPid(warehouseBills.getfId());
        // 如果没有明细则是未xxx
        if (CollectionUtils.isEmpty(warehousebillsitems)) {
            warehouseBills.setfItemsStatus(1L);
        } else {
            // 代表有明细数据
            long count;
            // 判断是否货权转移
            if (WarehouseTypeEnum.HQZY.getType().equals(warehouseBills.getfBilltype())) {
                count = warehousebillsitems.stream().filter(item ->
                        !Objects.equals(item.getfBillstatus(), 6L)
                ).count();
            } else {
                count = warehousebillsitems.stream().filter(item ->
                                Objects.equals(item.getfBillstatus(), 40L))
                        .count();
            }
            // 如果明细中不存在已经操作库存总帐的则判定xx中
            if (Objects.equals((long) warehousebillsitems.size(), count)) {
                warehouseBills.setfItemsStatus(4L);
            } else {
                // 最终判定 已经xxx
                warehouseBills.setfItemsStatus(2L);
            }
        }
        judgeFeeStatus(warehouseBills);
    }

    /**
     * 仓库计算明细件数、毛重、净重总和
     *
     * @param warehouseBills 仓库数据
     */
    @Override
    public void calculationWarehouseQuantity(TWarehouseBills warehouseBills) {
        TWarehousebillsitems warehousebillsitems = new TWarehousebillsitems();
        warehousebillsitems.setfPid(warehouseBills.getfId());
        List<TWarehousebillsitems> warehousebillsitemsList = warehousebillsitemsMapper.selectTWarehousebillsitemsList(warehousebillsitems);
        // 存在明细数据
        if (CollectionUtils.isNotEmpty(warehousebillsitemsList)) {
            Long qty = 0L;
            BigDecimal netweight = BigDecimal.ZERO;
            BigDecimal grossweight = BigDecimal.ZERO;
            Long planQty = 0L;
            BigDecimal planNetweight = BigDecimal.ZERO;
            BigDecimal planGrossweight = BigDecimal.ZERO;
            for (TWarehousebillsitems li : warehousebillsitemsList) {
                qty += li.getfQty();
                planQty += li.getfPlanqty();
                netweight = netweight.add(li.getfNetweight());
                planNetweight = planNetweight.add(li.getfPlannetweight());
                planGrossweight = planGrossweight.add(li.getfPlangrossweight());

            }
            warehouseBills.setfPlanqty(planQty);
            warehouseBills.setfPlannetweight(planNetweight);
            warehouseBills.setfPlangrossweight(planGrossweight);
            warehouseBills.setfQty(qty);
            warehouseBills.setfNetweight(netweight);
            warehouseBills.setfGrossweight(grossweight);
        }
    }

    /**
     * 仓库计算费用状态
     *
     * @param warehouseBills 主表数据
     */
    @Override
    public void calculationFeeStatus(TWarehouseBills warehouseBills) {
        judgeFeeStatus(warehouseBills);
    }

    /**
     * 判断修改费用状态
     *
     * @param warehouseBills 仓库主表
     */
    private void judgeFeeStatus(TWarehouseBills warehouseBills) {
        TWarehousebillsfees fees = new TWarehousebillsfees();
        fees.setfPid(warehouseBills.getfId());
        List<TWarehousebillsfees> warehousebillsfees = warehousebillsfeesMapper.selectTWarehousebillsfeesList(fees);
        if (CollectionUtils.isNotEmpty(warehousebillsfees)) {
            long count = warehousebillsfees.stream().filter(fee -> Objects.equals(fee.getfBillstatus(), 6L)).count();
            long size =  warehousebillsfees.size();
            if (Objects.equals(count, size)) {
                // 入账数和明细数相等，全部入账
                warehouseBills.setfFeeStatus(3L);
            } else if (count > 0 && count < size) {
                // 入账数 < 明细数，部分入账
                warehouseBills.setfFeeStatus(2L);
            } else {
                // 暂存
                warehouseBills.setfFeeStatus(1L);
            }
        } else {
            // 空，不显示状态
            warehouseBills.setfFeeStatus(0L);
        }
    }

}
