zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
commit | author | age
a18bfa 1     package com.changhong.autoform.mapper.data.impl;
Z 2
3
4 import com.changhong.autoform.core.mapper.BaseMapper;
5 import com.changhong.autoform.entity.FormData;
6 import com.changhong.autoform.entity.SelectForm;
7 import com.changhong.autoform.entity.SubForm;
8 import com.changhong.autoform.entity.page.PageResult;
9 import com.changhong.autoform.entity.sql.Where;
10 import com.changhong.autoform.entity.sql.Where.Conditions;
11 import com.changhong.autoform.entity.sql.Where.JoinType;
12 import com.changhong.autoform.entity.sql.Where.Type;
13 import com.changhong.autoform.entity.sql.delete.Delete;
14 import com.changhong.autoform.entity.sql.insert.Insert;
15 import com.changhong.autoform.entity.sql.select.Select;
16 import com.changhong.autoform.entity.sql.select.SelectPage;
17 import com.changhong.autoform.entity.sql.update.Update;
18 import com.changhong.autoform.mapper.data.DataMapper;
19 import com.iemsoft.framework.cloud.core.tools.JSONTool;
20 import org.springframework.stereotype.Component;
21 import org.springframework.ui.ModelMap;
22
23 import java.util.*;
24 /**
25  * 
26  * @author WangYT
27  *
28  */
29 @Component("simpleDataMapper")
30 public class DataMapperImpl extends BaseMapper implements DataMapper{
31
32     public static final String COUNT_KEY = "COUNT(0)";
33 //    public static final String TABLR_BILL_DATA = "epc_bill_data";
34 //    public static final String EXPEND_SUB = "journey";
35 //    public static final String[] TABLR_BILL_DATA_FIELD = {"bill_info"};
36     
37     @Override
38     public int insertData(FormData formData) {
39         System.err.println(JSONTool.toJson(formData));
40         Insert insert = new Insert(formData.getFormName()
41                 ,new ModelMap("tenantID", formData.getTenantID())
42                 .addAttribute("deleteFlg",0)
43                 .addAttribute("PARENTDATAROWNUM", 0)
44                 .addAttribute("CREATEUSER", formData.getUserName())
45                 .addAllAttributes(formData.getData()));
46         //添加主表
47         int count = execute(insert);
48         //获取最后插入的id
49         Integer lastInsertId = insert.getId();
50         //添加从表
51         List<SubForm> subFormList = formData.getSubFormList();
52         if(null != subFormList && !subFormList.isEmpty()){
53             for (SubForm subForm : subFormList) {
54                 List<Map<String,Object>> list = subForm.getData();
55                 System.out.println("记录错误位置---61");
56                 if(null !=list && !list.isEmpty()){
57                     for (Map<String, Object> map : list) {
58                         count+=execute(new Insert(subForm.getFormName()
59                                 ,new ModelMap("deleteFlg",0)
60                                 .addAttribute("tenantID", formData.getTenantID())
61                                 .addAttribute("PARENTDATAROWNUM", lastInsertId)
62                                 .addAllAttributes(map)));
63                         
64                     }
65                 }
66             }
67         }
68         if(count >0)
69             return lastInsertId;
70         else return -1;
71     }
72
73
74     //删除(修改deleteflg状态为1)
75     @Override
76     public int deleteDate(FormData formData) {
77         List<String> dataIds = formData.getDataIds();
78         int count = 0;
79         count = execute(new Update(formData.getFormName()
80                 , new ModelMap("deleteflg", "1")
81                 , new Where( new Conditions("tenantID", Type.EQ, formData.getTenantID())
82                         , new Conditions("dataRowNum", Type.IN, JoinType.AND, dataIds))));
83         List<SubForm> subFormList = formData.getSubFormList();
84         for (SubForm subForm : subFormList) {
85             count+=execute(new Update(subForm.getFormName()
86                     , new ModelMap("deleteflg", "1")
87                     , new Where(
88                             new Conditions("PARENTDATAROWNUM", Type.IN, dataIds))));
89         }
90         return count;
91     }
92
93     //修改
94     @Override
95     public int updateDate(FormData formData) {
96         System.out.println("修改传入表单数据:"+JSONTool.toJson(formData));
97         //更新主表
98         int count = execute( new Update(formData.getFormName()
99                 , formData.getData()
100                 , new Where(new Conditions("dataRowNum", Type.EQ, formData.getDataRowNum()))));
101         //更新从表  先删再更
102         List<SubForm> subFormList = formData.getSubFormList();
103         for (SubForm subForm : subFormList) {
104             execute(new Delete(subForm.getFormName()
105                     , new Where(
106                             new Conditions("PARENTDATAROWNUM",Type.EQ,formData.getDataRowNum()))));
107         }
108         for (SubForm subForm : subFormList) {
109             List<Map<String,Object>> list = subForm.getData();
110             for (Map<String, Object> map : list) {
111                 count++;
112                 addSubForm(map, formData,subForm.getFormName());
113             }
114         }
115         return count ;
116     }
117     
118     //子表单添加数据,用于重写
119     @Override
120     public int addSubForm(Map<String, Object> map,FormData formData,String formName){
121         Insert in = new Insert(formName
122                 ,new ModelMap("deleteFlg",0)
123                 .addAttribute("tenantID", formData.getTenantID())
124                 .addAttribute("PARENTDATAROWNUM",formData.getDataRowNum())
125                 .addAllAttributes(map));
126         execute(in);
127         return in.getId();
128     }
129
130     //查询所有数据
131     @Override
132     public List<Map<String, Object>> selectAll(SelectForm selectForm) {
133         Map<String, Object> fields = selectForm.getFields();
134         fields.put("DATAROWNUM", "DATAROWNUM");
135         String screenCondition = selectForm.getScreenCondition();
136         Select select = null;
137         if(screenCondition!=null && !"".equals(screenCondition)){
138             select = new SelectPage(String.format(selectForm.getFormName())
139                     , new Where(screenCondition,new Conditions("tenantID",Type.EQ,selectForm.getTenantID())
140                             , new Conditions("deleteFlg", Type.EQ, JoinType.AND, 0)
141                             )
142                     , fields.keySet().toArray(new String[fields.size()])
143                     , selectForm.getStartRow()
144                     , selectForm.getEndRow());
145         }else{
146             select = new SelectPage(String.format(selectForm.getFormName())
147                     , new Where(new Conditions("tenantID",Type.EQ,selectForm.getTenantID())
148                             , new Conditions("deleteFlg", Type.EQ, JoinType.AND, 0))
149                     , fields.keySet().toArray(new String[fields.size()])
150                     , selectForm.getStartRow()
151                     , selectForm.getEndRow());
152         }
153         select.setOrderName("DATAROWNUM");
154         return selectList(select, (index, key, rs, result)->{
155             if(key.equalsIgnoreCase("dataRowNum")){
156                 result.put(Objects.toString(fields.get(key)), rs.getInt(index));
157             }else{
158                 result.put(Objects.toString(fields.get(key)), rs.getString(index));
159             }
160         });
161     }
162     
163     //查询分页数据
164     @Override
165     public PageResult<Map<String, Object>> selectPage(SelectForm selectForm) {//fenye
166
167         Object count;
168         // 上面为注释掉的方法
169         Map<String, Object> fields = selectForm.getFields();
170         fields.put(selectForm.getFormName()+".DATAROWNUM", "DATAROWNUM");
171         String screenCondition = selectForm.getScreenCondition();
172         Select select;
173         Where oneWhere, listWhere;
174         if(screenCondition!=null && !"".equals(screenCondition)) {
175             oneWhere = new Where(screenCondition,new Conditions(selectForm.getFormName()+".tenantID",Type.EQ,selectForm.getTenantID())
176                     , new Conditions(selectForm.getFormName()+".deleteFlg", Type.EQ, JoinType.AND, 0));
177             listWhere = new Where(screenCondition,new Conditions(selectForm.getFormName()+".tenantID",Type.EQ,selectForm.getTenantID())
178                     , new Conditions(selectForm.getFormName()+".deleteFlg", Type.EQ, JoinType.AND, 0));
179         }else{
180             oneWhere = new Where(new Conditions(selectForm.getFormName()+".tenantID",Type.EQ,selectForm.getTenantID())
181                     , new Conditions(selectForm.getFormName()+".deleteFlg", Type.EQ, JoinType.AND, 0));
182             listWhere = new Where(new Conditions(selectForm.getFormName()+".tenantID",Type.EQ,selectForm.getTenantID())
183                     , new Conditions(selectForm.getFormName()+".deleteFlg", Type.EQ, JoinType.AND, 0));
184         }
185         //查询总条数
186         count = selectOne(new Select(selectForm.getFormName()
187                 , oneWhere
188                 , new String[]{COUNT_KEY})).get(COUNT_KEY);
189         if(count == null || Long.parseLong(count.toString()) <= 0)
190             return new PageResult<>(new ArrayList<>(), 0L);
191         //业务数据查询实体
192         select = new SelectPage(String.format(selectForm.getFormName())
193                 , listWhere
194                 , fields.keySet().toArray(new String[fields.size()])
195                 , selectForm.getStartRow()
196                 , selectForm.getEndRow());
197         select.setOrderName(selectForm.getFormName()+".DATAROWNUM");
198         return new PageResult<>(
199                 selectList(select, (index, key, rs, result)->{
200                     if(key.equalsIgnoreCase("dataRowNum")){
201                         result.put(Objects.toString(fields.get(key)), rs.getInt(index));
202                     }else{
203                         result.put(Objects.toString(fields.get(key)), rs.getString(index));
204                     }
205                 }), Long.parseLong(count.toString()));
206     }
207
208     //查询一条数据  
209     @Override
210     public Map<String, Object> selectOne(SelectForm selectForm) {
211         //主表数据
212         Map<String, Object> fields = selectForm.getFields();
213         fields.put("DATAROWNUM", "DATAROWNUM");
214         fields.put("orderCode", "orderCode");
215         fields.put("orderStatus", "orderStatus");
216         fields.put("voucherCode", "voucherCode");
217         Set<String> fieldKey = fields.keySet();
218         Select select =new Select(String.format(selectForm.getFormName())
219                 , new Where(new Conditions("deleteFlg",Type.EQ,0)
220                         , new Conditions("tenantID",Type.EQ,JoinType.AND,selectForm.getTenantID())
221                         , new Conditions("dataRowNum",Type.EQ,JoinType.AND,selectForm.getDataRowNum())
222                         , new Conditions("PARENTDATAROWNUM",Type.EQ,JoinType.AND,0))
223                 , fieldKey.toArray(new String[fieldKey.size()]));
224         Map<String, Object> formDataResult = selectOne(select,(index, key, rs, result)->{
225             if(key.equalsIgnoreCase("dataRowNum")){
226                 result.put(key, rs.getInt(index));
227             }else{
228                 String val = rs.getString(index);
229                 if(null == val){
230                     result.put(Objects.toString(fields.get(key)), "");   
231                 }else{
232                     result.put(Objects.toString(fields.get(key)), val);
233                 }
234             }
235         });
236         Map<Integer,List<Map<String,Object>>> subMap = new HashMap<>();
237         //从表数据
238         List<SubForm> subFormList = selectForm.getSubForm();
239         if(null!=subFormList&&!subFormList.isEmpty()){
240             for (SubForm subForm : subFormList) {
241                 Map<String, Object> subFields = subForm.getFields();
242                 subFields.put("DATAROWNUM", "DATAROWNUM");
243                 Set<String> subFieldkey = subFields.keySet();
244                 Select subSelect=new Select(subForm.getFormName()
245                         , new Where(new Conditions("deleteFlg",Type.EQ,0)
246                                 , new Conditions("tenantID",Type.EQ,JoinType.AND,selectForm.getTenantID())
247                                 , new Conditions("PARENTDATAROWNUM",Type.EQ,JoinType.AND,subForm.getSubDataRowNum()))
248                         , subFieldkey.toArray(new String[subFieldkey.size()]));
249                 subMap.put(subForm.getFormDataNum(), selectList(subSelect,(index, key, rs, result)->{
250                     if(key.equalsIgnoreCase("dataRowNum")){
251                         result.put(key, rs.getInt(index));
252                         // 注释详情带出票据信息
253 //                        String joury = subForm.getFormName().split("_")[subForm.getFormName().split("_").length -1];
254                         //if have joury and orderCode
255 //                        if(EXPEND_SUB.equals(joury) && formDataResult != null 
256 //                                && formDataResult.get("orderCode") != null && !"".equals(formDataResult.get("orderCode"))){
257 //                            Select billData = new Select(TABLR_BILL_DATA
258 //                                    , new Where(new Conditions("j_id",Type.EQ,rs.getInt(index))
259 //                                            , new Conditions("order_code",Type.EQ,JoinType.AND,formDataResult.get("orderCode")))
260 //                                    , TABLR_BILL_DATA_FIELD);
261 //                            result.put("billData", selectList(billData));
262 //                        }
263                     }else{
264                         String val = rs.getString(index);
265                         if(null == val){
266                             result.put(Objects.toString(subFields.get(key)), "");   
267                         }else{
268                             result.put(Objects.toString(subFields.get(key)), val);
269                         }
270                     }
271                 }));
272             }
273         }
274         return new ModelMap("main", formDataResult).addAttribute("subs", subMap);
275     }
276
277
278     @Override
279     public String getUserName(SelectForm selectForm) {
280         Select select =new Select(String.format(selectForm.getFormName())
281                         , new Where(new Conditions("deleteFlg",Type.EQ,0)
282                         , new Conditions("tenantID",Type.EQ,JoinType.AND,selectForm.getTenantID())
283                         , new Conditions("dataRowNum",Type.EQ,JoinType.AND,selectForm.getDataRowNum()))
284                         ,"CREATEUSER");
285         
286         Map<String, Object> formDataResult = selectOne(select,(index, key, rs, result)->{
287             System.err.println(key);
288             System.err.println();
289             String userName = rs.getString(index);
290             if(key.equals("CREATEUSER"))
291                 result.put("userName",userName);
292         });
293         return (String) formDataResult.get("userName");
294     }
295
296 }
297
298