package com.jwipc.supplier.logic.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jwipc.api.OaCallBackLogic;
import com.jwipc.constant.OaProcessType;
import com.jwipc.api.constant.OaStatus;
import com.jwipc.api.constant.RestCode;
import com.jwipc.api.dto.ApiResult;
import com.jwipc.supplier.constant.SupplierChangeBpmState;
import com.jwipc.supplier.convert.SupplierBankConvert;
import com.jwipc.supplier.entity.SupplierBank;
import com.jwipc.supplier.entity.SupplierBankInfo;
import com.jwipc.supplier.entity.SupplierChangeVersion;
import com.jwipc.supplier.entity.SupplierPurchaseCategory;
import com.jwipc.supplier.logic.SupplierChangeLogic;
import com.jwipc.supplier.service.ISupplierBankInfoService;
import com.jwipc.supplier.service.ISupplierBankService;
import com.jwipc.supplier.service.ISupplierChangeVersionService;
import com.jwipc.supplier.service.ISupplierPurchaseCategoryService;
import com.jwipc.sys.client.DictClient;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.jwipc.constant.DictCode.CURRENCY;

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Service("SupplierChangeCallBack")
@RequiredArgsConstructor
public class SupplierChangeCallBack implements OaCallBackLogic {

    private final SupplierChangeLogic supplierChangeLogic;

    private final ISupplierChangeVersionService supplierChangeVersionService;

    private final ISupplierBankService supplierBankService;

    private final ISupplierBankInfoService supplierBankInfoService;

    private final SupplierBankConvert supplierBankConvert;

    private final DictClient dictClient;

    private final ISupplierPurchaseCategoryService supplierPurchaseCategoryService;



    @Override
    public ApiResult processor(String workflowId, OaStatus status, String remark, String processCode, String data1,
                               String data2)
    {
        final SupplierChangeVersion changeVersion = supplierChangeVersionService.getOne(
                new LambdaQueryWrapper<SupplierChangeVersion>().eq(SupplierChangeVersion::getBpmCode, workflowId));


        if (Objects.isNull(changeVersion)) {
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("未找到匹配的流程数据!").build();
        }
        RestCode code = RestCode.SAP_SUCCESS;
        String mesg = "";
        try {
            if (status.equals(OaStatus.PASS)) {
                supplierChangeLogic.oaExamine(changeVersion.getId(), SupplierChangeBpmState.PASS.getVal());

            } else if(status.equals(OaStatus.REFUSE)|| (status.equals(OaStatus.DISCARDED))) {
                supplierChangeLogic.oaExamine(changeVersion.getId(), SupplierChangeBpmState.REJECT.getVal());
            }
        } catch (Exception e) {
            code = RestCode.SAP_ERROR;
            mesg = e.getMessage();
            e.printStackTrace();
        }
        return ApiResult.builder().code(code.getCode()).message(mesg).build();
    }

    @Override
    public OaProcessType getProcessType() {
        return OaProcessType.SUPPLIER_CHANGE;
    }
}
