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

import * as fuhuaqiData from "../../../../data/fuHuaQi/fuhuaqi";
import { getI18nRegisterId } from "../../../../tools/system";
import * as i18nRegisterData from "../../../../data/fuHuaQi/instituQualify/i18nRegister";
import { BizError } from "../../../../util/bizError";
import { ERRORENUM } from "../../../../config/errorEnum";
import { addFuHuaQiPoint } 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 i18nRegisterData.addRegister(id, uscc, fuHuaQiInfo.operationName, country, partners, cMode, cAchievements);

    addFuHuaQiPoint(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 i18nRegisterData.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();

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

    return {isSuccess:true};
}


/**
 * 回显 国际化登记 接口
 * @param id 记录标识
 * @returns 
 */
export async function selectI18nRegisterInfo(id:string) {
    let registerInfo = await i18nRegisterData.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 i18nRegisterData.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,
    };

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

    await i18nRegisterData.deleteI18nRegisterListById(id);

    return {isSuccess:true} 
}


/**
 * 国际化登记列表
 * @param uscc 孵化器统一信用代码
 * @returns 
 */
export async function i18nRegisterListData(uscc) {
    let registerList = await i18nRegisterData.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:"国际化登记"}
}

