package com.inkasso.factoring.creditmanagement.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inkasso.factoring.creditmanagement.entity.Enterprise;
import com.inkasso.factoring.creditmanagement.dao.EnterpriseMapper;
import com.inkasso.factoring.creditmanagement.entity.SupplierInfo;
import com.inkasso.factoring.creditmanagement.service.IEnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 企业信息 服务实现类
 * </p>
 *
 * @author liutao
 * @since 2023-11-25
 */
@Service
public class EnterpriseServiceImpl extends ServiceImpl<EnterpriseMapper, Enterprise> implements IEnterpriseService {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${sop.getBusinessByKeyWordUrl}")
    private String getBusinessByKeyWordUrl;

    @Override
    public List<Enterprise> getBusinessByKeyWord(String keyword) {
        String url = getBusinessByKeyWordUrl + "?keyword=" + keyword;
        JSONObject responseJson = restTemplate.getForObject(url, JSONObject.class);
        if(responseJson != null){
            List<JSONObject> list= responseJson.getBeanList("data", JSONObject.class);
            if(CollectionUtil.isNotEmpty(list)){
                List<Enterprise> result = new ArrayList<>();
                for (JSONObject jsonObject : list) {
                    Enterprise enterprise = new Enterprise(jsonObject);
                    result.add(enterprise);
                }
                return result;
            }
        }
        return null;
    }

    @Override
    public List<Enterprise> findSupplierInfo(Long sysUserId) {
        List<Enterprise> enterprises = this.baseMapper.findSupplierInfo(sysUserId);
        return enterprises;
    }

    @Override
    public List<Enterprise> findEnterpriseInfo(Long cSupplierId) {
        List<Enterprise> enterprises = this.baseMapper.findEnterpriseInfo(cSupplierId);
        return enterprises;
    }

    @Override
    public String getWebAddress(String keyword) {
        return baseMapper.getWebAddress(keyword);
    }
}
