import request from "@/api/request.js";
import { ElMessage } from "element-plus";

const request200 = requestP => {
	return requestP.then(data => {
		if (data.code === 200) {
			return data.data;
		}
		ElMessage({
			message: data.message,
			type: "error",
			duration: 3 * 1000
		});
		return null;
	});
};

// 实体清单总条数统计
/**
 * @returns {Promise<number>}
 */
export function getEntitiesDataCount() {
	return request200(
		request({
			method: "GET",
			// url: "/api/entitiesDataCount/countData",
			url: "/api/sanctionList/export/getTotalInfo"
		})
	);
}

// 最新实体清单信息查询
/**
 * @returns {Promise<{
 * chNum: number
 * entities:{
 *  entityNameZh: string
 *  entityName: string
 * }[]
 * organization:{
 *  orgName: string
 * }
 * domains: string[]
 * startTime: string
 *}>}
 */
// 最新出口管制政策
export function getEntitiesDataInfo() {
	return request200(
		request({
			method: "GET",
			// url: '/api/entitiesDataInfo/latestInfoSelect',
			url: "/api/entitiesDataInfo/getLatestInfo"
		})
	);
}

/**
 * 每年最多领域及该领域发布次数
 * @returns {Promise<{
 * year?: number
 * maxDomain: string
 * maxCount: number
 * }[]>}
 */
export function getIndustryCountByYear(sanTypeId) {
	return request200(
		request({
			method: "GET",
			// url: "/api/entitiesDataCount/industryCountByYear"
			url: "/api/entitiesDataCount/getAnnualCount",
			params: {
				sanTypeId
			}
		})
	);
}

/**
 * 不同领域每年数量
 * @returns {Promise<{
 * data:{
 *   year?: number
 *   domainNum: {
 *    [ key: string]: number;
 *   }
 * }[]
 * domains: string[]
 * }>}
 */
export function getCountDomainByYear(isRule, startYear = "2020", endYear = String(new Date().getFullYear())) {
	return request200(
		request({
			method: "POST",
			url: "/api/entitiesDataCount/getAnnualSanDomain",
			data: {
				isRule,
				startYear,
				endYear
			}
		})
	);
}

/**
 * 历次制裁过程（月份统计）
 * @returns {Promise<{
 * tittle: string
 * entitiesChNum: string
 * sanReason: string
 * }[]>}
 */
export function getSanctionsInfoCount() {
	return request200(
		request({
			method: "GET",
			url: "/api/entitiesDataCount/sanctionsInfoCount"
		})
	);
}

/**
 * 历次制裁过程（月份统计）-new
 * @returns {Promise<{
 * tittle: string
 * entitiesChNum: string
 * sanReason: string
 * }[]>}
 */
export function getSanctionProcess(typeName = "实体清单", pageNum = 1, pageSize = 10) {
	return request200(
		request({
			method: "POST",
			url: "/api/entitiesDataCount/getSanctionProcess",
			data: {
				typeName,
				pageNum,
				pageSize
			}
		})
	);
}

/**
 * 查询发布机构
 * @returns {Promise<{
 * orgName: string         //机构名称
 * orgIntroduction: string //相关措施
 * orgIntroduction: string //机构职责
 * }>}
 */
export function getOrganizationInfo(sanTypeId = 1) {
	return request200(
		request({
			method: "GET",
			// url: "/api/entitiesDataInfo/getOrganizationInfo"
			url: "/api/sanctionList/getPublishedOrg",
			params: {
				sanTypeId
			}
		})
	);
}

/**
 * 风险信号
 */
export function getRiskSignal(moduleId = "0103") {
	return request200(
		request({
			method: "GET",
			url: `/api/commonFeature/riskSignal/${moduleId}`
		})
	);
}

/**
 * 社交媒体
 */
export function getSocialMediaInfo(moduleId = "0103") {
	return request200(
		request({
			method: "GET",
			url: `/api/commonFeature/remarks/${moduleId}`
		})
	);
}

/**
 * 新闻资讯
 */
export function getNewsInfo(moduleId = "0103") {
	return request200(
		request({
			method: "GET",
			url: `/api/commonFeature/news/${moduleId}`
		})
	);
}

/**
 * 查询重点人物
 * @returns {Promise<{
 * name: string
 * ename: string
 * avatarUrl: string
 * positionTitle: string
 * }[]>}
 */
export function getPersonList() {
	return request200(
		request({
			method: "GET",
			url: "/api/entitiesDataInfo/getPersonList"
		})
	);
}

/**
 * 重点实体列表查询
 */
export function getKeyEntityList(date, keyword = "") {
	return request200(
		request({
			method: "GET",
			url: "/api/entitiesDataInfo/getKeyEntities",
			params: {
				sanctionDate: date || "2025-11-11",
				searchText: keyword
			}
		})
	);
}

/**
 * 区域分布查询
 */
export function getAreaDistribution(date) {
	return request200(
		request({
			method: "GET",
			url: "/api/entitiesDataInfo/getRegionDistribution",
			params: {
				sanctionDate: date || "2025-11-11"
			}
		})
	);
}

/**
 * 不同领域实体统计
 * @param {string} startTime - 统计开始时间，格式为 'YYYY-MM-DD'
 * @returns {Promise<{
 * domain: string
 * count: number
 * }[]>}
 */
export function getCountByDomain(startTime) {
	return request200(
		request({
			method: "POST",
			// url: "/api/entitiesDataCount/countByDomain",
			url: "/api/sanctionListCount/sanctionDomainCountByParam",
			data: {
				sanctionDate: startTime || "2025-11-11"
			}
		})
	);
}

/**
 * 不同类型实体统计
 * @param {string} startTime - 统计开始时间，格式为 'YYYY-MM-DD'
 * @returns {Promise<{
 * type: string
 * count: number
 * }[]>}
 */
export function getCountByType(startTime) {
	return request200(
		request({
			method: "GET",
			// url: "/api/entitiesDataCount/countByType",
			url: "/api/entitiesDataInfo/getTypeDistribution",
			params: {
				sanctionDate: startTime || "2025-11-11"
			}
		})
	);
}

/**
 * 查询制裁原因
 * @param {string} startTime - 统计开始时间，格式为 'YYYY-MM-DD'
 * @returns {Promise<string[]>}
 */
export function getSanReasonSelect(startTime) {
	return request200(
		request({
			method: "GET",
			// url: "/api/entitiesDataInfo/sanReasonSelect",
			url: "/api/entitiesDataInfo/getSanReason",
			params: {
				sanctionDate: startTime || "2025-11-11"
			}
		})
	);
}

/**
 * 查询实体清单列表
 * @param {string} startTime - 统计开始时间，格式为 'YYYY-MM-DD'
 * @returns {Promise<{
 * entityNameZh: string
 * techDomainList: string[]
 * startTime: staring
 * }[]>}
 */
export function getSelectEntitiesList(startTime) {
	return request200(
		request({
			method: "GET",
			url: "/api/entitiesDataInfo/selectEntitiesList",
			params: {
				startTime
			}
		})
	);
}

/**
 * 领域分布查询
 */
export function getDomainDistribution(sanctionDate = "2025-11-11") {
	return request200(
		request({
			method: "GET",
			url: "/api/entitiesDataInfo/getDomianDistribution",
			params: {
				sanctionDate
			}
		})
	);
}

/**
 * 分页查询实体清单列表
 * @param {string} typeName - 实体类型名称
 * @returns {Promise<{
 * entityNameZh: string
 * techDomainList: string[]
 * startTime: string
 * }[]>}
 */
export function getEntitiesList(typeName = "实体清单", pageNum = 1, pageSize = 10, sanctionDate = "", rule = false) {
	return request200(
		request({
			method: "POST",
			url: "/api/sanctionList/pageQuery",
			data: {
				typeName,
				pageNum,
				pageSize,
				sanctionDate,
				rule
			}
		})
	);
}

/**
 * 数量比对分析
 * @param {string} startTime - 统计开始时间，格式为 'YYYY-MM-DD'
 * @returns {Promise<{
 * countDomainLast: number
 * countDomainNow: number
 * countSanLast: number
 * countSanNow: number
 * countTypeLast: number
 * countTypeNow: number
 * }>}
 */
export function getCompareCountSan(startTime) {
	return request200(
		request({
			method: "GET",
			// url: "/api/entitiesDataCount/compareCountSan",
			url: "/api/entitiesDataInfo/getSanCountInfo",
			params: {
				sanctionDate: startTime || "2025-11-11"
			}
		})
	);
}

/**
 * 实体清单变化统计
 * @param {number} domain
 * @param {number} type
 * @returns {Promise<{
 * year:number
 * count:number
 * }[]>}
 */
export function getEntitiesChangeCount(domianId, typeId) {
	return request200(
		request({
			method: "GET",
			// url: '/api/entitiesDataCount/entitiesChangeCount',
			// url: "/api/entitiesDataCount/sanCountByYear",
			url: "/api/entitiesDataInfo/getCountByDomianAndType",
			params: {
				domianId,
				typeId
			}
		})
	);
}

/**
 * 新增实体数量增长趋势
 * @returns {Promise<{
 * year: string
 * percent: string //百分比'20%'
 * }>}
 */
export function getEntitiesGrowthTrend() {
	return request200(
		request({
			method: "GET",
			url: "/api/entitiesDataCount/entitiesGrowthTrend"
		})
	);
}

/**
 * 实体清单更新频率
 * @returns {Promise<{
 * series: number[]
 * xAxis: string[]
 * }>}
 */
export function getEntitiesUpdateCount(sanTypeId = 1) {
	return request200(
		request({
			method: "GET",
			url: "/api/entitiesDataCount/getAnnualCount",
			params: {
				sanTypeId
			}
		})
	);
}

/**
 * 制裁领域分析
 */
export function getSanDomainCount(rule) {
	return request200(
		request({
			method: "GET",
			url: "/api/entitiesDataCount/getSanDomainCount",
			params: {
				rule
			}
		})
	);
}

/**
 * 上市企业制裁强度
 */
export function getSanStrength() {
	return request200(
		request({
			method: "GET",
			url: "/api/entitiesDataInfo/listedEntity/sanInfo"
		})
	);
}

/**
 * 新增实体领域分布情况
 * @param {string} sanTime - 开始时间，格式为 'YYYY-MM-DD'
 * @returns {Promise<{
 * value: number
 * name: string
 * }[]>}
 */
export function getEntitiesAreaCountByYear(sanTime) {
	return request200(
		request({
			method: "GET",
			url: "/api/entitiesDataCount/entitiesAreaCountByYear",
			params: {
				sanTime
			}
		})
	);
}

/**
 * 历次制裁涉及领域数
 * @returns {Promise<{
 * series: number[]
 * xAxis: string[]
 * }>}
 */
export function getEntitiesDomainCount() {
	return request200(
		request({
			method: "GET",
			url: "/api/entitiesDataInfo/getPreviousDomian"
		})
	);
}

/**
 * 指定领域每年实体数量查询
 * @param {string} param
 * @returns {Promise<{
 * year: string
 * count: number
 * }[]>}
 */
export function getCountThisDomain(param) {
	return request200(
		request({
			method: "GET",
			url: "/api/entitiesDataCount/countThisDomain",
			params: {
				param
			}
		})
	);
}

/**
 * 历次制裁涉及类别数量
 * @returns {Promise<{
 * year: string
 * count: number
 * }[]>}
 */
export function getCountTypeByYear() {
	return request200(
		request({
			method: "GET",
			url: "/api/entitiesDataCount/countTypeByYear"
		})
	);
}

/**
 * 新增实体类别统计
 * @param {string} startTime - 开始时间，格式为 'YYYY-MM-DD'
 * @returns {Promise<{
 * type: string
 * count: number
 * }[]>}
 */
export function getCountSanTypeByTime(startTime) {
	return request200(
		request({
			method: "GET",
			// url: "/api/entitiesDataCount/countSanTypeByTime",
			url: "/api/entitiesDataInfo/getTypeDistribution",
			params: {
				sanctionDate: startTime || "2025-11-11"
			}
		})
	);
}

/**
 * 指定类别每年实体数量查询
 * @param {string} param
 * @returns {Promise<{
 * year: number
 * count: number
 * }[]>}
 */
export function getCountThisType(param) {
	return request200(
		request({
			method: "GET",
			url: "/api/entitiesDataCount/countThisType",
			params: {
				param
			}
		})
	);
}

/**
 * 产业链信息查询
 */
export function getChainFishbone(chainId) {
	return request200(
		request({
			method: "GET",
			url: "/api/chain/getChainFishbone",
			params: {
				chainId
			}
		})
	);
}

/**
 * 产业链结构信息查询
 */
export function getChainStructure(chainId = "") {
	return request200(
		request({
			method: "GET",
			url: "/api/chain/getChainTree",
			params: {
				chainId
			}
		})
	);
}

/**
 * 产业链实体列表查询
 */
export function getChainEntities(date, id) {
	return request200(
		request({
			method: "GET",
			url: "/api/entitiesDataInfo/getEntityList",
			params: {
				sanctionDate: date,
				domainId: id
			}
		})
	);
}

/**
 * 上市企业融资变化情况
 */
export function getEntityFinancing() {
	return request200(
		request({
			method: "GET",
			url: "/api/entitiesDataInfo/listedEntity/financing"
		})
	);
}

/**
 * 上市企业市值变化情况
 */
export function getEntityMarketValue() {
	return request200(
		request({
			method: "GET",
			url: "/api/entitiesDataInfo/listedEntity/market"
		})
	);
}

/**
 * 重点上市企业列表
 */
export function getKeyListedEntityList(date, keyword = "") {
	return request200(
		request({
			method: "GET",
			url: "/api/entitiesDataInfo/listedEntity/keyEntity",
			params: {
				sanctionDate: date,
				searchText: keyword
			}
		})
	);
}
