package cn.cerc.mis.services;

import com.google.gson.Gson;

import cn.cerc.core.ClassResource;
import cn.cerc.core.Record;
import cn.cerc.core.Utils;
import cn.cerc.db.cache.Redis;
import cn.cerc.db.core.IHandle;
import cn.cerc.mis.SummerMIS;
import cn.cerc.mis.core.CenterService;
import cn.cerc.mis.other.BookVersion;
import cn.cerc.mis.other.BufferType;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MemoryBookInfo {
    private static final ClassResource res = new ClassResource(MemoryBookInfo.class, SummerMIS.ID);

    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);
        }

        BookInfoRecord item = new BookInfoRecord();
        CenterService svr = new CenterService(handle);
        svr.setService("SvrBookInfo.getRecord");
        if (!svr.exec("corpNo", corpNo)) {
            log.error(svr.getMessage());
            item.setCode(corpNo);
            return item;
        }
        Record record = svr.getDataOut().getHead();

        item.setCode(record.getString("CorpNo_"));
        item.setShortName(record.getString("ShortName_"));
        item.setName(record.getString("Name_"));
        item.setAddress(record.getString("Address_"));
        item.setTel(record.getString("Tel_"));
        item.setFastTel(record.getString("FastTel_"));
        item.setManagerPhone(record.getString("ManagerPhone_"));
        item.setStartHost(record.getString("StartHost_"));
        item.setContact(record.getString("Contact_"));
        item.setAuthentication(record.getBoolean("Authentication_"));
        item.setStatus(record.getInt("Status_"));
        item.setCorpType(record.getInt("Type_"));
        item.setIndustry(record.getString("Industry_"));
        item.setCurrency(record.getString("Currency_"));
        item.setEmail(record.getString("Email_"));
        item.setFax(record.getString("Fax_"));

        Redis.set(getBuffKey(corpNo), gson.toJson(item));
        return item;
    }

    /**
     * 检查帐套代码是否是系统生成
     */
    public static boolean exist(IHandle handle, String corpNo) {
        BookInfoRecord book = MemoryBookInfo.get(handle, corpNo);
        return Utils.isNotEmpty(book.getShortName());
    }

    /**
     * @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(res.getString(1, "没有找到注册的帐套 %s"), corpNo));
        }
        return item.getStatus();
    }

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

    /**
     * @param handle 环境变量
     * @param corpNo 帐套代码
     * @return 返回指定帐套的版本类型
     */
    public static BookVersion getBookType(IHandle handle, String corpNo) {
        BookInfoRecord item = get(handle, corpNo);
        if (item == null) {
            throw new RuntimeException(String.format(res.getString(1, "没有找到注册的帐套 %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(res.getString(1,"没有找到注册的帐套 %s"), corpNo));
        }
        return item.getShortName();
    }

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

    public static String getIndustry(IHandle handle, String corpNo) {
        BookInfoRecord item = get(handle, corpNo);
        if (item == null) {
            throw new RuntimeException(String.format(res.getString(1, "没有找到注册的帐套 %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);
    }
}

@@ -638,32 +638,138 @@
 tring corpNo) %7B%0A
+        if (Utils.isEmpty(corpNo)) %7B%0A            throw new RuntimeException(%22corpNo is null.%22);%0A        %7D%0A
         Gson gso
@@ -3855,32 +3855,33 @@
 res.getString(1,
+ 
 %22%E6%B2%A1%E6%9C%89%E6%89%BE%E5%88%B0%E6%B3%A8%E5%86%8C%E7%9A%84%E5%B8%90%E5%A5%97 %25s%22),
@@ -4248,16 +4248,17 @@
 tring(1,
+ 
 %22%E6%B2%A1%E6%9C%89%E6%89%BE%E5%88%B0%E6%B3%A8%E5%86%8C%E7%9A%84

Update MemoryBookInfo.java