AccountServiceImpl.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package com.fuint.common.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  5. import com.fuint.common.dto.AccountDto;
  6. import com.fuint.common.dto.AccountInfo;
  7. import com.fuint.common.service.AccountService;
  8. import com.fuint.common.service.StaffService;
  9. import com.fuint.framework.annoation.OperationServiceLog;
  10. import com.fuint.framework.exception.BusinessCheckException;
  11. import com.fuint.framework.exception.BusinessRuntimeException;
  12. import com.fuint.framework.pagination.PaginationRequest;
  13. import com.fuint.framework.pagination.PaginationResponse;
  14. import com.fuint.repository.mapper.*;
  15. import com.fuint.repository.model.*;
  16. import com.fuint.utils.Digests;
  17. import com.fuint.utils.Encodes;
  18. import com.github.pagehelper.Page;
  19. import com.github.pagehelper.PageHelper;
  20. import org.apache.commons.lang.StringUtils;
  21. import org.springframework.beans.BeanUtils;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.data.domain.PageImpl;
  24. import org.springframework.data.domain.PageRequest;
  25. import org.springframework.stereotype.Service;
  26. import org.springframework.transaction.annotation.Transactional;
  27. import javax.annotation.Resource;
  28. import java.util.*;
  29. /**
  30. * 后台账号接口
  31. *
  32. * Created by FSQ
  33. * CopyRight https://www.fuint.cn
  34. */
  35. @Service
  36. public class AccountServiceImpl extends ServiceImpl<TAccountMapper, TAccount> implements AccountService {
  37. @Resource
  38. private TAccountMapper tAccountMapper;
  39. @Resource
  40. private TDutyMapper tDutyMapper;
  41. @Resource
  42. private TAccountDutyMapper tAccountDutyMapper;
  43. @Resource
  44. private MtMerchantMapper mtMerchantMapper;
  45. @Resource
  46. private MtStoreMapper mtStoreMapper;
  47. /**
  48. * 员工接口
  49. */
  50. @Autowired
  51. private StaffService staffService;
  52. /**
  53. * 分页查询账号列表
  54. *
  55. * @param paginationRequest
  56. * @return
  57. */
  58. @Override
  59. public PaginationResponse<AccountDto> getAccountListByPagination(PaginationRequest paginationRequest) {
  60. Page<MtBanner> pageHelper = PageHelper.startPage(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
  61. LambdaQueryWrapper<TAccount> lambdaQueryWrapper = Wrappers.lambdaQuery();
  62. lambdaQueryWrapper.ne(TAccount::getAccountStatus, -1); // 1:启用;0:禁用;-1:删除
  63. String name = paginationRequest.getSearchParams().get("name") == null ? "" : paginationRequest.getSearchParams().get("name").toString();
  64. if (StringUtils.isNotBlank(name)) {
  65. lambdaQueryWrapper.like(TAccount::getAccountName, name);
  66. }
  67. String realName = paginationRequest.getSearchParams().get("realName") == null ? "" : paginationRequest.getSearchParams().get("realName").toString();
  68. if (StringUtils.isNotBlank(realName)) {
  69. lambdaQueryWrapper.like(TAccount::getRealName, realName);
  70. }
  71. String status = paginationRequest.getSearchParams().get("status") == null ? "" : paginationRequest.getSearchParams().get("status").toString();
  72. if (StringUtils.isNotBlank(status)) {
  73. lambdaQueryWrapper.eq(TAccount::getAccountStatus, status);
  74. }
  75. String merchantId = paginationRequest.getSearchParams().get("merchantId") == null ? "" : paginationRequest.getSearchParams().get("merchantId").toString();
  76. if (StringUtils.isNotBlank(merchantId)) {
  77. lambdaQueryWrapper.eq(TAccount::getMerchantId, merchantId);
  78. }
  79. String storeId = paginationRequest.getSearchParams().get("storeId") == null ? "" : paginationRequest.getSearchParams().get("storeId").toString();
  80. if (StringUtils.isNotBlank(storeId)) {
  81. lambdaQueryWrapper.eq(TAccount::getStoreId, storeId);
  82. }
  83. lambdaQueryWrapper.orderByDesc(TAccount::getAcctId);
  84. List<TAccount> accountList = tAccountMapper.selectList(lambdaQueryWrapper);
  85. List<AccountDto> dataList = new ArrayList<>();
  86. for (TAccount tAccount : accountList) {
  87. AccountDto accountDto = new AccountDto();
  88. BeanUtils.copyProperties(tAccount, accountDto);
  89. accountDto.setId(tAccount.getAcctId());
  90. MtMerchant mtMerchant = mtMerchantMapper.selectById(tAccount.getMerchantId());
  91. if (mtMerchant != null) {
  92. accountDto.setMerchantName(mtMerchant.getName());
  93. }
  94. MtStore mtStore = mtStoreMapper.selectById(tAccount.getStoreId());
  95. if (mtStore != null) {
  96. accountDto.setStoreName(mtStore.getName());
  97. }
  98. dataList.add(accountDto);
  99. }
  100. PageRequest pageRequest = PageRequest.of(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
  101. PageImpl pageImpl = new PageImpl(dataList, pageRequest, pageHelper.getTotal());
  102. PaginationResponse<AccountDto> paginationResponse = new PaginationResponse(pageImpl, AccountDto.class);
  103. paginationResponse.setTotalPages(pageHelper.getPages());
  104. paginationResponse.setTotalElements(pageHelper.getTotal());
  105. paginationResponse.setContent(dataList);
  106. return paginationResponse;
  107. }
  108. @Override
  109. public AccountInfo getAccountByName(String userName) {
  110. Map<String, Object> param = new HashMap();
  111. param.put("account_name", userName);
  112. List<TAccount> accountList = tAccountMapper.selectByMap(param);
  113. if (accountList != null && accountList.size() > 0) {
  114. AccountInfo accountInfo = new AccountInfo();
  115. TAccount account = accountList.get(0);
  116. accountInfo.setId(account.getAcctId());
  117. accountInfo.setAccountName(account.getAccountName());
  118. accountInfo.setRealName(account.getRealName());
  119. accountInfo.setRoleIds(account.getRoleIds());
  120. accountInfo.setStaffId(account.getStaffId());
  121. accountInfo.setStoreId(account.getStoreId());
  122. accountInfo.setMerchantId(account.getMerchantId());
  123. if (account.getMerchantId() != null && account.getMerchantId() > 0) {
  124. MtMerchant mtMerchant = mtMerchantMapper.selectById(account.getMerchantId());
  125. if (mtMerchant != null) {
  126. accountInfo.setMerchantName(mtMerchant.getName());
  127. }
  128. }
  129. if (account.getStoreId() != null && account.getStoreId() > 0) {
  130. MtStore mtStore = mtStoreMapper.selectById(account.getStoreId());
  131. if (mtStore != null) {
  132. accountInfo.setStoreName(mtStore.getName());
  133. }
  134. }
  135. return accountInfo;
  136. } else {
  137. return null;
  138. }
  139. }
  140. @Override
  141. public TAccount getAccountInfoById(Integer id) {
  142. TAccount tAccount = tAccountMapper.selectById(id);
  143. return tAccount;
  144. }
  145. /**
  146. * 创建账号信息
  147. *
  148. * @param tAccount
  149. * @return
  150. * */
  151. @Override
  152. @OperationServiceLog(description = "新增后台账户")
  153. public TAccount createAccountInfo(TAccount tAccount, List<TDuty> duties) {
  154. TAccount account = new TAccount();
  155. account.setAccountKey(tAccount.getAccountKey());
  156. account.setAccountName(tAccount.getAccountName().toLowerCase());
  157. account.setAccountStatus(1);
  158. account.setRealName(tAccount.getRealName());
  159. account.setRoleIds(tAccount.getRoleIds());
  160. account.setStaffId(tAccount.getStaffId());
  161. account.setMerchantId(tAccount.getMerchantId());
  162. account.setStoreId(tAccount.getStoreId());
  163. account.setCreateDate(new Date());
  164. account.setModifyDate(new Date());
  165. account.setStoreId(tAccount.getStoreId());
  166. account.setStaffId(tAccount.getStaffId());
  167. account.setPassword(tAccount.getPassword());
  168. this.entryptPassword(account);
  169. int id = tAccountMapper.insert(account);
  170. if (id > 0 && duties != null && duties.size() > 0) {
  171. for (TDuty tDuty : duties) {
  172. TAccountDuty tAccountDuty = new TAccountDuty();
  173. tAccountDuty.setDutyId(tDuty.getDutyId());
  174. tAccountDuty.setAcctId(account.getAcctId());
  175. tAccountDutyMapper.insert(tAccountDuty);
  176. }
  177. }
  178. if (id > 0 ) {
  179. return this.getAccountInfoById(id);
  180. } else {
  181. throw new BusinessRuntimeException("创建账号错误");
  182. }
  183. }
  184. /**
  185. * 获取账号角色ID
  186. *
  187. * @param accountId
  188. * @return
  189. * */
  190. @Override
  191. public List<Long> getRoleIdsByAccountId(Integer accountId) {
  192. List<Long> roleIds = tDutyMapper.getRoleIdsByAccountId(accountId);
  193. return roleIds;
  194. }
  195. /**
  196. * 修改账户
  197. *
  198. * @param tAccount 账户实体
  199. * @throws BusinessCheckException
  200. */
  201. @Override
  202. @Transactional(rollbackFor = Exception.class)
  203. @OperationServiceLog(description = "修改后台账户")
  204. public void editAccount(TAccount tAccount, List<TDuty> duties) throws BusinessCheckException {
  205. TAccount oldAccount = tAccountMapper.selectById(tAccount.getAcctId());
  206. if (oldAccount == null) {
  207. throw new BusinessCheckException("账户不存在.");
  208. }
  209. tAccount.setModifyDate(new Date());
  210. if (duties != null && duties.size() > 0) {
  211. if (tAccount.getAcctId() != null && tAccount.getAcctId() > 0) {
  212. tAccountDutyMapper.deleteDutiesByAccountId(tAccount.getAcctId());
  213. for (TDuty tDuty : duties) {
  214. TAccountDuty tAccountDuty = new TAccountDuty();
  215. tAccountDuty.setDutyId(tDuty.getDutyId());
  216. tAccountDuty.setAcctId(tAccount.getAcctId());
  217. tAccountDutyMapper.insert(tAccountDuty);
  218. }
  219. }
  220. }
  221. if (tAccount.getStaffId() != null && tAccount.getStaffId() > 0) {
  222. MtStaff mtStaff = staffService.queryStaffById(tAccount.getStaffId());
  223. if (mtStaff == null) {
  224. tAccount.setStaffId(0);
  225. }
  226. }
  227. tAccountMapper.updateById(tAccount);
  228. }
  229. /**
  230. * 根据账户名称获取账户所分配的角色ID集合
  231. *
  232. * @param accountId 账户
  233. * @return 角色ID集合
  234. */
  235. @Override
  236. public List<Long> getDutyIdsByAccountId(long accountId) {
  237. return tAccountDutyMapper.getDutyIdsByAccountId(accountId);
  238. }
  239. /**
  240. * 更新账户
  241. *
  242. * @param tAccount
  243. */
  244. @Override
  245. @Transactional(rollbackFor = Exception.class)
  246. @OperationServiceLog(description = "修改后台账户")
  247. public void updateAccount(TAccount tAccount) {
  248. tAccountMapper.updateById(tAccount);
  249. }
  250. /**
  251. * 删除账号
  252. *
  253. * @param userId
  254. * @return
  255. * */
  256. @Override
  257. @Transactional(rollbackFor = Exception.class)
  258. @OperationServiceLog(description = "删除后台账户")
  259. public void deleteAccount(Long userId) {
  260. TAccount tAccount = tAccountMapper.selectById(userId);
  261. tAccount.setAccountStatus(-1);
  262. tAccount.setModifyDate(new Date());
  263. tAccountMapper.updateById(tAccount);
  264. }
  265. /**
  266. * 设定安全的密码
  267. *
  268. * @param user
  269. * @return
  270. */
  271. @Override
  272. public void entryptPassword(TAccount user) {
  273. byte[] salt = Digests.generateSalt(8);
  274. user.setSalt(Encodes.encodeHex(salt));
  275. byte[] hashPassword = Digests.sha1(user.getPassword().getBytes(), salt, 1024);
  276. user.setPassword(Encodes.encodeHex(hashPassword));
  277. }
  278. /**
  279. * 获取加密密码
  280. *
  281. * @param password
  282. * @param salt
  283. * @return
  284. * */
  285. @Override
  286. public String getEntryptPassword(String password, String salt) {
  287. byte[] salt1 = Encodes.decodeHex(salt);
  288. byte[] hashPassword = Digests.sha1(password.getBytes(), salt1, 1024);
  289. return Encodes.encodeHex(hashPassword);
  290. }
  291. }