package cn.cerc.mis.services;

import cn.cerc.core.IHandle;
import cn.cerc.core.Record;
import cn.cerc.core.Utils;
import cn.cerc.db.cache.Redis;
import cn.cerc.mis.client.IServiceProxy;
import cn.cerc.mis.client.ServiceFactory;
import cn.cerc.mis.other.BookVersion;
import cn.cerc.mis.other.BufferType;
import com.google.gson.Gson;

public class MemoryBookInfo {

    private static final String buffVersion = "5";

    public static BookInfoRecord get(IHandle handle, String corpNo) {
        Gson gson = new Gson();
        String tmp = Redis.get(getBuffKey(corpNo));
        if (Utils.isNotEmpty(tmp)) {
            return gson.fromJson(tmp, BookInfoRecord.class);
        }

        IServiceProxy svr = ServiceFactory.get(handle);
        svr.setService("SvrBookInfo.getRecord");
        if (!svr.exec("corpNo", corpNo)) {
            return null;
        }
        Record record = svr.getDataOut().getHead();

        BookInfoRecord result = new BookInfoRecord();
        result.setCode(record.getString("CorpNo_"));
        result.setShortName(record.getString("ShortName_"));
        result.setName(record.getString("Name_"));
        result.setAddress(record.getString("Address_"));
        result.setTel(record.getString("Tel_"));
        result.setManagerPhone(record.getString("ManagerPhone_"));
        result.setStartHost(record.getString("StartHost_"));
        result.setContact(record.getString("Contact_"));
        result.setAuthentication(record.getBoolean("Authentication_"));
        result.setStatus(record.getInt("Status_"));
        result.setCorpType(record.getInt("Type_"));
        result.setIndustry(record.getString("Industry_"));

        Redis.set(getBuffKey(corpNo), gson.toJson(result));

        return result;
    }

    /**
     * @param handle 环境变量
     * @param corpNo 帐套代码
     * @return 返回帐套状态
     */
    public static int getStatus(IHandle handle, String corpNo) {
        BookInfoRecord item = get(handle, corpNo);
        if (item == null) {
            throw new RuntimeException(String.format("没有找到注册的帐套  %s ", corpNo));
        }
        return item.getStatus();
    }

    /**
     * @param handle 环境变量
     * @return 返回当前帐套的版本类型
     */
    public static BookVersion getBookType(IHandle handle) {
        String corpNo = handle.getCorpNo();
        return getCorpType(handle, corpNo);
    }

    /**
     * @param handle 环境变量
     * @param corpNo 帐套代码
     * @return 返回指定帐套的版本类型
     */
    public static BookVersion getCorpType(IHandle handle, String corpNo) {
        BookInfoRecord item = get(handle, corpNo);
        if (item == null) {
            throw new RuntimeException(String.format("没有找到注册的帐套  %s ", corpNo));
        }
        int result = item.getCorpType();
        return BookVersion.values()[result];
    }

    /**
     * @param handle 环境变量
     * @param corpNo 帐套代码
     * @return 返回帐套简称
     */
    public static String getShortName(IHandle handle, String corpNo) {
        BookInfoRecord item = get(handle, corpNo);
        if (item == null) {
            throw new RuntimeException(String.format("没有找到注册的帐套  %s ", corpNo));
        }
        return item.getShortName();
    }

    public static String getIndustry(IHandle handle, String corpNo) {
        BookInfoRecord item = get(handle, corpNo);
        if (item == null) {
            throw new RuntimeException(String.format("没有找到注册的帐套  %s ", corpNo));
        }
        return item.getIndustry();
    }

    public static void clear(String corpNo) {
        Redis.delete(getBuffKey(corpNo));
    }

    private static String getBuffKey(String corpNo) {
        return String.format("%s.%s.%s", BufferType.getOurInfo, corpNo, buffVersion);
    }
}

@@ -932,22 +932,20 @@
 oRecord 
-result
+item
  = new B
@@ -965,30 +965,28 @@
 ();%0A        
-result
+item
 .setCode(rec
@@ -1016,30 +1016,28 @@
 ));%0A        
-result
+item
 .setShortNam
@@ -1075,30 +1075,28 @@
 ));%0A        
-result
+item
 .setName(rec
@@ -1124,30 +1124,28 @@
 ));%0A        
-result
+item
 .setAddress(
@@ -1179,30 +1179,28 @@
 ));%0A        
-result
+item
 .setTel(reco
@@ -1226,30 +1226,28 @@
 ));%0A        
-result
+item
 .setManagerP
@@ -1291,30 +1291,28 @@
 ));%0A        
-result
+item
 .setStartHos
@@ -1350,30 +1350,28 @@
 ));%0A        
-result
+item
 .setContact(
@@ -1405,30 +1405,28 @@
 ));%0A        
-result
+item
 .setAuthenti
@@ -1475,30 +1475,28 @@
 ));%0A        
-result
+item
 .setStatus(r
@@ -1525,30 +1525,28 @@
 ));%0A        
-result
+item
 .setCorpType
@@ -1579,22 +1579,20 @@
         
-result
+item
 .setIndu
@@ -1683,19 +1683,16 @@
 son(
-result
+item
 ));%0A
-%0A
     
@@ -1702,22 +1702,20 @@
  return 
-result
+item
 ;%0A    %7D%0A
@@ -2257,229 +2257,8 @@
 ();%0A
-        return getCorpType(handle, corpNo);%0A    %7D%0A%0A    /**%0A     * @param handle %E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%0A     * @param corpNo %E5%B8%90%E5%A5%97%E4%BB%A3%E7%A0%81%0A     * @return %E8%BF%94%E5%9B%9E%E6%8C%87%E5%AE%9A%E5%B8%90%E5%A5%97%E7%9A%84%E7%89%88%E6%9C%AC%E7%B1%BB%E5%9E%8B%0A     */%0A    public static BookVersion getCorpType(IHandle handle, String corpNo) %7B%0A
     

Update MemoryBookInfo.java