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
package com.changhong.epc.parsing.service.loop.impl;
 
import com.changhong.epc.bean.form.FormBaseEntity;
import com.changhong.epc.bean.parsing.ExcelLog;
import com.changhong.epc.constter.form.utf.FormUrlConst;
import com.changhong.epc.constter.system.SystemClients;
import com.changhong.epc.constter.system.run.EpcRestInterface;
import com.changhong.epc.constter.tenant.TenantUrlConst;
import com.changhong.epc.parsing.mapper.tenant.asset.FixedAssetMapper;
import com.changhong.epc.parsing.service.excel.ExcelService;
import com.changhong.epc.parsing.service.loop.ExcelOperationService;
import com.changhong.epc.parsing.service.util.impl.FromOperateImpl;
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.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
 
import javax.annotation.Resource;
import java.util.*;
 
/**
 * 表格导出服务
 */
@Component("excelOperationServiceImpl")
@Slf4j
public class ExcelOperationServiceImpl  extends LoopSuper implements SystemClients,ExcelOperationService {
 
    @Resource(name="excelServiceImpl")
    private ExcelService excelService;
 
    @Autowired
    private FromOperateImpl fromOperate;
 
    @Resource
    private FixedAssetMapper fixedAssetMapper;
 
    private static final String TAGATTRIBUTE = "tag_Attribute";
 
    private static final Integer PAGE_SIZE = 100;
 
 
    /**
     * 当前所有子表单别名
     */
    Map<String,String>  subs = new HashMap<>();
 
    @Override
    public void businessHandle() {
        operation(excelService.findAllExcel());
    }
 
    @Override
    public void operation(List<ExcelLog> exList) {
        if(ObjectUtil.empty(exList)){
            return;
        }
        for(ExcelLog ex: exList){
            try{
                startEmp(ex);
            }catch (Exception e){
                //导入错误,添加错误信息,修改状态码
                log.debug("导出错误:"+ex.getId());
                ex.setOverTime(new Date());
                ex.setState(3);
                excelService.updateExcelStart(ex);
                e.printStackTrace();
            }
        }
    }
 
 
 
    public void startEmp(ExcelLog excelLog){
        log.debug("开始导出");
        //获取表单字段,处理属性数据结构
        Object o = fromOperate.getFromRest(FormUrlConst.FORM_PROPERTY_URL,
                new ModelMap("formId",excelLog.getFormId())
                        .addAttribute("tenantId",excelLog.getTenantId()));
        FormBaseEntity formBaseEntity = JSONTool.toObj(JSONTool.toJson(o),FormBaseEntity.class);
        Map<String,FieldInfo> fields = formatFormData(JSONTool.toObj(formBaseEntity.getFieldset(),Map.class));
        //创建导出表格结构
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        //创建sheet页(工作表)
        HSSFSheet sheet = hssfWorkbook.createSheet("main"); //页名,页码
        //请求表单数据
        Integer count = fixedAssetMapper.selectFormCount(excelLog.getFormId());
        Integer max = countPage(count);//最大页数
        for(int i=1; i<=max;i++){
            //查询主表数据
            Integer pageNum = (i-1)*PAGE_SIZE;
            Integer pageSize = i*PAGE_SIZE;
            List<Map<String,Object>> resData = fixedAssetMapper.selectFormInfo(
                    getFormName(excelLog,null)
                    ,null,pageNum,pageSize);
            //第一次循环首先创建表格结构
            System.out.println("当前主表数据:"+ JSONTool.toJson(resData));
            if(i==1){
                //创建主表结构
 
            }
            //分页查询数据,处理填充到表格
        }
        //创建子表单数据
        this.subs.keySet().stream().forEach(oc->hssfWorkbook.createSheet(this.subs.get(oc)));
 
 
 
        //使用表单附件上传
 
 
 
 
 
 
    }
 
    //计算最大页码
    public Integer countPage(Integer max){
        Integer page;
        Integer pageSize =  max %PAGE_SIZE;
        if(pageSize>0){
            page = max /PAGE_SIZE+1;
        }else{
            page = max /PAGE_SIZE;
        }
        return page;
    }
 
    public String getFormName(ExcelLog excelLog,String subForm){
        if(subForm == null) {
            return "formdata_" + excelLog.getTenantId() + "_" + excelLog.getFormId();
        }else{
            return "formdata_" + excelLog.getTenantId() + "_" + excelLog.getFormId()+"_"+subForm;
        }
    }
 
 
    public Map<String,FieldInfo> formatFormData(Map<String,Object> formList){
        Map<String,FieldInfo> m = new HashMap<>();
        formList.keySet().stream().forEach(o->{
            FieldInfo fi = new FieldInfo();
            Map<String,Object> item = JSONTool.toObj(JSONTool.toJson(formList.get(o)),Map.class);
            fi.setTag_Type(Objects.toString(item.get("tag_Type")));
            fi.setColumnOrderNum(Integer.parseInt(Objects.toString(item.get("columnOrderNum"),"0")));
            fi.setParentsubFormNum(Integer.parseInt(Objects.toString(item.get("parentsubFormNum"),"0")));
            Map<String,Object> attr = JSONTool.toObj(JSONTool.toJson(item.get(TAGATTRIBUTE)),Map.class);
            fi.setAlias(Objects.toString(attr.get("alias")));
            fi.setTitle(Objects.toString(attr.get("title")));
            String tagType = Objects.toString(item.get("tag_Type"));
            //如果是下拉框或者树形
            if(Objects.equals(tagType,"dropdownlist") || Objects.equals(tagType,"tree")){
                fillMaster(attr,fi);
            }
            m.put(Objects.toString(attr.get("title")),fi);
            if(Objects.equals(tagType,"subform")){
                this.subs.put(Objects.toString(attr.get("title")),Objects.toString(attr.get("alias")));
            }
        });
        System.out.println("最终表单属性:"+ JSONTool.toJson(m));
        System.out.println("所有子表单:"+ JSONTool.toJson(this.subs));
        return m;
    }
 
    //填充主数据
    public void fillMaster(Map<String,Object> map,FieldInfo fi){
        String datasourcetype = Objects.toString(map.get("datasourcetype"),null);
        if(Objects.equals(datasourcetype,"masterdata")){
            //主数据填充
            Object m = map.get("masterData");
            if(m!=null) {
                Map<String, Object> master = JSONTool.toObj(JSONTool.toJson(m), Map.class);
                fi.setMasterOrcustom(getMasterData(Objects.toString(master.get("defindCode"),null)));
            }
        }else if(Objects.equals(datasourcetype,"custom")){
            Object m = map.get("custom");
            if(m!=null){
                List<Map> custom = JSONTool.toList(JSONTool.toJson(m),Map.class);
                Map<String,String> customData = new HashMap<>();
                custom.stream().forEach(o->
                        customData.put(Objects.toString(o.get("code")),Objects.toString(o.get("customData")))
                );
                fi.setMasterOrcustom(customData);
            }
        }
        //整理主数据参数,请求主数据,添加到fieldInfo中
    }
 
    public Map<String,String> getMasterData(String code){
        if(code==null || Objects.equals("",code)){
            return null;
        }
        Map<String,String> masterData = null;
        RestInterface restInterface = SpringUtil.getBean(SERVER_TENANT.getBeanName(), RestInterface.class);
        Object data = restInterface.post(TenantUrlConst.REST_TENANT_MASTERVALUE_SELECTVALUESEACHE,new ModelMap("defineCode",code),ResMsg.class,EpcRestInterface.getEpcHeads()).getData();
        if(data!=null){
            Map<String,Object> res = JSONTool.toObj(JSONTool.toJson(data),Map.class);
            if(res.get("values")!=null){
                List<Map> list = JSONTool.toList(JSONTool.toJson(res.get("values")),Map.class);
                if(list.size()>0) {
                    Map<String, String> finalMasterData = new HashMap<>();
                    list.stream().forEach(o-> finalMasterData.put(Objects.toString(o.get("code")),Objects.toString(o.get("value"))));
                    masterData = finalMasterData;
                }
            }
        }
        log.debug("当前主数据:"+code);
        log.debug("当前主数据值:"+ JSONTool.toJson(masterData));
        return masterData;
    }
 
 
    public static void main(String...a){
        Integer max = 13;
        if(max %PAGE_SIZE>0){
            Integer ad = max /PAGE_SIZE+1;
            System.out.println("1--"+ad);
        }else if(max % PAGE_SIZE==0){
            System.out.println("2--"+max /PAGE_SIZE);
        }else{
            System.out.println("3--"+1);
        }
 
        System.out.println(0-1);
    }
 
 
}
 
@Data
class FieldInfo{
 
    //控件类型
    private String tag_Type;
    //控件别名
    private String alias;
    //数据源类型
    private String datasourcetype = null;
    //控件顺序序号
    private Integer columnOrderNum;
    //是否是子表单0为子表单,不为0则对应子表单顺序号
    private Integer parentsubFormNum;
    //标题
    private String title;
    //子表单关联id
    private String num;
    //主数据
    private Map<String,String> masterOrcustom;
 
}