BackendAccountController.java 16 KB

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