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

import cn.hutool.core.collection.CollectionUtil;
import com.zfire.mall.manager.bean.k3.bill.SalesOrderBean;
import com.zfire.mall.manager.bean.k3.bill.SyncK3OtherReceBean;
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 com.zfire.mall.manager.helper.ResponseHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 其他应收单-金蝶对接
 */
@Slf4j
@Service
public class K3OtherReceLogic extends CommonOrderLogic{

    /**
     * 同步其他应收单到金蝶
     * @param salesOrderBeanList
     * @return
     */
    public ResponseHelper syncOtherBillData(List<SyncK3OtherReceBean> salesOrderBeanList) {
        try {
            ResultBean resultBean = super.syncBillDatabatch(salesOrderBeanList, KingDeeCons.AR_OTHER_REC_ABLE, true);
            if (CollectionUtil.isEmpty(resultBean.getResponseStatus().getSuccessEntitys())) {
                throw new RemoteServiceException("未返回下游标识");
            }
            SuccessEntitys successEntitys = resultBean.getResponseStatus().getSuccessEntitys().get(0);
            return ResponseHelper.success(new ResultEntity(successEntitys.getId(), successEntitys.getNumber(), successEntitys.getDIndex(), null));
        } catch (Exception e) {
            log.error("其他应收单同步失败！原因:", e);
            return ResponseHelper.error(e.getMessage());
        }
    }

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

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

}
