package com.sunxung.factoring.service.async.impl;

import cn.hutool.json.JSONUtil;
import com.sunxung.factoring.component.exception.BusinessException;
import com.sunxung.factoring.component.util.CodeUtil;
import com.sunxung.factoring.entity.ResponseJson;
import com.sunxung.factoring.entity.ledger.LedgerFundGoodsAssign;
import com.sunxung.factoring.service.async.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author liutao
 * @date 2023-02-20
 */
@Service
@EnableAsync
public class AsyncServiceImpl implements AsyncService {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${config.planInfo.send.msg.url}")
    private String sendMsgUrl;


    @Override
    @Async
    public void sendMsg(List<LedgerFundGoodsAssign> fundGoodsAssignList) {
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(sendMsgUrl, fundGoodsAssignList, String.class);
        if (responseEntity.getStatusCode().value() != HttpStatus.OK.value()) {
            ResponseJson responseJson = JSONUtil.toBean(responseEntity.getBody(), ResponseJson.class);
            throw new BusinessException(CodeUtil.FAIL, "再融资入账通知发送失败：" + responseJson.getMsg());
        }
    }
}
