KuaishouLiveUserAccountServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package com.ruixuan.live.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.ruixuan.common.core.domain.ResultResponse;
  6. import com.ruixuan.common.core.domain.entity.SysUser;
  7. import com.ruixuan.common.utils.Check;
  8. import com.ruixuan.common.utils.PageUtils;
  9. import com.ruixuan.data.utils.LiveDataExcelUtils;
  10. import com.ruixuan.live.entity.KuaishouLiveProject;
  11. import com.ruixuan.live.entity.KuaishouLiveUserAccount;
  12. import com.ruixuan.live.mapper.KuaishouLiveProjectMapper;
  13. import com.ruixuan.live.mapper.KuaishouLiveUserAccountMapper;
  14. import com.ruixuan.live.mapper.KuaishouLiveUserAccountPartMapper;
  15. import com.ruixuan.live.service.IKuaishouLiveUserAccountService;
  16. import com.ruixuan.system.service.ISysRoleService;
  17. import com.ruixuan.system.service.ISysUserService;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Service;
  20. import org.springframework.web.multipart.MultipartFile;
  21. import java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.List;
  24. import java.util.UUID;
  25. /**
  26. * 快手直播账户Service业务层处理
  27. *
  28. * @author ruoyi
  29. * @date 2022-05-31
  30. */
  31. @Service
  32. public class KuaishouLiveUserAccountServiceImpl implements IKuaishouLiveUserAccountService {
  33. @Autowired
  34. private KuaishouLiveUserAccountMapper kuaishouLiveUserAccountMapper;
  35. @Autowired
  36. private KuaishouLiveUserAccountPartMapper accountPartMapper;
  37. @Autowired
  38. private KuaishouLiveProjectMapper kuaishouLiveProjectMapper;
  39. @Autowired
  40. private ISysUserService sysUserService;
  41. @Autowired
  42. private ISysRoleService roleService;
  43. /**
  44. * 查询快手直播账户
  45. *
  46. * @param id 快手直播账户主键
  47. * @return 快手直播账户
  48. */
  49. @Override
  50. public JSONObject selectKuaishouLiveUserAccountById(String id) {
  51. KuaishouLiveUserAccount account = kuaishouLiveUserAccountMapper.selectKuaishouLiveUserAccountById(id);
  52. JSONObject object = JSONObject.parseObject(JSON.toJSONString(account));
  53. if (object != null) {
  54. List<JSONObject> parts = accountPartMapper.queryCollaboratorsById(account.getKsId());
  55. object.put("collaborators", parts);
  56. }
  57. return object;
  58. }
  59. /**
  60. * 查询快手直播账户列表
  61. *
  62. * @param kuaishouLiveUserAccount 快手直播账户
  63. * @return 快手直播账户
  64. */
  65. @Override
  66. public List<KuaishouLiveUserAccount> selectKuaishouLiveUserAccountList(KuaishouLiveUserAccount kuaishouLiveUserAccount) {
  67. List<KuaishouLiveUserAccount> list = new ArrayList<>();
  68. if (Check.isNull(kuaishouLiveUserAccount.getAccountType())) {
  69. kuaishouLiveUserAccount.setAccountType("cljn");
  70. }
  71. String roleCode = roleService.selectRoleByUserId(kuaishouLiveUserAccount.getUserId());
  72. if ("admin".equals(roleCode)) {
  73. PageUtils.startPage();
  74. list = kuaishouLiveUserAccountMapper.selectKuaishouLiveUserAccountList(kuaishouLiveUserAccount);
  75. } else {
  76. List<String> accountNames = kuaishouLiveUserAccountMapper.queryAccountNamesByUserId(kuaishouLiveUserAccount.getUserId());
  77. if (Check.isNotNull(accountNames) && accountNames.size() > 0) {
  78. kuaishouLiveUserAccount.setAccountNames(accountNames);
  79. PageUtils.startPage();
  80. list = kuaishouLiveUserAccountMapper.selectAllListByAccountNames(kuaishouLiveUserAccount);
  81. }
  82. }
  83. return list;
  84. }
  85. @Override
  86. public List<KuaishouLiveUserAccount> queryAllList(KuaishouLiveUserAccount userAccount) {
  87. List<KuaishouLiveUserAccount> list = new ArrayList<>();
  88. String roleCode = roleService.selectRoleByUserId(userAccount.getUserId());
  89. if ("admin".equals(roleCode)) {
  90. PageUtils.startPage();
  91. list = kuaishouLiveUserAccountMapper.queryAllList(userAccount);
  92. } else {
  93. List<String> accountNames = kuaishouLiveUserAccountMapper.queryAccountNamesByUserId(userAccount.getUserId());
  94. if (Check.isNotNull(accountNames) && accountNames.size() > 0) {
  95. userAccount.setAccountNames(accountNames);
  96. PageUtils.startPage();
  97. list = kuaishouLiveUserAccountMapper.queryAllListByAccountNames(userAccount);
  98. }
  99. }
  100. return list;
  101. }
  102. /**
  103. * @param kuaishouLiveUserAccount 快手直播账户
  104. * @return 结果
  105. */
  106. @Override
  107. public ResultResponse insertKuaishouLiveUserAccount(KuaishouLiveUserAccount account) {
  108. KuaishouLiveUserAccount one = kuaishouLiveUserAccountMapper.getOne(account.getKsId(), account.getAccountName());
  109. if (one != null) {
  110. return ResultResponse.error("快手ID:" + account.getKsId() + "下已存在该账户名,请重新命名!");
  111. }
  112. String collaboratorList = account.getCollaboratorList();
  113. if (collaboratorList != null) {
  114. List<JSONObject> collaborators = JSONArray.parseArray(collaboratorList, JSONObject.class);
  115. for (JSONObject obj : collaborators) {
  116. obj.put("ksId", account.getKsId());
  117. obj.put("ksName", account.getKsName());
  118. obj.put("accountId", account.getAccountId());
  119. obj.put("accountName", account.getAccountName());
  120. obj.put("createrId", account.getCreaterId());
  121. }
  122. if (Check.isNotNull(collaborators)) {
  123. accountPartMapper.replaceBatch(collaborators);
  124. }
  125. }
  126. account.setId(UUID.randomUUID().toString().replace("-", ""));
  127. account.setProjectId(getAndInsertProject(account.getKsId(), account.getKsName()));
  128. account.setAccountStatus("审核通过");
  129. int num = kuaishouLiveUserAccountMapper.insertKuaishouLiveUserAccount(account);
  130. if (num > 0) {
  131. return ResultResponse.success();
  132. }
  133. return ResultResponse.error("fail");
  134. }
  135. private Long getAndInsertProject(Long id, String projectName) {
  136. KuaishouLiveProject project = kuaishouLiveProjectMapper.getOne(id, projectName);
  137. if (Check.isNull(project)) {
  138. KuaishouLiveProject kuaishouLiveProject = new KuaishouLiveProject();
  139. kuaishouLiveProject.setId(id);
  140. kuaishouLiveProject.setProjectName(projectName);
  141. kuaishouLiveProjectMapper.insertKuaishouLiveProject(kuaishouLiveProject);
  142. }
  143. return id;
  144. }
  145. @Override
  146. public ResultResponse insertByExcel(MultipartFile file, Long createrId) {
  147. List<KuaishouLiveUserAccount> accountList = new ArrayList<>();
  148. try {
  149. String fileName = file.getOriginalFilename();
  150. List<JSONObject> list = LiveDataExcelUtils.analysisFile(file, fileName);
  151. for (JSONObject object : list) {
  152. KuaishouLiveUserAccount account = new KuaishouLiveUserAccount(object, createrId);
  153. account.setOperatorLeaderId(getUserIdByName(account.getOperatorLeader()));
  154. account.setSaleLeaderId(getUserIdByName(account.getSaleLeader()));
  155. account.setProjectId(getAndInsertProject(account.getKsId(), account.getProjectName()));
  156. insertAccountPart(account);
  157. accountList.add(account);
  158. }
  159. } catch (Exception e) {
  160. e.printStackTrace();
  161. return ResultResponse.error("导入异常");
  162. }
  163. if (Check.isNotNull(accountList)) {
  164. kuaishouLiveUserAccountMapper.replaceBatch(accountList);
  165. }
  166. return ResultResponse.success();
  167. }
  168. private void insertAccountPart(KuaishouLiveUserAccount account) {
  169. String collaborators = account.getCollaboratorList();
  170. if (Check.isNotNull(collaborators)) {
  171. List<String> users = Arrays.asList(collaborators.split(";"));
  172. List<JSONObject> list = new ArrayList<>();
  173. for (String user : users) {
  174. JSONObject obj = new JSONObject();
  175. obj.put("ksId", account.getKsId());
  176. obj.put("ksName", account.getKsName());
  177. obj.put("accountId", account.getAccountId());
  178. obj.put("accountName", account.getAccountName());
  179. obj.put("createrId", account.getCreaterId());
  180. String idAndName = user.replace("(", "(").replace(")", ")").trim();
  181. String[] split = idAndName.split("(");
  182. obj.put("userId", getUserIdByName(split[0]));
  183. obj.put("nickName", split[0]);
  184. list.add(obj);
  185. }
  186. if (Check.isNotNull(collaborators)) {
  187. accountPartMapper.replaceBatch(list);
  188. }
  189. }
  190. }
  191. // 通过人员名字查询人员ID
  192. private Long getUserIdByName(String name) {
  193. if (Check.isNull(name)) {
  194. return null;
  195. }
  196. SysUser user = sysUserService.selectUserByNickName(name);
  197. if (Check.isNotNull(user)) {
  198. return user.getUserId();
  199. }
  200. return null;
  201. }
  202. /**
  203. * 修改快手直播账户
  204. */
  205. @Override
  206. public ResultResponse updateKuaishouLiveUserAccount(KuaishouLiveUserAccount account) {
  207. KuaishouLiveUserAccount one = kuaishouLiveUserAccountMapper.getOne(account.getKsId(), account.getAccountName());
  208. if (one == null) {
  209. return ResultResponse.error("未查询到账户");
  210. }
  211. int i = kuaishouLiveUserAccountMapper.updateKuaishouLiveUserAccount(account);
  212. if (i > 0) {
  213. String collaboratorList = account.getCollaboratorList();
  214. if (Check.isNotNull(collaboratorList)) {
  215. List<JSONObject> collaborators = JSONArray.parseArray(collaboratorList, JSONObject.class);
  216. for (JSONObject obj : collaborators) {
  217. obj.put("ksId", account.getKsId());
  218. obj.put("ksName", account.getKsName());
  219. obj.put("accountId", account.getAccountId());
  220. obj.put("accountName", account.getAccountName());
  221. obj.put("createrId", account.getCreaterId());
  222. }
  223. if (Check.isNotNull(collaborators)) {
  224. accountPartMapper.deleteKuaishouLiveUserAccountPartById(account.getKsId());
  225. accountPartMapper.replaceBatch(collaborators);
  226. }
  227. }
  228. return ResultResponse.success();
  229. }
  230. return ResultResponse.error("fail");
  231. }
  232. /**
  233. * 批量删除快手直播账户
  234. *
  235. * @param ids 需要删除的快手直播账户主键
  236. * @return 结果
  237. */
  238. @Override
  239. public int deleteKuaishouLiveUserAccountByIds(String[] ids) {
  240. return kuaishouLiveUserAccountMapper.deleteKuaishouLiveUserAccountByIds(ids);
  241. }
  242. /**
  243. * 删除快手直播账户信息
  244. *
  245. * @param id 快手直播账户主键
  246. * @return 结果
  247. */
  248. @Override
  249. public int deleteKuaishouLiveUserAccountById(String id) {
  250. return kuaishouLiveUserAccountMapper.deleteKuaishouLiveUserAccountById(id);
  251. }
  252. @Override
  253. public List<JSONObject> queryNicknameList() {
  254. return kuaishouLiveUserAccountMapper.queryNicknameList();
  255. }
  256. @Override
  257. public List<JSONObject> queryAccountListByKsId(Long ksId) {
  258. return kuaishouLiveUserAccountMapper.queryAccountListByKsId(ksId);
  259. }
  260. @Override
  261. public ResultResponse queryRoles(String type) {
  262. List<JSONObject> list = sysUserService.queryRoles(type);
  263. return ResultResponse.success(list);
  264. }
  265. }