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",
			url: "/api/sanctionList/export/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
			}
		})
	);
}

/**
 * 涉及50%规则的实体数
 */
export function getEntitiesCountBy50PercentRules(
	typeName = "实体清单",
	pageNum = 1,
	pageSize = 10,
	sanctionDate = "",
	isCn = false
) {
	return request200(
		request({
			method: "POST",
			url: "/api/sanctionList/getRuleCount",
			data: {
				typeName,
				pageNum,
				pageSize,
				sanctionDate,
				isCn
			}
		})
	);
}

/**
 * 获取前序事件
 */
// /entitiesDataInfo/precedingEvents
export function getPrecedingEvents(startTime = "2025-11-11", pageNum = 1, pageSize = 10) {
	return request200(
		request({
			method: "POST",
			url: "/api/entitiesDataInfo/precedingEvents",
			data: {
				sanctionDate: startTime,
				pageNum,
				pageSize
			}
		})
	);
}

/**
 * 领域分布查询
 */
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 = "", isCn = false) {
	return request200(
		request({
			method: "POST",
			url: "/api/sanctionList/pageQuery",
			data: {
				typeName,
				pageNum,
				pageSize,
				sanctionDate,
				isCn
			}
		})
	);
}

/**
 * 数量比对分析
 * @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",
			url: "/api/entitiesDataInfo/annual/researchInstitute",
			params: {
				param
			}
		})
	);
}

/**
 * 企业类实体历史制裁情况
 */
export function getEnterpriseSanCount() {
	return request200(
		request({
			method: "GET",
			url: "/api/entitiesDataInfo/annual/enterprise"
		})
	);
}

/**
 * 根据领域获取产业链信息
 */
export function getChainInfoByDomainId(domainId) {
	return request200(
		request({
			method: "GET",
			url: "/api/chain/getChainInfo",
			params: {
				domainId
			}
		})
	);
}

/**
 * 产业链中国企业实体信息查询
 */
export function getCnEntityOnChain(chainId) {
	return request200(
		request({
			method: "GET",
			url: "/api/chain/getChainEntityStat",
			params: {
				chainId
			}
		})
	);
}

/**
 * 产业链信息查询
 */
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
			}
		})
	);
}

/**
 * 获取科学仪器对美依赖数量
 */
export function getScientificInstrumentDependencyCount(orgIds = "12100000400012916R,121000004013595328,12100000435230200R") {
	return request200(
		request({
			method: "GET",
			url: "/api/instrument/getDependencyUS",
			params: {
				orgIds
			}
		})
	);
}

/**
 * 科学仪器近十年对美依赖度升高风险分析
 * url: /instrument/getDependencyUSRisk
 */
export function getScientificInstrumentDependencyRisk() {
	return request200(
		request({
			method: "GET",
			url: "/api/instrument/getDependencyUSRisk"
		})
	);
}

/**
 * 科学仪器近十年国产化数量统计
 * url:/instrument/getLocalizationRisk
 */
export function getScientificInstrumentLocalizationRisk() {
	return request200(
		request({
			method: "GET",
			url: "/api/instrument/getLocalizationRisk"
		})
	);
}

/**
 * 科学仪器进口国统计
 * url: /instrument/getOriginCount
 */
export function getScientificInstrumentOriginCount(orgIds = "12100000400012916R,121000004013595328,12100000435230200R") {
	return request200(
		request({
			method: "GET",
			url: "/api/instrument/getOriginCount",
			params: {
				orgIds
			}
		})
	);
}

/**
 * 新增科研机构列表
 * url: /entitiesDataInfo/scientificImpact/entityList
 */
export function getScientificImpactEntityList(startTime) {
	return request200(
		request({
			method: "GET",
			url: "/api/entitiesDataInfo/scientificImpact/entityList",
			params: {
				sanctionDate: startTime || "2025-11-11"
			}
		})
	);
}


/**
 * 概览页--获取出口管制制裁措施
 * @param {Object} data
 * @param {string} [data.typeName="实体清单"] - 类型名称（实体清单、商业管制清单、军事最终用户清单）
 * @param {boolean} [data.isCn=true] - 是否只看涉华
 * @param {Array<string>} [data.techDomainIds] - 科技领域ID
 * @param {Array<number>} [data.years] - 制裁年份
 */
export function getExportControlList(data) {
	return request200(
		request({
			method: "POST",
			url: "/api/entitiesDataCount/getSanctionProcess",
			data
		})
	);
}



// 