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

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

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

            // 审核后需要生效
            resultBean = super.takeEffectSalesChange(salesChangeBean.getBillNo(), KingDeeCons.SALE_CHANGE);

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

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


    /**
     * 同步更新-零售变更单
     * @param salesChangeBean
     * @return
     */
    public ResultEntity syncBillDataRetail(SalesChangeBean salesChangeBean) {
        try {
            ResultBean resultBean = super.syncBillData(salesChangeBean, KingDeeCons.SALE_CHANGE, false);
            if (CollectionUtil.isEmpty(resultBean.getResponseStatus().getSuccessEntitys())) {
                throw new RemoteServiceException("未返回下游标识");
            }

            // 审核后需要生效
            resultBean = super.takeEffectSalesChange(salesChangeBean.getBillNo(), KingDeeCons.SALE_CHANGE);

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

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


}
