package com.changhong.epc.tenant.service.norm.impl; import com.changhong.epc.bean.admin.CorresElField; import com.changhong.epc.bean.admin.MasterDefaultMapping; import com.changhong.epc.bean.admin.MasterElementMapping; import com.changhong.epc.bean.admin.MasterValueToCode; import com.changhong.epc.bean.count.MeInfo; import com.changhong.epc.bean.tenant.cost.CostType; import com.changhong.epc.bean.tenant.master.*; import com.changhong.epc.bean.tenant.master.extend.MasterDefinesExtend; import com.changhong.epc.bean.tenant.master.extend.MasterValueListExtend; import com.changhong.epc.config.service.MasterDefineService; import com.changhong.epc.config.service.MasterValueService; import com.changhong.epc.constter.admin.AdminUrlConst; import com.changhong.epc.constter.base.BaseConst; import com.changhong.epc.constter.system.SystemClients; import com.changhong.epc.constter.system.run.EpcRestInterface; import com.changhong.epc.rely.api.epc.admin.CorresElFieldApi; import com.changhong.epc.rely.api.tool.ExcelTool; import com.changhong.epc.tenant.mapper.tenant.cost.CostTypeMapper; import com.changhong.epc.tenant.mapper.tenant.norm.MasterDefineMapper; import com.changhong.epc.tenant.mapper.tenant.norm.MasterElementMapper; import com.changhong.epc.tenant.mapper.tenant.norm.MasterValueMapper; import com.changhong.epc.tenant.service.norm.impl.value.AutoMasterTools; import com.changhong.epc.tenant.service.org.OrgFormatService; import com.github.andyczy.java.excel.ExcelUtils; import com.github.andyczy.java.excel.LocalExcelUtils; import com.iemsoft.framework.cloud.core.base.ResMsg; import com.iemsoft.framework.cloud.core.tools.JSONTool; import com.iemsoft.framework.cloud.core.tools.ObjectUtil; import com.iemsoft.framework.cloud.core.tools.SpringUtil; import com.iemsoft.framework.cloud.ribbon.RestInterface; import lombok.extern.slf4j.Slf4j; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.ui.ModelMap; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; import static com.changhong.epc.constter.parsing.subassembly.billinfo.TransverterConstKey.COST_TYPE; @Slf4j @Service("masterValueServiceImpl") public class MasterValueServiceImpl implements MasterValueService, SystemClients, BaseConst { @Resource private MasterValueMapper masterValueMapper; @Resource private MasterElementMapper masterElementMapper; @Resource private CostTypeMapper costTypeMapper; @Resource private MasterDefineMapper masterDefineMapper; @Resource private AutoMasterTools autoMasterTools; @Autowired private MasterDefineService masterDefineService; @SuppressWarnings("unchecked") @Override public List selectMasterValueListExtend(MasterValue masterValue) { // System.err.println("请求主数据参数:" + JSONTool.toJson(masterValue)); // 常量库查询关联主数据 List> list = new ArrayList>(); //已选择得主数据和要查询得主数据 List> list1 = new ArrayList>(); MasterDefine master = new MasterDefine(); master.setCode(masterValue.getDefineCode()); RestInterface restInterface = SpringUtil.getBean(SERVER_ADMIN.getBeanName(), RestInterface.class); Object data = restInterface.post(AdminUrlConst.MASTER_SELECTMASTERDEFAULT, master, ResMsg.class, EpcRestInterface.getEpcHeads()).getData(); MasterDefaultMapping mdm = JSONTool.toObj(JSONTool.toJson(data), MasterDefaultMapping.class); if (mdm != null) { master.setDef1(masterValue.getDef1()); master.setDef2(masterValue.getDef2()); master.setDef3(masterValue.getDef3()); master.setDef4(masterValue.getEleCode()); master.setDef5(masterValue.getEleValue()); mdm.setParams(masterValue.getParams()); Object data2 = restInterface.post(AdminUrlConst.MASTER_SELECTMASTERELEMENT, mdm, ResMsg.class).getData(); List lists = JSONTool.toList(JSONTool.toJson(data2), MasterElementMapping.class); switch (mdm.getType()) { case "10": master.setParams(masterValue.getParams()); Object datas = restInterface.post(AdminUrlConst.MASTER_SELECT, master, ResMsg.class).getData(); return JSONTool.toObj(JSONTool.toJson(datas), List.class); case "15": return masterValueMapper.selectValues(mdm.getTableName(), lists); case "20": return selectOrg(lists); } } if (masterDefineMapper.queryMaster(masterValue.getDefineCode()) <= 0) { Object obj = MasterDefineServiceImpl.masterRPC(AdminUrlConst.VALUE_SELECTMASTERVALUELISTEXTEND, masterValue); if (obj != null) { return JSONTool.toList(JSONTool.toJson(obj), Map.class); } } List codes = null; if (!ObjectUtil.empty(masterValue.getDef2())) { codes = JSONTool.toList(masterValue.getDef2(), String.class); if (codes.size() <= 0) { codes = null; } } if(ObjectUtil.empty(masterValue.getParams())) { masterValue.setParams( masterElementMapper.selectByCode(masterValue.getDefineCode()) .stream() .map(ele -> new CodeAndAlias(ele.getCode(), ele.getCode())) .collect(Collectors.toList()) ); } MasterValueListExtend masterValueListExtend1 = masterValueMapper.selectMasterValueListExtend(masterValue.getDefineCode(), masterValue.getParams(), masterValue.getDef1(), codes, masterValue.getDef3()); list = masterValueListExtend1 != null ? masterValueListExtend1.getValues().getList() : Collections.EMPTY_LIST; if ("one".equals(masterValue.getFlag())) { list.stream().forEach(map -> { if (map.get("value") != null && map.get("code") == null) { map.put("code", map.get("value")); } if (map.get("value") == null && map.get("code") != null) { map.put("value", map.get("code")); } }); return list; } return list; } @Resource private OrgFormatService orgFormatService; // orgFormatService.getFormatOrg(); @SuppressWarnings("unchecked") public List> selectOrg(List list) { // System.err.println(JSONTool.toJson(list)); Object orgs = orgFormatService.getFormatOrg(); List> orga = JSONTool.toObj(JSONTool.toJson(orgs), List.class); // System.out.println("组织机构:" + JSONTool.toJson(orgs)); orga.stream().forEach(org -> { org.put("pid", org.get("parentCode")); org.put("value", org.get("name")); }); return orga; } @SuppressWarnings("unchecked") public String selectOrg(String code, String value) { Object orgs = orgFormatService.getFormatOrg(); List> orga = JSONTool.toObj(JSONTool.toJson(orgs), List.class); if(ObjectUtil.empty(orga)){ return null; } for (Map ma : orga) { if (code != null && code.equals(ma.get("code"))) { return ma.get("code"); } if (value != null && value.equals(ma.get("name"))) { return ma.get("name"); } } return null; } @Override public MasterValue CodeConVal(MasterEleValue masterEleValue) { //查询映射主数据 Object a = getMaster(masterEleValue); MasterValue master = new MasterValue(); //判断为住户,通用 Integer flag = masterDefineMapper.queryMaster(masterEleValue.getDefineCode()); //映射主数据方法 if (a != null) { MasterDefaultMapping mdm = JSONTool.toObj(JSONTool.toJson(a), MasterDefaultMapping.class); switch (mdm.getType()) { case "10": MasterValueToCode mvt = new MasterValueToCode(); mvt.setAlias(masterEleValue.getAlias()); mvt.setMId(mdm.getId().toString()); mvt.setTableName(mdm.getTableName()); mvt.setDefineCode(mdm.getMdCode()); mvt.setCode(masterEleValue.getMvCode()); if (flag > 0) { mvt.setEleCode(masterElementMapper.geteleCode(mvt.getDefineCode(), "code")); mvt.setEleValue(masterElementMapper.geteleCode(mvt.getDefineCode(), "value")); } else { RestInterface restInterface = SpringUtil.getBean(SERVER_ADMIN.getBeanName(), RestInterface.class); Object data = restInterface.post(AdminUrlConst.ADMIN_DEFINE_CODE, masterEleValue, ResMsg.class, EpcRestInterface.getEpcHeads()).getData(); MasterElement masterElement = JSONTool.toObj(JSONTool.toJson(data), MasterElement.class); mvt.setEleCode(masterElement.getCode()); //name为value mvt.setEleValue(masterElement.getName()); } master = JSONTool.toObj(JSONTool.toJson(ValueToCode(AdminUrlConst.MASTER_CODECONVALUE, mvt)), MasterValue.class); return master; case "15": CostType cost = new CostType(); cost.setId(Integer.parseInt(masterEleValue.getMvCode())); CostType costs = costTypeMapper.getCost(cost); // System.out.println("费用类型:" + JSONTool.toJson(costs)); master.setEleValue(costs.getNodeName()); return master; } } if (flag > 0) { for (MasterElement met : masterValueMapper.selectCode(masterEleValue.getDefineCode())) { if (met.getAlias().equals("code")) { masterEleValue.setInMeCode(met.getCode()); } else if (met.getAlias().equals("value")) { masterEleValue.setOutMeCode(met.getCode()); } } master.setEleValue(masterValueMapper.getVal(masterEleValue).toString()); } else { // 通用 RestInterface restInterface = SpringUtil.getBean(SERVER_ADMIN.getBeanName(), RestInterface.class); Object data = restInterface.post(AdminUrlConst.VALUE_CODECONVAL, masterEleValue, ResMsg.class, EpcRestInterface.getEpcHeads()).getData(); MasterValue masterValueAdmin = JSONTool.toObj(JSONTool.toJson(data), MasterValue.class); master.setEleValue(masterValueAdmin.getEleValue()); } return master; } @Override public MasterValue ValConCode(MasterEleValue masterEleValue) { Object a = getMaster(masterEleValue); MasterValue master = new MasterValue(); //String code = // String value = masterDefineMapper.queryMaster(masterEleValue.getDefineCode());); Integer flag = masterDefineMapper.queryMaster(masterEleValue.getDefineCode()); /*if (ObjectUtil.empty(code) || ObjectUtil.empty(value)) { master.setCodeFlag(false); return master; }*/ //查看映射主数据 if (a != null) { MasterDefaultMapping mdm = JSONTool.toObj(JSONTool.toJson(a), MasterDefaultMapping.class); switch (mdm.getType()) { case "10": MasterValueToCode mvt = new MasterValueToCode(); mvt.setMId(mdm.getId().toString()); mvt.setTableName(mdm.getTableName()); mvt.setDefineCode(mdm.getMdCode()); mvt.setValue(masterEleValue.getMvCode()); master.setCodeFlag(true); if(flag>0){ mvt.setEleCode(masterElementMapper.geteleCode(masterEleValue.getDefineCode(), "code")); mvt.setEleValue(masterElementMapper.geteleCode(masterEleValue.getDefineCode(), "value")); }else{ RestInterface restInterface = SpringUtil.getBean(SERVER_ADMIN.getBeanName(), RestInterface.class); Object data = restInterface.post(AdminUrlConst.ADMIN_DEFINE_CODE, masterEleValue, ResMsg.class, EpcRestInterface.getEpcHeads()).getData(); MasterElement masterElement = JSONTool.toObj(JSONTool.toJson(data), MasterElement.class); mvt.setEleCode(masterElement.getCode()); //name为value mvt.setEleValue(masterElement.getName()); } master = JSONTool.toObj(JSONTool.toJson(ValueToCode(AdminUrlConst.MASTER_VALUECONCODE, mvt)), MasterValue.class); // master.setCodeFlag(true); return master; case "20": master.setEleValue(selectOrg(null, masterEleValue.getMvCode())); master.setCodeFlag(true); return master; case "15": CostType cost = new CostType(); cost.setNodeName(masterEleValue.getMvCode()); CostType costs = costTypeMapper.getCost(cost); master.setEleValue(Objects.toString(costs.getId(), "0")); master.setCodeFlag(true); return master; } } if(flag>0){ //租户一维 for (MasterElement met : masterValueMapper.selectCode(masterEleValue.getDefineCode())) { if (met.getAlias().equals("value")) { masterEleValue.setInMeCode(met.getCode()); } else if (met.getAlias().equals("code")) { masterEleValue.setOutMeCode(met.getCode()); } } master.setEleValue(masterValueMapper.getVal(masterEleValue).toString()); }else{ //通用一维 RestInterface restInterface = SpringUtil.getBean(SERVER_ADMIN.getBeanName(), RestInterface.class); Object data = restInterface.post(AdminUrlConst.VALUE_VALCONCODE, masterEleValue, ResMsg.class, EpcRestInterface.getEpcHeads()).getData(); MasterValue masterValueAdmin = JSONTool.toObj(JSONTool.toJson(data), MasterValue.class); master.setEleValue(masterValueAdmin.getEleValue()); } return master; } /** * 查询费用项 */ public List> selectCostItem() { //从admin中取出常量 String defineCode = SpringUtil.getBean(CorresElFieldApi.class).getVal(COST_TYPE, CorresElField::getMdCode); List> value = new ArrayList<>(); boolean flag = true; Map map = null; for (MasterValue mv : masterValueMapper.selectValue(defineCode)) { if (flag) { map = new HashMap<>(); value.add(map); } if (flag) { map.put("code", mv.getEleValue()); } else { map.put("name", mv.getEleValue()); } flag = !flag; } return value; } /** * 根据多条主数据 获得第一条返回结果 String类型 */ @Override public String getValString(String mdCode, String mecode, List params) { return masterValueMapper.getValString(mdCode, mecode, params); } /** * 查询映射主数据 */ public Object getMaster(MasterEleValue masterEleValue){ MasterDefine master = new MasterDefine(); master.setCode(masterEleValue.getDefineCode()); RestInterface restInterface = SpringUtil.getBean(SERVER_ADMIN.getBeanName(), RestInterface.class); Object data = restInterface.post(AdminUrlConst.MASTER_SELECTMASTERDEFAULT, master, ResMsg.class, EpcRestInterface.getEpcHeads()).getData(); return data; } /** * valueCode互转请求 */ public Object ValueToCode(String url,Object o){ RestInterface restInterface = SpringUtil.getBean(SERVER_ADMIN.getBeanName(), RestInterface.class); Object data = restInterface.post(url, o, ResMsg.class, EpcRestInterface.getEpcHeads()).getData(); return data; } @Override public Map getMasterValString(Map parm) { String mdCode = Objects.toString(parm.get("defineCode")); String resCode= Objects.toString(parm.get("masterCode")); return new ModelMap("code", resCode) .addAttribute("value" , autoMasterTools.getVal(mdCode , AutoMasterVal.of(mdCode , resCode , Arrays.asList( MeInfo.of(Objects.toString(parm.get("eleCode")) , Objects.toString(parm.get("eleValue"))) ))) ); } @Override public String getValTwoDimensional(String defMapTableName, String defMapTalbeField, List params) { return masterValueMapper.getValTwoDimensional(defMapTableName, defMapTalbeField, params); } @Override public Object seacheValues(MasterValue masterValue) { return null; } @Override public int importExcel(InputStream inputStream, String name, String defineCode) throws IOException { log.debug("导入excel名:{},定义名:{}", name, defineCode); MasterValue masterValue = new MasterValue(); masterValue.setDefineCode(defineCode); return ExcelTool.importExcel(inputStream, name, (line)->{ masterValue.setData(line); addConfigMaster(masterValue); }); } @Override public void outTemplate(String defineCode, OutputStream os) throws IOException { List masterDefinesExtends = masterDefineService.selectEle(MasterDefine.of(defineCode)); if(ObjectUtil.empty(masterDefinesExtends)){ return; } Map title = masterDefinesExtends .stream() .findFirst() .map(MasterDefinesExtend::getEleList) .map(List::stream) .orElse(Stream.of()) .reduce(new HashMap<>() , (me, a)->{ me.put(a.getCode(), a.getName()); return me; }, (t, s)->{ t.putAll(s); return t; }); ExcelTool.outExcel(os, title, Collections.EMPTY_LIST); } @Override public List getSelectValue(String defineCode) { return masterValueMapper.selectValue(defineCode); } public String getMasterCode(MasterValue masterValue) { return masterValueMapper.getMasterCode(masterValue); } @Override public Object selectValueSeache(MasterValue masterValue) { return null; } public int addConfigMaster(MasterValue master) { MasterDefaultMapping ma = getTableName(master.getDefineCode()); Map data = getField(master.getData(), ma); return masterValueMapper.addConfigMaster(data, ma.getTableName()); } public int deleteConfigMaster(MasterValue master) { MasterDefaultMapping ma = getTableName(master.getDefineCode()); return masterValueMapper.deleteConfigMaster(master.getId(), ma.getTableName()); } public int updateConfigMaster(MasterValue master) { MasterDefaultMapping ma = getTableName(master.getDefineCode()); Map data = getField(master.getData(), ma); //更新的同时把自定义传过来的公司代码和公司名称一起修改 return masterValueMapper.updateConfigMaster(master.getId(), data, ma.getTableName(),master.getData().get("012001"),master.getData().get("012000")); } public Map getField(Map data, MasterDefaultMapping mdm) { Map map = new HashMap<>(); Object data2 = MasterDefineServiceImpl.masterRPC(AdminUrlConst.MASTER_SELECTMASTERELEMENT, mdm); List lists = JSONTool.toList(JSONTool.toJson(data2), MasterElementMapping.class); lists.stream() .filter(li -> data.get(li.getMeCode()) != null) .forEach(o -> map.put(o.getTableField(), data.get(o.getMeCode()))); return map; } public MasterDefaultMapping getTableName(String code) { Object data = MasterDefineServiceImpl.masterRPC(AdminUrlConst.MASTER_SELECTMASTERDEFAULT, new ModelMap("code", code)); MasterDefaultMapping mdm = JSONTool.toObj(JSONTool.toJson(data), MasterDefaultMapping.class); return mdm; } @Override public Object selectValue(MasterValue masterValue) { Map> group = new TreeMap<>(); List list; Integer flag = masterDefineMapper.queryMaster(masterValue.getDefineCode()); if(flag>0){ list = getSelectValue(masterValue.getDefineCode()); }else{ Object o = MasterDefineServiceImpl.masterRPC(AdminUrlConst.VALUE_SELECTVALUE, masterValue); list = JSONTool.toList(JSONTool.toJson(o), MasterValue.class); } if(ObjectUtil.empty(list)){ list = new ArrayList<>(); } boolean flog = true; for (MasterValue mv : list) { Map temp; if(null == (temp = group.get(mv.getRowNo()))){ temp = new HashMap<>(); group.put(mv.getRowNo(),temp ); } if(flog){ // System.out.println("code"); temp.put("code", mv.getEleValue()); }else{ temp.put("value", mv.getEleValue()); // System.out.println("val"); } // System.out.println(flog); flog = !flog; } return group.values(); } }