package com.ruoyi.warehouseBusiness.component.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.reportManagement.domain.TWhgenleg;
import com.ruoyi.reportManagement.mapper.TWhgenlegMapper;
import com.ruoyi.warehouseBusiness.component.QueryWhgenlegService;
import com.ruoyi.warehouseBusiness.domain.TWarehouseBills;
import com.ruoyi.warehouseBusiness.domain.TWarehousebillsitems;
import com.ruoyi.warehouseBusiness.domain.enums.WarehouseTypeEnum;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author caifc
 * @date 2021-11-11 9:15
 */
@Slf4j
@Component
@AllArgsConstructor
public class QueryWhgenlegServiceImpl implements QueryWhgenlegService {

    private final TWhgenlegMapper tWhgenlegMapper;

    private static final String FIRST = "one";

    private static final String SECOND = "two";

    /**
     * 仓库查询库存总帐
     *
     * @param warehouseBills 主表信息
     * @param item           明细表信息
     * @param ifCntrno       是否判断箱号
     * @param operate        操作（确认、撤回）
     * @return 库存总帐
     */
    @Override
    public TWhgenleg stockWhgenleg(TWarehouseBills warehouseBills, TWarehousebillsitems item, String ifCntrno, String operate) {
        TWhgenleg whgenleg = new TWhgenleg();
        // 提单号
        whgenleg.setfMblno(item.getfMblno());
        // 业务类型
        whgenleg.setfBusinessType(item.getfBusinessType());
        // 是否判断箱号
        if (WarehouseTypeEnum.JUDGE_BOX.getType().equals(ifCntrno)) {
            whgenleg.setfCntrno(item.getfCntrno());
        }
        // 业务类型详情
        whgenleg.setfMarks(item.getfMarks());
        // 货物品名
        whgenleg.setfGoodsid(item.getfGoodsid());
        if (WarehouseTypeEnum.SJRK.getType().equals(warehouseBills.getfBilltype())) {
            inStock(whgenleg, warehouseBills, item);
        }
        if (WarehouseTypeEnum.SJCK.getType().equals(warehouseBills.getfBilltype())) {
            outStock(whgenleg, warehouseBills, item);
        }
        if (WarehouseTypeEnum.HQZY.getType().equals(warehouseBills.getfBilltype())) {
            goodsTransfer(whgenleg, warehouseBills, item, operate, FIRST);
        }
        if (WarehouseTypeEnum.CKDB.getType().equals(warehouseBills.getfBilltype())) {
            stockTransfer(whgenleg, warehouseBills, item, operate, FIRST);
        }
        if (WarehouseTypeEnum.HWTG.getType().equals(warehouseBills.getfBilltype())) {
            cargoClearance(whgenleg, warehouseBills, item, operate, FIRST);
        }
        return tWhgenlegMapper.selectTWhgenleg(whgenleg);
    }

    /**
     * 被货转查询库存总帐
     *
     * @param warehouseBills 主表信息
     * @param item           明细表信息
     * @param ifCntrno       是否判断箱号
     * @param operate        操作（确认、撤回）
     * @return 库存总帐
     */
    @Override
    public TWhgenleg secondStockWhgenleg(TWarehouseBills warehouseBills, TWarehousebillsitems item, String ifCntrno, String operate) {
        TWhgenleg whgenleg = new TWhgenleg();
        // 提单号
        whgenleg.setfMblno(item.getfMblno());
        // 业务类型
        whgenleg.setfBusinessType(item.getfBusinessType());
        // 是否判断箱号
        if (WarehouseTypeEnum.JUDGE_BOX.getType().equals(ifCntrno)) {
            whgenleg.setfCntrno(item.getfCntrno());
        }
        // 货物品名
        whgenleg.setfGoodsid(item.getfGoodsid());
        if (WarehouseTypeEnum.HQZY.getType().equals(warehouseBills.getfBilltype())) {
            goodsTransfer(whgenleg, warehouseBills, item, operate, SECOND);
        }
        if (WarehouseTypeEnum.CKDB.getType().equals(warehouseBills.getfBilltype())) {
            stockTransfer(whgenleg, warehouseBills, item, operate, SECOND);
        }
        if (WarehouseTypeEnum.HWTG.getType().equals(warehouseBills.getfBilltype())) {
            cargoClearance(whgenleg, warehouseBills, item, operate, SECOND);
        }

        TWhgenleg tWhgenleg = tWhgenlegMapper.selectTWhgenleg(whgenleg);

        return tWhgenleg;
    }


    /**
     * 入库查询库存总帐
     *
     * @param whgenleg       查询条件
     * @param warehouseBills 主表信息
     * @param item           明细表信息
     * @return 库存总帐
     */
    private void inStock(TWhgenleg whgenleg, TWarehouseBills warehouseBills, TWarehousebillsitems item) {
        // 客户
        whgenleg.setfCorpid(warehouseBills.getfCorpid());
        // 贸易方式
        whgenleg.setfTrademodeid(warehouseBills.getfTrademodeid());
        // 业务日期
        whgenleg.setfBsdate(DateUtils.dateConversionMin(item.getfOriginalbilldate()));
        // 库区
        whgenleg.setfWarehouseLocationid(item.getfWarehouselocid());
    }

    /**
     * 出库查询库存总帐
     *
     * @param whgenleg       查询条件
     * @param warehouseBills 主表信息
     * @param item           明细表信息
     * @return 库存总帐
     */
    private void outStock(TWhgenleg whgenleg, TWarehouseBills warehouseBills, TWarehousebillsitems item) {
        // 客户
        whgenleg.setfCorpid(warehouseBills.getfCorpid());
        // 贸易方式
        whgenleg.setfTrademodeid(warehouseBills.getfTrademodeid());
        // 业务日期
        whgenleg.setfBsdate(DateUtils.dateConversionMin(item.getfSrcBsdate()));
        // 库区
        whgenleg.setfWarehouseLocationid(item.getfWarehouselocid());
    }

    /**
     * 货转查询库存总帐
     *
     * @param whgenleg       查询条件
     * @param warehouseBills 主表信息
     * @param item           明细表信息
     * @param operate        操作（确认、撤回）
     * @return 库存总帐
     */
    private void goodsTransfer(TWhgenleg whgenleg, TWarehouseBills warehouseBills, TWarehousebillsitems item, String operate, String frequency) {
        // 客户
        if (operate.equals(WarehouseTypeEnum.CONFIRM_OPERATION.getType()) && !frequency.equals(FIRST)) {
            whgenleg.setfCorpid(warehouseBills.getfTocorpid());
        } else {
            whgenleg.setfCorpid(warehouseBills.getfCorpid());
        }
        // 贸易方式
        whgenleg.setfTrademodeid(warehouseBills.getfTrademodeid());
        // 业务日期
        if (frequency.equals(FIRST)) {
            whgenleg.setfBsdate(DateUtils.dateConversionMin(item.getfSrcBsdate()));
        } else {
            whgenleg.setfBsdate(DateUtils.dateConversionMin(item.getfBsdate()));
        }
        // 库区
        whgenleg.setfWarehouseLocationid(item.getfWarehouselocid());
    }

    /**
     * 调拨查询库存总帐
     *
     * @param whgenleg       查询条件
     * @param warehouseBills 主表信息
     * @param item           明细表信息
     * @param operate        操作（确认、撤回）
     * @return 库存总帐
     */
    private void stockTransfer(TWhgenleg whgenleg, TWarehouseBills warehouseBills, TWarehousebillsitems item, String operate, String frequency) {
        // 客户
        whgenleg.setfCorpid(warehouseBills.getfCorpid());
        // 贸易方式
        whgenleg.setfTrademodeid(warehouseBills.getfTrademodeid());
        // 提单号
        whgenleg.setfMblno(item.getfMblno());
        // 业务日期
        if (frequency.equals(FIRST)) {
            whgenleg.setfBsdate(DateUtils.dateConversionMin(item.getfSrcBsdate()));
        } else {
            whgenleg.setfBsdate(DateUtils.dateConversionMin(item.getfBsdate()));
        }
        // 库区
        if (operate.equals(WarehouseTypeEnum.CONFIRM_OPERATION.getType()) && !frequency.equals(FIRST)) {
            whgenleg.setfWarehouseLocationid(item.getfTransferWarehouselocid());
        } else {
            whgenleg.setfWarehouseLocationid(item.getfWarehouselocid());
        }
    }

    /**
     * 货物通关查询库存总帐
     *
     * @param whgenleg       查询条件
     * @param warehouseBills 主表信息
     * @param item           明细表信息
     * @param operate        操作（确认、撤回）
     * @return 库存总帐
     */
    private void cargoClearance(TWhgenleg whgenleg, TWarehouseBills warehouseBills, TWarehousebillsitems item, String operate, String frequency) {
        // 客户
        whgenleg.setfCorpid(warehouseBills.getfCorpid());
        // 贸易方式
        if (operate.equals(WarehouseTypeEnum.CONFIRM_OPERATION.getType()) && !frequency.equals(FIRST)) {
            whgenleg.setfTrademodeid(warehouseBills.getfNewTrademodeid());
        } else {
            whgenleg.setfTrademodeid(warehouseBills.getfTrademodeid());
        }
        // 提单号
        whgenleg.setfMblno(item.getfMblno());
        // 业务日期
        if (frequency.equals(FIRST)) {
            whgenleg.setfBsdate(DateUtils.dateConversionMin(item.getfSrcBsdate()));
        } else {
            whgenleg.setfBsdate(DateUtils.dateConversionMin(item.getfBsdate()));
        }
        // 库区
        if (operate.equals(WarehouseTypeEnum.CONFIRM_OPERATION.getType()) && !frequency.equals(FIRST)) {
            whgenleg.setfWarehouseLocationid(item.getfTransferWarehouselocid());
        } else {
            whgenleg.setfWarehouseLocationid(item.getfWarehouselocid());
        }
    }


}
