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

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.github.yulichang.query.MPJLambdaQueryWrapper;

import link.at17.mid.tushare.annotation.UpdateMethod;
import link.at17.mid.tushare.dao.StockInfoDao;
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.StockInfo;
import link.at17.mid.tushare.enums.ListStatus;
import link.at17.mid.tushare.service.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class StockInfoService extends BaseServiceImpl<StockInfoDao, StockInfo> {
    

    @Override
    public List<StockInfo> list() {
        return baseMapper.selectList(null);
    }
    
    /**
     * 该方法涉及到 ThsBelongins 联查
     */
    @Override
    public List<StockInfo> list(Wrapper<StockInfo> ew) {
        return baseMapper.selectList(ew);
    }
    
    /**
     * 该方法为单表查询，不涉及 ThsBelongins 联查
     * @param ew
     * @return
     */
    public List<StockInfo> listWithoutThs(Wrapper<StockInfo> ew) {
        return baseMapper.selectListWithoutThs(ew);
    }
    
    /**
     * 用于插入从 Tushare 获取的内容
     * @param list
     */
    public void insertOrUpdateList(List<JSONObject> list) {
        baseMapper.insertOrUpdateList(list);
    }
    
    /**
     * 根据上市状态列举对应所有股票
     * @param listStatus
     * @return
     */
    public List<StockInfo> listByListStatus(ListStatus listStatus) {
        MPJLambdaQueryWrapper<StockInfo> ew = new MPJLambdaQueryWrapper<>();
        return baseMapper.selectList(ew.setAlias("i").eq(StockInfo::getListStatus, listStatus));
    }
    
    /**
     * <a href="https://tushare.pro/document/2?doc_id=25"><b>股票列表</b></a><br>
     * <b>更新股票列表</b><br/>
     * 接口：stock_basic，可以通过数据工具调试和查看数据<br>
     * 描述：获取基础信息数据，包括股票代码、名称、上市日期、退市日期等<br>
     * 积分：2000积分起<br>
     * 包括上市、退市和暂停上市，无权限（积分不足）状态下每小时最多访问该接口 1 次
     * 
     * 
     * @param queryWay 查询方法，本例中为 null，因为更新股票列表不需要指定 QueryWay
     */

    @UpdateMethod(name="股票列表", order=1)
    public boolean updateData() {
        try {
            List<JSONObject> stockInfos = new ArrayList<>();
            // Tushare 经常改请求参数的规则，2024/11/13 更新：默认不提供 list_status 时，默认值是 L
            TushareRequestBody requestBody = new TushareRequestBody("stock_basic")
                    .addFields("ts_code,name,area,industry,market,list_status,list_date,delist_date");

            requestBody.addParam("list_status", "L");
            stockInfos.addAll(TushareClient.queryList(requestBody));
            
            requestBody.addParam("list_status", "D");
            stockInfos.addAll(TushareClient.queryList(requestBody));
            
            requestBody.addParam("list_status", "P");
            stockInfos.addAll(TushareClient.queryList(requestBody));
            
            insertOrUpdateList(stockInfos);
            
            log.info("更新股票列表完成");
            return true;
        } catch (JSONException | IOException e) {
            log.error("更新股票列表时发生错误", e);
            return false;
        }
    }

}
