package com.inkasso.factoring.businessProcessing;

import cn.hutool.json.JSONObject;
import com.inkasso.adapter.exception.BusinessException;
import com.inkasso.adapter.util.CodeUtil;
import com.inkasso.adapter.util.StringUtil;
import com.inkasso.factoring.common.entity.ResponseJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.xml.bind.ValidationException;

/**
 * @Description 流程
 * @Author Yafan Wang
 * @Date 2024/3/18 16:05
 */
@RestController
@RequestMapping("/businessProcessingTask")
public class BusinessProcessingTaskController {

    @Autowired
    private RestTemplate restTemplate;
    @Value("${trade.doBusinessCHistoryViewUrl}")
    private String doBusinessCHistoryViewUrl;

    /**
     * 加载流程业务内各个节点处理的历史信息
     */
    @GetMapping(value = "/doBusinessCHistoryView")
    @ResponseBody
    public ResponseJson doBusinessCHistoryView(@RequestParam Long businessKey, @RequestParam String businessType) throws ValidationException {
        if (businessKey == null || StringUtil.isEmpty(businessType)) {
            throw new ValidationException("查询条件不能为空！");
        }
        JSONObject jsonObject = restTemplate.getForObject(doBusinessCHistoryViewUrl + "?businessKey=" + businessKey + "&businessType=" + businessType, JSONObject.class);
        if (!jsonObject.get("code").equals("200")) {
            throw new BusinessException(CodeUtil.FAIL, jsonObject.get("msg").toString());
        }
        return new ResponseJson(jsonObject.get("data"));
    }

}
