import { api } from '../api'
import { request } from 'umi'

export interface ListType {
    // startTime?: string,
    // endTime?: string,
    timeUnit?: 'minute' | 'hour' | 'day',//时间维度
    adgroup?: any,//广告ID
    campaign?: any,//计划ID
    accountId?: any,//广告账户
    sysUserId?: any,//投手
    pageNum?: number,//页数
    pageSize?: number//条数
    sortField?: string, // "排序字段"
    sort?: 'ASC' | 'DESC'  // 排序方式
    topN?: number
    groupAccountIds?: number[]
    adgroupName?: string // 广告名称
}

/** 获取起量广告列表总表 */
export async function getPlanListApi(params: ListType) {
    Object.keys(params).forEach(key => {
        if (!params[key as keyof typeof params]) {
            delete params[key as keyof typeof params]
        }
    })
    return request(`${api}/tencentMonitor/adControl/up/ad/list`, {
        method: 'POST',
        data: params
    })
}
/** 获取起量广告列表明细 */
export async function getDetailListApi(params: ListType) {
    Object.keys(params).forEach(key => {
        if (!params[key as keyof typeof params]) {
            delete params[key as keyof typeof params]
        }
    })
    return request(`${api}/tencentMonitor/adControl/up/ad/detail/list`, {
        method: 'POST',
        data: params
    })
}

export interface ListHourProps {
    pageNum: number,
    pageSize: number,

    accountIdStr?: string, // 广告账号（字符串文本，随便输，可以输入多个）

    adCreateTimeMin?: string // 广告创建时间
    adCreateTimeMax?: string // 

    adgroupIdStr?: string // 广告 id（字符串文本，随便输，可以输入多个）
    columns?: string[] // 列

    conversionsCountDayMin?: number, // 最低今日转化数
    conversionsCountTotalMin?: number // 最低总转化数
    costDayMin?: number, // 最低今日消耗
    costTotalMin?: number,  // 最小总消耗

    promotedObjectName?: string // 推广目标名称
    promotedObjectType?: string // 推广目标类型
    sortAsc?: boolean,    // 是否顺序
    sortColumn?: string,  // 排序列
    sysUserIds?: number[],
    thousandDisplayPriceDayMin?: number,  // 最低今日千次曝光成本
    thousandDisplayPriceTotalMin?: number  // 最低总千次曝光成本
}
export async function getListForHourApi(params: ListHourProps) {
    return request(`${api}/tencentMonitor/adData/listForHour`, {
        method: 'POST',
        data: params
    })
}

/**
 * 3.0
 * @param params 
 * @returns 
 */
export async function getListForHourV3Api(params: ListHourProps) {
    return request(`${api}/tencentMonitor/adV3Data/listForHour`, {
        method: 'POST',
        data: params
    })
}

/** 获取起量广告列表5min */
export async function getMinuteListApi(params: ListType) {
    Object.keys(params).forEach(key => {
        if (!params[key as keyof typeof params]) {
            delete params[key as keyof typeof params]
        }
    })
    return request(`${api}/tencentMonitor/adControl/up/ad/detail/list/minute`, {
        method: 'POST',
        data: params
    })
}

/** 获取今日计划总消耗图谱 折线图 */
export async function getTotalCostApi(params: ListType) {
    Object.keys(params).forEach(key => {
        if (!params[key as keyof typeof params]) {
            delete params[key as keyof typeof params]
        }
    })
    return request(`${api}/tencentMonitor/adControl/total/cost/list`, {
        method: 'GET',
        params
    })
}

/** 获取计划消耗图谱 z柱状图 */
export async function getPlanCostApi(params: ListType) {
    Object.keys(params).forEach(key => {
        if (!params[key as keyof typeof params]) {
            delete params[key as keyof typeof params]
        }
    })
    return request(`${api}/tencentMonitor/adControl/ad/cost/list`, {
        method: 'GET',
        params
    })
}

/** 获取消耗速度详情(弹窗) */
export async function getCostSpeedApi(params: ListType) {
    Object.keys(params).forEach(key => {
        if (!params[key as keyof typeof params]) {
            delete params[key as keyof typeof params]
        }
    })
    return request(`${api}/tencentMonitor/adControl/cost/speed/info`, {
        method: 'POST',
        data: params
    })
}

/** 获取组员 */
export async function getUserGroupApi() {
    return request(`${api}/erp/userGroup/memberUser/`, {
        method: 'GET'
    })
}


export interface allPlanProps {
    pageNum?: number,
    pageSize?: number,
    adSeat?: any[],// 广告位置 1 : 朋友圈信息流, 2 : 公众号文章底部, 3 : 公众号文中广告, 4 : 公众号文章视频贴片, 5 : 小程序banner广告, 6 : 激励式广告
    createStartTime?: string, // 创建开始时间
    createEndTime?: string,  // 创建结束时间
    adgroup?: string,  // 广告ID & 广告名称
    creativeId?: string,  // 创意ID & 创意名称
    creativeStatus?: 1 | 2 | 3,  // 创意状态, 0或者null : 全部, 1 : 审核通过, 2 : 审核中, 3 : 未通过
    dataStartTime?: string,  // 数据结束日期(格式 : yyyy-MM-dd)
    dataEndTime?: string,  // 数据结束日期(格式 : yyyy-MM-dd)
    followStatus?: 1 | 2,  // 关注状态, 0或者null : 全部, 1 : 已关注, 2 : 未关注
    mainBookList?: any[],   // 主投书籍列表
    accountId?: any,  // 投放计划数据源, 账号
    sysUserId?: any,  // 投手id列表
    campaign?: string, // 计划id或者计划名称
    adStatus?: number, // 计划状态, 0或者null : 全部, 1 : 投放中, …依次递增
    sortField?: string, // "排序字段"
    sort?: 'ASC' | 'DESC',  // 排序方式
}
/** 计划列表 */
export async function getAllPlanListApi(data: allPlanProps) {
    Object.keys(data).forEach(key => {
        if (!data[key as keyof typeof data]) {
            delete data[key as keyof typeof data]
        }
    })
    return request(`${api}/tencentMonitor/adControl/all/ad/list`, {
        method: 'POST',
        data
    })
}


/** 获取所有书籍 */
export async function getBookListAllApi() {
    return request(`${api}/erp/book/all`, {
        method: 'GET'
    })
}

/** 下载 */
/** 下载起量广告总表 */
export async function downLoadUpAdApi(params: ListType) {
    return request(`${api}/tencentMonitor/adExcel/up/ad/list`, {
        method: 'POST',
        data: params,
        responseType: 'blob'
    })
}

/** 下载单个广告明细表(小时) */
export async function downLoadDetailApi(params: ListType) {
    return request(`${api}/tencentMonitor/adExcel/up/ad/detail/list`, {
        method: 'POST',
        data: params,
        responseType: 'blob'
    })
}

/** 下载单个广告明细表(5分钟) */
export async function downLoadDetailMinuteApi(params: ListType) {
    return request(`${api}/tencentMonitor/adExcel/up/ad/detail/list/minute`, {
        method: 'POST',
        data: params,
        responseType: 'blob'
    })
}

/** 下载消耗速度详情(弹窗) */
export async function downLoadSpeedApi(params: ListType) {
    return request(`${api}/tencentMonitor/adExcel/cost/speed/info`, {
        method: 'POST',
        data: params,
        responseType: 'blob'
    })
}

/** 下载计划列表 */
export async function downLoadAllAdListApi(data: allPlanProps) {
    Object.keys(data).forEach(key => {
        if (!data[key as keyof typeof data]) {
            delete data[key as keyof typeof data]
        }
    })
    return request(`${api}/tencentMonitor/adExcel/all/ad/list`, {
        method: 'POST',
        data,
        responseType: 'blob'
    })
}


// 监控数据源
// 查询分组
export async function getAdGroupListApi() {
    return request(`${api}/tencentMonitor/adGroup/list`, {
        method: 'GET'
    })
}

// 查询分组
export async function addEditGroupApi(data: { groupName: string, id?: number, remark?: string }) {
    return request(`${api}/tencentMonitor/adGroup/add/or/update`, {
        method: 'POST',
        data
    })
}

// 删除分组
export async function deleteAdGroupApi(params: { id: number }) {
    return request(`${api}/tencentMonitor/adGroup/delete`, {
        method: 'DELETE',
        params
    })
}

// 分组账号查询
export interface AccountListProps {
    // beginDate?: string,
    // endDate?: string,
    groupId?: number,
    mpAppIdList?: string[],
    pitcherIdList?: string[],
    pageNum: number,
    pageSize: number
}
export async function getAccountListApi(data: AccountListProps) {
    Object.keys(data).forEach(key => {
        if (!data[key as keyof typeof data]) {
            delete data[key as keyof typeof data]
        }
    })
    return request(`${api}/tencentMonitor/adGroup/accountList`, {
        method: 'POST',
        data
    })
}

// 账号绑定或者解绑分组
export async function addDelAccountApi(data: { accountIdList: number[], groupId: number, type: 0 | 1 }) {
    return request(`${api}/tencentMonitor/adGroup/account/update`, {
        method: 'POST',
        data
    })
}

/**
 * NEW 广告消耗趋势 获取今日计划总消耗图谱 折线图
 * @param data 
 * @returns 
 */
export async function getCostTrendListApi(data: ListType) {
    Object.keys(data).forEach(key => {
        if (!data[key as keyof typeof data]) {
            delete data[key as keyof typeof data]
        }
    })
    return request(`${api}/tencentMonitor/adData/costTrend`, {
        method: 'POST',
        data
    })
}

/**
 * NEW 3.0 广告消耗趋势 获取今日计划总消耗图谱 折线图
 * @param data 
 * @returns 
 */
export async function getCostTrendV3ListApi(data: ListType) {
    Object.keys(data).forEach(key => {
        if (!data[key as keyof typeof data]) {
            delete data[key as keyof typeof data]
        }
    })
    return request(`${api}/tencentMonitor/adV3Data/costTrend`, {
        method: 'POST',
        data
    })
}

/**
 * NEW 广告起量排行
 * @param params 
 * @returns 
 */
export async function getCostTopListApi(data: ListType) {
    Object.keys(data).forEach(key => {
        if (!data[key as keyof typeof data]) {
            delete data[key as keyof typeof data]
        }
    })
    return request(`${api}/tencentMonitor/adData/costTop`, {
        method: 'POST',
        data
    })
}

/**
 * NEW3.0 广告起量排行
 * @param params 
 * @returns 
 */
export async function getCostTopV3ListApi(data: ListType) {
    Object.keys(data).forEach(key => {
        if (!data[key as keyof typeof data]) {
            delete data[key as keyof typeof data]
        }
    })
    return request(`${api}/tencentMonitor/adV3Data/costTop`, {
        method: 'POST',
        data
    })
}


export interface GetColumnTrendProps {
    accountId: number
    adgroupId: number
    timeUnit: 'day' | 'hour' | 'minute'
    trendColumns: string[]

    dataTimeMin?: string
    dataTimeMax?: string
}
/**
 * NEW 广告指标趋势
 * @param data 
 * @returns 
 */
export async function getColumnTrendApi(data: GetColumnTrendProps) {
    return request(`${api}/tencentMonitor/adData/columnTrend`, {
        method: 'POST',
        data
    })
}

/**
 * NEW3.0 广告指标趋势
 * @param data 
 * @returns 
 */
export async function getColumnTrendV3Api(data: GetColumnTrendProps) {
    return request(`${api}/tencentMonitor/adV3Data/columnTrend`, {
        method: 'POST',
        data
    })
}

/**
 * NEW 广告数据-指定广告的总数据
 * @param data 
 * @returns 
 */
export async function getAdTotalDataApi(data: { accountId: number, adgroupId: number, columns: string[] }) {
    return request(`${api}/tencentMonitor/adData/adTotalData`, {
        method: 'POST',
        data
    })
}

/**
 * NEW3.0 广告数据-指定广告的总数据
 * @param data 
 * @returns 
 */
export async function getAdTotalDataV3Api(data: { accountId: number, adgroupId: number, columns: string[] }) {
    return request(`${api}/tencentMonitor/adV3Data/adTotalData`, {
        method: 'POST',
        data
    })
}

export interface GetListForAdProps {
    accountId: number,
    adgroupId: number,
    columns: string[],
    pageNum: number,
    pageSize: number,
    timeUnit: 'day' | 'hour' | 'minute'
    sortAsc?: boolean
    dataTimeMin?: string
    dataTimeMax?: string
}
/**
 * NEW 广告数据列表-指定广告的所有数据
 * @param data 
 * @returns 
 */
export async function getListForAdApi(data: GetListForAdProps) {
    return request(`${api}/tencentMonitor/adData/listForAd`, {
        method: 'POST',
        data
    })
}

/**
 * NEW3.0 广告数据列表-指定广告的所有数据
 * @param data 
 * @returns 
 */
export async function getListForAdV3Api(data: GetListForAdProps) {
    return request(`${api}/tencentMonitor/adV3Data/listForAd`, {
        method: 'POST',
        data
    })
}


/**
 * 广告列表指标趋势
 */
export interface GetAdColumnTrendProps {
    timeUnit: 'day' | 'hour',
    pageParams: AdListProps,
    trendColumns: string[]
    dataTimeMin?: string,
    dataTimeMax?: string,
}
export async function getAdColumnTrendApi(data: GetAdColumnTrendProps) {
    return request(`${api}/tencentMonitor/ad/columnTrend`, {
        method: 'POST',
        data
    })
}

/**
 * v3指标趋势
 * @param data 
 * @returns 
 */
export async function getAdV3ColumnTrendApi(data: GetAdColumnTrendProps) {
    return request(`${api}/tencentMonitor/adV3/columnTrend`, {
        method: 'POST',
        data
    })
}

export interface AdListProps {
    pageNum: number
    pageSize: number
    columns: string[]
    adCreateTimeMax?: string
    adCreateTimeMin?: string
    conversionsCountDayMin?: number
    conversionsCountTotalMin?: number,
    costDayMin?: number
    costTotalMin?: number
    thousandDisplayPriceDayMin?: number
    thousandDisplayPriceTotalMin?: number
    promotedObjectName?: string
    accountIdStr?: string,
    accountMemo?: string,
    accountRemark?: string,
    adgroupIdStr?: string,
    adgroupName?: string
    isDeepConversionSpec?: boolean
    isDeleted?: boolean
    optimizationGoal?: string
    promotedObjectType?: string

    sortAsc?: boolean
    sortColumn?: string
    status?: string
    sysUserIds?: number

    putDateBegin?: string,
    putDateEnd?: string

    groupAccountIds?: number[]

    dataTimeMin?: string
    dataTimeMax?: string
}
export async function getAdListApi(data: AdListProps) {
    return request(`${api}/tencentMonitor/ad/list`, {
        method: 'POST',
        data
    })
}

/**
 * 3.0
 * @param data 
 * @returns 
 */
export async function getAdV3ListApi(data: AdListProps) {
    return request(`${api}/tencentMonitor/adV3/list`, {
        method: 'POST',
        data
    })
}

/**
 * 广告详情
 * @param param0 
 * @returns 
 */
export async function getAdgroupsDetailsApi({ accountId, adgroupId }: { accountId: any, adgroupId: any }) {
    return request(`${api}/adq/adgroups/detail/${accountId}/${adgroupId}`, {
        method: 'POST'
    })
}
/**
 * 广告详情3.0
 * @param param0 
 * @returns 
 */
export async function getAdgroupDetailsApi({ accountId, adgroupId }: { accountId: any, adgroupId: any }) {
    return request(`${api}/adq/adgroup/detail/${accountId}/${adgroupId}`, {
        method: 'GET'
    })
}

/**
 * 移除广告的告警规则---广告告警规则表的操作
 * @param param0 
 * @returns 
 */
export async function delAdWarningRuleApi({ accountId, adgroupId, ruleId }: { accountId: any, adgroupId: any, ruleId: number }) {
    return request(`${api}/adq/sysWarningRule/removeAdgroup/${accountId}/${adgroupId}/${ruleId}`, {
        method: 'PUT'
    })
}

/**
 *  将指定广告加入规则黑名单---默认列表 + 广告账号列表的操作
 * @param param0 
 * @returns 
 */
export async function addAdToRuleBlackListApi({ accountId, adgroupId, ruleId }: { accountId: any, adgroupId: any, ruleId: number }) {
    return request(`${api}/adq/sysWarningRule/configRuleBlackList/${accountId}/${adgroupId}/${ruleId}`, {
        method: 'PUT'
    })
}

/**
 * 将广告移出告警规则的黑名单---黑名单列表的操作
 * @param param0 
 * @returns 
 */
export async function delAdToRuleBlackListApi({ accountId, adgroupId, ruleId }: { accountId: any, adgroupId: any, ruleId: number }) {
    return request(`${api}/adq/sysWarningRule/removeBlackList/${accountId}/${adgroupId}/${ruleId}`, {
        method: 'PUT'
    })
}

/**
 * 配置广告的告警规则
 * @param param0 
 * @returns 
 */
export async function configAdWarningRuleApi({ accountId, adgroupId, ruleIds }: { accountId: any, adgroupId: any, ruleIds: string }) {
    return request(`${api}/adq/sysWarningRule/configRuleAdgroup/${accountId}/${adgroupId}/${ruleIds}`, {
        method: 'PUT'
    })
}


/**
 * 配置账号下告警
 * @param param0 
 * @returns 
 */
export async function configRuleAccountApi({ accountIds, ruleId }: { accountIds: string, ruleId: string }) {
    return request(`${api}/adq/sysWarningRule/configRuleAccount/${accountIds}/${ruleId}`, {
        method: 'PUT'
    })
}

export interface GetDynamicCreativeMonitorProps {
    pageNum: number
    pageSize: number
    columns: string[]
    sortAsc?: boolean
    sortColumn?: string
    accountIdStr?: string,
    adgroupIds?: string[],              // 广告id
    adgroupName?: string,               // 广告名称
    configuredStatus?: string[]         // 创意启用状态
    conversionsCountTotalMin?: number,  // 最低转化数
    costTotalMin?: number               // 最小总消耗

    createdTimeMax?: string,            // 创意创建时间 max
    createdTimeMin?: string,            // 	创意创建时间 min

    dataTimeMin?: string                // 创意数据时间（end）
    dataTimeMax?: string                // 创意数据时间（begin）

    deliveryMode?: string               // 投放模式
    dynamicCreativeId?: string[],       // 创意id
    dynamicCreativeName?: string        // 创意名称
    groupAccountIds?: number[],         // 广告分组
    isDeleted?: boolean,                // 是否删除
    sysUserIds?: number                 // 投手ID
    thousandDisplayPriceTotalMin?: number // 最低总千次曝光成本
}

/**
 * 创意数据
 * @param data 
 * @returns 
 */
export async function getDynamicCreativeMonitorListApi(data: GetDynamicCreativeMonitorProps) {
    return request(`${api}/tencentMonitor/dynamicCreativeMonitor/list`, {
        method: 'POST',
        data
    })
}