package com.platform.service.impl; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.apache.log4j.Logger; import org.springframework.stereotype.Service; import com.platform.dao.PreDataInfoDao; import com.platform.entities.DefaultDataDescription; import com.platform.entities.MyFileEntity; import com.platform.entities.PreDataInfo; import com.platform.service.IScriptMakeService; import com.platform.utils.Configs; import com.platform.utils.Constant; import com.platform.utils.FileOperateHelper; import com.platform.utils.XmlOperationByDom4j; @Service(value = "scriptMakeService") public class ScriptMakeService implements IScriptMakeService { public static Logger log = Configs.DAILY_ROLLING_LOGGER.getLogger(PreDataInfoServiceImpl.class); @Resource(name = "preDataInfoDao") private PreDataInfoDao preDataInfoDao; @Override public int makeCfg() throws Exception { log.info("makeCfg"); List systemInfoList = preDataInfoDao.findAll(); String path = FileOperateHelper.addLastSeparator(Configs.sql_script_path_standard); StringBuffer sb = new StringBuffer(); sb.append("市\t县(市、区)\t行政区划代码\r\n"); Map startData = new HashMap(); for (PreDataInfo preDataInfo : systemInfoList) { startData.put(preDataInfo.getAreaCode(), preDataInfo); } //以地区为准--一个地区一个xml for ( String key : startData.keySet()) { PreDataInfo preDataInfo = (PreDataInfo) startData.get(key); sb.append(preDataInfo.getCityName()); sb.append("\t"); sb.append(preDataInfo.getDistrictName()); sb.append("\t"); sb.append(preDataInfo.getAreaCode()); sb.append("\r\n"); } FileOperateHelper.fileWrite(path+Constant.cfgFileName, sb.toString()); log.info("makeCfg--end"); return 1; } @Override public int makeXml() throws Exception { log.info("makeXml"); List systemInfoList = preDataInfoDao.findAll(); String dirpath = FileOperateHelper.addLastSeparator(Configs.sql_script_path_standard); Map> map = new HashMap>(); for (PreDataInfo preDataInfo : systemInfoList) { putSystemByAreaCode(map, preDataInfo.getAreaCode(), preDataInfo); } //一个地区一个xml for ( String key : map.keySet()) { String areaPath = dirpath + key; File f = new File(areaPath); f.mkdir(); List defList = new ArrayList(); List list = map.get(key); for (Object object : list) { DefaultDataDescription defaul = new DefaultDataDescription(); defaul.setvalue((PreDataInfo) object); defList.add(defaul); } XmlOperationByDom4j.createXml(FileOperateHelper.addLastSeparator(areaPath)+key+".xml", defList, "Data_info", "workRange"); } log.info("makeXml--end"); return 1; } @Override public int moveFiles(MyFileEntity file) throws Exception { // TODO Auto-generated method stub return 0; } @Override public List FindFiles() throws Exception { // TODO Auto-generated method stub return null; } /** * @param map * @param code * @param data * @return */ private Map> putSystemByAreaCode(Map> map,String code, PreDataInfo data) { List list = map.get(code); if (null == list) { list = new ArrayList(); } list.add(data); map.put(code, list); return map; } }