package com.jwipc.supplier.logic.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jwipc.api.OaCallBackLogic;
import com.jwipc.api.constant.OaStatus;
import com.jwipc.api.constant.RestCode;
import com.jwipc.api.dto.ApiResult;
import com.jwipc.constant.OaProcessType;
import com.jwipc.supplier.constant.SupplierMaterialDemoStatus;
import com.jwipc.supplier.entity.SupplierMaterialDemo;
import com.jwipc.supplier.service.ISupplierMaterialDemoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Service("materialDemoCallBack")
@RequiredArgsConstructor
@Slf4j
public class MaterialDemoCallBack implements OaCallBackLogic {
    private final ISupplierMaterialDemoService supplierMaterialDemoService;

    @Override
    public ApiResult processor(String workflowId, OaStatus status, String remark, String processCode, String data1,
                               String data2)
    {
        final SupplierMaterialDemo materialDemo = supplierMaterialDemoService.getOne(
                new LambdaQueryWrapper<SupplierMaterialDemo>().eq(SupplierMaterialDemo::getOaId, workflowId));
        if (Objects.isNull(materialDemo)) {
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("未找到匹配的流程数据!").build();
        }
        RestCode code = RestCode.SAP_SUCCESS;
        String mesg = "";
        try {
            final LambdaUpdateWrapper<SupplierMaterialDemo> updateWrapper = new LambdaUpdateWrapper<>();
            if (status.equals(OaStatus.PASS)) {
                updateWrapper.set(SupplierMaterialDemo::getState, SupplierMaterialDemoStatus.TO_BE_CONFIRMED.getCode());
                updateWrapper.set(SupplierMaterialDemo::getExamineFileIds, data2);

            } else if(status.equals(OaStatus.REFUSE))  {
                updateWrapper.set(SupplierMaterialDemo::getState, SupplierMaterialDemoStatus.REJECTED.getCode());
            } else if(status.equals(OaStatus.DISCARDED))  {
               updateWrapper.set(SupplierMaterialDemo::getState, SupplierMaterialDemoStatus.REJECTED.getCode());
           }
            updateWrapper.eq(SupplierMaterialDemo::getOaId, workflowId);
            supplierMaterialDemoService.update(updateWrapper);
        } 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.MATERIAL_DEMO;
    }
}
