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

import cn.hutool.core.collection.CollectionUtil;
import com.zfire.mall.manager.bean.k3.bill.SalesOutStockBean;
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 SalesOutStockLogic extends CommonOrderLogic {

    private static final String retFieldKeys = "FEntity_FSeq,FEntity_FENTRYID,FMaterialId,F_kls_Text";

    /**
     * 同步已下推销售出库单
     * @param salesOutStockBean
     * @return
     */
    public ResultEntity syncPushBillData(SalesOutStockBean salesOutStockBean) {
        try {
            ResultBean resultBean = super.syncBillData(salesOutStockBean, KingDeeCons.SALE_OUT_STOCK, 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) {
            unAuditById(salesOutStockBean.getId());
            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_OUT_STOCK);
        } catch (Exception e) {
            log.error("销售出库单反审失败！原因:", e);
            return false;
        }
    }

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

    /**
     * 销售出库单下推销售退货单
     * @param billNo
     * @return
     */
    public ResultEntity pushRet(String billNo, List<String> ids) {
        try {
            ResultBean resultBean = super.push(billNo, ids, KingDeeCons.SALE_OUT_STOCK, KingDeeCons.PUSH_SALE_RET_STOCK);
            if (CollectionUtil.isEmpty(resultBean.getResponseStatus().getSuccessEntitys())) {
                throw new RemoteServiceException("未返回下游标识");
            }
            SuccessEntitys successEntitys = resultBean.getResponseStatus().getSuccessEntitys().get(0);
            List<BillItem> billItems = super.queryBillItemRowInfo(getQueryId(), successEntitys.getId(), retFieldKeys, KingDeeCons.SALE_RET_BILL);

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