/**
 * 月度表单逻辑层 
 * 作者: 陈金晶
 * 月度表单相关逻辑 包括孵化器月度填报的增删改查
 * 月度表单在孵化器任务生成时 就会在库里存在数据
 */
import { ERRORENUM } from "../../config/errorEnum";
import { BizError } from "../../util/bizError";
import * as monthData from "../../data/fuHuaQi/monthTable";
import { changeEnumValue, extractData, getTaskId } from "../../util/tools";
import { findAllFuHuaQiOperationNameMap } from "../../data/fuHuaQi/fuhuaqi";
import { MonthConfig } from "../../config/ojbectResultKeyConfig";


/**
 * 新添加孵化器月度填报
 * 小程序端
 * 逻辑是 将填报编辑状态修改
 * @param uscc 孵化器的统一信用代码
 * @param occupancyRate 本月出租率
 * @returns {isSuccess:true/false}
 */
export async function createReport(uscc:string, occupancyRate:number) {
    const TaskId = getTaskId(uscc);
    let dataBaseInfo = await monthData.findMonthTableByTaskId(TaskId);

    if ( dataBaseInfo.isUpdate ) throw new BizError(ERRORENUM.该孵化器月度填报已存在, `${uscc}重复提交了月度填报值为${occupancyRate}`);

    dataBaseInfo.isUpdate = true;
    dataBaseInfo.occupancyRate = occupancyRate;

    await dataBaseInfo.save();
    
    return {isSuccess:true};
}


/**
 * 修改孵化器月度报表
 * 小程序端
 * @param uscc 孵化器的统一信用代码
 * @param occupancyRate 本月出租率
 * @returns {isSuccess:true/false}
 */
export async function updateReport(uscc:string, occupancyRate:number) {
    const TaskId = getTaskId(uscc);

    let dataBaseInfo = await monthData.findMonthTableByTaskId(TaskId);
    if (!dataBaseInfo || !dataBaseInfo.taskId) throw new BizError(ERRORENUM.未找到数据, `未找到${uscc}的月度报表` );
    
    dataBaseInfo.occupancyRate = occupancyRate;
    await dataBaseInfo.save();

    return {isSuccess:true};
}

/**
 * 删除孵化器月度报表
 * 小程序端
 * @param uscc 孵化器的统一信用代码
 * @returns  {isSuccess:true/false}
 */
export async function deleteReport(uscc:string) {
    const TaskId = getTaskId(uscc);

    let dataBaseInfo = await monthData.findMonthTableByTaskId(TaskId);
    if (!dataBaseInfo.isUpdate) throw new BizError(ERRORENUM.未找到数据);

    dataBaseInfo.isUpdate = false; //修改编辑状态

    await dataBaseInfo.save();

    return {isSuccess:true};
}

/**
 * 根据uscc查询月度报表
 * 小程序端  回显
 * @param uscc 企业统一信用代码
 * @returns {occupancyRate:0} occupancyRate 本月出租率
 */
export async function getMonthByUscc(uscc:string) {
    const TaskId = getTaskId(uscc);

    let dataBaseInfo = await monthData.findMonthTableByTaskId(TaskId);
    if ( !dataBaseInfo.isUpdate ) throw new BizError(ERRORENUM.未找到数据, `未找到${uscc}的月度报表` );

    let data = extractData(MonthConfig, dataBaseInfo);

    return { data };
}


/**
 * 月度出租率数据列表
 * 管理后台端
 * @param state 填报状态
 * @param year 数据年份
 * @param month 数据月份
 * @param page 页面
 */
export async function monthTableList(state:number, year:number, month:number, page:number) {
    let selectParam:any = {};
    // if (state) selectParam.state = state;
    if (state == 2 || state == 3) {
        selectParam.draftLock = state == 2 ? true : false;
    }
    if (year) selectParam.year = year;
    if (month) selectParam.month = month;

    let dataBaseList = await monthData.findMonthTableList(selectParam, (page-1)*10 );
    let count = await monthData.findMonthTableListCount(selectParam);

    let operationNameMap = await findAllFuHuaQiOperationNameMap();

    let dataList = [];
    dataBaseList.forEach( info => {
        let onceInfo = {
            operationName:operationNameMap[info.fuHuaQiUscc],
            state:info.draftLock==false?'未填报':'已填报',
            time:`${info.year}年${info.month}月`,
            occupancyRate:info.occupancyRate ||  '-'
        };
        dataList.push(onceInfo);
    });

    return {count, dataList}
}