package com.changhong.autoform.mapper.data.impl; import com.changhong.autoform.core.mapper.BaseMapper; import com.changhong.autoform.entity.FormData; import com.changhong.autoform.entity.SelectForm; import com.changhong.autoform.entity.SubForm; import com.changhong.autoform.entity.page.PageResult; import com.changhong.autoform.entity.sql.Where; import com.changhong.autoform.entity.sql.Where.Conditions; import com.changhong.autoform.entity.sql.Where.JoinType; import com.changhong.autoform.entity.sql.Where.Type; import com.changhong.autoform.entity.sql.delete.Delete; import com.changhong.autoform.entity.sql.insert.Insert; import com.changhong.autoform.entity.sql.select.Select; import com.changhong.autoform.entity.sql.select.SelectPage; import com.changhong.autoform.entity.sql.update.Update; import com.changhong.autoform.mapper.data.DataMapper; import com.iemsoft.framework.cloud.core.tools.JSONTool; import org.springframework.stereotype.Component; import org.springframework.ui.ModelMap; import java.util.*; /** * * @author WangYT * */ @Component("simpleDataMapper") public class DataMapperImpl extends BaseMapper implements DataMapper{ public static final String COUNT_KEY = "COUNT(0)"; // public static final String TABLR_BILL_DATA = "epc_bill_data"; // public static final String EXPEND_SUB = "journey"; // public static final String[] TABLR_BILL_DATA_FIELD = {"bill_info"}; @Override public int insertData(FormData formData) { System.err.println(JSONTool.toJson(formData)); Insert insert = new Insert(formData.getFormName() ,new ModelMap("tenantID", formData.getTenantID()) .addAttribute("deleteFlg",0) .addAttribute("PARENTDATAROWNUM", 0) .addAttribute("CREATEUSER", formData.getUserName()) .addAllAttributes(formData.getData())); //添加主表 int count = execute(insert); //获取最后插入的id Integer lastInsertId = insert.getId(); //添加从表 List subFormList = formData.getSubFormList(); if(null != subFormList && !subFormList.isEmpty()){ for (SubForm subForm : subFormList) { List> list = subForm.getData(); System.out.println("记录错误位置---61"); if(null !=list && !list.isEmpty()){ for (Map map : list) { count+=execute(new Insert(subForm.getFormName() ,new ModelMap("deleteFlg",0) .addAttribute("tenantID", formData.getTenantID()) .addAttribute("PARENTDATAROWNUM", lastInsertId) .addAllAttributes(map))); } } } } if(count >0) return lastInsertId; else return -1; } //删除(修改deleteflg状态为1) @Override public int deleteDate(FormData formData) { List dataIds = formData.getDataIds(); int count = 0; count = execute(new Update(formData.getFormName() , new ModelMap("deleteflg", "1") , new Where( new Conditions("tenantID", Type.EQ, formData.getTenantID()) , new Conditions("dataRowNum", Type.IN, JoinType.AND, dataIds)))); List subFormList = formData.getSubFormList(); for (SubForm subForm : subFormList) { count+=execute(new Update(subForm.getFormName() , new ModelMap("deleteflg", "1") , new Where( new Conditions("PARENTDATAROWNUM", Type.IN, dataIds)))); } return count; } //修改 @Override public int updateDate(FormData formData) { System.out.println("修改传入表单数据:"+JSONTool.toJson(formData)); //更新主表 int count = execute( new Update(formData.getFormName() , formData.getData() , new Where(new Conditions("dataRowNum", Type.EQ, formData.getDataRowNum())))); //更新从表 先删再更 List subFormList = formData.getSubFormList(); for (SubForm subForm : subFormList) { execute(new Delete(subForm.getFormName() , new Where( new Conditions("PARENTDATAROWNUM",Type.EQ,formData.getDataRowNum())))); } for (SubForm subForm : subFormList) { List> list = subForm.getData(); for (Map map : list) { count++; addSubForm(map, formData,subForm.getFormName()); } } return count ; } //子表单添加数据,用于重写 @Override public int addSubForm(Map map,FormData formData,String formName){ Insert in = new Insert(formName ,new ModelMap("deleteFlg",0) .addAttribute("tenantID", formData.getTenantID()) .addAttribute("PARENTDATAROWNUM",formData.getDataRowNum()) .addAllAttributes(map)); execute(in); return in.getId(); } //查询所有数据 @Override public List> selectAll(SelectForm selectForm) { Map fields = selectForm.getFields(); fields.put("DATAROWNUM", "DATAROWNUM"); String screenCondition = selectForm.getScreenCondition(); Select select = null; if(screenCondition!=null && !"".equals(screenCondition)){ select = new SelectPage(String.format(selectForm.getFormName()) , new Where(screenCondition,new Conditions("tenantID",Type.EQ,selectForm.getTenantID()) , new Conditions("deleteFlg", Type.EQ, JoinType.AND, 0) ) , fields.keySet().toArray(new String[fields.size()]) , selectForm.getStartRow() , selectForm.getEndRow()); }else{ select = new SelectPage(String.format(selectForm.getFormName()) , new Where(new Conditions("tenantID",Type.EQ,selectForm.getTenantID()) , new Conditions("deleteFlg", Type.EQ, JoinType.AND, 0)) , fields.keySet().toArray(new String[fields.size()]) , selectForm.getStartRow() , selectForm.getEndRow()); } select.setOrderName("DATAROWNUM"); return selectList(select, (index, key, rs, result)->{ if(key.equalsIgnoreCase("dataRowNum")){ result.put(Objects.toString(fields.get(key)), rs.getInt(index)); }else{ result.put(Objects.toString(fields.get(key)), rs.getString(index)); } }); } //查询分页数据 @Override public PageResult> selectPage(SelectForm selectForm) {//fenye Object count; // 上面为注释掉的方法 Map fields = selectForm.getFields(); fields.put(selectForm.getFormName()+".DATAROWNUM", "DATAROWNUM"); String screenCondition = selectForm.getScreenCondition(); Select select; Where oneWhere, listWhere; if(screenCondition!=null && !"".equals(screenCondition)) { oneWhere = new Where(screenCondition,new Conditions(selectForm.getFormName()+".tenantID",Type.EQ,selectForm.getTenantID()) , new Conditions(selectForm.getFormName()+".deleteFlg", Type.EQ, JoinType.AND, 0)); listWhere = new Where(screenCondition,new Conditions(selectForm.getFormName()+".tenantID",Type.EQ,selectForm.getTenantID()) , new Conditions(selectForm.getFormName()+".deleteFlg", Type.EQ, JoinType.AND, 0)); }else{ oneWhere = new Where(new Conditions(selectForm.getFormName()+".tenantID",Type.EQ,selectForm.getTenantID()) , new Conditions(selectForm.getFormName()+".deleteFlg", Type.EQ, JoinType.AND, 0)); listWhere = new Where(new Conditions(selectForm.getFormName()+".tenantID",Type.EQ,selectForm.getTenantID()) , new Conditions(selectForm.getFormName()+".deleteFlg", Type.EQ, JoinType.AND, 0)); } //查询总条数 count = selectOne(new Select(selectForm.getFormName() , oneWhere , new String[]{COUNT_KEY})).get(COUNT_KEY); if(count == null || Long.parseLong(count.toString()) <= 0) return new PageResult<>(new ArrayList<>(), 0L); //业务数据查询实体 select = new SelectPage(String.format(selectForm.getFormName()) , listWhere , fields.keySet().toArray(new String[fields.size()]) , selectForm.getStartRow() , selectForm.getEndRow()); select.setOrderName(selectForm.getFormName()+".DATAROWNUM"); return new PageResult<>( selectList(select, (index, key, rs, result)->{ if(key.equalsIgnoreCase("dataRowNum")){ result.put(Objects.toString(fields.get(key)), rs.getInt(index)); }else{ result.put(Objects.toString(fields.get(key)), rs.getString(index)); } }), Long.parseLong(count.toString())); } //查询一条数据 @Override public Map selectOne(SelectForm selectForm) { //主表数据 Map fields = selectForm.getFields(); fields.put("DATAROWNUM", "DATAROWNUM"); fields.put("orderCode", "orderCode"); fields.put("orderStatus", "orderStatus"); fields.put("voucherCode", "voucherCode"); Set fieldKey = fields.keySet(); Select select =new Select(String.format(selectForm.getFormName()) , new Where(new Conditions("deleteFlg",Type.EQ,0) , new Conditions("tenantID",Type.EQ,JoinType.AND,selectForm.getTenantID()) , new Conditions("dataRowNum",Type.EQ,JoinType.AND,selectForm.getDataRowNum()) , new Conditions("PARENTDATAROWNUM",Type.EQ,JoinType.AND,0)) , fieldKey.toArray(new String[fieldKey.size()])); Map formDataResult = selectOne(select,(index, key, rs, result)->{ if(key.equalsIgnoreCase("dataRowNum")){ result.put(key, rs.getInt(index)); }else{ String val = rs.getString(index); if(null == val){ result.put(Objects.toString(fields.get(key)), ""); }else{ result.put(Objects.toString(fields.get(key)), val); } } }); Map>> subMap = new HashMap<>(); //从表数据 List subFormList = selectForm.getSubForm(); if(null!=subFormList&&!subFormList.isEmpty()){ for (SubForm subForm : subFormList) { Map subFields = subForm.getFields(); subFields.put("DATAROWNUM", "DATAROWNUM"); Set subFieldkey = subFields.keySet(); Select subSelect=new Select(subForm.getFormName() , new Where(new Conditions("deleteFlg",Type.EQ,0) , new Conditions("tenantID",Type.EQ,JoinType.AND,selectForm.getTenantID()) , new Conditions("PARENTDATAROWNUM",Type.EQ,JoinType.AND,subForm.getSubDataRowNum())) , subFieldkey.toArray(new String[subFieldkey.size()])); subMap.put(subForm.getFormDataNum(), selectList(subSelect,(index, key, rs, result)->{ if(key.equalsIgnoreCase("dataRowNum")){ result.put(key, rs.getInt(index)); // 注释详情带出票据信息 // String joury = subForm.getFormName().split("_")[subForm.getFormName().split("_").length -1]; //if have joury and orderCode // if(EXPEND_SUB.equals(joury) && formDataResult != null // && formDataResult.get("orderCode") != null && !"".equals(formDataResult.get("orderCode"))){ // Select billData = new Select(TABLR_BILL_DATA // , new Where(new Conditions("j_id",Type.EQ,rs.getInt(index)) // , new Conditions("order_code",Type.EQ,JoinType.AND,formDataResult.get("orderCode"))) // , TABLR_BILL_DATA_FIELD); // result.put("billData", selectList(billData)); // } }else{ String val = rs.getString(index); if(null == val){ result.put(Objects.toString(subFields.get(key)), ""); }else{ result.put(Objects.toString(subFields.get(key)), val); } } })); } } return new ModelMap("main", formDataResult).addAttribute("subs", subMap); } @Override public String getUserName(SelectForm selectForm) { Select select =new Select(String.format(selectForm.getFormName()) , new Where(new Conditions("deleteFlg",Type.EQ,0) , new Conditions("tenantID",Type.EQ,JoinType.AND,selectForm.getTenantID()) , new Conditions("dataRowNum",Type.EQ,JoinType.AND,selectForm.getDataRowNum())) ,"CREATEUSER"); Map formDataResult = selectOne(select,(index, key, rs, result)->{ System.err.println(key); System.err.println(); String userName = rs.getString(index); if(key.equals("CREATEUSER")) result.put("userName",userName); }); return (String) formDataResult.get("userName"); } }