package com.ruoyi.warehouseBusiness.component.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.exception.WarehouseException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.warehouseBusiness.component.WarehouseBillsfilterService;
import com.ruoyi.warehouseBusiness.domain.TWarehousebillsfees;
import com.ruoyi.warehouseBusiness.domain.TWarehousebillsitems;
import com.ruoyi.warehouseBusiness.domain.dto.WarehousebillsDTO;
import com.ruoyi.warehouseBusiness.domain.enums.WarehouseTypeEnum;
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-15 9:55
 */
@Slf4j
@Component
@AllArgsConstructor
public class WarehouseBillsfilterServiceImpl implements WarehouseBillsfilterService {


    @Override
    public void filter(WarehousebillsDTO warehousebillsDTO, String criteria) {
        // 校验仓储信息
        filterWarehouseBills(warehousebillsDTO);
        // 校验明细
        if (CollectionUtils.isNotEmpty(warehousebillsDTO.getWarehousebillsitemsList())) {
            filterItem(warehousebillsDTO.getWarehousebillsitemsList(), criteria);
        }
        // 校验费用明细
        if (CollectionUtils.isNotEmpty(warehousebillsDTO.getWarehousebillsfeesList())) {
            filterFees(warehousebillsDTO.getWarehousebillsfeesList());
        }
    }

    /**
     * 校验仓储主表信息
     *
     * @param warehousebillsDTO 主表信息
     */
    private void filterWarehouseBills(WarehousebillsDTO warehousebillsDTO) {
        if (StringUtils.isNull(warehousebillsDTO.getfCorpid())) {
            throw new WarehouseException("请维护客户信息");
        }
        if (StringUtils.isNull(warehousebillsDTO.getfBsdate())) {
            throw new WarehouseException("请维护计划日期");
        }
        if (StringUtils.isNull(warehousebillsDTO.getfWarehouseid())) {
            throw new WarehouseException("请维护仓库信息");
        }
        if (StringUtils.isEmpty(warehousebillsDTO.getfStorekeeper())) {
            throw new WarehouseException("请维护仓库人");
        }
        if (StringUtils.isNull(warehousebillsDTO.getfTrademodeid())) {
            throw new WarehouseException("请维护贸易方式");
        }
        if (StringUtils.isEmpty(warehousebillsDTO.getfMblno())) {
            throw new WarehouseException("请维护提单号");
        }
        // 入库校验
        if (warehousebillsDTO.getfBilltype().equals(WarehouseTypeEnum.SJRK.getType())) {
            if (StringUtils.isEmpty(warehousebillsDTO.getfChargetype())) {
                throw new WarehouseException("请维护仓储费方式");
            }
            if (StringUtils.isNull(warehousebillsDTO.getfGoodsid())) {
                throw new WarehouseException("请维护商品信息");
            }
            if (StringUtils.isEmpty(warehousebillsDTO.getfBusinessType())) {
                throw new WarehouseException("请维护作业类型");
            }
        }
        // 出库校验
        if (warehousebillsDTO.getfBilltype().equals(WarehouseTypeEnum.SJCK.getType())) {

            if (StringUtils.isEmpty(warehousebillsDTO.getfFeetunit())) {
                throw new WarehouseException("请维护计费单位");
            }
        }
    }

    /**
     * 校验明细信息
     *
     * @param itemList 明细信息
     * @param criteria  判断条件
     */
    private void filterItem(List<TWarehousebillsitems> itemList, String criteria) {
        int line = 1;
        boolean status = criteria.equals(WarehouseTypeEnum.SAVE.getType());

        for (TWarehousebillsitems li : itemList) {
            if (StringUtils.isNull(li.getfGoodsid())) {
                throw new WarehouseException("序号" + line + "明细仓储明细未选择商品");
            }
            if (!status && StringUtils.isNull(li.getfWarehouselocid())) {
                throw new WarehouseException("序号" + line + "明细仓储明细未选择库区");
            }
            if (StringUtils.isNull(li.getfBsdate())) {
                throw new WarehouseException("序号" + line + "明细仓储明细未选择业务日期");
            }
            if (StringUtils.isNull(li.getfBusinessType())) {
                throw new WarehouseException("序号" + line + "明细仓储明细未选择货物属性");
            }
            if (StringUtils.isEmpty(li.getfMarks())) {
                throw new WarehouseException("序号" + line + "明细仓储明细未选择货物详情");
            }
            if (StringUtils.isNull(li.getfQty()) || Objects.equals(li.getfQty(), 0L)) {
                throw new WarehouseException("序号" + line + "明细仓储明细未选填入数量");
            }
            if (!status && (StringUtils.isNull(li.getfGrossweight()) || li.getfGrossweight().compareTo(BigDecimal.ZERO) <= 0)) {
                throw new WarehouseException("序号" + line + "明细仓储明细未选填入毛重");
            }
            if (!status && (StringUtils.isNull(li.getfNetweight()) || li.getfNetweight().compareTo(BigDecimal.ZERO) <= 0)) {
                throw new WarehouseException("序号" + line + "明细仓储明细未选填入净重");
            }
            line++;
        }
    }

    /**
     * 校验仓储费用信息
     *
     * @param warehousebillsfeesList 费用信息
     */
    private void filterFees(List<TWarehousebillsfees> warehousebillsfeesList) {
        int line = 1;
        for (TWarehousebillsfees li : warehousebillsfeesList) {
            if (StringUtils.isNull(li.getfCorpid())) {
                throw new WarehouseException("序号" + line + "未选择客户信息");
            }
            if (StringUtils.isNull(li.getfFeeid())) {
                throw new WarehouseException("序号" + line + "未选择费用信息");
            }
            if (StringUtils.isNull(li.getfFeeunitid())) {
                throw new WarehouseException("序号" + line + "未选择计价单位");
            }
            if (StringUtils.isNull(li.getfBusinessType())) {
                throw new WarehouseException("序号" + line + "未选择作业类型");
            }
            if (StringUtils.isNull(li.getfDc())) {
                throw new WarehouseException("序号" + line + "未选择收费、付费");
            }
            if (StringUtils.isNull(li.getfQty()) || li.getfQty().compareTo(BigDecimal.ZERO) < 0) {
                throw new WarehouseException("序号" + line + "请填写数量");
            }
            if (StringUtils.isNull(li.getfUnitprice()) || li.getfUnitprice().compareTo(BigDecimal.ZERO) < 0) {
                throw new WarehouseException("序号" + line + "请填写单价");
            }
            if (StringUtils.isNull(li.getfAmount()) || li.getfAmount().compareTo(BigDecimal.ZERO) < 0) {
                throw new WarehouseException("序号" + line + "请填写金额");
            }
        }
    }

}
