AdminSuperviserApiController.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 (null != jo) {
  65. Admin admin = jo.toJavaObject(Admin.class);
  66. if (StringUtils.isBlank(admin.getMobile())) {
  67. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "登录帐号为空", "登录帐号"));
  68. return res;
  69. }
  70. if (StringUtils.isBlank(admin.getName())) {
  71. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "用户名为空", "用户名"));
  72. return res;
  73. }
  74. Admin a = newAdminService.selectByMobile(admin.getMobile().trim());
  75. if (null != a) {
  76. res.getError().add(buildError(ErrorConstants.USER_ALREADY_EXIST, "当前用户已注册!"));
  77. return res;
  78. }
  79. Admin ad = new Admin();
  80. BeanUtils.copyProperties(admin, ad);
  81. /*ad.setId(UUID.randomUUID().toString());*/
  82. Calendar now = Calendar.getInstance();
  83. now.set(Calendar.MILLISECOND, 0);
  84. ad.setCreateTime(now.getTime());
  85. if (StringUtils.isBlank(ad.getPassword())) {
  86. ad.setPassword(AFTConstants.INITIALPASSWORD);
  87. }
  88. ad.setPassword(passwordUtil.getEncryptPwd(ad));
  89. List<String> roles = new ArrayList<String>();
  90. if (roleIds != null && roleIds.length > 0) {
  91. for (String role : roleIds) {
  92. roles.add(role);
  93. }
  94. }
  95. res.setData(newAdminService.insertNewAdmin(ad,roles));
  96. }
  97. return res;
  98. }
  99. /**
  100. * 修改管理员信息
  101. */
  102. @RequestMapping(value = "/updateAdmin", method = RequestMethod.POST)
  103. public Result updateAdmin(@RequestParam(value = "roles[]", required = false) String[] roleIds, String data) {
  104. Result res = new Result();
  105. JSONObject jo = (JSONObject) JSON.parse(data);
  106. if (null != jo) {
  107. Admin admin = jo.toJavaObject(Admin.class);
  108. if (null == admin.getId()) {
  109. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "用户id"));
  110. return res;
  111. }
  112. if (null == admin.getMobile()) {
  113. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "登录帐号为空", "登录帐号"));
  114. return res;
  115. }
  116. if (null == admin.getName()) {
  117. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "用户名为空", "用户名"));
  118. return res;
  119. }
  120. Admin aa = newAdminService.selectByMobile(admin.getMobile().trim());
  121. if (null != aa && !admin.getId().equals(aa.getId())) {
  122. res.getError().add(buildError(ErrorConstants.USER_ALREADY_EXIST, "当前用户已注册!"));
  123. return res;
  124. }
  125. Admin a = newAdminService.selectByPrimaryKey(admin.getId());
  126. if (null == a) {
  127. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "用户id"));
  128. return res;
  129. }
  130. Admin ad = new Admin();
  131. BeanUtils.copyProperties(admin, ad);
  132. res = disposeAdminLocationList(res, jo, ad);
  133. if (!res.getError().isEmpty()) {
  134. return res;
  135. }
  136. List<String> roles = new ArrayList<String>();
  137. if (roleIds != null && roleIds.length > 0) {
  138. for (String role : roleIds) {
  139. roles.add(role);
  140. }
  141. }
  142. res.setData(newAdminService.updateByPrimaryKeySelective(ad, roles));
  143. }
  144. return res;
  145. }
  146. /**
  147. * 重置管理员密码
  148. */
  149. @RequestMapping(value = "/resetPwd", method = RequestMethod.POST)
  150. public Result resetPwd(String id) {
  151. Result res = new Result();
  152. if (StringUtils.isBlank(id)) {
  153. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "用户id"));
  154. return res;
  155. }
  156. Admin admin = newAdminService.selectByPrimaryKey(id);
  157. if (null == admin) {
  158. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "用户id"));
  159. return res;
  160. }
  161. Admin a = new Admin();
  162. a.setId(admin.getId());
  163. a.setCreateTime(admin.getCreateTime());
  164. a.setPassword(AFTConstants.INITIALPASSWORD);
  165. a.setPassword(passwordUtil.getEncryptPwd(a));
  166. res.setData(newAdminService.updateByPrimaryKey(a));
  167. return res;
  168. }
  169. private Result disposeAdminLocationList(Result res, JSONObject jo, Admin ad) {
  170. JSONArray locations = jo.getJSONArray("locations");
  171. if (null == locations || locations.size()<1){
  172. return res;
  173. }
  174. List<AdminLocation> adminLocationList = new ArrayList<>();
  175. AdminLocation al = null;
  176. Boolean areaAdminFlag = false;
  177. List<Integer> locationList = null;
  178. if (TokenManager.hasRole(AFTConstants.AREAADMIN)) {
  179. locationList = new ArrayList<>();
  180. areaAdminFlag = true;
  181. }
  182. for (int i = 0; i < locations.size(); i++) {
  183. AdminLocationBo alb = locations.getJSONObject(i).toJavaObject(AdminLocationBo.class);
  184. Integer province = alb.getProvince();
  185. String city = alb.getCity();
  186. if (null == province) {
  187. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "省份为空", "所在省份"));
  188. return res;
  189. }
  190. if (province.intValue() > AFTConstants.PROVINCEMAXNUM) {
  191. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "所在省份"));
  192. return res;
  193. }
  194. if (areaAdminFlag) {
  195. locationList.add(province);
  196. }
  197. if (!StringUtils.isBlank(city)) {
  198. String[] arr = city.trim().split(",|,");
  199. if (null != arr && arr.length > 0) {
  200. for (String s : arr) {
  201. Integer c = Integer.valueOf(s);
  202. if (c.intValue() > AFTConstants.CITYMAXNUM) {
  203. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "所在市"));
  204. return res;
  205. }
  206. adminLocationList.add(disposeAdminLocation(al, ad, province, c));
  207. if (areaAdminFlag) {
  208. locationList.add(c);
  209. }
  210. }
  211. }
  212. } else {
  213. adminLocationList.add(disposeAdminLocation(al, ad, province, null));
  214. }
  215. }
  216. if (areaAdminFlag) {
  217. List<Integer> albList = disposeAdminLocationBo();
  218. if (!albList.containsAll(locationList)) {
  219. res.getError().add(buildError("", "所选所在地区域不在负责区域,无法添加或修改"));
  220. return res;
  221. }
  222. }
  223. res.setData(adminLocationList);
  224. return res;
  225. }
  226. private AdminLocation disposeAdminLocation(AdminLocation al, Admin ad, Integer province, Integer c) {
  227. al = new AdminLocation();
  228. al.setId(UUID.randomUUID().toString());
  229. al.setAdminId(ad.getId());
  230. al.setProvince(province);
  231. if (null != c) {
  232. al.setCity(c);
  233. }
  234. return al;
  235. }
  236. private List<Integer> disposeAdminLocationBo() {
  237. List<AdminLocationBo> alb = adminLocationService.findLocation(TokenManager.getAdminId());
  238. List<Integer> albList = null;
  239. if (!alb.isEmpty()) {
  240. Integer albProvince = null;
  241. albList = new ArrayList<>();
  242. for (AdminLocationBo bo : alb) {
  243. albProvince = bo.getProvince();
  244. albList.add(albProvince);
  245. if (!StringUtils.isBlank(bo.getCity())) {
  246. String[] albArr = bo.getCity().trim().split(",|,");
  247. if (null != albArr && albArr.length > 0) {
  248. for (String ss : albArr) {
  249. albList.add(Integer.valueOf(ss));
  250. }
  251. }
  252. }
  253. }
  254. }
  255. return albList;
  256. }
  257. /** 图片上传 **/
  258. @RequestMapping(value = "/uploadAdminImg", method = RequestMethod.POST)
  259. public Result uploadCustomerImg(HttpServletRequest req,String sign){
  260. Result res = new Result();
  261. res.setData(handleFile(res, "/customer_sys_file/", false, req, sign));
  262. return res;
  263. }
  264. /**新增获取ID**/
  265. @RequestMapping(value = "/newId", method = RequestMethod.POST)
  266. public Result newId(){
  267. Result res = new Result();
  268. res.setData(UUID.randomUUID().toString());
  269. return res;
  270. }
  271. /**编辑页面信息获取**/
  272. @RequestMapping(value = "/selectAllByid", method = RequestMethod.POST)
  273. public Result selectAllByid(String id){
  274. Result res = new Result();
  275. res.setData(newAdminService.selectAllByid(id));
  276. return res;
  277. }
  278. }