package com.jwipc.sys.controller;

import cn.hutool.json.JSONUtil;
import com.jwipc.api.ShareCallBackLogic;
import com.jwipc.api.constant.RestCode;
import com.jwipc.api.constant.ShareStatus;
import com.jwipc.api.dto.ApiResult;
import com.jwipc.log.dto.ShareCallBackDTO;
import com.jwipc.constant.ShareProcessType;
import com.jwipc.log.annotation.ApiOperateLog;
import com.jwipc.log.constant.ApiDirection;
import com.jwipc.log.constant.ThirdSysType;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <p> </p>
 *
 * @author 邓京术
 * @version 1.0
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/share/api")
@RequiredArgsConstructor
@Slf4j
public class ShareCallBackController {

    @Autowired
    private List<ShareCallBackLogic> shareCallBackLogicList;


    @PostMapping("callback")
    @ApiOperateLog(code = "callBack", name = "共享审批流回调SRM", direction = ApiDirection.OTHER_TO_SRM,
            thirdSysType = ThirdSysType.SHARE)
    public ApiResult callBack(@RequestBody ShareCallBackDTO callBackDTO) {
        log.info("{}", callBackDTO);
        ShareCallBackDTO.ItData data = callBackDTO.getIT_DATA();
        if (StringUtils.isEmpty(data.getWorkflowId())){
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("请传入流程id").build();
        }
        if (null == data.getType()){
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("请传入业务类型").build();
        }
        if (null == data.getAuditState() && !data.getType().equals(ShareProcessType.PAYMENT_CHANGE.getType())){
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("请传入单据整体状态").build();
        }

//        if (StringUtils.isEmpty(data.getProcessCode())){
//            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("请传入流程号").build();
//        }
        if(!data.getType().equals(ShareProcessType.PAYMENT_CHANGE.getType())){
            if (data.getAuditState()==2&&StringUtils.isEmpty(data.getAuditMessage())){
                return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("请传入审批意见").build();
            }
        }else if(StringUtils.isEmpty(data.getAuditMessage())){
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("请传入审批意见").build();
        }

        for (ShareCallBackLogic service : shareCallBackLogicList) {
            final ShareProcessType processType = service.getProcessType();
            if (data.type == processType.getType()) {
                return service.processor(callBackDTO.getIT_DATA().workflowId,
                        data.getType().equals(ShareProcessType.PAYMENT_CHANGE.getType()) ? ShareStatus.forVal(callBackDTO.getIT_DATA().cauditState) : ShareStatus.forVal(callBackDTO.getIT_DATA().auditState),
                        callBackDTO.getIT_DATA().auditMessage, callBackDTO.getIT_DATA().processCode,
                        callBackDTO.getIT_DATA().code,
                        callBackDTO.getIT_DATA().list, callBackDTO.getIT_DATA().data, JSONUtil.toJsonStr(callBackDTO.getIT_DATA()));
            }
        }
        return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("未找到业务处理逻辑").build();
    }
}
