BackendAccountController.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. package com.fuint.module.backendApi.controller;
  2. import com.fuint.common.Constants;
  3. import com.fuint.common.dto.AccountDto;
  4. import com.fuint.common.dto.AccountInfo;
  5. import com.fuint.common.dto.RoleDto;
  6. import com.fuint.common.enums.StatusEnum;
  7. import com.fuint.common.service.AccountService;
  8. import com.fuint.common.service.DutyService;
  9. import com.fuint.common.service.MerchantService;
  10. import com.fuint.common.service.StoreService;
  11. import com.fuint.common.util.CommonUtil;
  12. import com.fuint.common.util.TokenUtil;
  13. import com.fuint.framework.exception.BusinessCheckException;
  14. import com.fuint.framework.pagination.PaginationRequest;
  15. import com.fuint.framework.pagination.PaginationResponse;
  16. import com.fuint.framework.web.BaseController;
  17. import com.fuint.framework.web.ResponseObject;
  18. import com.fuint.repository.model.MtMerchant;
  19. import com.fuint.repository.model.MtStore;
  20. import com.fuint.repository.model.TAccount;
  21. import com.fuint.repository.model.TDuty;
  22. import com.fuint.utils.StringUtil;
  23. import io.swagger.annotations.Api;
  24. import io.swagger.annotations.ApiOperation;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.security.access.prepost.PreAuthorize;
  27. import org.springframework.web.bind.annotation.*;
  28. import javax.servlet.http.HttpServletRequest;
  29. import java.util.ArrayList;
  30. import java.util.HashMap;
  31. import java.util.List;
  32. import java.util.Map;
  33. /**
  34. * 后台管理员管理
  35. *
  36. * Created by FSQ
  37. * CopyRight https://www.fuint.cn
  38. */
  39. @Api(tags="管理端-管理员相关接口")
  40. @RestController
  41. @RequestMapping(value = "/backendApi/account")
  42. public class BackendAccountController extends BaseController {
  43. /**
  44. * 账户接口
  45. */
  46. @Autowired
  47. private AccountService tAccountService;
  48. /**
  49. * 角色接口
  50. */
  51. @Autowired
  52. private DutyService tDutyService;
  53. /**
  54. * 店铺接口
  55. */
  56. @Autowired
  57. private StoreService storeService;
  58. /**
  59. * 商户服务接口
  60. */
  61. @Autowired
  62. private MerchantService merchantService;
  63. /**
  64. * 账户信息列表
  65. *
  66. * @param request HttpServletRequest对象
  67. * @return 账户信息列表
  68. */
  69. @ApiOperation(value = "账户信息列表")
  70. @RequestMapping(value = "/list", method = RequestMethod.GET)
  71. @CrossOrigin
  72. @PreAuthorize("@pms.hasPermission('system:account:index')")
  73. public ResponseObject list(HttpServletRequest request) throws BusinessCheckException {
  74. String token = request.getHeader("Access-Token");
  75. Integer page = request.getParameter("page") == null ? Constants.PAGE_NUMBER : Integer.parseInt(request.getParameter("page"));
  76. Integer pageSize = request.getParameter("pageSize") == null ? Constants.PAGE_SIZE : Integer.parseInt(request.getParameter("pageSize"));
  77. String accountName = request.getParameter("accountName") == null ? "" : request.getParameter("accountName");
  78. String realName = request.getParameter("realName") == null ? "" : request.getParameter("realName");
  79. String accountStatus = request.getParameter("accountStatus") == null ? "" : request.getParameter("accountStatus");
  80. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  81. if (accountInfo == null) {
  82. return getFailureResult(1001, "请先登录");
  83. }
  84. PaginationRequest paginationRequest = new PaginationRequest();
  85. paginationRequest.setCurrentPage(page);
  86. paginationRequest.setPageSize(pageSize);
  87. Map<String, Object> searchParams = new HashMap<>();
  88. if (StringUtil.isNotEmpty(accountName)) {
  89. searchParams.put("name", accountName);
  90. }
  91. if (StringUtil.isNotEmpty(realName)) {
  92. searchParams.put("realName", realName);
  93. }
  94. if (StringUtil.isNotEmpty(accountStatus)) {
  95. searchParams.put("status", accountStatus);
  96. }
  97. if (StringUtil.isNotEmpty(accountStatus)) {
  98. searchParams.put("status", accountStatus);
  99. }
  100. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  101. searchParams.put("merchantId", accountInfo.getMerchantId());
  102. }
  103. if (accountInfo.getStoreId() != null && accountInfo.getStoreId() > 0) {
  104. searchParams.put("storeId", accountInfo.getStoreId());
  105. }
  106. paginationRequest.setSearchParams(searchParams);
  107. PaginationResponse<AccountDto> paginationResponse = tAccountService.getAccountListByPagination(paginationRequest);
  108. return getSuccessResult(paginationResponse);
  109. }
  110. /**
  111. * 获取账户详情
  112. *
  113. * @param request
  114. * @param userId 账号ID
  115. * @return 账户详情
  116. */
  117. @ApiOperation(value = "获取账户详情")
  118. @RequestMapping(value = "/info/{userId}", method = RequestMethod.GET)
  119. @CrossOrigin
  120. public ResponseObject info(HttpServletRequest request, @PathVariable("userId") Long userId) throws BusinessCheckException {
  121. String token = request.getHeader("Access-Token");
  122. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  123. if (accountInfo == null) {
  124. return getFailureResult(1001, "请先登录");
  125. }
  126. Map<String, Object> result = new HashMap<>();
  127. List<TDuty> roleList = tDutyService.getAvailableRoles(accountInfo.getMerchantId(), accountInfo.getId());
  128. List<RoleDto> roles = new ArrayList<>();
  129. if (roleList.size() > 0) {
  130. for (TDuty duty : roleList) {
  131. RoleDto e = new RoleDto();
  132. e.setId(duty.getDutyId().longValue());
  133. e.setName(duty.getDutyName());
  134. e.setStatus(duty.getStatus());
  135. roles.add(e);
  136. }
  137. }
  138. result.put("roles", roles);
  139. Map<String, Object> params = new HashMap<>();
  140. params.put("status", StatusEnum.ENABLED.getKey());
  141. if (accountInfo.getStoreId() != null && accountInfo.getStoreId() > 0) {
  142. params.put("storeId", accountInfo.getStoreId());
  143. }
  144. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  145. params.put("merchantId", accountInfo.getMerchantId());
  146. }
  147. List<MtStore> stores = storeService.queryStoresByParams(params);
  148. result.put("stores", stores);
  149. List<MtMerchant> merchants = merchantService.queryMerchantByParams(params);
  150. result.put("merchants", merchants);
  151. AccountDto accountDto = null;
  152. if (userId > 0) {
  153. TAccount tAccount = tAccountService.getAccountInfoById(userId.intValue());
  154. accountDto = new AccountDto();
  155. accountDto.setId(tAccount.getAcctId());
  156. accountDto.setAccountKey(tAccount.getAccountKey());
  157. accountDto.setAccountName(tAccount.getAccountName());
  158. accountDto.setAccountStatus(tAccount.getAccountStatus());
  159. accountDto.setCreateDate(tAccount.getCreateDate());
  160. accountDto.setRealName(tAccount.getRealName());
  161. accountDto.setModifyDate(tAccount.getModifyDate());
  162. accountDto.setStaffId(tAccount.getStaffId());
  163. accountDto.setMerchantId(tAccount.getMerchantId());
  164. if (tAccount.getStoreId() > 0) {
  165. accountDto.setStoreId(tAccount.getStoreId());
  166. }
  167. if (tAccount.getStoreId() > 0) {
  168. MtStore mtStore = storeService.queryStoreById(tAccount.getStoreId());
  169. if (mtStore != null) {
  170. accountDto.setStoreName(mtStore.getName());
  171. }
  172. }
  173. if (tAccount != null) {
  174. List<Long> roleIds = tAccountService.getRoleIdsByAccountId(tAccount.getAcctId());
  175. result.put("roleIds", roleIds);
  176. }
  177. } else {
  178. result.put("roleIds", "");
  179. }
  180. result.put("account", accountDto);
  181. return getSuccessResult(result);
  182. }
  183. /**
  184. * 新增账户
  185. *
  186. * @return 新增账户
  187. * @throws BusinessCheckException
  188. */
  189. @ApiOperation(value = "新增账户")
  190. @RequestMapping(value = "/doCreate", method = RequestMethod.POST)
  191. @CrossOrigin
  192. @PreAuthorize("@pms.hasPermission('system:account:add')")
  193. public ResponseObject doCreate(HttpServletRequest request, @RequestBody Map<String, Object> param) {
  194. String token = request.getHeader("Access-Token");
  195. AccountInfo loginAccount = TokenUtil.getAccountInfoByToken(token);
  196. if (loginAccount == null) {
  197. return getFailureResult(1001, "请先登录");
  198. }
  199. List<Integer> roleIds = (List) param.get("roleIds");
  200. String accountName = param.get("accountName").toString();
  201. String accountStatus = param.get("accountStatus").toString();
  202. String realName = param.get("realName").toString();
  203. String password = param.get("password").toString();
  204. String storeId = param.get("storeId") == null ? "0" : param.get("storeId").toString();
  205. String merchantId = param.get("merchantId") == null ? "0" : param.get("merchantId").toString();
  206. String staffId = param.get("staffId") == null ? "0" : param.get("staffId").toString();
  207. AccountInfo accountInfo = tAccountService.getAccountByName(accountName);
  208. if (accountInfo != null) {
  209. return getFailureResult(201, "该用户名已存在");
  210. }
  211. List<TDuty> duties = new ArrayList<>();
  212. if (roleIds.size() > 0) {
  213. Integer[] roles = roleIds.toArray(new Integer[roleIds.size()]);
  214. String[] ids = new String[roles.length];
  215. for (int i = 0; i < roles.length; i++) {
  216. ids[i] = roles[i].toString();
  217. }
  218. duties = tDutyService.findDatasByIds(ids);
  219. if (duties.size() < roleIds.size()) {
  220. return getFailureResult(201, "您分配的角色不存在");
  221. }
  222. }
  223. TAccount tAccount = new TAccount();
  224. tAccount.setAccountKey(CommonUtil.createAccountKey());
  225. tAccount.setRealName(realName);
  226. tAccount.setAccountName(accountName);
  227. tAccount.setAccountStatus(Integer.parseInt(accountStatus));
  228. tAccount.setPassword(password);
  229. tAccount.setIsActive(1);
  230. tAccount.setLocked(0);
  231. tAccount.setStoreId(Integer.parseInt(storeId));
  232. tAccount.setMerchantId(Integer.parseInt(merchantId));
  233. tAccount.setStaffId(Integer.parseInt(staffId));
  234. tAccountService.createAccountInfo(tAccount, duties);
  235. return getSuccessResult(true);
  236. }
  237. /**
  238. * 修改账户信息
  239. *
  240. * @return
  241. * @throws BusinessCheckException
  242. */
  243. @ApiOperation(value = "修改账户信息")
  244. @RequestMapping(value = "/update", method = RequestMethod.POST)
  245. @CrossOrigin
  246. @PreAuthorize("@pms.hasPermission('system:account:edit')")
  247. public ResponseObject update(HttpServletRequest request, @RequestBody Map<String, Object> param) throws BusinessCheckException {
  248. String token = request.getHeader("Access-Token");
  249. List<Integer> roleIds = (List) param.get("roleIds");
  250. String realName = param.get("realName").toString();
  251. String accountName = param.get("accountName").toString();
  252. String accountStatus = param.get("accountStatus").toString();
  253. String storeId = param.get("storeId") == null ? "" : param.get("storeId").toString();
  254. String staffId = param.get("staffId") == null ? "" : param.get("staffId").toString();
  255. String merchantId = param.get("merchantId") == null ? "" : param.get("merchantId").toString();
  256. Long id = Long.parseLong(param.get("id").toString());
  257. AccountInfo loginAccount = TokenUtil.getAccountInfoByToken(token);
  258. if (loginAccount == null) {
  259. return getFailureResult(1001, "请先登录");
  260. }
  261. TAccount tAccount = tAccountService.getAccountInfoById(id.intValue());
  262. tAccount.setAcctId(id.intValue());
  263. tAccount.setRealName(realName);
  264. if (StringUtil.isNotEmpty(accountName)) {
  265. tAccount.setAccountName(accountName);
  266. }
  267. if (StringUtil.isNotEmpty(accountStatus)) {
  268. tAccount.setAccountStatus(Integer.parseInt(accountStatus));
  269. }
  270. if (StringUtil.isNotEmpty(storeId)) {
  271. tAccount.setStoreId(Integer.parseInt(storeId));
  272. }
  273. if (StringUtil.isNotEmpty(staffId)) {
  274. tAccount.setStaffId(Integer.parseInt(staffId));
  275. }
  276. if (StringUtil.isNotEmpty(merchantId)) {
  277. tAccount.setMerchantId(Integer.parseInt(merchantId));
  278. }
  279. AccountInfo accountInfo = tAccountService.getAccountByName(accountName);
  280. if (accountInfo != null && accountInfo.getId() != id.intValue()) {
  281. return getFailureResult(201, "该用户名已存在");
  282. }
  283. List<TDuty> duties = null;
  284. if (roleIds.size() > 0) {
  285. Integer[] roles = roleIds.toArray(new Integer[roleIds.size()]);
  286. String[] ids = new String[roles.length];
  287. for (int i = 0; i < roles.length; i++) {
  288. ids[i] = roles[i].toString();
  289. }
  290. duties = tDutyService.findDatasByIds(ids);
  291. if (duties.size() < roleIds.size()) {
  292. return getFailureResult(201, "您分配的角色不存在");
  293. }
  294. }
  295. tAccountService.editAccount(tAccount, duties);
  296. return getSuccessResult(true);
  297. }
  298. /**
  299. * 删除账户信息
  300. *
  301. * @param request HttpServletRequest对象
  302. * @param userIds 账户ID(逗号隔开)
  303. * @return
  304. * @throws BusinessCheckException
  305. */
  306. @ApiOperation(value = "删除账户信息")
  307. @RequestMapping(value = "/delete/{userIds}", method = RequestMethod.GET)
  308. @CrossOrigin
  309. @PreAuthorize("@pms.hasPermission('system:account:delete')")
  310. public ResponseObject deleteAccount(HttpServletRequest request, @PathVariable("userIds") String userIds) {
  311. String token = request.getHeader("Access-Token");
  312. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  313. if (accountInfo == null) {
  314. return getFailureResult(1001, "请先登录");
  315. }
  316. String ids[] = userIds.split(",");
  317. if (ids.length > 0) {
  318. for (int i = 0; i < ids.length; i++) {
  319. if (StringUtil.isNotEmpty(ids[i])) {
  320. Integer userId = Integer.parseInt(ids[i]);
  321. TAccount tAccount = tAccountService.getAccountInfoById(userId.intValue());
  322. if (tAccount == null) {
  323. return getFailureResult(201, "账户不存在");
  324. }
  325. if (StringUtil.equals(accountInfo.getAccountName(), tAccount.getAccountName())) {
  326. return getFailureResult(201, "您不能删除自己");
  327. }
  328. }
  329. }
  330. for (int i = 0; i < ids.length; i++) {
  331. if (StringUtil.isNotEmpty(ids[i])) {
  332. Long userId = Long.parseLong(ids[i]);
  333. tAccountService.deleteAccount(userId);
  334. }
  335. }
  336. }
  337. return getSuccessResult(true);
  338. }
  339. /**
  340. * 更新账户状态
  341. *
  342. * @return
  343. */
  344. @ApiOperation(value = "更新账户状态")
  345. @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
  346. @CrossOrigin
  347. @PreAuthorize("@pms.hasPermission('system:account:edit')")
  348. public ResponseObject updateStatus(HttpServletRequest request, @RequestBody Map<String, Object> param) throws BusinessCheckException {
  349. String token = request.getHeader("Access-Token");
  350. Integer userId = param.get("userId") == null ? 0 : Integer.parseInt(param.get("userId").toString());
  351. Integer status = param.get("status") == null ? 0 : Integer.parseInt(param.get("status").toString());
  352. AccountInfo accountDto = TokenUtil.getAccountInfoByToken(token);
  353. if (accountDto == null) {
  354. return getFailureResult(1001, "请先登录");
  355. }
  356. TAccount tAccount = tAccountService.getAccountInfoById(userId.intValue());
  357. if (tAccount == null) {
  358. return getFailureResult(201, "账户不存在");
  359. }
  360. tAccount.setAccountStatus(status);
  361. tAccountService.updateAccount(tAccount);
  362. return getSuccessResult(true);
  363. }
  364. /**
  365. * 修改账户密码
  366. *
  367. * @return
  368. */
  369. @ApiOperation(value = "修改账户密码")
  370. @RequestMapping(value = "/resetPwd", method = RequestMethod.POST)
  371. @CrossOrigin
  372. @PreAuthorize("@pms.hasPermission('system:account:edit')")
  373. public ResponseObject resetPwd(HttpServletRequest request, @RequestBody Map<String, Object> param) {
  374. String token = request.getHeader("Access-Token");
  375. Integer userId = param.get("userId") == null ? 0 : Integer.parseInt(param.get("userId").toString());
  376. String password = param.get("password") == null ? "" : param.get("password").toString();
  377. AccountInfo accountDto = TokenUtil.getAccountInfoByToken(token);
  378. if (accountDto == null) {
  379. return getFailureResult(1001, "请先登录");
  380. }
  381. TAccount tAccount = tAccountService.getAccountInfoById(userId.intValue());
  382. tAccount.setPassword(password);
  383. if (tAccount != null) {
  384. tAccountService.entryptPassword(tAccount);
  385. tAccountService.updateAccount(tAccount);
  386. }
  387. return getSuccessResult(true);
  388. }
  389. }