package link.at17.mid.tushare.data.service;

import java.util.List;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;

import link.at17.mid.tushare.annotation.UpdateMethod;
import link.at17.mid.tushare.dao.ThsListDao;
import link.at17.mid.tushare.data.crawler.tushare.TushareClient;
import link.at17.mid.tushare.data.crawler.tushare.TushareRequestBody;
import link.at17.mid.tushare.data.models.ThsStockInfo;
import link.at17.mid.tushare.service.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class ThsListService extends BaseServiceImpl<ThsListDao, ThsStockInfo> {

    /**
     * <a href="https://tushare.pro/document/2?doc_id=259"><b>同花顺概念和行业指数</b></a><br>
     * 接口：ths_index<br>
     * 描述：获取同花顺板块指数。注：数据版权归属同花顺，如做商业用途，请主动联系同花顺，如需帮助请联系微信migedata 。<br>
     * 限量：本接口需获得600积分，单次最大5000，一次可提取全部数据，请勿循环提取。
     * @return
     */
    @UpdateMethod(name="同花顺板块指数列表", order=6)
    public boolean updateData() {
        try {
            List<JSONObject> thsIndexes = TushareClient.queryList(new TushareRequestBody("ths_index"));
            insertOrUpdateList(thsIndexes);
            log.info("更新同花顺板块列表完成");
            return true;
        } catch (Exception e) {
            log.error("更新同花顺板块列表时发生错误", e);
            return false;
        }
    }
    
    /**
     * 插入从 Tushare 获取的数据
     * @param list
     * @return 插入是否成功
     */
    public boolean insertOrUpdateList(List<JSONObject> list) {
        return SqlHelper.retBool(baseMapper.insertOrUpdateList(list));
    }

}
