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

import cn.hutool.core.collection.CollectionUtil;
import com.zfire.mall.manager.bean.k3.bill.SalesReturnBean;
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;

@Service
@Slf4j
public class SalesReturnLogic extends CommonOrderLogic {

    /**
     * 同步更新销售退货单
     * @param salesReturnBean
     * @return
     */
    public ResultEntity syncPushBillData(SalesReturnBean salesReturnBean) {
        try {
            ResultBean resultBean = super.syncBillData(salesReturnBean, KingDeeCons.SALE_RET_BILL, false);
            if (CollectionUtil.isEmpty(resultBean.getResponseStatus().getSuccessEntitys())) {
                throw new RemoteServiceException("未返回下游标识");
            }

            SuccessEntitys successEntitys = resultBean.getResponseStatus().getSuccessEntitys().get(0);

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

    /**
     * 反审核销售退货单根据id
     * @param id
     * @return
     */
    public boolean unAuditById(String id) {
        try {
            return super.unAudit(getQueryId(), id, KingDeeCons.SALE_RET_BILL);
        } catch (Exception e) {
            log.error("销售退货单反审失败！原因:", e);
            return false;
        }
    }

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



    /**
     * 反审采购退料单
     * @param billNo
     * @return
     */
    public boolean unAuditByPurchaseStockOutBillNo(String billNo) {
        try {
            return super.unAudit(getQueryBillNo(), billNo, KingDeeCons.PURCHASE_RET_BILL);
        } catch (Exception e) {
            log.error("销售退货单反审失败！原因:", e);
            return false;
        }
    }


    /**
     * 反审其他出库
     * @param billNo
     * @return
     */
    public boolean unAuditByOtherStockOutBillNo(String billNo) {
        try {
            return super.unAudit(getQueryBillNo(), billNo, KingDeeCons.OTHER_OUT_STOCK);
        } catch (Exception e) {
            log.error("销售退货单反审失败！原因:", e);
            return false;
        }
    }


    /**
     * 反审其他入库
     * @param billNo
     * @return
     */
    public boolean unAuditByOtherStockInBillNo(String billNo) {
        try {
            return super.unAudit(getQueryBillNo(), billNo, KingDeeCons.OTHER_IN_STOCK);
        } catch (Exception e) {
            log.error("销售退货单反审失败！原因:", e);
            return false;
        }
    }


    /**
     * 反审采购入库
     * @param billNo
     * @return
     */
    public boolean unAuditByPurchaseStockInBillNo(String billNo) {
        try {
            return super.unAudit(getQueryBillNo(), billNo, KingDeeCons.PURCHASE_IN_STOCK);
        } catch (Exception e) {
            log.error("销售退货单反审失败！原因:", e);
            return false;
        }
    }
}
