package com.taover.codegenerate.business.util; import java.io.File; import java.io.StringWriter; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.taover.codegenerate.business.Constants; import com.taover.codegenerate.db.Tools; import freemarker.template.Configuration; import freemarker.template.Template; public class TempVelocity { public static void main(String args[]) throws SQLException{ String cnFunc = "代销平台毛利=1-代销平台成本/代销平台报价"; // String func = "promotProfit=promotPrice-promotAllCost-shareReward*shareRewardRate/100"; generate(cnFunc); } public static void generate(String cnFunc) { try{ File f = new File(Tools.getPath() + Constants.TEMPATE_PATH); Configuration cfg = new Configuration(); cfg.setDirectoryForTemplateLoading(f); StringWriter htmlOut = new StringWriter(); Template htmlTemplate = cfg.getTemplate("Temp.ftl", "UTF-8"); Map data = new HashMap(); data.put("cnFunc", cnFunc); parseData(data); htmlTemplate.process(data, htmlOut); System.out.println(htmlOut.toString()); htmlOut.close(); }catch(Exception e){ e.printStackTrace(); } } public static void parseData(Map data){ //propertyList,result String cnFunc = (String) data.get("cnFunc"); String result = cnFunc.split("=")[0]; String resultEn = getEnName(result); String propertyStr = cnFunc.split("=")[1]; List propertyList = new ArrayList(); // Pattern pattern = Pattern.compile("[a-zA-Z]+", Pattern.CASE_INSENSITIVE); Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]+", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(propertyStr); List cnNameList = new ArrayList(); String enFunc = cnFunc; while(matcher.find()){ String tempGroup = matcher.group(); cnNameList.add(tempGroup); } for(String cnName : cnNameList){ String enName = getEnName(cnName); if(enName == null){ enName = "errorVal"; } propertyList.add(enName); enFunc = enFunc.replaceAll(cnName+"\\b", enName); } enFunc = enFunc.replaceAll(result, resultEn); data.put("enFunc", enFunc); data.put("result", resultEn); data.put("propertyList", propertyList); } public static String getEnName(String cnName){ Map keyReflect = new HashMap(); keyReflect.put("供货价格","goodsMoney"); keyReflect.put("物料成本","materielFee"); keyReflect.put("运费","freight"); keyReflect.put("赠品成本","freeGift"); keyReflect.put("检测成本","detectPer"); keyReflect.put("预计件数","preNumber"); keyReflect.put("产品成本","productCost"); keyReflect.put("分享金","shareReward"); keyReflect.put("平台售价","platPrice"); keyReflect.put("平台优惠券","platCoupon"); keyReflect.put("平台销售成本","platSaleCost"); keyReflect.put("平台总成本","platAllCost"); keyReflect.put("平台毛利","platProfit"); keyReflect.put("平台毛利率","platProfitRate"); keyReflect.put("初级分享师返利","primaryShareReward"); keyReflect.put("高级分享师返利","seniorShareReward"); keyReflect.put("分享后平台最低毛利率","afterSharePlatLowestProfit"); keyReflect.put("促销价格","promotPrice"); keyReflect.put("促销产品成本","promotCost"); keyReflect.put("促销优惠券","promotCoupon"); keyReflect.put("促销总成本","promotAllCost"); keyReflect.put("促销毛利","promotProfit"); keyReflect.put("促销毛利率","promotProfitRate"); keyReflect.put("促销初级分享师返利","promotPrimaryShareReward"); keyReflect.put("促销高级分享师返利","promotSeniorShareReward"); keyReflect.put("促销分享后平台最低毛利","promotAfterShareLowestProfit"); keyReflect.put("产销联盟报价","psUionPrice"); keyReflect.put("产销联盟渠道成本","psUnionChannelCost"); keyReflect.put("联盟毛利率","psUnionProfitRate"); keyReflect.put("采购渠道报价","channelPrice"); keyReflect.put("采购渠道成本","purchaseChannelCost"); keyReflect.put("采购毛利率","purchaseChannelProfitRate"); keyReflect.put("代销平台报价","agentPrice"); keyReflect.put("代销平台成本","agentCost"); keyReflect.put("代销平台毛利","agentProfit"); keyReflect.put("试吃供货价格","shichiGoodsMoney"); keyReflect.put("试吃售价","shichiPrice"); keyReflect.put("试吃份数","shichiNumber"); keyReflect.put("试吃成本","shichiCost"); keyReflect.put("试吃利润","shichiProfit"); keyReflect.put("积分使用率","integralUseRate"); keyReflect.put("买手提成","ticheng"); keyReflect.put("运营提点","operateRate"); keyReflect.put("初级分享师返点","primaryAgentRate"); keyReflect.put("高级分享师返点","seniorAgentRate"); keyReflect.put("渠道拓展提点","channelExpRate"); keyReflect.put("渠道维护提点","channelProRate"); keyReflect.put("转账费率","transAccountRate"); keyReflect.put("损耗","lossRate"); keyReflect.put("分享金比例","shareRewardRate"); return keyReflect.get(cnName); } }