/*
 * @Author: tengmingxue 1473375109@qq.com
 * @Date: 2023-09-11 19:36:34
 * @LastEditors: tengmingxue 1473375109@qq.com
 * @LastEditTime: 2023-09-25 21:47:12
 * @FilePath: \xld-gis-admin\src\api\resource\examine.ts
 * @Description: 流程配置信息api
 */
import { deleteBussinfo } from './plat';
import { defHttp } from '/@/utils/http/axios';

enum Api {
    SelectFlowInfo = '/base-center/flow/selectFlowInfo',   //查询流程信息
    SaveFlowInfo = '/base-center/flow/saveFlowInfo',        //新增、更新流程信息
    SaveFlowInfoAll = '/base-center/flow/insertFlowInfoAll', //新增流程、节点、节点人员信息
    DelFLowInfo = '/base-center/flow/deleteFlowInfo',    //删除流程
    SelectLowConfig = '/base-center/flow/selectFlowInfoAll',  //查询流程、节点、节点人员信息
    SubmitExamine = '/base-center/flow/saveBussinfo',//发起申请
    SubminExamineResult = '/base-center/flow/submitBussinfo',//审核操作
    GetFlowInfoAll = '/base-center/flow/getFlowInfoAll',//获取所有业务
    GetFlowInfoDB = '/base-center/flow/getFlowInfoDB',//查询待办业务
    GetFlowInfoYB = '/base-center/flow/getFlowInfoYB',//查询待办业务
    GetResourceGroup = '/datasearch-center/space/getResourceGroup',//分组统计所有资源数量
    SelectFlowRecord = "/base-center/flow/selectFlowRecord",//查询业务流转记录信息
    SelectBussinfoById = '/base-center/flow/selectBussinfoById'
}


/**
 * @description：分组统计所有资源数量
*/
export const getResourceGroup = () => {
    return new Promise<void>((resolve) => {
        defHttp.post({
            url: Api.GetResourceGroup
        }).then((res) => {
            resolve(res)
        })
    })
}
/**
 * @description: 查询流程信息
 * @param: page:页数              must
 * @param: rows:每页数据条数       must
 * @param: keyStr:模糊查询关键字
 * @param: id:流程id
 */
export const queryFlowInfoPage = (params) => {
    params['rows'] = params?.pageSize ? params?.pageSize : 10
    return new Promise<void>((resolve) => {
        defHttp.post({ url: Api.SelectFlowInfo, params: params }).then((res) => {
            const result = res.resp_code === 0 ? res.datas.pageData : []
            result.forEach(item => {
                item['id'] = item['ID']
            });
            result.sort((a, b) => { return new Date(a.CREATETIME) - new Date(b.CREATETIME) });
            resolve(result)
        })
    })
};

/**
 * @description：根据流程业务id查询流程配置所有信息
 * @param: ids：字符串数组
*/
export const queryFlowInfoById = (param) => {
    const data = param.toString()
    // const dataFrom = new FormData()
    // dataFrom.append('',data)
    return new Promise<void>((resolve) => {
        defHttp.post({
            url: Api.SelectLowConfig,
            data,
            headers: {
                'Content-Type': 'application/json;charset=UTF-8'
            },
        }).then((res) => {
            const result = (res.resp_code === 0 && res.datas.length > 0) ? res.datas[0] : null
            resolve(result)
        })
    })
}




/**
 * @description：根据流程业务查询流程配置所有信息
 * @param: ids：字符串数组
*/
export const queryFlowInfo = async (params) => {
    return new Promise<void>((resolve) => {
        defHttp.post({
            url: Api.SelectLowConfig,
            params,
            headers: {
                'Content-Type': 'application/json;charset=UTF-8',
            },
        }).then((res) => {
            const result = (res.resp_code === 0) ? res.datas : null
            resolve(result)
        })
    })
}

/**
 * @description: 一次提交流程配置信息
 * @param: flowInfo:流程信息              must
 * @param: flowNode:流程步骤结点信息       must
 * @param: flowNodePerson:流程审核人信息   must
 * 
 */
export const selectFlowRecord = (id) => {
    var params = {
        "bussInfoId": id,
        "keyStr": "",
        "page": 1,
        "rows": 10
    }
    return new Promise<void>((resolve) => {
        defHttp.post({ url: Api.SelectFlowRecord, params }).then((res) => {
            resolve(res)
        })
    })
};

/**
 * @description: 一次提交流程配置信息
 * @param: flowInfo:流程信息              must
 * @param: flowNode:流程步骤结点信息       must
 * @param: flowNodePerson:流程审核人信息   must
 * 
 */
export const addFlowInfoAll = (params) => {
    return new Promise<void>((resolve) => {
        defHttp.post({ url: Api.SaveFlowInfoAll, params }).then((res) => {
            resolve(res)
        })
    })
};


/**
 * @description: 批量删除流程信息
 * @param:  ids:流程信息id       must
 * 
 */
export const delFlowInfo = (params) => {
    const data = params.toString()
    return new Promise<void>((resolve) => {
        defHttp.post({
            url: Api.DelFLowInfo, data, headers: {
                'Content-Type': 'application/json;charset=UTF-8'
            },
        }).then((res) => {
            resolve(res)
        })
    })
};

/**
 * 根据业务类型名称获取流程配置
*/
export const getFlowConfigByBusinessName = async (name) => {
    return new Promise<void>(async (resolve) => {
        const params = {
            page: 1,
            pageSize: 200,
        }
        const list = await queryFlowInfoPage(params)
        let result = null
        if (list.length > 0) {
            const obj = list.find(item => item['FLOWNAME'] === name)
            if (obj) {
                const data = {
                    flowInfo: {
                        id: obj['ID']
                    }
                }
                result = await queryFlowInfo(data)
            }
            else result = null
        }
        else {
            result = null
        }
        resolve(result)
    })
}



/**
 * @description: 提交审核信息
 * @param: bussInfo:流程信息              must
 * @param: bussInfo.bussname   业务名称（流程名称）
 * @param: bussInfo.flowid     流程id
 * @param: bussInfo.serverids  资源id
 * 
 */
export const submitExamine = async (params, obj) => {
    var res = true;
    if (obj?.resInCarId) {
        res = await deleteBussinfo({ id: obj.resInCarId })
    }
    if (res) {
        return new Promise<void>((resolve) => {
            defHttp.post({ url: Api.SubmitExamine, params }).then((res) => {
                resolve(res)
            })
        })
    }
};


/**
 * @description: 提交审核结果
 * @param: bussInfoId:  业务id
 * @param: ispass       是否通过
 * @param: opinion      意见           required:false
 * @param: userId       用户ID         required:false
 * 
 */
export const subminExamineResult = (params) => {
    return new Promise<void>((resolve) => {
        defHttp.post({ url: Api.SubminExamineResult, params }).then((res) => {
            resolve(res)
        }).catch(() => {
            resolve()
        })
    })
};

/**
 * @description: 查询待办的业务信息
 * @param: page:页数              must
 * @param: rows:每页数据条数       must
 * @param: keyStr:模糊查询关键字
 * @param: userId:用户id
 */
export const queryTaskInfoPage = (params) => {
    console.log("dbyb:", params)
    // params['rows'] = params?.pageSize ? params?.pageSize : 10
    params.shtype = "1";
    //params.serviceType = "MR";//"MR,SR,ER,DR,interface"
    return new Promise<void>((resolve) => {
        defHttp.post({ url: Api.GetFlowInfoDB, params: params }).then((res) => {
            const result = res.resp_code === 0 ? res.datas.pageData : []
            // result.forEach(item => {
            //     item['id'] = item['ID']
            // });
            resolve(result)
            // resolve(res)
        })
    })
};
/**
 * @description: 查询已办的业务信息
 * @param: page:页数              must
 * @param: rows:每页数据条数       must
 * @param: keyStr:模糊查询关键字
 * @param: userId:用户id
 */
export const queryTaskYbInfoPage = (params) => {
    // params['rows'] = params?.pageSize ? params?.pageSize : 10
    console.log("dbyb:", params)
    params.shtype = "1";
    //params.serviceType = "MR";//"MR,SR,ER,DR,interface"//mr是地图资源，dr是文件，er场景，sr组件
    return new Promise<void>((resolve) => {
        defHttp.post({ url: Api.GetFlowInfoYB, params: params }).then((res) => {
            const result = res.resp_code === 0 ? res.datas.pageData : []
            // result.forEach(item => {
            //     item['id'] = item['ID']
            // });
            resolve(result)
            // resolve(res)
        })
    })
};

/**
 * @description: 根据id查询业务信息
 * @param: bussid:业务id
 * @param: page:页数              must
 * @param: rows:每页数据条数       must
 * @param: keyStr:模糊查询关键字
 * @param: userId:用户id
 */
export const queryBussInfoById = (params) => {
    return new Promise<void>((resolve) => {
        defHttp.post({ url: Api.SelectBussinfoById, params: params }).then((res) => {
            resolve(res)
        })
    })
};
