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

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunxung.factoring.component.util.ElectronicSignatureUtil;
import com.sunxung.factoring.entity.entprise.Enterprise;
import com.sunxung.factoring.mapper.entprise.EnterpriseMapper;
import com.sunxung.factoring.service.entprise.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 void urlJob() {
        List<Enterprise> list = lambdaQuery().eq(Enterprise::getAuthStatus, false).isNotNull(Enterprise::getAuthUrl).list();
        if(CollectionUtil.isNotEmpty(list)){
            for (Enterprise enterprise : list) {
                String newShortUrl = ElectronicSignatureUtil.shortUrl(null, enterprise.getAuthUrl());
                enterprise.setShortAuthUrl(newShortUrl);
                updateById(enterprise);
            }
        }
        List<Enterprise> list1 = lambdaQuery().eq(Enterprise::getCertificateSignStatus, 0).isNotNull(Enterprise::getCertificateSignUrl).list();
        if(CollectionUtil.isNotEmpty(list1)){
            for (Enterprise enterprise : list1) {
                String newShortUrl = ElectronicSignatureUtil.shortUrl(null, enterprise.getCertificateSignUrl());
                enterprise.setCertificateShortSignUrl(newShortUrl);
                updateById(enterprise);
            }
        }
        List<Enterprise> list2 = lambdaQuery().eq(Enterprise::getAutoSignStatus, 0).isNotNull(Enterprise::getAutoSignUrl).list();
        if(CollectionUtil.isNotEmpty(list2)){
            for (Enterprise enterprise : list2) {
                String newShortUrl = ElectronicSignatureUtil.shortUrl(null, enterprise.getAutoSignUrl());
                enterprise.setShortAutoSignUrl(newShortUrl);
                updateById(enterprise);
            }
        }
    }
}
