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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package com.ibeetl.admin.console.web;
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
 
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.*;
import org.springframework.web.servlet.ModelAndView;
 
import com.ibeetl.admin.console.service.OrgConsoleService;
import com.ibeetl.admin.console.service.RoleConsoleService;
import com.ibeetl.admin.console.service.UserConsoleService;
import com.ibeetl.admin.console.web.dto.UserExcelExportData;
import com.ibeetl.admin.console.web.query.UserQuery;
import com.ibeetl.admin.console.web.query.UserRoleQuery;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.annotation.Query;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.entity.CoreUserRole;
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.AnnotationUtil;
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.util.enums.GeneralStateEnum;
import com.ibeetl.admin.core.web.JsonResult;
 
/**
 * 用户管理接口
 * 
 * @author xiandafu
 */
@Controller
public class UserConsoleController {
    private final Log log = LogFactory.getLog(this.getClass());
    private static final String MODEL = "/admin/user";
 
    @Autowired
    UserConsoleService userConsoleService;
 
    @Autowired
    CorePlatformService platformService;
 
    @Autowired
    RoleConsoleService roleConsoleService;
    @Autowired
    OrgConsoleService orgConsoleService;
    @Autowired
    FileService fileService;
 
 
 
    /* 页面 */
 
    @GetMapping(MODEL + "/index.do")
    @Function("user")
    public ModelAndView index() {
        ModelAndView view = new ModelAndView("/admin/user/index.html");
        view.addObject("search", UserQuery.class.getName());
        return view;
    }
 
    @GetMapping(MODEL + "/edit.do")
    @Function("user.edit")
    public ModelAndView edit(String id) {
        ModelAndView view = new ModelAndView("/admin/user/edit.html");
        CoreUser user = userConsoleService.queryById(id);
        view.addObject("user", user);
        return view;
    }
 
    @GetMapping(MODEL + "/add.do")
    @Function("user.add")
    public ModelAndView add() {
        ModelAndView view = new ModelAndView("/admin/user/add.html");
        return view;
    }
 
    @GetMapping(MODEL + "/changePassword.do")
    @Function("user.add")
    public ModelAndView changePassword(Long id) {
        CoreUser user = userConsoleService.queryById(id);
        ModelAndView view = new ModelAndView("/admin/user/changePassword.html");
        view.addObject("user", user);
        return view;
    }
 
    @GetMapping(MODEL + "/role/list.do")
    @Function("user.role")
    public ModelAndView userRoleIndex(Long id) {
        CoreUser user = userConsoleService.queryById(id);
        ModelAndView view = new ModelAndView("/admin/user/userRole.html");
        view.addObject("search", UserRoleQuery.class.getName());
        view.addObject("user", user);
        return view;
    }
 
    @GetMapping(MODEL + "/role/add.do")
    @Function("user.role")
    public ModelAndView userRoleAdd(Long id) {
        CoreUser user = userConsoleService.queryById(id);
        ModelAndView view = new ModelAndView("/admin/user/userRoleAdd.html");
        view.addObject("user", user);
        return view;
    }
 
    /* Json */
 
    @PostMapping(MODEL + "/delete.json")
    @Function("user.delete")
    @ResponseBody
    public JsonResult delete(String ids) {
        List<Long> dels = ConvertUtil.str2longs(ids);
        userConsoleService.batchDelSysUser(dels);
        return JsonResult.success();
    }
 
    @PostMapping(MODEL + "/update.json")
    @Function("user.update")
    @ResponseBody
    public JsonResult update(@Validated(ValidateConfig.UPDATE.class)  CoreUser user) {
        boolean success = userConsoleService.updateTemplate(user);
        if (success) {
            this.platformService.clearFunctionCache();
            return JsonResult.success();
        } else {
            return JsonResult.failMessage("保存失败!");
        }
    }
 
    @PostMapping(MODEL + "/add.json")
    @Function("user.add")
    @ResponseBody
    public JsonResult<Long> add(@Validated(ValidateConfig.ADD.class) CoreUser user) {
        if (!platformService.isAllowUserName(user.getCode())) {
            return JsonResult.failMessage("不允许的注册名字 " + user.getCode());
        }
        user.setCreateTime(new Date());
        //userConsoleService.saveUser(user);
        return JsonResult.success(user.getId());
    }
 
    @PostMapping(MODEL + "/view.json")
    @ResponseBody
    @Function("user.query")
    public JsonResult<CoreUser> view(Long id) {
        CoreUser user = userConsoleService.queryById(id);
        return JsonResult.success(user);
    }
 
    /*@PostMapping(MODEL + "/list.json")
    @Function("user.query")
    @ResponseBody
    public JsonResult<PageQuery<CoreUser>> index(UserQuery condtion) {
 
        PageQuery<CoreUser> page = condtion.getPageQuery();
        userConsoleService.queryByCondtion(page);
        return JsonResult.success(page);
    }*/
 
    @PostMapping(MODEL + "/list/condition.json")
    @Function("user.query")
    @ResponseBody
    public JsonResult<List<Map<String, Object>>> indexCondtion() {
        List<Map<String, Object>> list = AnnotationUtil.getInstance().getAnnotations(Query.class, UserQuery.class);
        return JsonResult.success(list);
    }
 
    @PostMapping(MODEL + "/disable.json")
    @Function("user.disable")
    @ResponseBody
    public JsonResult disableUser(String ids) {
 
        List<Long> dels = ConvertUtil.str2longs(ids);
 
        userConsoleService.batchUpdateUserState(dels, GeneralStateEnum.DISABLE);
        for (Long id : dels) {
            CoreUser user = userConsoleService.queryById(id);
            this.platformService.restUserSession(user.getCode());
        }
        return JsonResult.success();
 
    }
 
    /**
     * 启用用户操作
     * 
     * @return
     */
    @PostMapping(MODEL + "/enable.json")
    @Function("user.enable")
    @ResponseBody
    public JsonResult enableUser(String ids) {
 
        List<Long> enables = ConvertUtil.str2longs(ids);
        userConsoleService.batchUpdateUserState(enables, GeneralStateEnum.ENABLE);
        return JsonResult.success();
 
    }
 
    /**
     * 管理员重置用户密码
     * 
     * @return
     */
    @PostMapping(MODEL + "/changePassword.json")
    @Function("user.reset")
    @ResponseBody
    public JsonResult changePassword(Long id, String password) {
 
        userConsoleService.resetPassword(id, password);
        return new JsonResult().success();
    }
 
    /**
     * 用户所有授权角色列表
     * 
     * @param id
     *            用户id
     * @return
     */
    @PostMapping(MODEL + "/role/list.json")
    @Function("user.role")
    @ResponseBody
    public JsonResult<List<CoreUserRole>> getRoleList(UserRoleQuery roleQuery) {
        List<CoreUserRole> list = userConsoleService.getUserRoles(roleQuery);
        return JsonResult.success(list);
    }
 
    /**
     * 用户添加授权角色页
     * 
     * @return
     */
    @PostMapping(MODEL + "/role/add.json")
    @Function("user.role")
    @ResponseBody
    public JsonResult saveUserRole(@Validated CoreUserRole userRole) {
        userRole.setCreateTime(new Date());
        this.userConsoleService.saveUserRole(userRole);
        this.platformService.clearFunctionCache();
        return JsonResult.success(userRole.getId());
 
    }
 
    /**
     * 删除用户角色授权
     * 
     * @return
     */
    @PostMapping(MODEL + "/role/delete.json")
    @Function("user.role")
    @ResponseBody
    public JsonResult delUserRole(String ids) {
        List<Long> dels = ConvertUtil.str2longs(ids);
 
        userConsoleService.deleteUserRoles(dels);
        this.platformService.clearFunctionCache();
        return JsonResult.success();
    }
    
    
    //@PostMapping(MODEL + "/excel/export.json")
    //@Function("user.export")
    //@ResponseBody
    /*public JsonResult<String> export(HttpServletResponse response,UserQuery condtion) {
        String excelTemplate ="excelTemplates/admin/user/user_collection_template.xls";
        PageQuery<CoreUser> page = condtion.getPageQuery();
        //取出全部符合条件的
        page.setPageSize(Integer.MAX_VALUE);
        page.setPageNumber(1);
        page.setTotalRow(Integer.MAX_VALUE);
 
        List<UserExcelExportData> users =userConsoleService.queryExcel(page);
        try(InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(excelTemplate)) {
            if(is==null) {
                throw new PlatformException("模板资源不存在:"+excelTemplate);
            }
            FileItem item = fileService.createFileTemp("user_collection.xls");
            OutputStream os = item.openOutpuStream();
            Context context = new Context();
            context.putVar("users", users);
            JxlsHelper.getInstance().processTemplate(is, os, context);
            //下载参考FileSystemContorller
            return  JsonResult.success(item.getPath());
        } catch (IOException e) {
            throw new PlatformException(e.getMessage());
        }
        
    }*/
    
    
    
    
    
 
}