
/**  
 * 孵化器 机构资质 主要逻辑
 * 作者：lxm
 * 
 */

import * as fuhuaqiData from "../../../../data/fuHuaQi/fuhuaqi";
import { getI18nRegisterId } from "../../../../tools/system";
import { addRegister, deleteI18nRegisterListById, findI18nRegisterListByParam, findOneI18nRegister } from "../../../../data/fuHuaQi/instituQualify/i18nRegister";
import { BizError } from "../../../../util/bizError";
import { ERRORENUM } from "../../../../config/errorEnum";
import { addPoint } from "../../../point";
import { FUHUAQINODEENUM, OPERATETYPEENUM, POINTTYPEENUM } from "../../../../config/pointConfig";


/**
 * 创建国际化登记
 * @param uscc 企业统一信用代码
 * @param country 国家
 * @param partners 合作机构
 * @param cMode 合作方式
 * @param cAchievements 合作成果
 * @returns 
 */
export async function createI18nRegister(uscc:string, country:string, partners:string, cMode:string, cAchievements:string) {
    let fuHuaQiInfo = await fuhuaqiData.findFuHuaQiByUSCC(uscc);

    let id = getI18nRegisterId(uscc);
    await addRegister(id, uscc, fuHuaQiInfo.name, country, partners, cMode, cAchievements);

    addPoint(POINTTYPEENUM.孵化器, uscc, FUHUAQINODEENUM.机构资质_国际化登记, {country, partners, cMode, cAchievements}, null, OPERATETYPEENUM.添加 );
    
    return {isSuccess:true};
}


/**
 * 修改国际化登记
 * @param uscc 企业统一信用代码
 * @param id 标识
 * @param country 国家
 * @param partners 合作机构
 * @param cMode 合作方式
 * @param cAchievements 合作成果
 * @returns 
 */
export async function updateI18nRegister(uscc:string, id:string, country:string, partners:string, cMode:string, cAchievements:string) {
    let registerInfo = await findOneI18nRegister(id);
    if (uscc != registerInfo.uscc) throw new BizError(ERRORENUM.参数错误, "修改国际化登记信息时", `${uscc}尝试修改${registerInfo.uscc}的信息`);

    let oldData = {
        country: registerInfo.country,
        partners: registerInfo.partners,
        cMode: registerInfo.cMode,
        cAchievements: registerInfo.cAchievements,
    };
    let newData = { country, partners, cMode, cAchievements };

    registerInfo.country = country;
    registerInfo.partners = partners;
    registerInfo.cMode = cMode;
    registerInfo.cAchievements = cAchievements;

    await registerInfo.save();

    addPoint(POINTTYPEENUM.孵化器, uscc, FUHUAQINODEENUM.机构资质_国际化登记, oldData, newData, OPERATETYPEENUM.修改 );

    return {isSuccess:true};
}


/**
 * 回显 国际化登记 接口
 * @param id 记录标识
 * @returns 
 */
export async function selectI18nRegisterInfo(id:string) {
    let registerInfo = await findOneI18nRegister(id);

    let registerData = {
        country : registerInfo.country,
        partners : registerInfo.partners,
        cMode : registerInfo.cMode,
        cAchievements : registerInfo.cAchievements,
    };

    return {registerData} 
}


/**
 * 删除国际化登记数据 
 * @param id 标识
 * @returns 
 */
export async function deleteI18nRegisterInfo(uscc:string, id:string) {
    let registerInfo = await findOneI18nRegister(id);
    if (uscc != registerInfo.uscc) throw new BizError(ERRORENUM.参数错误, "删除国际化登记信息时", `${uscc}尝试修改${registerInfo.uscc}的信息`);

    let oldData = {
        country: registerInfo.country,
        partners: registerInfo.partners,
        cMode: registerInfo.cMode,
        cAchievements: registerInfo.cAchievements,
    };

    addPoint(POINTTYPEENUM.孵化器, uscc, FUHUAQINODEENUM.机构资质_国际化登记, oldData, null, OPERATETYPEENUM.删除 );

    await deleteI18nRegisterListById(id);

    return {isSuccess:true} 
}


/**
 * 国际化登记列表
 * @param uscc 孵化器统一信用代码
 * @returns 
 */
export async function i18nRegisterListData(uscc) {
    let registerList = await findI18nRegisterListByParam({uscc});

    let dataList = [];
    registerList.forEach(info => {
        dataList.push({
            id:info.id,
            country:info.country,
            partners:info.partners,
            cMode:info.cMode,
            cAchievements:info.cAchievements,
        });
    });

    return {dataList, title:"国际化登记"}
}

