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.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) {
        // 校验仓储信息
        filterWarehouseBills(warehousebillsDTO);
        // 校验明细
        List<TWarehousebillsitems> itemList = warehousebillsDTO.getWarehousebillsitemsList();
        if (CollectionUtils.isNotEmpty(itemList)) {
            filterItem(itemList);
        }
    }

    /**
     * 校验仓储主表信息
     *
     * @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 (warehousebillsDTO.getfBilltype().equals(WarehouseTypeEnum.SJRK.getType())) {
            if (StringUtils.isEmpty(warehousebillsDTO.getfChargetype())) {
                throw new WarehouseException("请维护仓储费方式");
            }
            if (StringUtils.isNull(warehousebillsDTO.getfGoodsid())) {
                throw new WarehouseException("请维护商品信息");
            }
            if (StringUtils.isNull(warehousebillsDTO.getfPlanqty()) ||
                    Objects.equals(warehousebillsDTO.getfPlanqty(), 0L)) {
                throw new WarehouseException("请维护计划件数");
            }
            if (StringUtils.isNull(warehousebillsDTO.getfPlangrossweight()) ||
                    warehousebillsDTO.getfPlangrossweight().compareTo(BigDecimal.ZERO) == 0) {
                throw new WarehouseException("请维护计划毛重");
            }
            if (StringUtils.isNull(warehousebillsDTO.getfPlannetweight()) ||
                    warehousebillsDTO.getfPlannetweight().compareTo(BigDecimal.ZERO) == 0) {
                throw new WarehouseException("请维护计划净重");
            }
            if (StringUtils.isEmpty(warehousebillsDTO.getfBusinessType())) {
                throw new WarehouseException("请维护作业类型");
            }
        }
        // 出库校验
        if (warehousebillsDTO.getfBilltype().equals(WarehouseTypeEnum.SJCK.getType())) {
            if (StringUtils.isEmpty(warehousebillsDTO.getfMblno())) {
                throw new WarehouseException("请维护提单号");
            }
            if (StringUtils.isEmpty(warehousebillsDTO.getfFeetunit())) {
                throw new WarehouseException("请维护计费单位");
            }
        }
    }

    /**
     * 校验明细信息
     *
     * @param itemList 明细信息
     */
    private void filterItem(List<TWarehousebillsitems> itemList) {
        int line = 1;
        for (TWarehousebillsitems li : itemList) {
            if (StringUtils.isNull(li.getfGoodsid())) {
                throw new WarehouseException("序号" + line + "明细仓储明细未选择商品");
            }
            if (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.isEmpty(li.getfCntrno())) {
                throw new WarehouseException("序号" + line + "明细仓储明细未选择箱号");
            }
            if (StringUtils.isNull(li.getfQty()) || Objects.equals(li.getfQty(), 0L)) {
                throw new WarehouseException("序号" + line + "明细仓储明细未选填入数量");
            }
            line++;
        }
    }

}
