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"
		})
	);
}

// 最新实体清单信息查询
/**
 * @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() {
	return request200(
		request({
			method: "GET",
			// url: "/api/entitiesDataCount/industryCountByYear"
			url: "/api/entitiesDataCount/getAnnualCount"
		})
	);
}

/**
 * 不同领域每年数量
 * @returns {Promise<{
 * data:{
 *   year?: number
 *   domainNum: {
 *    [ key: string]: number;
 *   }
 * }[]
 * domains: string[]
 * }>}
 */
export function getCountDomainByYear() {
	return request200(
		request({
			method: "GET",
			url: "/api/entitiesDataCount/countDomainByYear"
		})
	);
}

/**
 * 历次制裁过程（月份统计）
 * @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
			}
		})
	);
}

/**
 * 查询重点人物
 * @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
			}
		})
	);
}

/**
 * 不同领域实体统计
 * @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) {
	return request200(
		request({
			method: "POST",
			url: "/api/sanctionList/pageQuery",
			data: {
				typeName,
				pageNum,
				pageSize
			}
		})
	);
}

/**
 * 数量比对分析
 * @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",
			params: {
				startTime
			}
		})
	);
}

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

/**
 * 新增实体数量增长趋势
 * @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() {
	return request200(
		request({
			method: "GET",
			url: "/api/entitiesDataCount/entitiesUpdateCount"
		})
	);
}

/**
 * 新增实体领域分布情况
 * @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/entitiesDataCount/entitiesDomainCount"
		})
	);
}

/**
 * 指定领域每年实体数量查询
 * @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",
			params: {
				startTime
			}
		})
	);
}

/**
 * 指定类别每年实体数量查询
 * @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"
		})
	);
}
