UserController.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. package com.zjt.web;
  2. import com.alibaba.fastjson.JSON;
  3. import com.google.gson.JsonArray;
  4. import com.google.gson.JsonObject;
  5. import com.zjt.entity.*;
  6. import com.zjt.service.MaterialType;
  7. import com.zjt.service.TmenuService;
  8. import com.zjt.service.TroleService;
  9. import com.zjt.service.TuserService;
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.apache.shiro.SecurityUtils;
  12. import org.apache.shiro.authc.UsernamePasswordToken;
  13. import org.apache.shiro.subject.Subject;
  14. import org.springframework.stereotype.Controller;
  15. import org.springframework.ui.Model;
  16. import org.springframework.validation.BindingResult;
  17. import org.springframework.web.bind.annotation.*;
  18. import tk.mybatis.mapper.entity.Example;
  19. import javax.servlet.http.HttpSession;
  20. import javax.annotation.Resource;
  21. import javax.servlet.http.HttpSession;
  22. import javax.validation.Valid;
  23. import java.text.SimpleDateFormat;
  24. import java.util.Date;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.function.Consumer;
  29. import java.util.function.Predicate;
  30. import java.util.stream.Collectors;
  31. @Controller
  32. @RequestMapping("/user")
  33. public class UserController {
  34. private static final String ADMIN_ROLE = "admin";
  35. private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
  36. private static final String ID_PREFIX = "S";
  37. @Resource
  38. private MaterialType materialType;
  39. @Resource
  40. private TroleService troleService;
  41. @Resource
  42. private TuserService tuserService;
  43. @Resource
  44. private TmenuService tmenuService;
  45. @RequestMapping("list")
  46. public String select(String id, String storeCode, String createtime, String receive, Model model, HttpSession session) {
  47. Tuser users = (Tuser) session.getAttribute("currentUser");
  48. List<cgmaterial> collect1;
  49. String id1 = "";
  50. if (StringUtils.isNotEmpty(id)) {
  51. id1 = id.substring(1);
  52. }
  53. SimpleDateFormat sdf1 = new SimpleDateFormat(DATE_FORMAT);
  54. if (StringUtils.isNotEmpty(createtime)) {
  55. String[] split1 = createtime.split(" - ");
  56. if (StringUtils.isNotEmpty(storeCode)) {
  57. if (ADMIN_ROLE.equals(users.getTrueName())) {
  58. collect1 = materialType.selectDataBy(id1, storeCode, split1[0], split1[1], receive);
  59. } else {
  60. collect1 = materialType.selectDataBy(id1, users.getUserName(), split1[0], split1[1], receive);
  61. }
  62. } else {
  63. if (ADMIN_ROLE.equals(users.getTrueName())) {
  64. collect1 = materialType.selectDataBy(id1, "", split1[0], split1[1], receive);
  65. } else {
  66. collect1 = materialType.selectDataBy(id1, users.getUserName(), split1[0], split1[1], receive);
  67. }
  68. }
  69. } else {
  70. if (StringUtils.isNotEmpty(storeCode)) {
  71. if (ADMIN_ROLE.equals(users.getTrueName())) {
  72. collect1 = materialType.selectDataBy(id1, storeCode, "", "", receive);
  73. } else {
  74. collect1 = materialType.selectDataBy(id1, users.getUserName(), "", "", receive);
  75. }
  76. } else {
  77. if (ADMIN_ROLE.equals(users.getTrueName())) {
  78. collect1 = materialType.selectDataBy(id1, "", "", "", receive);
  79. } else {
  80. collect1 = materialType.selectDataBy(id1, users.getUserName(), "", "", receive);
  81. }
  82. }
  83. }
  84. model.addAttribute("listcg", collect1);
  85. Predicate<cgmaterial> predicate = a -> {
  86. if (!ADMIN_ROLE.equals(users.getTrueName())) {
  87. return a.getUsername().equals(users.getTrueName());
  88. }
  89. return true;
  90. };
  91. Predicate<cgmaterial> predicate2 = a -> {
  92. if (StringUtils.isNotEmpty(createtime)) {
  93. String[] split = createtime.split(" - ");
  94. SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
  95. String createTimeStr = sdf.format(a.getCreatetime());
  96. return createTimeStr.compareTo(split[0]) > 0 && createTimeStr.compareTo(split[1]) < 0;
  97. }
  98. return true;
  99. };
  100. Predicate<cgmaterial> predicate4 = a -> StringUtils.isEmpty(storeCode) || a.getStore().equals(storeCode);
  101. Predicate<cgmaterial> predicate5 = a -> StringUtils.isEmpty(id) || (ID_PREFIX + a.getId()).equals(id.replace(",", ""));
  102. // 合并所有过滤条件
  103. List<cgmaterial> collect = collect1.stream()
  104. .filter(predicate)
  105. .filter(predicate2)
  106. .filter(predicate4)
  107. .filter(predicate5)
  108. .collect(Collectors.toList());
  109. return "mypackage/selectcg";
  110. }
  111. @RequestMapping("input")
  112. public String input() {
  113. return "mypackage/input";
  114. }
  115. @RequestMapping("test")
  116. public String test() {
  117. return "mypackage/test";
  118. }
  119. @RequestMapping("insert")
  120. public String insert(@RequestParam("list") String userList, HttpSession session) {
  121. List<cgmaterial> cgmaterials = JSON.parseArray(userList, cgmaterial.class);
  122. Tuser user = (Tuser) session.getAttribute("currentUser");
  123. SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
  124. String s = user.getId() + "" + df.format(new Date());
  125. Consumer<cgmaterial> consumer = materialType::insertCg;
  126. cgmaterials.stream()
  127. .filter(a -> a.getMaterialcode() != null && a.getWz() != null && a.getLy() != null)
  128. .map(a -> {
  129. a.setId(Long.parseLong(s));
  130. a.setUsername(user.getTrueName());
  131. a.setCreatetime(new Date());
  132. a.setStore(user.getUserName());
  133. return a;
  134. })
  135. .forEach(consumer);
  136. return "mypackage/test";
  137. }
  138. @ResponseBody
  139. @PostMapping("/login")
  140. public Map<String, Object> login(String imageCode, @Valid Tuser user, BindingResult bindingResult, HttpSession session) {
  141. Map<String, Object> map = new HashMap<>();
  142. if (StringUtils.isEmpty(imageCode)) {
  143. map.put("success", false);
  144. map.put("errorInfo", "请输入验证码!");
  145. return map;
  146. }
  147. if (!session.getAttribute("checkcode").equals(imageCode)) {
  148. map.put("success", false);
  149. map.put("errorInfo", "验证码输入错误!");
  150. return map;
  151. }
  152. if (bindingResult.hasErrors()) {
  153. map.put("success", false);
  154. map.put("errorInfo", bindingResult.getFieldError().getDefaultMessage());
  155. return map;
  156. }
  157. Subject subject = SecurityUtils.getSubject();
  158. UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(), user.getPassword());
  159. try {
  160. subject.login(token);
  161. if (!subject.isAuthenticated()) {
  162. map.put("success", false);
  163. map.put("errorInfo", "认证失败!");
  164. return map;
  165. }
  166. String userName = (String) SecurityUtils.getSubject().getPrincipal();
  167. Example tuserExample = new Example(Tuser.class);
  168. tuserExample.or().andEqualTo("userName", userName);
  169. Tuser currentUser = tuserService.selectByExample(tuserExample).get(0);
  170. session.setAttribute("currentUser", currentUser);
  171. List<Trole> roleList = troleService.selectRolesByUserId(currentUser.getId());
  172. map.put("roleList", roleList);
  173. map.put("roleSize", roleList.size());
  174. map.put("success", true);
  175. return map;
  176. } catch (Exception e) {
  177. e.printStackTrace();
  178. map.put("success", false);
  179. map.put("errorInfo", "用户名或者密码错误!");
  180. return map;
  181. }
  182. }
  183. @ResponseBody
  184. @PostMapping("/saveRole")
  185. public Map<String, Object> saveRole(Integer roleId, HttpSession session) {
  186. Map<String, Object> map = new HashMap<>();
  187. Trole currentRole = troleService.selectByKey(roleId);
  188. session.setAttribute("currentRole", currentRole);
  189. putTmenuOneClassListIntoSession(session);
  190. map.put("success", true);
  191. return map;
  192. }
  193. @GetMapping("/logout")
  194. public String logout() {
  195. SecurityUtils.getSubject().logout();
  196. return "redirect:/tologin";
  197. }
  198. @ResponseBody
  199. @GetMapping("/loadMenuInfo")
  200. public String loadMenuInfo(HttpSession session, Integer parentId) {
  201. putTmenuOneClassListIntoSession(session);
  202. Trole currentRole = (Trole) session.getAttribute("currentRole");
  203. String json = getAllMenuByParentId(parentId, currentRole.getId()).toString();
  204. return json;
  205. }
  206. private JsonObject getAllMenuByParentId(Integer parentId, Integer roleId) {
  207. JsonObject resultObject = new JsonObject();
  208. JsonArray jsonArray = getMenuByParentId(parentId, roleId);
  209. for (int i = 0; i < jsonArray.size(); i++) {
  210. JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();
  211. Example example = new Example(Tmenu.class);
  212. example.or().andEqualTo("pId", jsonObject.get("id").getAsString());
  213. if (tmenuService.selectCountByExample(example) == 0) {
  214. continue;
  215. } else {
  216. resultObject.add(jsonObject.get("title").getAsString(), getAllMenuJsonArrayByParentId(jsonObject.get("id").getAsInt(), roleId));
  217. }
  218. }
  219. return resultObject;
  220. }
  221. private JsonArray getAllMenuJsonArrayByParentId(Integer parentId, Integer roleId) {
  222. JsonArray jsonArray = getMenuByParentId(parentId, roleId);
  223. for (int i = 0; i < jsonArray.size(); i++) {
  224. JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();
  225. Example example = new Example(Tmenu.class);
  226. example.or().andEqualTo("pId", jsonObject.get("id").getAsString());
  227. if (tmenuService.selectCountByExample(example) == 0) {
  228. continue;
  229. } else {
  230. jsonObject.add("children", getAllMenuJsonArrayByParentId(jsonObject.get("id").getAsInt(), roleId));
  231. }
  232. }
  233. return jsonArray;
  234. }
  235. private JsonArray getMenuByParentId(Integer parentId, Integer roleId) {
  236. HashMap<String, Object> paraMap = new HashMap<>();
  237. paraMap.put("pid", parentId);
  238. paraMap.put("roleid", roleId);
  239. List<Tmenu> menuList = tmenuService.selectByParentIdAndRoleId(paraMap);
  240. JsonArray jsonArray = new JsonArray();
  241. for (Tmenu menu : menuList) {
  242. JsonObject jsonObject = new JsonObject();
  243. jsonObject.addProperty("id", menu.getId());
  244. jsonObject.addProperty("title", menu.getName());
  245. jsonObject.addProperty("spread", false);
  246. jsonObject.addProperty("icon", menu.getIcon());
  247. if (StringUtils.isNotEmpty(menu.getUrl())) {
  248. jsonObject.addProperty("href", menu.getUrl());
  249. }
  250. jsonArray.add(jsonObject);
  251. }
  252. return jsonArray;
  253. }
  254. public void putTmenuOneClassListIntoSession(HttpSession session) {
  255. Example example = new Example(Tmenu.class);
  256. example.or().andEqualTo("pId", 1);
  257. List<Tmenu> tmenuOneClassList = tmenuService.selectByExample(example);
  258. session.setAttribute("tmenuOneClassList", tmenuOneClassList);
  259. }
  260. }