package com.murong.ecp.app.mbu.action.bmbuurm8;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.murong.ecp.app.mbu.common.BuiMessageCode;
import com.murong.ecp.app.mbu.dao.bmbuurm8.XyHealthDictMapper;
import com.murong.ecp.app.pub.atc.PUBATCUtil;
import com.yuangou.ecp.biz.transengine.AbstractTransaction;
import com.yuangou.ecp.biz.transengine.Utils;
import com.yuangou.ecp.bp.core.common.exception.YGException;
import com.yuangou.ecp.bp.core.common.message.YGBizMessage;
import com.yuangou.ecp.bp.core.common.message.YGBizMessageContext;
import com.yuangou.ecp.bp.core.common.message.YGEDB;
import com.yuangou.ecp.bp.core.common.yglog4j.Logger;
import com.yuangou.ecp.bp.core.common.yglog4j.YGLogger;
@Service("healthdictexport")
public class XyHealthDictExportAction extends AbstractTransaction{
@Autowired
XyHealthDictMapper xyHealthDictMapper;
@Override
public void doProcess(YGBizMessageContext bizCtx) throws YGException {
YGBizMessage yGBizMessage = bizCtx.getCurrentMsg();
Logger logger = YGLogger.getLogger(yGBizMessage);
YGEDB edb = yGBizMessage.getEDBBody();
edb.setData("rsp_ajx", "true");
PUBATCUtil.GDASetMsg(bizCtx, BuiMessageCode.SUCCESS, "");
logger.info("导出医疗字典到js文件start");
logger.info("开启游标:");
PUBATCUtil.openCursor(bizCtx, "qryHealthDictList", "healthDictListCursor");
logger.info("开启游标:");
List<String> typList = new ArrayList<>();
//将数据拼接成字符串
StringBuffer stringBuffer=new StringBuffer("var mrdict = new Object();");
int i=0;
while (true) {
PUBATCUtil.fetchCursor(bizCtx, "healthDictListCursor", "healthDictListCursor"+i);
if (Utils.eval(bizCtx, "~retcod=2")) {
logger.info("关闭游标:11");
PUBATCUtil.closeCursor(bizCtx, "healthDictListCursor");
logger.info("关闭游标:12");
break;
}else{
String idStr = edb.getData("healthDictListCursor"+i+".id");
String textStr = edb.getData("healthDictListCursor"+i+".text");
String typStr = edb.getData("healthDictListCursor"+i+".typ");
//直接拼接
logger.info("i======================================="+i);
if (typList.contains(typStr)) {
stringBuffer.append(",").append("""+idStr+"":""+textStr+""");
}else {
//先将typName放入Set,再拼接
typList.add(typStr);
//非首次进入判断才追加"}"
if (i != 0) {
stringBuffer.append("};");
}
stringBuffer.append("
").append("mrdict.").append(typStr).append("={""+idStr+"":""+textStr+""");
}
i++;
}
}
logger.info("游标执行总数="+i);
logger.info("关闭游标:21");
PUBATCUtil.closeCursor(bizCtx, "healthDictListCursor");
logger.info("关闭游标:22");
String line = "
";
String t =" ";
String fn = "Formatter = function(val,row) {"+line+t+
"var data = mrdict.";
String fn1 = line+t+"try { "+line+t+
"if( !data.hasOwnProperty(val) ) {"+line+t+
"return val;"+line+t+
"}"+line+t+
" } catch(error) {"+line+t+
"console.error(error);"+line+t+
"return val;"+line+t+
" }"+line+t+
"return data[val];"+line+
"}";
stringBuffer.append("};");
for (int j = 0; j < typList.size(); j++) {
stringBuffer.append("
").append("mrdict.").append(typList.get(j)).append(fn).append(typList.get(j)+";").append(fn1);
}
//上传到服务器的位置
String path = "/home/iver/ejbs/mrbui/healthDict.js";
try {
OutputStream output = new FileOutputStream(path);
byte[] date=stringBuffer.toString().getBytes();
output.write(date);
output.close();
logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
} catch (Exception e) {
logger.info("医疗字典导出js文件异常");
logger.info(e);
}
PUBATCUtil.GDASetMsg(bizCtx, "CMM00000", "");
logger.info("导出医疗字典到js文件over");
}
}