zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
    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<SubForm> subFormList = formData.getSubFormList();
        if(null != subFormList && !subFormList.isEmpty()){
            for (SubForm subForm : subFormList) {
                List<Map<String,Object>> list = subForm.getData();
                System.out.println("记录错误位置---61");
                if(null !=list && !list.isEmpty()){
                    for (Map<String, Object> 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<String> 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<SubForm> 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<SubForm> 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<Map<String,Object>> list = subForm.getData();
            for (Map<String, Object> map : list) {
                count++;
                addSubForm(map, formData,subForm.getFormName());
            }
        }
        return count ;
    }
    
    //子表单添加数据,用于重写
    @Override
    public int addSubForm(Map<String, Object> 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<Map<String, Object>> selectAll(SelectForm selectForm) {
        Map<String, Object> 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<Map<String, Object>> selectPage(SelectForm selectForm) {//fenye
 
        Object count;
        // 上面为注释掉的方法
        Map<String, Object> 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<String, Object> selectOne(SelectForm selectForm) {
        //主表数据
        Map<String, Object> fields = selectForm.getFields();
        fields.put("DATAROWNUM", "DATAROWNUM");
        fields.put("orderCode", "orderCode");
        fields.put("orderStatus", "orderStatus");
        fields.put("voucherCode", "voucherCode");
        Set<String> 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<String, Object> 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<Integer,List<Map<String,Object>>> subMap = new HashMap<>();
        //从表数据
        List<SubForm> subFormList = selectForm.getSubForm();
        if(null!=subFormList&&!subFormList.isEmpty()){
            for (SubForm subForm : subFormList) {
                Map<String, Object> subFields = subForm.getFields();
                subFields.put("DATAROWNUM", "DATAROWNUM");
                Set<String> 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<String, Object> 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");
    }
 
}