package cn.cerc.mis.other;

import cn.cerc.core.IHandle;
import cn.cerc.core.Record;
import cn.cerc.db.mysql.SqlQuery;
import cn.cerc.mis.client.RemoteService;
import cn.cerc.mis.config.ApplicationProperties;
import cn.cerc.mis.core.Application;
import cn.cerc.mis.core.ISystemTable;

/**
 * FIXME 2019-12-17 改为从项目本身创建
 */
public class Memory_UserInfo {

    public static int count = 0;

    public static MemoryBuffer get(IHandle handle, String userCode) {
        MemoryBuffer buff = new MemoryBuffer(BufferType.getAccount, userCode);
        if (!buff.isNull()) {
            count++;
            return buff;
        }

        Record record;
        if (ApplicationProperties.isMaster()) {
            // 主服务器
            ISystemTable systemTable = Application.getBean("systemTable", ISystemTable.class);
            SqlQuery ds = new SqlQuery(handle);
            ds.add("select a.Code_,a.Enabled_,a.Name_,a.SuperUser_,a.DiyRole_,a.RoleCode_,oi.Type_,a.ImageUrl_ ");
            ds.add("from %s a ", systemTable.getUserInfo());
            ds.add("inner join %s oi on a.CorpNo_=oi.CorpNo_ ", systemTable.getBookInfo());
            ds.add("where a.Code_='%s'", userCode);
            ds.open();
            if (ds.eof())
                throw new RuntimeException(String.format("用户代码 %s 不存在!", userCode));
            record = ds.getCurrent();
        } else {
            // 从服务器
            RemoteService svr = new RemoteService(handle, ISystemTable.Public, "ApiUserInfo.getUserInfo");
            if (!svr.exec("UserCode_", userCode)) {
                throw new RuntimeException(svr.getMessage());
            }
            record = svr.getDataOut().getCurrent();
        }

        buff.setField("Name_", record.getString("Name_"));
        buff.setField("Enabled_", record.getInt("Enabled_"));
        buff.setField("SuperUser_", record.getBoolean("SuperUser_"));
        buff.setField("ImageUrl_", record.getString("ImageUrl_"));
        if (record.getBoolean("DiyRole_")) {
            buff.setField("RoleCode_", record.getString("Code_"));
        } else {
            buff.setField("RoleCode_", record.getString("RoleCode_"));
        }
        buff.setField("CorpType_", "" + record.getInt("Type_") + ",");
        return buff;
    }

    public static void clear(String userCode) {
        try (MemoryBuffer buff = new MemoryBuffer(BufferType.getAccount, userCode)) {
            buff.clear();
        }
    }

}

@@ -82,42 +82,8 @@
 rd;%0A
-import cn.cerc.db.mysql.SqlQuery;%0A
 impo
@@ -104,29 +104,29 @@
 .client.
-Remote
+I
 Service
+Proxy
 ;%0Aimport
@@ -143,72 +143,28 @@
 is.c
-onfig.ApplicationProperties;%0Aimport cn.cerc.mis.core.Application
+lient.ServiceFactory
 ;%0Aim
@@ -552,812 +552,46 @@
     
-Record record;%0A        if (ApplicationProperties.isMaster()) %7B%0A            // %E4%B8%BB%E6%9C%8D%E5%8A%A1%E5%99%A8%0A            ISystemTable systemTable = Application.getBean(%22systemTable%22, ISystemTable.class);%0A            SqlQuery ds = new SqlQuery(handle);%0A            ds.add(%22select a.Code_,a.Enabled_,a.Name_,a.SuperUser_,a.DiyRole_,a.RoleCode_,oi.Type_,a.ImageUrl_ %22);%0A            ds.add(%22from %25s a %22, systemTable.getUserInfo());%0A            ds.add(%22inner join %25s oi on a.CorpNo_=oi.CorpNo_ %22, systemTable.getBookInfo());%0A            ds.add(%22where a.Code_='%25s'%22, userCode);%0A            ds.open();%0A            if (ds.eof())%0A                throw new RuntimeException(String.format(%22%E7%94%A8%E6%88%B7%E4%BB%A3%E7%A0%81 %25s %E4%B8%8D%E5%AD%98%E5%9C%A8!%22, userCode));%0A            record = ds.getCurrent();%0A        %7D else %7B%0A            // %E4%BB%8E%E6%9C%8D%E5%8A%A1%E5%99%A8%0A            RemoteService svr = new RemoteService
+IServiceProxy svr = ServiceFactory.get
 (han
@@ -648,28 +648,24 @@
 %22);%0A        
-    
 if (!svr.exe
@@ -704,20 +704,16 @@
         
-  
-  
 throw ne
@@ -754,28 +754,24 @@
 ));%0A        
-    
 %7D%0A          
@@ -768,19 +768,22 @@
         
-   
+Record
  record 
@@ -814,26 +814,16 @@
 rrent();
-%0A        %7D
 %0A%0A      

Update Memory_UserInfo.java