package com.xiang.xservice.application.script.xb.service;

import com.alibaba.fastjson2.JSONObject;
import com.google.common.collect.Lists;
import com.xiang.xservice.basic.utils.HttpUtils;
import com.xiang.xservice.application.script.xb.common.URLConstants;
import com.xiang.xservice.application.script.xb.entity.pojo.xb.XbFundCount;
import com.xiang.xservice.application.script.xb.entity.pojo.xb.XbFundList;
import com.xiang.xservice.application.script.xb.entity.response.xbyj.fund.FundInfo;
import com.xiang.xservice.application.script.xb.entity.response.xbyj.fund.FundList;
import com.xiang.xservice.application.script.xb.entity.response.xbyj.fund.QueryFundInfoResponse;
import com.xiang.xservice.application.script.xb.entity.response.xbyj.fund.QueryFundListResponse;
import com.xiang.xservice.application.script.xb.repository.XBFundMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author: xiang
 * @Date: 2025-05-14 15:42
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class FundServiceImpl implements FundService{


    private final XBFundMapper xbFundMapper;

    @Value("${xiaobei.unionId}")
    private String unionId;

    @Value("${xiaobei.codeArr}")
    private String codeArrStr;

    private static final Integer SUCCESS_200 = 200;
    private static final Integer SUCCESS_0= 0;

    @Override
    public List<FundList> queryTodayList(String date, String dataResources, Boolean dataSourceSwitch, List<String> codeArr) {
        if (CollectionUtils.isEmpty(codeArr)) {
            String[] split = codeArrStr.split(", ");
            codeArr = Arrays.stream(split).collect(Collectors.toList());
        }

        JSONObject json = new JSONObject();
        json.put("date", date);
        json.put("unionId", unionId);
        json.put("dataResources", dataResources);
        json.put("dataSourceSwitch", dataSourceSwitch);
        json.put("codeArr", codeArr);

        String resp = "";
        try {
           resp = HttpUtils.doPost(URLConstants.XB_QUERY_LIST, null, JSONObject.toJSONString(json));
           log.info("[基金列表查询] 查询成功， 请求地址:{}, 请求参数：{}, 请求结果：{}", URLConstants.XB_QUERY_LIST, JSONObject.toJSONString(json), resp);
           if (StringUtils.isNotBlank(resp)) {
               QueryFundListResponse response = JSONObject.parseObject(resp, QueryFundListResponse.class);
               if (Objects.nonNull(response)) {
                   if (Objects.equals(SUCCESS_200, response.getCode())) {
                       return response.getData();
                   }
               }
           }
        } catch (Exception e) {
            log.error("[查询基金列表]，请求失败， 请求参数:{}, 请求结果:{}", JSONObject.toJSONString(json), resp);
        }
        return Lists.newArrayList();
    }

    @Override
    public FundInfo queryFundInfo(String code) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", code);
        jsonObject.put("unionId", unionId);
        String resp = HttpUtils.doPost(URLConstants.XB_QUERY_FUND_INFO, null, JSONObject.toJSONString(jsonObject));
        if (StringUtils.isNotBlank(resp)) {
            log.info("[小蓓养基] 基金详情查询：请求参数:{}, 请求结果:{}", JSONObject.toJSONString(jsonObject), resp);
            QueryFundInfoResponse response = JSONObject.parseObject(resp, QueryFundInfoResponse.class);
            if (Objects.nonNull(response)) {
                if (Objects.equals(response.getCode(), SUCCESS_200)) {
                    return response.getData();
                }
            }
        }
        return null;
    }

    @Override
    public List<XbFundList> queryFundList(Integer type) {
        return xbFundMapper.queryFundList(type);
    }

    @Override
    public List<XbFundList> queryFundList() {
        return xbFundMapper.queryAllFundList();
    }

    @Override
    public int addCounts(List<XbFundCount> list) {
        return xbFundMapper.addCounts(list);
    }

    @Override
    public List<XbFundCount> queryFundCountInWeek() {
        return xbFundMapper.queryFundInWeek();
    }
}
