AdminSuperviserApiController.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package com.goafanti.admin.controller;
  2. import java.util.ArrayList;
  3. import java.util.Calendar;
  4. import java.util.List;
  5. import java.util.UUID;
  6. import javax.annotation.Resource;
  7. import javax.servlet.http.HttpServletRequest;
  8. import org.springframework.beans.BeanUtils;
  9. import org.springframework.stereotype.Controller;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestMethod;
  12. import org.springframework.web.bind.annotation.RequestParam;
  13. import com.alibaba.fastjson.JSON;
  14. import com.alibaba.fastjson.JSONArray;
  15. import com.alibaba.fastjson.JSONObject;
  16. import com.goafanti.admin.bo.AdminListBo;
  17. import com.goafanti.admin.bo.AdminLocationBo;
  18. import com.goafanti.admin.service.AdminLocationService;
  19. import com.goafanti.admin.service.NewAdminService;
  20. import com.goafanti.common.bo.Result;
  21. import com.goafanti.common.constant.AFTConstants;
  22. import com.goafanti.common.constant.ErrorConstants;
  23. import com.goafanti.common.controller.CertifyApiController;
  24. import com.goafanti.common.model.Admin;
  25. import com.goafanti.common.model.AdminLocation;
  26. import com.goafanti.common.utils.PasswordUtil;
  27. import com.goafanti.common.utils.StringUtils;
  28. import com.goafanti.core.shiro.token.TokenManager;
  29. import com.goafanti.user.service.UserService;
  30. @Controller
  31. @RequestMapping(value = "api/admin/superviser")
  32. public class AdminSuperviserApiController extends CertifyApiController {
  33. @Resource
  34. private NewAdminService newAdminService;
  35. @Resource(name = "passwordUtil")
  36. private PasswordUtil passwordUtil;
  37. @Resource
  38. private AdminLocationService adminLocationService;
  39. @Resource
  40. private UserService userService;
  41. /**管理员列表*/
  42. @RequestMapping(value = "/adminList", method = RequestMethod.GET)
  43. public Result adminList(AdminListBo alb, String rid, String pageNo,
  44. String pageSize) {
  45. Result res = new Result();
  46. Integer pNo = 1;
  47. Integer pSize = 10;
  48. if (StringUtils.isNumeric(pageSize)) {
  49. pSize = Integer.parseInt(pageSize);
  50. }
  51. if (StringUtils.isNumeric(pageNo)) {
  52. pNo = Integer.parseInt(pageNo);
  53. }
  54. res.setData(newAdminService.listAdmin(alb,rid, pNo, pSize));
  55. return res;
  56. }
  57. /**
  58. * 新增管理员
  59. */
  60. @RequestMapping(value = "/insertAdmin", method = RequestMethod.POST)
  61. public Result insertAdmin(@RequestParam(value = "roles[]", required = false) String[] roleIds, String data) {
  62. Result res = new Result();
  63. JSONObject jo = (JSONObject) JSON.parse(data);
  64. if (roleIds==null||roleIds.length<=0) {
  65. res.getError().add(buildError("","角色不能为空"));
  66. return res;
  67. }
  68. if (null != jo) {
  69. Admin admin = jo.toJavaObject(Admin.class);
  70. if (StringUtils.isBlank(admin.getMobile())) {
  71. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "登录帐号为空", "登录帐号"));
  72. return res;
  73. }
  74. if (StringUtils.isBlank(admin.getName())) {
  75. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "用户名为空", "用户名"));
  76. return res;
  77. }
  78. Admin a = newAdminService.selectByMobile(admin.getMobile().trim());
  79. if (null != a) {
  80. res.getError().add(buildError(ErrorConstants.USER_ALREADY_EXIST, "当前用户已注册!"));
  81. return res;
  82. }
  83. Admin ad = new Admin();
  84. BeanUtils.copyProperties(admin, ad);
  85. /*ad.setId(UUID.randomUUID().toString());*/
  86. Calendar now = Calendar.getInstance();
  87. now.set(Calendar.MILLISECOND, 0);
  88. ad.setCreateTime(now.getTime());
  89. if (StringUtils.isBlank(ad.getPassword())) {
  90. ad.setPassword(AFTConstants.INITIALPASSWORD);
  91. }
  92. ad.setPassword(passwordUtil.getEncryptPwd(ad));
  93. List<String> roles = new ArrayList<String>();
  94. if (roleIds != null && roleIds.length > 0) {
  95. for (String role : roleIds) {
  96. roles.add(role);
  97. }
  98. }
  99. res.setData(newAdminService.insertNewAdmin(ad,roles));
  100. }
  101. return res;
  102. }
  103. /**
  104. * 修改管理员信息
  105. */
  106. @RequestMapping(value = "/updateAdmin", method = RequestMethod.POST)
  107. public Result updateAdmin(@RequestParam(value = "roles[]", required = false) String[] roleIds, String data) {
  108. Result res = new Result();
  109. JSONObject jo = (JSONObject) JSON.parse(data);
  110. if (null != jo) {
  111. Admin admin = jo.toJavaObject(Admin.class);
  112. if (null == admin.getId()) {
  113. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "用户id"));
  114. return res;
  115. }
  116. if (null == admin.getMobile()) {
  117. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "登录帐号为空", "登录帐号"));
  118. return res;
  119. }
  120. if (null == admin.getName()) {
  121. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "用户名为空", "用户名"));
  122. return res;
  123. }
  124. Admin aa = newAdminService.selectByMobile(admin.getMobile().trim());
  125. if (null != aa && !admin.getId().equals(aa.getId())) {
  126. res.getError().add(buildError(ErrorConstants.USER_ALREADY_EXIST, "当前用户已注册!"));
  127. return res;
  128. }
  129. Admin a = newAdminService.selectByPrimaryKey(admin.getId());
  130. if (null == a) {
  131. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "用户id"));
  132. return res;
  133. }
  134. Admin ad = new Admin();
  135. BeanUtils.copyProperties(admin, ad);
  136. res = disposeAdminLocationList(res, jo, ad);
  137. if (!res.getError().isEmpty()) {
  138. return res;
  139. }
  140. List<String> roles = new ArrayList<String>();
  141. if (roleIds != null && roleIds.length > 0) {
  142. for (String role : roleIds) {
  143. roles.add(role);
  144. }
  145. }
  146. res.setData(newAdminService.updateByPrimaryKeySelective(ad, roles));
  147. }
  148. return res;
  149. }
  150. /**
  151. * 重置管理员密码
  152. */
  153. @RequestMapping(value = "/resetPwd", method = RequestMethod.POST)
  154. public Result resetPwd(String id) {
  155. Result res = new Result();
  156. if (StringUtils.isBlank(id)) {
  157. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "用户id"));
  158. return res;
  159. }
  160. Admin admin = newAdminService.selectByPrimaryKey(id);
  161. if (null == admin) {
  162. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "用户id"));
  163. return res;
  164. }
  165. Admin a = new Admin();
  166. a.setId(admin.getId());
  167. a.setCreateTime(admin.getCreateTime());
  168. a.setPassword(AFTConstants.INITIALPASSWORD);
  169. a.setPassword(passwordUtil.getEncryptPwd(a));
  170. res.setData(newAdminService.updateByPrimaryKey(a));
  171. return res;
  172. }
  173. private Result disposeAdminLocationList(Result res, JSONObject jo, Admin ad) {
  174. JSONArray locations = jo.getJSONArray("locations");
  175. if (null == locations || locations.size()<1){
  176. return res;
  177. }
  178. List<AdminLocation> adminLocationList = new ArrayList<>();
  179. AdminLocation al = null;
  180. Boolean areaAdminFlag = false;
  181. List<Integer> locationList = null;
  182. if (TokenManager.hasRole(AFTConstants.AREAADMIN)) {
  183. locationList = new ArrayList<>();
  184. areaAdminFlag = true;
  185. }
  186. for (int i = 0; i < locations.size(); i++) {
  187. AdminLocationBo alb = locations.getJSONObject(i).toJavaObject(AdminLocationBo.class);
  188. Integer province = alb.getProvince();
  189. String city = alb.getCity();
  190. if (null == province) {
  191. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "省份为空", "所在省份"));
  192. return res;
  193. }
  194. if (province.intValue() > AFTConstants.PROVINCEMAXNUM) {
  195. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "所在省份"));
  196. return res;
  197. }
  198. if (areaAdminFlag) {
  199. locationList.add(province);
  200. }
  201. if (!StringUtils.isBlank(city)) {
  202. String[] arr = city.trim().split(",|,");
  203. if (null != arr && arr.length > 0) {
  204. for (String s : arr) {
  205. Integer c = Integer.valueOf(s);
  206. if (c.intValue() > AFTConstants.CITYMAXNUM) {
  207. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "所在市"));
  208. return res;
  209. }
  210. adminLocationList.add(disposeAdminLocation(al, ad, province, c));
  211. if (areaAdminFlag) {
  212. locationList.add(c);
  213. }
  214. }
  215. }
  216. } else {
  217. adminLocationList.add(disposeAdminLocation(al, ad, province, null));
  218. }
  219. }
  220. if (areaAdminFlag) {
  221. List<Integer> albList = disposeAdminLocationBo();
  222. if (!albList.containsAll(locationList)) {
  223. res.getError().add(buildError("", "所选所在地区域不在负责区域,无法添加或修改"));
  224. return res;
  225. }
  226. }
  227. res.setData(adminLocationList);
  228. return res;
  229. }
  230. private AdminLocation disposeAdminLocation(AdminLocation al, Admin ad, Integer province, Integer c) {
  231. al = new AdminLocation();
  232. al.setId(UUID.randomUUID().toString());
  233. al.setAdminId(ad.getId());
  234. al.setProvince(province);
  235. if (null != c) {
  236. al.setCity(c);
  237. }
  238. return al;
  239. }
  240. private List<Integer> disposeAdminLocationBo() {
  241. List<AdminLocationBo> alb = adminLocationService.findLocation(TokenManager.getAdminId());
  242. List<Integer> albList = null;
  243. if (!alb.isEmpty()) {
  244. Integer albProvince = null;
  245. albList = new ArrayList<>();
  246. for (AdminLocationBo bo : alb) {
  247. albProvince = bo.getProvince();
  248. albList.add(albProvince);
  249. if (!StringUtils.isBlank(bo.getCity())) {
  250. String[] albArr = bo.getCity().trim().split(",|,");
  251. if (null != albArr && albArr.length > 0) {
  252. for (String ss : albArr) {
  253. albList.add(Integer.valueOf(ss));
  254. }
  255. }
  256. }
  257. }
  258. }
  259. return albList;
  260. }
  261. /** 图片上传 **/
  262. @RequestMapping(value = "/uploadAdminImg", method = RequestMethod.POST)
  263. public Result uploadCustomerImg(HttpServletRequest req,String sign){
  264. Result res = new Result();
  265. res.setData(handleFile(res, "/customer_sys_file/", false, req, sign));
  266. return res;
  267. }
  268. /**新增获取ID**/
  269. @RequestMapping(value = "/newId", method = RequestMethod.POST)
  270. public Result newId(){
  271. Result res = new Result();
  272. res.setData(UUID.randomUUID().toString());
  273. return res;
  274. }
  275. /**编辑页面信息获取**/
  276. @RequestMapping(value = "/selectAllByid", method = RequestMethod.POST)
  277. public Result selectAllByid(String id){
  278. Result res = new Result();
  279. res.setData(newAdminService.selectAllByid(id));
  280. return res;
  281. }
  282. /**删除用户**/
  283. @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
  284. public Result deleteById(String id){
  285. Result res = new Result();
  286. newAdminService.deleteById(id);
  287. return res;
  288. }
  289. }