/**
 * 孵化器基础数据相关逻辑
 * 作者: 李某
 * 孵化器相关逻辑 包括孵化器的增删改查
 * 
 */
import { BaseParamUpdateConfig } from "../../config/checkParamConfig";
import { findEnterpriseCount, findEnterpriseCountByFuHuaQiUscc, groupFindEnterprise } from "../../data/enterprise/enterprise";
import { findFuHuaQiByUSCC, findFuHuaQiCount, findFuHuaQiList } from "../../data/fuHuaQi/fuhuaqi";
import * as tools from "../../util/tools";
import { FuHuaQiHomeDataConfig, FuHuaQiBaseListConfig, FuHuaQiBaseDataConfig } from "../../config/ojbectResultKeyConfig";
import { FUHUAINDUSTRY, FUHUAQILNVESTMENTSTYLE, FUHUAQILV, INSTITUTIONALNATURE } from "../../config/enum";
import { extractData } from "../../util/tools";


/**
 * 主界面基础数据
 * 小程序端  首页的基础数据
 * 评分数据还没有
 * @param uscc 孵化器统一信用代码
 * @returns {} "incubatedAcreage", "acreageTotal", "acreagePersonalUse", "name", "lv", "institutionalNature"
 */
export async function homeData(uscc:string) {
    let dataBaseInfo = await findFuHuaQiByUSCC(uscc);

    let data:any = extractData(FuHuaQiHomeDataConfig, dataBaseInfo);

    data.enterpriseCount  = await findEnterpriseCountByFuHuaQiUscc(uscc);
    data.lv = tools.changeEnumValue(FUHUAQILV, dataBaseInfo.lv);
    data.institutionalNature = tools.changeEnumValue(INSTITUTIONALNATURE, dataBaseInfo.institutionalNature);

    return data;
}


/**
 * 孵化器详细数据
 * 小程序端  点击我的数据之后拉取的详细数据
 * @param uscc 孵化器统一信用代码
 * @returns data 基础数据字段
 */
export async function selectFuHuaQiBaseData(uscc:string) {
    let dataBaseInfo = await findFuHuaQiByUSCC(uscc);

    let enterpriseTotal = await findEnterpriseCountByFuHuaQiUscc(uscc); //企业总数
    
    let data:any = extractData(FuHuaQiBaseDataConfig, dataBaseInfo);
    
    data.enterpriseTotal = enterpriseTotal;
    data.numOfEntities = enterpriseTotal - dataBaseInfo.virtualEnterpriseNum; //企业总数 - 虚拟企业数量

    return data;
}


/**
 * 更新孵化器基础数据
 * 小程序端
 * @param uscc 孵化器统一信用代码
 * @param param 表单内容
 * @returns {isSuccess:true/false};
 */
export async function updateFuHuaQiBaseData(uscc:string, param) {
    tools.checkParamater("更新孵化器基础数据", BaseParamUpdateConfig, param);

    let baseDataInfo = await findFuHuaQiByUSCC(uscc);
    
    //todo  这里有个bug
    let changeList = tools.checkChange(param, baseDataInfo);
    for (let i = 0; i < changeList.length; i++) {
        let key = changeList[i];
        baseDataInfo[key] = param[key];
    }

    await baseDataInfo.save();

    return {isSuccess:true};
}


/**
 * 获取孵化器基本信息列表
 * 管理后台端
 * @param lv 孵化器等级
 * @param institutionalNature 机构性质
 * @param industry 领域
 * @param page 页数
 * @returns 
 */
export async function fuHuaQiBaseList(lv:number, institutionalNature:number, industry:number, page:number) {
    let selectParam:any = {};
    if (lv) selectParam.lv = lv;
    if (institutionalNature) selectParam.institutionalNature = institutionalNature;
    if (industry) selectParam.industry = industry;

    let enterpriseMap = await groupFindEnterprise();//企业总数

    let dataBaseList = await findFuHuaQiList(selectParam, (page-1)*10 );

    let count = await findFuHuaQiCount(selectParam);
    let dataList = [];

    dataBaseList.forEach( info => {
        let changeData:any = tools.extractData(FuHuaQiBaseListConfig, info);
        // let hatchingGround = info.hatchingGround;
        // hatchingGround.forEach (info => {
        //     let {personInChargeAdd, siteAcreage, leasePrice} = info;
        //     changeData.personInChargeAdd = personInChargeAdd;
        //     changeData.siteAcreage = siteAcreage;
        //     changeData.leasePrice = leasePrice;
        // })
        let enterpriseTotal = enterpriseMap[info.uscc] || 0;
        let numOfEntities = enterpriseTotal - (info.virtualEnterpriseNum || 0 );
        numOfEntities = numOfEntities < 0 ? 0 : numOfEntities;

        changeData.enterpriseTotal = enterpriseTotal;
        changeData.numOfEntities = numOfEntities;

        changeData.lv = tools.changeEnumValue(FUHUAQILV, changeData.lv);
        changeData.industry = tools.changeEnumValue(FUHUAINDUSTRY, changeData.industry);
        changeData.institutionalNature = tools.changeEnumValue(INSTITUTIONALNATURE, changeData.institutionalNature);

        dataList.push(changeData);
    });

    return {count, dataList};
}