/**
 * 数据看板 张江看板  孵化器信息
 */

import moment = require("moment");
import { FUHUAINDUSTRY, FUHUAQILV, INSTITUTIONALNATURE } from "../../../../config/enum";
import { findFuHuaQiList } from "../../../../data/fuHuaQi/fuhuaqi";
import { changeEnumValue } from "../../../../util/verificationEnum";
import { findMonthTableListCount, statsFuHuaQiMonthDataCount } from "../../../../data/fuHuaQi/monthTask/monthTable";
import { findScoreDataByParam } from "../../../../data/fuHuaQi/score";

export async function fuHuaQiBaseData() {
    let fuHuaQiList = await findFuHuaQiList({});

    let fuHuaQiCount = 0;

    let institutionalNatureMap = {};//用户计算机构性质分布
    let logonTimeMap = {//用于计算注册时间分布
        "未满1年":{key:"未满1年", count:0},
        "1年-2年":{key:"1年-2年", count:0},
        "2年-3年":{key:"2年-3年", count:0},
        "3年-5年":{key:"3年-5年", count:0},
        "5年以上":{key:"5年以上", count:0},
    };
    let lvMap = {};//用于计算级别分布
    let industryMap = {};//用于计算孵化领域
    let nameMap = {};
    fuHuaQiList.forEach( info => {
        let { userState, institutionalNature, logonTime, lv, industry, uscc, operationName } = info;
        nameMap[uscc] = operationName;
        if (!userState) fuHuaQiCount += 1;
        let institutionalStr = changeEnumValue( INSTITUTIONALNATURE, institutionalNature);

        if (!institutionalNatureMap[institutionalStr]) institutionalNatureMap[institutionalStr] = {count:0, key:institutionalStr};
        institutionalNatureMap[institutionalStr].count += 1;
        logonTime = new Date('2023-02-01 00:11:00')
        if (logonTime) {
            let ago = moment(logonTime).fromNow(true);
            if (ago.indexOf('months') > -1) {
                logonTimeMap["未满1年"].count += 1;
            } else {
                if (parseInt(ago) <= 2) logonTimeMap["1年-2年"].count += 1;
                else if (parseInt(ago) <= 3)logonTimeMap["2年-3年"].count += 1;
                else if (parseInt(ago) <= 5)logonTimeMap["3年-5年"].count += 1;
                else logonTimeMap["5年以上"].count += 1;
            }
        }
        if (lv) {
            let lvStr = changeEnumValue(FUHUAQILV, lv);
            if (!lvMap[lvStr]) lvMap[lvStr] = {key:lvStr, count:0};
            lvMap[lvStr].count += 1;
        }
        if (industry) {
            industry.forEach(item => {
                let industryStr = changeEnumValue(FUHUAINDUSTRY, item);
                if (!industryMap[industryStr]) industryMap[industryStr] = {key:industryStr, count:0};
                industryMap[industryStr].count += 1;
            });
        }


    });

    let zcsjfb = Object.values(logonTimeMap);//注册时间分布
    let jgxzfb = Object.values(institutionalNatureMap); //机构性质分布
    let fhqjbfb = Object.values(lvMap);//孵化器级别分布
    let fhqlyfb = Object.values(industryMap);//孵化器领域分布

    let scoreDataList = await findScoreDataByParam({});
    
    let rankList = [];
    scoreDataList.forEach((info) => {
        let name = nameMap[info.uscc]
        if (!name) return;
        let score = Math.ceil(info.startScore+info.myDataScore+info.baseDataScore+info.myEnterpriseScore+info.taskScore);
        rankList.push({ name, score });
    });
    rankList.sort((a, b) => {return a.score - b.socre});
    let fhqpfpm = rankList.slice(0, 10);

    /**出租率趋势 */
    let czlqsInfo = await czlqs();

    return {czlqsInfo, fhqpfpm, zcsjfb, jgxzfb, fhqjbfb, fhqlyfb};
}

async function czlqs() {
    let dataList = [];
    for (let i = 0; i < 6; i++) {
        let dataMonth = moment().subtract(6, 'months').month() +1
        let dataYear = moment().subtract(6, 'months').year();
        let ms = moment().subtract(6, 'months').valueOf();

        let dataSum = await statsFuHuaQiMonthDataCount(dataYear, dataMonth);
        let dataCount = await findMonthTableListCount({year:dataYear, month:dataMonth, draftLock:true});
        
        dataList.push({avg:Math.ceil((dataSum/dataCount) * 100)/100, month:dataMonth, ms });
    }
    dataList.sort( (a, b) => {return a.ms-b.ms});
    let monthList = [];
    let czlList = [];
    dataList.forEach(item => {
        monthList.push(item.month);
        czlList.push(item.avg);
    });
    return {czlList, monthList};
}