import { sysGraphHttpUtils } from "@/utils/http/sysGraphHttpUtils";
import { logicConfig } from "@/logicConfig";

export class sysGraphController {
    private static sysGraphHttpUtilsInstance = new sysGraphHttpUtils();
    /**
     * 查询系统图列表
     * @param grpphName 系统图名称
     */
    static async getDiagrams(grpphName: string) {
        var resultObj = await this.sysGraphHttpUtilsInstance.postRequest('/diagram/getDiagrams', { name: grpphName });
        if (resultObj.result != logicConfig.resultObj.success)
            console.error(resultObj.reason);
        var newDataArr = [];
        var dataArr = resultObj.data || [];
        dataArr.forEach(_c => {
            //图状态（Draft: 未发布, WaitCheck: 待审核, Checked: 审核完成, Recyle: 回收站, Publish: 发布）
            _c.stateName = _c.state == 'Draft' ? '草稿' : _c.state == 'Publish' ? '上架' : _c.state == 'Recyle' ? '下架' : '';
            newDataArr.push({
                id: _c.id,
                name: _c.name,
                sysType: _c.system,
                sysInstance: _c.systemId,
                sysTemplate: _c.templateId,
                //state: i % 3 == 1 ? 'Publish' : i % 3 == 2 ? 'Draft' : 'Recyle',
                createTime: '2021/12/27 13:13'
            });
        });
        return newDataArr;
    }

    /**
     * 根据项目查询该项目下的专业>系统类型>系统实例
     * @param projectId 项目ID
     * @param groupCode 集团编码
     */
    static async getSysInstanceByProject(projectId: string, groupCode: string) {
        var url = '/diagram/major/system/instance?projectId=' + projectId + '&groupCode=' + groupCode;
        var resultObj = await this.sysGraphHttpUtilsInstance.postRequest(url, {});
        if (resultObj.result != logicConfig.resultObj.success)
            console.error(resultObj.reason);
        var dataArr = resultObj.data || [];
        var newDataArr = [];

        dataArr.forEach(_c => {
            //_c为专业
            let zhuanye = { value: _c.id, label: _c.label, code: _c.code, children: [] };
            let sysTypeArr = [];
            (_c.children || []).forEach(_c2 => {
                //_c2为系统类型
                let sysType = { value: _c2.id, label: _c2.aliasName, code: _c2.code, children: [] };
                let sysInstanceArr = [];
                (_c2.children || []).forEach(_c3 => {
                    //_c3为系统实例
                    sysInstanceArr.push({ value: _c3.id, label: _c3.name, code: _c3.code });
                });
                sysType.children = sysInstanceArr;
                sysTypeArr.push(sysType);
            });
            zhuanye.children = sysTypeArr;
            newDataArr.push(zhuanye);
        });
        return newDataArr;
    }

    /**
     * 根据系统类型查询系统图模板，数据结构：系统图类型 ->  系统图模版
     * @param sysCode 系统类型编码
     */
    static async getTemplateBySysCode(sysCode: string) {
        var url = '/diagram/systemType/template';
        var resultObj = await this.sysGraphHttpUtilsInstance.postRequest(url, { system: sysCode });
        if (resultObj.result != logicConfig.resultObj.success)
            console.error(resultObj.reason);
        var dataArr = resultObj.data || [];
        var newDataArr = [];

        dataArr.forEach(_sysGraphType => {
            //_sysGraphType为系统图类型
            let sysGraphType = { value: _sysGraphType.id, label: _sysGraphType.name, code: _sysGraphType.code, children: [] };
            let templateArr = [];
            (_sysGraphType.children || []).forEach(_template => {
                //_template为系统类型
                let template = { value: _template.id, label: _template.name, code: _template.code };
                templateArr.push(template);
            });
            sysGraphType.children = templateArr;
            newDataArr.push(sysGraphType);
        });
        return newDataArr;
    }

    /**
     * 新增系统图
     * @param _paramObj {name:'系统图名称',type:'系统图类型编码',templateId:'引用的模板id',system:'所属的系统类型',projectId:'项目id',systemId:'对应的系统实例id',groupCode:'集团编码',}
     */
    static async createSysGraph(_paramObj: any) {
        var url = '/diagram/newDiagram';
        var resultObj = await this.sysGraphHttpUtilsInstance.postRequest(url, _paramObj);
        if (resultObj.result != logicConfig.resultObj.success) {
            console.error(resultObj.reason);
            return resultObj;
        }

        var dataObj = resultObj.data || {};
        if (!dataObj.id)
            return { result: logicConfig.resultObj.failure, reason: '确实系统图ID' };

        //新增成功后，要加载数据
        var loadDataUrl = '/diagram/loadData';
        var loadResultObj = await this.sysGraphHttpUtilsInstance.postRequest(loadDataUrl, { id: dataObj.id });
        if(loadResultObj.result!=logicConfig.resultObj.success){
            return { result: logicConfig.resultObj.failure, reason: '加载模板数据失败，原因：' + loadResultObj.reason };
        }
console.log(loadResultObj.data);
        return loadResultObj.data;
    }
}