package com.topsun.posclient.common;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import com.topsun.posclient.common.core.CommonCoreActivator;
import com.topsun.posclient.common.service.impl.BaseServiceImpl;
import com.topsun.posclient.datamodel.Auth;
import com.topsun.posclient.datamodel.SettingData;
import com.topsun.posclient.datamodel.User;
/**
* @author Dong
*
*/
public class POSClientApp {
static POSClientApp posClientApp;
static Map<Integer, User> map = new TreeMap<Integer, User>();
static {
BaseServiceImpl baseServiceImpl = new BaseServiceImpl();
try {
List <User> list = baseServiceImpl.getAllUser();
if(null != list && list.size() > 0){
for (int i = 0; i < list.size(); i++) {
map.put(i, list.get(i));
}
}
} catch (POSException e) {
LoggerUtil.logError(CommonCoreActivator.PLUGIN_ID, "POSClientApp初始化用户数据出错", e);
}
}
/**
* 保存系统相关数据
*/
private Map<String, Object> contextData = new HashMap<String, Object>();
private POSClientApp() {}
public static POSClientApp get() {
if (null == posClientApp) {
posClientApp = new POSClientApp();
}
return posClientApp;
}
public void saveLoginUser(User loginUser) {
contextData.put(AppConstants.LOGIN_USER, loginUser);
}
public Map<Integer, User> getUserRelation(){
return map;
}
public User getLoginUser() {
return (User) contextData.get(AppConstants.LOGIN_USER);
}
public List<Auth> getLoginUserAuths(){
User loginUser = this.getLoginUser();
if(null == loginUser){
return null;
}
List<Auth> auths = loginUser.getAuths();
return auths;
}
public void setData(String key ,Object value){
if(key == null){
return;
}
contextData.put(key,value);
}
public Object getData(String key){
if(key == null){
return null;
}
return contextData.get(key);
}
/**
* 更新系统配置信息
* @param settingData
* @throws POSException
*/
public void updateSysConfig(SettingData settingData) throws Exception{
String filepath = ProjectUtil.getRuntimeClassPath()+AppConstants.SEETING_FILE;
try{
Properties prop = new Properties();
OutputStream outputStream = new FileOutputStream(filepath);
prop.setProperty(AppConstants.SERVER_IP, settingData.getServerIp());
prop.setProperty(AppConstants.SERVER_PORT, settingData.getServerPort());
prop.setProperty(AppConstants.POSNO, settingData.getPosNo());
prop.setProperty(AppConstants.CDKEY, settingData.getCdKey());
prop.setProperty(AppConstants.OWNER_SHOP, settingData.getOwnerShop());
prop.setProperty(AppConstants.DOCNUM, settingData.getDocNum());
prop.setProperty(AppConstants.OG_DOCNUM, settingData.getOgDocNum());
prop.setProperty(AppConstants.R_DOCNUM, settingData.getrDocNum());
prop.setProperty(AppConstants.B_DOCNUM, settingData.getbDocNum());
prop.setProperty(AppConstants.G_DOCNUM, settingData.getgDocNum());
prop.setProperty(AppConstants.INVOICE_NUMBER, settingData.getInvoiceNumber());
prop.setProperty(AppConstants.INVOICE_CODE, settingData.getInvoiceCode());
prop.setProperty(AppConstants.LAST_UPDATE_TIME, settingData.getLastUpdateTime());
prop.store(outputStream, "author: topsun");
outputStream.flush();
outputStream.close();
}catch(Exception e){
LoggerUtil.logError(CommonCoreActivator.PLUGIN_ID, e);
throw new Exception("更新配置失败");
}
}
/**
* 获取系统配置信息
* @return
*/
public SettingData getSysConfig() {
String filepath = ProjectUtil.getRuntimeClassPath()+AppConstants.SEETING_FILE;
SettingData settingData = new SettingData();
try {
String serverIP = ProjectUtil.readValue(filepath, AppConstants.SERVER_IP);
String serverPort = ProjectUtil.readValue(filepath, AppConstants.SERVER_PORT);
String cdKey = ProjectUtil.readValue(filepath, AppConstants.CDKEY);
String posNo = ProjectUtil.readValue(filepath, AppConstants.POSNO);
String ogDocNum = ProjectUtil.readValue(filepath, AppConstants.OG_DOCNUM);
String rDocNum = ProjectUtil.readValue(filepath, AppConstants.R_DOCNUM);
String bDocNum = ProjectUtil.readValue(filepath, AppConstants.B_DOCNUM);
String gDocNum = ProjectUtil.readValue(filepath, AppConstants.G_DOCNUM);
String docNum = ProjectUtil.readValue(filepath, AppConstants.DOCNUM);
String ownerShop = ProjectUtil.readValue(filepath, AppConstants.OWNER_SHOP);
String invoiceNumber = ProjectUtil.readValue(filepath, AppConstants.INVOICE_NUMBER);
String invoiceCode = ProjectUtil.readValue(filepath, AppConstants.INVOICE_CODE);
String lastUpdateTime = ProjectUtil.readValue(filepath, AppConstants.LAST_UPDATE_TIME);
settingData.setServerIp(null == serverIP?"127.0.0.1":serverIP);
settingData.setServerPort(null == serverPort?"8088":serverPort);
settingData.setCdKey(null == cdKey?"POS01":cdKey);
settingData.setPosNo(null == posNo?"POS01":posNo);
String currentDate = ProjectUtil.getCurrentDateByFormat("yyMMdd");
settingData.setOgDocNum(null == ogDocNum?currentDate+"0000":ogDocNum);
settingData.setrDocNum(null == rDocNum?currentDate+"0000":rDocNum);
settingData.setbDocNum(null == bDocNum?currentDate+"0000":bDocNum);
settingData.setgDocNum(null == gDocNum?currentDate+"0000":gDocNum);
settingData.setDocNum(null == docNum?currentDate+"0000":docNum);
settingData.setOwnerShop(null == ownerShop?"":ownerShop);
settingData.setInvoiceNumber(null == invoiceNumber?"00000000":invoiceNumber);
settingData.setInvoiceCode(null == invoiceCode?"000000000000":invoiceCode);
settingData.setLastUpdateTime(null == lastUpdateTime?String.valueOf(System.currentTimeMillis()):lastUpdateTime);
} catch (Exception e) {
LoggerUtil.logError(CommonCoreActivator.PLUGIN_ID, e);
}
return settingData;
}
/**
* @throws Exception
*/
public void doExit() throws Exception{
// SettingData setData = POSClientApp.get().getSysConfig();
// setData.setOgDocNum("0000");
// setData.setDocNum("0000");
// setData.setrDocNum("0000");
// setData.setbDocNum("0000");
// setData.setgDocNum("0000");
// this.updateSysConfig(setData);
}
}