Helius
2021-06-16 5728be2af515b2200e782aa201ca5d4d67d9ea47
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
package com.ibeetl.admin.console.web;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.servlet.http.HttpServletResponse;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.sql.core.engine.PageQuery;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
 
import com.ibeetl.admin.console.service.DictConsoleService;
import com.ibeetl.admin.console.web.dto.DictExcelImportData;
import com.ibeetl.admin.console.web.query.CoreDictQuery;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.CoreDict;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.file.FileItem;
import com.ibeetl.admin.core.file.FileService;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.util.ConvertUtil;
import com.ibeetl.admin.core.util.PlatformException;
import com.ibeetl.admin.core.util.ValidateConfig;
import com.ibeetl.admin.core.web.JsonResult;
 
/**
 * CoreDict 接口
 */
@Controller
public class DictConsoleController{
 
    private final Log log = LogFactory.getLog(this.getClass());
    private static final String MODEL = "/admin/dict";
 
 
    @Autowired private DictConsoleService dictService;
    @Autowired
    FileService fileService;
    
    @Autowired
    CorePlatformService platformService = null;
    /* 页面 */
 
    @GetMapping(MODEL + "/index.do")
    @Function("dict.query")
    public ModelAndView index() {
        ModelAndView view = new ModelAndView("/admin/dict/index.html") ;
        view.addObject("search", CoreDictQuery.class.getName());
        return view;
    }
 
    @GetMapping(MODEL + "/edit.do")
    @Function("dict.edit")
    public ModelAndView edit(Long id) {
        ModelAndView view = new ModelAndView("/admin/dict/edit.html");
        CoreDict dict = dictService.queryById(id);
        view.addObject("dict", dict);
        return view;
    }
 
    @GetMapping(MODEL + "/add.do")
    @Function("dict.add")
    public ModelAndView add() {
        ModelAndView view = new ModelAndView("/admin/dict/add.html");
        return view;
    }
 
    /* ajax json */
 
    @PostMapping(MODEL + "/list.json")
    @Function("dict.query")
    @ResponseBody
    public JsonResult<PageQuery> list(CoreDictQuery condtion)
    {
        PageQuery page = condtion.getPageQuery();
        dictService.queryByCondition(page);
        return JsonResult.success(page);
    }
 
    @PostMapping(MODEL + "/add.json")
    @Function("dict.add")
    @ResponseBody
    public JsonResult add(@Validated(ValidateConfig.ADD.class)CoreDict dict)
    {
        dict.setCreateTime(new Date());
        dictService.save(dict);
        platformService.clearDictCache();
        return new JsonResult().success();
    }
 
    @PostMapping(MODEL + "/update.json")
    @Function("dict.update")
    @ResponseBody
    public JsonResult<String> update(@Validated(ValidateConfig.UPDATE.class)  CoreDict dict) {
        boolean success = dictService.update(dict);
        if (success) {
            platformService.clearDictCache();
            return new JsonResult().success();
        } else {
            return JsonResult.failMessage("保存失败");
        }
    }
 
 
   
    @GetMapping(MODEL + "/view.json")
    @Function("dict.query")
    @ResponseBody
    public JsonResult<CoreDict>queryInfo(Long id) {
        CoreDict dict = dictService.queryById(id);
        return  JsonResult.success(dict);
    }
    
 
    @PostMapping(MODEL + "/delete.json")
    @Function("dict.delete")
    @ResponseBody
    public JsonResult delete(String ids) {
        List<Long> dels = ConvertUtil.str2longs(ids);
        dictService.batchDelCoreDict(dels);
        platformService.clearDictCache();
        return new JsonResult().success();
    }
    
    @PostMapping(MODEL + "/excel/export.json")
    @Function("dict.export")
    @ResponseBody
    public JsonResult<String> export(HttpServletResponse response,CoreDictQuery condtion) {
        String excelTemplate ="excelTemplates/admin/dict/dict_collection_template.xls";
        PageQuery<CoreUser> page = condtion.getPageQuery();
        //取出全部符合条件的
        page.setPageSize(Integer.MAX_VALUE);
        page.setPageNumber(1);
        page.setTotalRow(Integer.MAX_VALUE);
        List<CoreDict> dicts =dictService.queryExcel(page);
        try(InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(excelTemplate)) {
            if(is==null) {
                throw new PlatformException("模板资源不存在:"+excelTemplate);
            }
            FileItem item = fileService.createFileTemp("dict_collection.xls");
            OutputStream os = item.openOutpuStream();
            Context context = new Context();
            context.putVar("dicts", dicts);
            JxlsHelper.getInstance().processTemplate(is, os, context);
            os.close();
            //下载参考FileSystemContorller
            return  JsonResult.success(item.getPath());
        } catch (IOException e) {
            throw new PlatformException(e.getMessage());
        }
        
    }
    
    /*@PostMapping(MODEL + "/excel/import.do")
    @Function("dict.import")
    @ResponseBody
    public JsonResult importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
           return JsonResult.fail();
        }
        InputStream ins = file.getInputStream();
        
        InputStream inputXML = Thread.currentThread().getContextClassLoader().getResourceAsStream("excelTemplates/admin/dict/dict_mapping.xml");  
        XLSReader mainReader = ReaderBuilder.buildFromXML( inputXML );  
        InputStream inputXLS = ins;  
        List<DictExcelImportData> dicts = new ArrayList<DictExcelImportData>();  
        Map beans = new HashMap();  
        beans.put("list", dicts);
        ReaderConfig.getInstance().setSkipErrors( true );
        XLSReadStatus readStatus = mainReader.read( inputXLS, beans); 
        List<XLSReadMessage>  errors = readStatus.getReadMessages();
        if(!errors.isEmpty()) {
            StringBuilder sb = new StringBuilder();
            for(XLSReadMessage msg:errors) {
                sb.append(parseXLSReadMessage(msg));
                sb.append(",");
            }
            sb.setLength(sb.length()-1);
            return JsonResult.failMessage("解析excel出错:"+sb.toString());
        }
//        this.dictService.batchInsert(dicts);//layui对话框不能正确处理http 500错误,改成下面方式
//        return JsonResult.success();
        try {
            this.dictService.batchInsert(dicts);
            return JsonResult.success();
        }catch(Exception ex) {
            return JsonResult.failMessage(ex.getMessage());
        }
        
    }
    
    private String parseXLSReadMessage(XLSReadMessage msg) {
//        String message = "Can't read cell " + getCellName(mapping, rowShift) + " on " + cursor.getSheetName() + " spreadsheet";
        String str = msg.getMessage();
        int start = "Can't read cell ".length();
        int end = str.indexOf("on");
        return str.substring(start,end);
    }*/
 
}