import * as asyncHandler from 'express-async-handler';
import * as userBiz from '../biz/fuHuqQi/user';
import * as baseBiz from '../biz/fuHuqQi/base';
import * as enterpriseBiz from '../biz/enterprise/enterprise';
import * as financingBiz from '../biz/enterprise/financing';
import * as guanWeiHuiBiz from '../biz/guanWeiHui/user';
import { checkParamaterType, checkReqParam } from '../util/tools';
import { BizError } from '../util/bizError';
import { ERRORENUM } from '../config/errorEnum';
import { monthTableList, outPutMonthTableList } from '../biz/fuHuqQi/month';
import { OUTPUTDATATYPE, OUTPUTTYPE } from '../config/enum';


export function setRouter(httpServer) {
    // /**登录 */
    httpServer.post('/admin/login', asyncHandler(login));
    // /** 孵化器*/
    httpServer.post('/admin/fuhuaqi/baselist', asyncHandler(fuHuaQiBaseList));
    httpServer.post('/admin/fuhuaqi/monthoccupancyrate', asyncHandler(getMonthList));
    httpServer.post('/admin/fuhuaqi/userlist', asyncHandler(getFuHuaQiUserList));
    httpServer.post('/admin/fuhuaqi/updatestate', asyncHandler(updateState));
    httpServer.post('/admin/fuhuaqi/adduser',asyncHandler(addUser));
    /**企业 */
    httpServer.post('/admin/enterprise/list', asyncHandler(getEnterpriseList));
    httpServer.post('/admin/enterprise/financinglist', asyncHandler(getFinancingList));
    httpServer.post('/admin/enterprise/affiliationfuhuaqi',asyncHandler(affiliationFuHuaQi));
    /**导出数据 */
    httpServer.post('/admin/data/output/fuhuaqi', asyncHandler(outPutFuHuaQiData));
    httpServer.post('/admin/data/output/enterprise', asyncHandler(outPutEnterpriseData));
    httpServer.post('/admin/data/output/financing', asyncHandler(outPutFinancingData));
    httpServer.post('/admin/data/output/month', asyncHandler(outPutMonthData));
}
    

/**
 * 管理后台登录
 * @param req 
 * @param res 
 */
async function login(req, res) {
    let reqConf = {loginId:'String', pwd:'String'};
    let {loginId, pwd} = checkReqParam(reqConf, req.body);

    let userInfo = await guanWeiHuiBiz.login(loginId, pwd);
    res.success(userInfo);
}


/**
 * 孵化器基本信息列表
 * @param req 
 * @param res 
 */
async function fuHuaQiBaseList(req, res) {
    let reqConf = {lv: 'Number', institutionalNature: 'Number', industry: 'Number', page: 'Number' };
    let {lv, institutionalNature, industry, page} = checkParamaterType(reqConf, req.body);
    
    let result = await baseBiz.fuHuaQiBaseList(lv, institutionalNature, industry, page);

    res.success(result);
}


/**
 * 后台管理  获取月度出租率信息列表
 * @param req 
 * @param res 
 */
async function getMonthList(req, res) {
    let  reqConf = {state: 'Number', year: 'Number', month: 'Number', page: 'Number'};
    let { state, year, month, page } = checkParamaterType(reqConf, req.body);
    
    let result = await monthTableList(state, year, month, page);
    res.success(result);
}


/**
 * 后台管理 获取孵化器账号信息列表 
 * @param req 
 * @param res 
 */
 async function getFuHuaQiUserList(req, res) {
    let reqConf = {operationName: 'String', page: 'Number' };
    let { operationName, page } = checkParamaterType(reqConf, req.body);

    let result = await userBiz.fuHuaQiUserList(operationName, page);
    res.success(result);
}


/**
 * 后台管理 获取融资企业信息列表 
 * @param req 
 * @param res
 */
 async function getFinancingList(req, res) {
    let reqConf = {monthData: 'String', fuHuaQiName: 'String', fuHuaQiInvestment: 'Number', industry: '[Number]', page: 'Number' };
    let {monthData, fuHuaQiName, industry, fuHuaQiInvestment, page} = checkParamaterType(reqConf, req.body);

    let result = await financingBiz.financingList(monthData, fuHuaQiName, industry, fuHuaQiInvestment, page);
    res.success(result);
}


/**
 * 后台管理 获取在孵企业信息列表 
 * @param req 
 * @param res 
 */
 async function getEnterpriseList(req, res) {
    let reqConf = {time: 'Number', fuHuaQiUscc: 'String', industry: '[Number]', isNaturalPersonHolding: 'Number', page: 'Number' };
    let { time, fuHuaQiUscc, industry, isNaturalPersonHolding, page } = checkParamaterType(reqConf, req.body);

    let result = await enterpriseBiz.enterpriseList(time, fuHuaQiUscc, industry, isNaturalPersonHolding, page);
    res.success(result);
}


/**
 * 后台管理 修改孵化器账号状态 
 * @param req 
 * @param res 
 */
 async function updateState(req, res) {
    let reqConf = {userState: 'Boolean', uscc: 'String' };
    let { userState, uscc } = checkReqParam(reqConf, req.body);

    let result = await userBiz.updateState(uscc, userState);
    res.success(result);
}

/**
 * 后台管理 新增孵化器账号
 * @param req 
 * @param res 
 */
async function addUser(req, res) {
    let reqConf = {form: 'Object' };
    let { form } = checkReqParam(reqConf, req.body);

    let result = await userBiz.addUser(form);
    res.success(result);
} 


/**
 * 所属孵化器
 * @param req 
 * @param res 
 */
async function affiliationFuHuaQi(req, res) {
    let  result = await enterpriseBiz.getAffiliationFuHuaQi();
    res.success(result);
}


async function outPutFuHuaQiData(req, res) {
    let reqConf = {lv: 'Number', institutionalNature: 'Number', industry: 'Number', dataType:'Number', type:'Number', page:'Number' };
    let { lv, institutionalNature, industry, type, page } = checkParamaterType(reqConf, req.body);

    let dataList = [];
    if (type == OUTPUTTYPE.全部数据) dataList = await baseBiz.outPutFuHuaQiBaseData(lv, institutionalNature, industry);
    else  {
        let baseData = await baseBiz.fuHuaQiBaseList(lv, institutionalNature, industry, page);
        dataList = baseData.dataList;
    }
    
    let keyList = [
        "name","operationName","uscc","virtualEnterpriseNum","logonTime",
        "incubatedAcreage", "acreageTotal", "acreagePersonalUse", "lv", "identificationTime",
        "industry", "institutionalNature", "liaison", "liaisonPhone", "personInCharge",
        "personInChargePhone", "personInChargeAdd", "siteAcreage", "leasePrice"
    ];
    let titleList = [
        "名称", "运营机构名称", "统一信用代码", "虚拟企业数量", "注册时间",
        "在孵面积", "孵化器总面积", "孵化器自用面积", "孵化器级别", "认定时间",
        "领域", "机构性质", "联系人", "联系电话", "负责人",
        "负责人联系电话", "孵化场地地址", "孵化场地面积", "孵化场地出租单价"
    ];
    let result = [titleList];
    dataList.forEach(info => {
        let subList = [];
        keyList.forEach(subInfo => {
            subList.push(info[subInfo] || '');
        });
        result.push(subList);
    });

    res.success({data:result});
    
}


async function outPutEnterpriseData(req, res) {
    let reqConf = {time: 'Number', fuHuaQiUscc: 'String', industry: '[Number]', isNaturalPersonHolding: 'Number', page: 'Number', type:'Number' };
    let { time, fuHuaQiUscc, industry, isNaturalPersonHolding, page, type } = checkParamaterType(reqConf, req.body);
    
    let dataList = [];
    if (type == OUTPUTTYPE.全部数据) dataList = await enterpriseBiz.outPutEnterpriseData(time, fuHuaQiUscc, industry, isNaturalPersonHolding);
    else  {
        let baseData = await enterpriseBiz.enterpriseList(time, fuHuaQiUscc, industry, isNaturalPersonHolding, page);
        dataList = baseData.dataList;
    }

    let keyList = [
        "name", "createTime", "fuhuaqiUscc", "uscc", "industry", "logonTime",
        "firstIncubationTime", "timeOfImmigration", "isNaturalPersonHolding", "oldLogonAdd",
        "logonAdd", "operatingAdd", "leasedArea"
    ];
    let titleList = [
        "企业名称", "入库时间", "所属孵化器", "统一信用代码", "行业领域",
        "注册时间", "首次入孵时间", "迁入时间", "是否自然人控股", "迁入前注册地址",
        "注册地址", "经营地址", "租赁面积（平方米）"
    ];
    let result = [titleList];
    dataList.forEach(info => {
        let subList = [];
        keyList.forEach(subInfo => {
            subList.push(info[subInfo] || '');
        });
        result.push(subList);
    });

    res.success({data:result});
}

async function outPutFinancingData(req, res) {
    let reqConf = {monthData: 'String', fuHuaQiName: 'String', fuHuaQiInvestment: 'Number', industry: '[Number]', page: 'Number', type:'Number' };
    let {monthData, fuHuaQiName, industry, fuHuaQiInvestment, page, type} = checkParamaterType(reqConf, req.body);
    
    let dataList = [];
    if (type == OUTPUTTYPE.全部数据) dataList = await financingBiz.outPutFinancingList(monthData, fuHuaQiName, industry, fuHuaQiInvestment);
    else  {
        let baseData = await financingBiz.financingList(monthData, fuHuaQiName, industry, fuHuaQiInvestment, page);
        dataList = baseData.dataList;
    }

    let keyList = [
        "name", "logonAdd", "operatingAdd", "financingAmount","investmentInstitutionsName",
        "timeToObtainInvestment", "fuHuaQiInvestment", "fuHuaQiInvestmentAmount", "fuHuaQiInvestmentStyle", "createTime"
    ];
    let titleList = [
        "企业名称","注册地址","经营地址","融资金额(万元)","投资机构名称","获得投资时间",
        "孵化器是否投资","孵化器投资金额(万元)","孵化器投资方式","录入时间",
    ];
    let result = [titleList];
    dataList.forEach(info => {
        let subList = [];
        keyList.forEach(subInfo => {
            subList.push(info[subInfo] || '');
        });
        result.push(subList);
    });

    res.success({data:result});
}


async function outPutMonthData(req, res) {
    let  reqConf = {state: 'Number', year: 'Number', month: 'Number', page: 'Number', type:'Number'};
    let { state, year, month, page, type } = checkParamaterType(reqConf, req.body);
    
    let dataList = [];
    if (type == OUTPUTTYPE.全部数据) dataList = await outPutMonthTableList(state, year, month);
    else  {
        let baseData = await await monthTableList(state, year, month, page);
        dataList = baseData.dataList;
    }

    let keyList = [
        "operationName",
        "state",
        "time",
        "occupancyRate",
    ];
    let titleList = [
        "运营机构名称","填报状态","数据月份","出租率(%)"
    ];
    let result = [titleList];
    dataList.forEach(info => {
        let subList = [];
        keyList.forEach(subInfo => {
            subList.push(info[subInfo] || '');
        });
        result.push(subList);
    });

    res.success({data:result});
}