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;
|
|
}
|