package com.hzs.scm.pub.provider;

import cn.hutool.core.bean.BeanUtil;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EApprovalBusiness;
import com.hzs.common.core.enums.EApproveStatus;
import com.hzs.common.core.enums.EScmOrderStatus;
import com.hzs.common.core.exception.base.BaseException;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import com.hzs.common.domain.scm.general.ext.IcGeneralInHExt;
import com.hzs.common.domain.scm.general.ext.IcGeneralOutHExt;
import com.hzs.common.domain.scm.ic.IcAvailableNum;
import com.hzs.common.domain.scm.ic.ext.IcAvailableNumExt;
import com.hzs.common.domain.scm.inventory.ext.IcInventoryHExt;
import com.hzs.common.domain.scm.io.ext.IoOrderExt;
import com.hzs.common.domain.scm.po.ext.PoOrderExt;
import com.hzs.common.domain.scm.pub.ScmOrderApproveLog;
import com.hzs.common.domain.scm.purchase.ext.IcPurchaseInHExt;
import com.hzs.common.domain.scm.sale.ext.IcSaleoutHExt;
import com.hzs.common.domain.scm.trans.ext.IcTransInHExt;
import com.hzs.common.domain.scm.trans.ext.IcTransOutHExt;
import com.hzs.common.domain.scm.trans.ext.TransferOrderExt;
import com.hzs.ground.product.service.IThProductService;
import com.hzs.scm.general.service.IIcGeneralInHService;
import com.hzs.scm.general.service.IIcGeneralOutHService;
import com.hzs.scm.ic.service.IIcAvailableNumService;
import com.hzs.scm.inventory.service.IIcInventoryHService;
import com.hzs.scm.io.service.IIoOrderService;
import com.hzs.scm.no.service.IScmBillNoService;
import com.hzs.scm.po.service.IPoOrderService;
import com.hzs.scm.pub.IScmBillServiceApi;
import com.hzs.scm.pub.dto.BillDTO;
import com.hzs.scm.pub.dto.BillItemDTO;
import com.hzs.scm.pub.service.IScmOrderApproveLogService;
import com.hzs.scm.purchase.service.IIcPurchaseInHService;
import com.hzs.scm.sale.service.IIcSaleoutHService;
import com.hzs.scm.trans.service.IIcTransInHService;
import com.hzs.scm.trans.service.IIcTransOutHService;
import com.hzs.scm.trans.service.ITransferOrderService;
import com.hzs.system.sys.dto.ApprovalBusinessResultDTO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 供应链单据对外接口发布
 * @author: sui q
 * @time: 2023/11/25 09:41
 * @classname: ScmBillServiceProvider
 * @package_name: com.hzs.scm.pub.provider
 * version 1.0.0
 */
@DubboService
public class ScmBillServiceProvider implements IScmBillServiceApi {

    @Autowired
    private IScmOrderApproveLogService scmOrderApproveLogService;

    @Autowired
    private IPoOrderService poOrderService;

    @Autowired
    private IIcPurchaseInHService purchaseInhService;

    @Autowired
    private IIcGeneralInHService generalInhService;
    @Autowired
    private IThProductService productService;
    @Autowired
    private IIcGeneralOutHService generalOutService;

    @Autowired
    private ITransferOrderService transferOrderService;

    @Autowired
    private IIcTransOutHService transOutService;

    @Autowired
    private IIcTransInHService transInService;

    @Autowired
    private IIcAvailableNumService availableNumService;

    @Autowired
    private IIcInventoryHService inventoryService;

    @Autowired
    private IIcSaleoutHService saleOutService;

    @Autowired
    private IIoOrderService ioOrderService;

    @Autowired
    private IScmBillNoService scmBillNoService;

    @Override
    public R<Boolean> approvalCallback(ApprovalBusinessResultDTO approvalBusinessResultDTO) {
        try {
            // 返回内容
            String str = null;
            if (EApproveStatus.FINISH.getValue() == approvalBusinessResultDTO.getEApproveStatus().getValue()) {
                // 通过处理
                switch (approvalBusinessResultDTO.getEApprovalBusiness()) {
                    case PO_ORDER_APPROVE:
                        // 采购订单
                        str = poOrderService.auditPoOrderByPoOrderAgree(approvalBusinessResultDTO);
                        break;
                    case IC_PURCHASE_APPROVE:
                        // 采购入库
                        str = purchaseInhService.auditPurchaseByPoOrderAgree(approvalBusinessResultDTO);
                        break;
                    case IC_GENERAL_IN_APPROVE:
                        // 其他入库
                        str = generalInhService.auditGeneralInByApproveParamAgree(approvalBusinessResultDTO);
                        break;
                    case IC_GENERAL_OUT_APPROVE:
                        // 其他出库
                        str = generalOutService.auditGeneralOutByApproveParamAgree(approvalBusinessResultDTO);
                        break;
                    case TRANSFER_ORDER_APPROVE:
                        // 调拨订单
                        str = transferOrderService.auditTransferOrderTransferOrderAgree(approvalBusinessResultDTO);
                        break;
                    case TRANSFER_OUT_APPROVE:
                        // 调拨出库
                        str = transOutService.auditTransferOutByApproveParamAgree(approvalBusinessResultDTO);
                        break;
                    case TRANSFER_IN_APPROVE:
                        // 调拨入库
                        str = transInService.auditTransferInByPoOrderAgree(approvalBusinessResultDTO);
                        break;
                    case INVENTORY_ORDER_APPROVE:
                        // 库存盘点单
                        str = inventoryService.auditInventoryByApproveParamAgree(approvalBusinessResultDTO);
                        break;
                    case BACK_SALE_OUT_APPROVE:
                        // 销售退货单
                        str = saleOutService.auditBackSaleOutAgree(approvalBusinessResultDTO);
                        break;
                    case BACK_PURCHASE_APPROVE:
                        // 采购退货单
                        str = purchaseInhService.auditPurchaseOutByPoOrderAgree(approvalBusinessResultDTO);
                        break;
                    case IO_ORDER_APPROVE:
                        // 业务领用订单
                        str = ioOrderService.auditIoOrderByIoOrderAgree(approvalBusinessResultDTO);
                        break;
                }
            } else {
                // 驳回处理
                switch (approvalBusinessResultDTO.getEApprovalBusiness()) {
                    case PO_ORDER_APPROVE:
                        // 采购订单
                        str = poOrderService.auditPoOrderByPoOrderReject(approvalBusinessResultDTO);
                        break;
                    case IC_PURCHASE_APPROVE:
                        // 采购入库
                        str = purchaseInhService.auditPurchaseByPoOrderReject(approvalBusinessResultDTO);
                        break;
                    case IC_GENERAL_IN_APPROVE:
                        // 其他入库
                        str = generalInhService.auditGeneralInByApproveParamReject(approvalBusinessResultDTO);
                        break;
                    case IC_GENERAL_OUT_APPROVE:
                        // 其他出库
                        str = generalOutService.auditGeneralOutByApproveParamReject(approvalBusinessResultDTO);
                        break;
                    case TRANSFER_ORDER_APPROVE:
                        // 调拨订单
                        str = transferOrderService.auditTransferOrderByTransferOrderReject(approvalBusinessResultDTO);
                        break;
                    case TRANSFER_OUT_APPROVE:
                        // 调拨出库
                        str = transOutService.auditTransferOutByApproveParamReject(approvalBusinessResultDTO);
                        break;
                    case TRANSFER_IN_APPROVE:
                        // 调拨入库
                        str = transInService.auditTransferInByPoOrderReject(approvalBusinessResultDTO);
                        break;
                    case INVENTORY_ORDER_APPROVE:
                        // 库存盘点单
                        str = inventoryService.auditInventoryByApproveParamReject(approvalBusinessResultDTO);
                        break;
                    case BACK_SALE_OUT_APPROVE:
                        // 销售退货单
                        str = saleOutService.auditBackSaleOutReject(approvalBusinessResultDTO);
                        break;
                    case BACK_PURCHASE_APPROVE:
                        // 采购退货单
                        str = purchaseInhService.auditPurchaseOutByPoOrderReject(approvalBusinessResultDTO);
                        break;
                    case IO_ORDER_APPROVE:
                        // 业务领用订单
                        str = ioOrderService.auditIoOrderByIoOrderReject(approvalBusinessResultDTO);
                        break;
                }
            }
            if (StringUtils.isNotEmpty(str)) {
                // 返回有信息，说明有问题，抛出异常
                throw new BaseException(str);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.ok();
    }

    @Override
    public R<List<BillDTO>> queryBillDtoByCondition(EApprovalBusiness eApprovalBusiness, String businessCode) {
        List<ScmOrderApproveLog> scmOrderApproveLogList = scmOrderApproveLogService.queryScmOrderApproveLogByCondition(null, eApprovalBusiness.getValue(), businessCode);
        if(scmOrderApproveLogList == null || scmOrderApproveLogList.size() == 0){
            return R.fail("没有查找到对应的签呈");
        }
        List<BillDTO> billDtoList = new ArrayList<>();
        switch (eApprovalBusiness) {
            case PO_ORDER_APPROVE:
                PoOrderExt poOrderExt = poOrderService.queryDisPoOrderById(scmOrderApproveLogList.get(0).getPkOrder(), EScmOrderStatus.SUBMIT.getValue());
                if(poOrderExt != null) {
                    BillDTO billDTO = BeanUtil.copyProperties(poOrderExt, BillDTO.class, "billDtoList");
                    billDTO.setOrderDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, poOrderExt.getOrderDate()));
                    List<BillItemDTO> billItemDtoList = BeanUtil.copyToList(poOrderExt.getPoOrderItemsExtList(), BillItemDTO.class);
                    billDTO.setBillDtoList(billItemDtoList);
                    billDtoList.add(billDTO);
                }
            case IC_PURCHASE_APPROVE:
            case BACK_PURCHASE_APPROVE:
                IcPurchaseInHExt icPurchaseInhExt = purchaseInhService.queryDisPurchaseInhExtById(scmOrderApproveLogList.get(0).getPkOrder(), EScmOrderStatus.SUBMIT.getValue());
                if(icPurchaseInhExt != null) {
                    BillDTO billDTO = BeanUtil.copyProperties(icPurchaseInhExt, BillDTO.class, "billDtoList");
                    billDTO.setOrderDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, icPurchaseInhExt.getOrderDate()));
                    List<BillItemDTO> billItemDtoList = BeanUtil.copyToList(icPurchaseInhExt.getIcPurchaseInbList(), BillItemDTO.class);
                    billDTO.setBillDtoList(billItemDtoList);
                    billDtoList.add(billDTO);
                }
                break;
            case IC_GENERAL_IN_APPROVE:
                IcGeneralInHExt generalInExt = generalInhService.queryDisGeneralInExtById(scmOrderApproveLogList.get(0).getPkOrder(), EScmOrderStatus.SUBMIT.getValue());
                if(generalInExt != null) {
                    BillDTO billDTO = BeanUtil.copyProperties(generalInExt, BillDTO.class, "billDtoList");
                    billDTO.setOrderDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, generalInExt.getOrderDate()));
                    List<BillItemDTO> billItemDtoList = BeanUtil.copyToList(generalInExt.getGeneralInbExtList(), BillItemDTO.class);
                    billDTO.setBillDtoList(billItemDtoList);
                    billDtoList.add(billDTO);
                }
                break;
            case IC_GENERAL_OUT_APPROVE:
                IcGeneralOutHExt generalOutExt = generalOutService.queryDisGeneralOutExtById(scmOrderApproveLogList.get(0).getPkOrder(), EScmOrderStatus.SUBMIT.getValue());
                if(generalOutExt != null) {
                    BillDTO billDTO = BeanUtil.copyProperties(generalOutExt, BillDTO.class, "billDtoList");
                    billDTO.setOrderDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, generalOutExt.getOrderDate()));
                    List<BillItemDTO> billItemDtoList = BeanUtil.copyToList(generalOutExt.getGeneralOutbExtList(), BillItemDTO.class);
                    billDTO.setBillDtoList(billItemDtoList);
                    billDtoList.add(billDTO);
                }
                break;
            case TRANSFER_ORDER_APPROVE:
                TransferOrderExt transferOrderExt = transferOrderService.queryDisTransferOrderById(scmOrderApproveLogList.get(0).getPkOrder(), EScmOrderStatus.SUBMIT.getValue());
                if(transferOrderExt != null) {
                    BillDTO billDTO = BeanUtil.copyProperties(transferOrderExt, BillDTO.class, "billDtoList");
                    billDTO.setOrderDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, transferOrderExt.getOrderDate()));
                    List<BillItemDTO> billItemDtoList = BeanUtil.copyToList(transferOrderExt.getTransferOrderItemsList(), BillItemDTO.class);
                    billDTO.setBillDtoList(billItemDtoList);
                    billDtoList.add(billDTO);
                }
                break;
            case TRANSFER_IN_APPROVE:
                IcTransInHExt transInExt = transInService.queryDisTransferInInhExtById(scmOrderApproveLogList.get(0).getPkOrder(), EScmOrderStatus.SUBMIT.getValue());
                if(transInExt != null) {
                    BillDTO billDTO = BeanUtil.copyProperties(transInExt, BillDTO.class, "billDtoList");
                    billDTO.setOrderDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, transInExt.getOrderDate()));
                    List<BillItemDTO> billItemDtoList = BeanUtil.copyToList(transInExt.getTransInbExtList(), BillItemDTO.class);
                    billDTO.setBillDtoList(billItemDtoList);
                    billDtoList.add(billDTO);
                }
                break;
            case TRANSFER_OUT_APPROVE:
                IcTransOutHExt transOutExt = transOutService.queryDisTransferOutExtById(scmOrderApproveLogList.get(0).getPkOrder(), EScmOrderStatus.SUBMIT.getValue());
                if(transOutExt != null) {
                    BillDTO billDTO = BeanUtil.copyProperties(transOutExt, BillDTO.class, "billDtoList");
                    billDTO.setOrderDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, transOutExt.getOrderDate()));
                    List<BillItemDTO> billItemDtoList = BeanUtil.copyToList(transOutExt.getTransOutbExtList(), BillItemDTO.class);
                    billDTO.setBillDtoList(billItemDtoList);
                    billDtoList.add(billDTO);
                }
                break;
            case INVENTORY_ORDER_APPROVE:
                IcInventoryHExt inventoryExt = inventoryService.queryDisInventoryExtById(scmOrderApproveLogList.get(0).getPkOrder(), EScmOrderStatus.SUBMIT.getValue());
                if(inventoryExt != null) {
                    BillDTO billDTO = BeanUtil.copyProperties(inventoryExt, BillDTO.class, "billDtoList");
                    billDTO.setOrderDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, inventoryExt.getOrderDate()));
                    List<BillItemDTO> billItemDtoList = BeanUtil.copyToList(inventoryExt.getInventoryBiExtList(), BillItemDTO.class);
                    billDTO.setBillDtoList(billItemDtoList);
                    billDtoList.add(billDTO);
                }
                break;
            case BACK_SALE_OUT_APPROVE:
                IcSaleoutHExt saleOutExt = saleOutService.queryDisSaleOutExtById(scmOrderApproveLogList.get(0).getPkOrder(), EScmOrderStatus.SUBMIT.getValue());
                if(saleOutExt != null) {
                    BillDTO billDTO = BeanUtil.copyProperties(saleOutExt, BillDTO.class, "billDtoList");
                    billDTO.setOrderDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, saleOutExt.getOrderDate()));
                    List<BillItemDTO> billItemDtoList = BeanUtil.copyToList(saleOutExt.getSaleoutbExtList(), BillItemDTO.class);
                    billDTO.setBillDtoList(billItemDtoList);
                    billDtoList.add(billDTO);
                }
                break;
            case IO_ORDER_APPROVE:
                IoOrderExt ioOrderExt = ioOrderService.queryDisIoOrderById(scmOrderApproveLogList.get(0).getPkOrder(), EScmOrderStatus.SUBMIT.getValue());
                if(ioOrderExt != null) {
                    BillDTO billDTO = BeanUtil.copyProperties(ioOrderExt, BillDTO.class, "billDtoList");
                    billDTO.setOrderDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, ioOrderExt.getOrderDate()));
                    List<BillItemDTO> billItemDtoList = BeanUtil.copyToList(ioOrderExt.getOrderItemsExtList(), BillItemDTO.class);
                    billDTO.setBillDtoList(billItemDtoList);
                    billDtoList.add(billDTO);
                }
                break;
        }
        return R.ok(billDtoList);
    }

    @Override
    public R<List<IcAvailableNumExt>> queryAvailableNumByProduct(List<IcAvailableNum> availableNumList) {
        return R.ok(availableNumService.queryAvailableNumByProduct(availableNumList));
    }

    @Override
    public R<Boolean> updateDeductIcAvailableNumByList(List<IcAvailableNumExt> availableNumList) {
        try {
            availableNumService.updateDeductIcAvailableNumByList(availableNumList);
        }catch (Exception e){
            return R.fail();
        }
        return R.ok(Boolean.TRUE);
    }

    @Override
    public R<Boolean> updateScmBillNoNextValue() {
        try {
            scmBillNoService.updateScmBillNoNextValue();
        }catch (Exception e){
            return R.fail();
        }
        return R.ok(Boolean.TRUE);
    }

    @Override
    public R<Boolean> backSaOrderAvailable(SaOrderExt saOrderExt) {
        try {
            availableNumService.mergeIcAvailableNumByOrder(saOrderExt);
        }catch (Exception e){
            e.printStackTrace();
            return R.fail();
        }
        return R.ok(Boolean.TRUE);
    }
}
