/**
 * 小程序端孵化器的路由入口
 */
import * as asyncHandler from 'express-async-handler';
import * as userBiz from '../biz/fuHuqQi/user';
import * as baseBiz from '../biz/fuHuqQi/base';
import * as taskBiz from '../biz/fuHuqQi/task';
import * as monthBiz from '../biz/fuHuqQi/month';
import * as enterpriseBiz from '../biz/enterprise/enterprise';
import * as financingBiz from '../biz/enterprise/financing';
import { checkReqParam } from '../util/tools';
import { checkFuHuaQiToken } from '../middleware/user';

export function setRouter(httpServer) {
    httpServer.post('/fuhuaqi/login', asyncHandler(login));
    httpServer.post('/fuhuaqi/login/firstupdate', checkFuHuaQiToken, asyncHandler(firstUpdatePwd));
    
    /**基础数据 */
    httpServer.post('/fuhuaqi/base', checkFuHuaQiToken, asyncHandler(baseInfo));
    httpServer.post('/fuhuaqi/mydata', checkFuHuaQiToken, asyncHandler(myDataInfo));
    httpServer.post('/fuhuaqi/base/update', checkFuHuaQiToken, asyncHandler(updateMyDataInfo));
    /**任务 */
    httpServer.post('/fuhuaqi/task/list', checkFuHuaQiToken, asyncHandler(taskList));
    httpServer.post('/fuhuaqi/task/submit', checkFuHuaQiToken, asyncHandler(submitTask));
    
    /**月度报表 */
    httpServer.post('/fuhuaqi/month/addreport', checkFuHuaQiToken, asyncHandler(addReport));
    httpServer.post('/fuhuaqi/month/updatereport', checkFuHuaQiToken, asyncHandler(updateReport));
    httpServer.post('/fuhuaqi/month/deletereport', checkFuHuaQiToken, asyncHandler(deleteReport));
    httpServer.post('/fuhuaqi/month/select', checkFuHuaQiToken, asyncHandler(selectMonthInfo));

    /**融资企业填报 */
    httpServer.post('/fuhuaqi/financing/create', checkFuHuaQiToken, asyncHandler(createFinancingInfo));
    httpServer.post('/fuhuaqi/financing/update', checkFuHuaQiToken, asyncHandler(updateFinancingInfo));
    httpServer.post('/fuhuaqi/financing/select', checkFuHuaQiToken, asyncHandler(selectEnterpriseFinancingInfo));
    httpServer.post('/fuhuaqi/financing/delete', checkFuHuaQiToken, asyncHandler(delEnterpriseFinancingInfo));
    httpServer.post('/admin/financing/namelist', checkFuHuaQiToken, asyncHandler(getFinancingInfoByName));

    /**新注册或迁入企业 */
    httpServer.post('/fuhuaqi/enterprise/register', checkFuHuaQiToken, asyncHandler(registerEnterprise));
    httpServer.post('/fuhuaqi/enterprise/updateregister', checkFuHuaQiToken, asyncHandler(updateRegisterEnterprise));
    httpServer.post('/fuhuaqi/enterprise/move', checkFuHuaQiToken, asyncHandler(moveInEnterprise));
    httpServer.post('/fuhuaqi/enterprise/updatemove', checkFuHuaQiToken, asyncHandler(updateMoveInEnterprise));
    httpServer.post('/fuhuaqi/enterprise/select', checkFuHuaQiToken, asyncHandler(selectEnterpriseInfo));
    httpServer.post('/fuhuaqi/enterprise/delete', checkFuHuaQiToken, asyncHandler(delEnterpriseInfo));
    
}

/**
 * 创建企业融资信息 
 * @param req 
 * @param res 
 */
 async function createFinancingInfo(req, res) {
    let reqConf = {form: 'Object' };
    let { form } = checkReqParam(reqConf, req.body);
    const Uscc = req.headers.uscc;

    let result = await financingBiz.createFinancingInfo(Uscc, form);
    res.success(result);
}

/**
 * 更新融资企业信息 
 * @param req 
 * @param res 
 */
 async function updateFinancingInfo(req, res) {
    let reqConf = {form: 'Object' };
    let { form } = checkReqParam(reqConf, req.body);
    const Uscc = req.headers.uscc;

    let result = await financingBiz.updateFinancingInfo(Uscc, form);
    res.success(result);
}


/**
 * 新注册企业 
 * @param req 
 * @param res 
 */
async function registerEnterprise(req, res) {
    let reqConf = {form: 'Object' };
    let { form } = checkReqParam(reqConf, req.body);
    const Uscc = req.headers.uscc;

    let result = await enterpriseBiz.registerEnterprise(Uscc, form);
    res.success(result);
}


/**
 * 新注册企业表单修改 
 * @param req 
 * @param res 
 */
async function updateRegisterEnterprise(req, res) {
    let reqConf = {form: 'Object' };
    let { form } = checkReqParam(reqConf, req.body);
    const Uscc = req.headers.uscc;

    let result = await enterpriseBiz.updateRegisterEnterprise(Uscc, form);
    res.success(result);
}


/**
 * 迁入企业登记 
 * @param req 
 * @param res 
 */
 async function moveInEnterprise(req, res) {
    let reqConf = {form: 'Object' };
    let { form } = checkReqParam(reqConf, req.body);
    const Uscc = req.headers.uscc;

    let result = await enterpriseBiz.moveInEnterprise(Uscc, form);
    res.success(result);
}


/**
 * 查询迁入企业登记信息或新建企业登录信息 
 * @param req 
 * @param res 
 */
 async function selectEnterpriseInfo(req, res) {
    let reqConf = {uscc: 'String' };
    let { uscc } = checkReqParam(reqConf, req.body);

    let result = await enterpriseBiz.getEnterpriseByUscc(uscc);
    res.success(result);
}


/**
 * 查询企业融资信息 
 * @param req 
 * @param res 
 */
async function selectEnterpriseFinancingInfo(req, res) {
    let reqConf = {uscc: 'String' };
    let { uscc } = checkReqParam(reqConf, req.body);
    const FHQUscc = req.headers.uscc;

    let result = await financingBiz.getEnterpriseFinancingByUscc(FHQUscc, uscc);
    res.success(result);
}


/**
 * 删除迁入企业登记信息或新建企业登录信息 
 * @param req 
 * @param res 
 */
async function delEnterpriseInfo(req, res) {
    let reqConf = {uscc: 'String' };
    let { uscc } = checkReqParam(reqConf, req.body);

    let result = await enterpriseBiz.deleteEnterpriseByUscc(uscc);
    res.success(result);
}


/**
 * 根据融资企业名称获取融资企业名称 
 * @param req 
 * @param res 
 */
 async function getFinancingInfoByName(req, res) {
    const Uscc = req.headers.uscc;
    
    let result = await financingBiz.getFuHuaQiEnterpriseForSelect(Uscc);
    res.success(result);
}


/**
 * 查询企业融资信息 
 * @param req 
 * @param res 
 */
async function delEnterpriseFinancingInfo(req, res) {
    let reqConf = {uscc: 'String' };
    let { uscc } = checkReqParam(reqConf, req.body);
    const FHQUscc = req.headers.uscc;

    let result = await financingBiz.deleteEnterpriseFinancingByUscc(FHQUscc, uscc);
    res.success(result);
}


//=======================

/**
 * 修改迁入企业登记信息 
 * @param req 
 * @param res 
 */
async function updateMoveInEnterprise(req, res) {
    let reqConf = {form: 'Object' };
    let { form } = checkReqParam(reqConf, req.body);
    const Uscc = req.headers.uscc;

    let result = await enterpriseBiz.updateMoveInEnterprise(Uscc, form);
    res.success(result);
}


//==============

/**
 * 孵化器登录 
 * @param req 
 * @param res 
 */
async function login(req, res) {
    let reqConf = {uscc:'String', pwd:'String'};
    let {uscc, pwd} = checkReqParam(reqConf, req.body);
    
    let result = await userBiz.login(uscc, pwd);

    res.success(result);
}


/**
 * 孵化器首次登录 
 * @param req 
 * @param res 
 */
async function firstUpdatePwd(req, res) {
    let reqConf = {confirmPwd:'String', pwd:'String', };
    let {confirmPwd, pwd} = checkReqParam(reqConf, req.body);

    const Uscc = req.headers.uscc;
    
    let result = await userBiz.firstLoginChangePwd(Uscc, pwd, confirmPwd);

    res.success(result);
}



/**
 * 主界面信息 
 * 包括首页的 我的数据 评分明细
 * @param req 
 * @param res 
 */
async function baseInfo(req, res) {
    const Uscc = req.headers.uscc;

    let userInfo = await baseBiz.homeData(Uscc);

    res.success(userInfo);
}


/**
 * 我的数据 详细数据
 * @param req 
 * @param res 
 */
async function myDataInfo(req, res) {
    const Uscc = req.headers.uscc;

    let userInfo = await baseBiz.selectFuHuaQiBaseData(Uscc);

    res.success(userInfo);
}


/**
 * 任务列表
 * @param req 
 * @param res 
 */
async function taskList(req, res) {
    const Uscc = req.headers.uscc;

    let result = await taskBiz.fuHuaQiTaskList(Uscc);

    res.success(result);
}


/**
 * 提交任务
 * @param req 
 * @param res 
 */
async function submitTask(req, res) {
    let reqConf = {taskType:'Number'};
    let { taskType } = checkReqParam(reqConf, req.body);
    const Uscc = req.headers.uscc;

    let result = await taskBiz.checkTask(Uscc, taskType);
    res.success(result);
}


/**
 * 更新基础数据
 * @param req 
 * @param res 
 */
async function updateMyDataInfo(req, res) {
    let reqConf = {form: 'Object' };
    let { form } = checkReqParam(reqConf, req.body);
    const Uscc = req.headers.uscc;

    let result = await baseBiz.updateFuHuaQiBaseData(Uscc, form);
    res.success(result);
}

/**
 * 孵化器月度填报 新增报表
 * @param req 
 * @param res 
 */
async function addReport(req, res) {
    let reqConf = {occupancyRate: 'Number' };
    let { occupancyRate } = checkReqParam(reqConf, req.body);
    const Uscc = req.headers.uscc;

    let monthInfo = monthBiz.createReport(Uscc, occupancyRate);
    res.success(monthInfo);
}

/**
 * 孵化器月度填报  删除报表
 * @param req 
 * @param res 
 */
async function deleteReport(req, res) {
    const Uscc = req.headers.uscc;

    let monthInfo = monthBiz.deleteReport(Uscc);
    res.success(monthInfo);
}


/**
 * 查询迁入企业登记信息或新建企业登录信息 
 * @param req 
 * @param res 
 */
 async function selectMonthInfo(req, res) {
    let reqConf = {uscc: 'String' };
    let { uscc } = checkReqParam(reqConf, req.body);

    let result = await monthBiz.getMonthByUscc(uscc);
    res.success(result);
}


/**
 * 孵化器月度填报 修改报表
 * @param req 
 * @param res 
 */
async function updateReport(req, res) {
    let reqConf = {occupancyRate: 'Number' };
    let { occupancyRate } = checkReqParam(reqConf, req.body);
    const Uscc = req.headers.uscc;

    let monthInfo = monthBiz.updateReport(Uscc, occupancyRate);
    res.success(monthInfo);
}