package com.zfire.mall.manager.logic.k3.bill;

import cn.hutool.core.collection.CollectionUtil;
import com.zfire.mall.manager.bean.k3.RequestExcuteOperationBean;
import com.zfire.mall.manager.bean.k3.bill.SalesOrderBean;
import com.zfire.mall.manager.bean.k3.bill.TransferOrderBean;
import com.zfire.mall.manager.bean.k3.bill.result.BillItem;
import com.zfire.mall.manager.bean.k3.bill.result.ResultBean;
import com.zfire.mall.manager.bean.k3.bill.result.ResultEntity;
import com.zfire.mall.manager.bean.k3.bill.result.SuccessEntitys;
import com.zfire.mall.manager.constant.KingDeeCons;
import com.zfire.mall.manager.exception.RemoteServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SalesOrderLogic extends CommonOrderLogic {

    private static final String salesFieldKeys = "FSaleOrderEntry_FSeq,FSaleOrderEntry_FEntryID,FMaterialId,F_kls_Text";

    private static final String transferFieldKeys = "FBillEntry_FSeq,FBillEntry_FEntryID,FMaterialId,F_kls_Text";

    private static final String outStockFieldKeys = "FEntity_FSeq,FEntity_FEntryID,FMaterialID,F_kls_Text";

    private static final String salesChangeFieldKeys = "FSaleOrderEntry_FSeq, FSaleOrderEntry_FentryID, FMaterialId, F_kls_Text, FID";

    /**
     * 同步销售订单
     *
     * @param salesOrderBean
     * @return
     */
    public ResultEntity syncBillData(SalesOrderBean salesOrderBean) {
        try {
            ResultBean resultBean = super.syncBillData(salesOrderBean, KingDeeCons.SALE_ORDER, true);
            if (CollectionUtil.isEmpty(resultBean.getResponseStatus().getSuccessEntitys())) {
                throw new RemoteServiceException("未返回下游标识");
            }

            SuccessEntitys successEntitys = resultBean.getResponseStatus().getSuccessEntitys().get(0);
            List<BillItem> billItems = super.queryBillItemRowInfo(getQueryId(), successEntitys.getId(), salesFieldKeys, KingDeeCons.SALE_ORDER);

            return new ResultEntity(successEntitys.getId(), successEntitys.getNumber(), successEntitys.getDIndex(), billItems);
        } catch (Exception e) {
            log.error("销售订单同步失败！原因:", e);
            throw new RemoteServiceException("销售订单同步失败！原因:" + e.getMessage());
        }
    }

    /**
     * 反审核销售订单根据id
     *
     * @param id
     * @return
     */
    public boolean unAuditById(String id) {
        try {
            return super.unAudit(getQueryId(), id, KingDeeCons.SALE_ORDER);
        } catch (Exception e) {
            log.error("销售订单反审失败！原因:", e);
            throw new RemoteServiceException("销售订单反审失败！原因:" + e.getMessage());
        }
    }

    /**
     * 反审核销售订单根据单据编号
     *
     * @param billNo
     * @return
     */
    public boolean unAuditByBillNo(String billNo) {
        try {
            return super.unAudit(getQueryBillNo(), billNo, KingDeeCons.SALE_ORDER);
        } catch (Exception e) {
            log.error("销售订单反审失败！原因:", e);
            throw new RemoteServiceException("销售订单反审失败！原因:" + e.getMessage());
        }
    }


    /**
     * 同步直接调拨
     * @param transferOrderBean
     * @return
     */
    public ResultEntity syncBillTransferData(TransferOrderBean transferOrderBean) {
        try {
            ResultBean resultBean = super.syncBillData(transferOrderBean, KingDeeCons.STOCK_TRANSFER, true);
            if (CollectionUtil.isEmpty(resultBean.getResponseStatus().getSuccessEntitys())) {
                throw new RemoteServiceException("未返回下游标识");
            }

            SuccessEntitys successEntitys = resultBean.getResponseStatus().getSuccessEntitys().get(0);
            List<BillItem> billItems = super.queryBillItemRowInfo(getQueryId(),successEntitys.getId(),transferFieldKeys, KingDeeCons.STOCK_TRANSFER);

            return new ResultEntity(successEntitys.getId(), successEntitys.getNumber(), successEntitys.getDIndex(), billItems);
        } catch (Exception e) {
            unAuditByIdTransfer(transferOrderBean.getId());
            log.error("销售订单同步失败！原因:", e);
            throw new RemoteServiceException(e.getMessage());
        }
    }


    /**
     * 反审核直接调拨单订单根据id
     *
     * @param id
     * @return
     */
    public boolean unAuditByIdTransfer(String id) {
        try {
            return super.unAudit(getQueryId(), id, KingDeeCons.STOCK_TRANSFER);
        } catch (Exception e) {
            log.error("销售订单反审失败！原因:", e);
            throw new RemoteServiceException("销售订单反审失败！原因:" + e.getMessage());
        }
    }


    /**
     * 销售订单下推直接调拨
     *
     * @param billNo
     * @return
     */
    public ResultEntity pushTransfer(String billNo) {
        try {
            ResultBean resultBean = super.push(billNo, null, KingDeeCons.SALE_ORDER, KingDeeCons.PUSH_SALE_TRANSFER);
            if (CollectionUtil.isEmpty(resultBean.getResponseStatus().getSuccessEntitys())) {
                throw new RemoteServiceException("未返回下游标识");
            }

            SuccessEntitys successEntitys = resultBean.getResponseStatus().getSuccessEntitys().get(0);
            List<BillItem> billItems = super.queryBillItemRowInfo(getQueryId(), successEntitys.getId(), transferFieldKeys, KingDeeCons.STOCK_TRANSFER);

            return new ResultEntity(successEntitys.getId(), successEntitys.getNumber(), successEntitys.getDIndex(), billItems);
        } catch (Exception e) {

            log.error("销售订单下推失败！原因:", e);
            throw new RemoteServiceException("销售订单下推失败！原因:" + e.getMessage());
        }
    }

    /**
     * 销售订单下推销售出库单
     *
     * @param billNo
     * @return
     */
    public ResultEntity pushOutStock(String billNo, List<String> ids) {
        try {
            return pushCommonOutStock(billNo, ids, KingDeeCons.SALE_ORDER, KingDeeCons.PUSH_SALE_OUT_STOCK);
        } catch (Exception e) {
            log.error("销售订单下推失败！原因:", e);
            throw new RemoteServiceException("销售订单下推失败！原因:" + e.getMessage());
        }
    }

    /**
     * 直接调拨下推销售出库单
     *
     * @param billNo
     * @return
     */
    public ResultEntity pushTransferToOutStock(String billNo, List<String> ids) {
        try {
            return pushCommonOutStock(billNo, ids, KingDeeCons.STOCK_TRANSFER, KingDeeCons.PUSH_TRANSFER_OUT_STOCK);
        } catch (Exception e) {
            log.error("直接调拨下推失败！原因:", e);
            throw new RemoteServiceException("直接调拨下推失败！原因:" + e.getMessage());
        }
    }

    private ResultEntity pushCommonOutStock(String billNo, List<String> ids, String formId, String ruleId) throws Exception {
        ResultBean resultBean = super.push(billNo, ids, formId, ruleId);
        if (CollectionUtil.isEmpty(resultBean.getResponseStatus().getSuccessEntitys())) {
            throw new RemoteServiceException("未返回下游标识");
        }
        SuccessEntitys successEntitys = resultBean.getResponseStatus().getSuccessEntitys().get(0);
        List<BillItem> billItems = super.queryBillItemRowInfo(getQueryId(), successEntitys.getId(), outStockFieldKeys, KingDeeCons.SALE_OUT_STOCK);

        return new ResultEntity(successEntitys.getId(), successEntitys.getNumber(), successEntitys.getDIndex(), billItems);
    }


    /**
     * 销售订单下推销售新变更单
     *
     * @param ids
     * @return
     */
    public ResultEntity pushSalesChange(String id, List<String> ids) {
        try {
            String billNo = super.pushSalesChangeNew(id, ids);
//            String message = resultBean.getResponseStatus().getMessage();
//            String billNo = message.substring(message.indexOf("【") + 1, message.indexOf("】"));

            List<BillItem> billItems = super.queryBillItemRowInfo(getQueryBillNo(), billNo, salesChangeFieldKeys, KingDeeCons.SALE_CHANGE);

            return new ResultEntity(billItems.get(0).getParentId(), billNo, 0, billItems);
        } catch (Exception e) {
            log.error("销售订单下推失败！原因:", e);
            throw new RemoteServiceException("销售订单下推失败！原因:" + e.getMessage());
        }
    }

    public void excuteOperationEnd(RequestExcuteOperationBean bean) {
        try {
            super.excuteOperation(bean, KingDeeCons.SALE_ORDER, "YLTerminate");
        } catch (Exception e) {
            log.error("销售订单明细业务终止失败！原因:", e);
            throw new RemoteServiceException("销售订单明细业务终止失败！原因:" + e.getMessage());
        }
    }



    /**
     * 重查订单明细信息
     *
     * @param interfaceOrderType
     * @param id
     * @return
     * @throws Exception
     */
    public List<BillItem> retQueryBillItemRowInfo(String interfaceOrderType, String id) throws Exception {
        List<BillItem> billItems = null;
        if (interfaceOrderType.equals(KingDeeCons.SALE_ORDER)) {
            billItems = super.queryBillItemRowInfo(getQueryId(), id, salesFieldKeys, KingDeeCons.SALE_ORDER);
        } else if (interfaceOrderType.equals(KingDeeCons.STOCK_TRANSFER)) {
            billItems = super.queryBillItemRowInfo(getQueryId(), id, salesFieldKeys, KingDeeCons.STOCK_TRANSFER);
        } else if (interfaceOrderType.equals(KingDeeCons.SALE_OUT_STOCK)) {
            billItems = super.queryBillItemRowInfo(getQueryId(), id, salesFieldKeys, KingDeeCons.SALE_OUT_STOCK);
        }
        return billItems;
    }

}
