KuaishouLiveUserAccountServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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(), account.getAccountId());
  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<Long> accountIds = kuaishouLiveUserAccountMapper.queryAccountIdsByUserId(kuaishouLiveUserAccount.getUserId());
  77. if (Check.isNotNull(accountIds) && accountIds.size() > 0) {
  78. kuaishouLiveUserAccount.setAccountIds(accountIds);
  79. PageUtils.startPage();
  80. list = kuaishouLiveUserAccountMapper.selectAllListByAccountIds(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<Long> accountIds = kuaishouLiveUserAccountMapper.queryAccountIdsByUserId(userAccount.getUserId());
  94. if (Check.isNotNull(accountIds) && accountIds.size() > 0) {
  95. userAccount.setAccountIds(accountIds);
  96. PageUtils.startPage();
  97. list = kuaishouLiveUserAccountMapper.queryAllListByAccountIds(userAccount);
  98. }
  99. }
  100. return list;
  101. }
  102. /**
  103. * @param kuaishouLiveUserAccount 快手直播账户
  104. * @return 结果
  105. */
  106. @Override
  107. public ResultResponse insertKuaishouLiveUserAccount(KuaishouLiveUserAccount account) {
  108. if (Check.isNull(account.getAccountId())) {
  109. account.setAccountId(System.currentTimeMillis());
  110. }
  111. KuaishouLiveUserAccount one = kuaishouLiveUserAccountMapper.getOne(account);
  112. if (one != null) {
  113. return ResultResponse.error("快手ID:" + account.getKsId() + "下已存在该账户ID,请重新填写!");
  114. }
  115. String collaboratorList = account.getCollaboratorList();
  116. if (collaboratorList != null) {
  117. List<JSONObject> collaborators = JSONArray.parseArray(collaboratorList, JSONObject.class);
  118. for (JSONObject obj : collaborators) {
  119. obj.put("ksId", account.getKsId());
  120. obj.put("ksName", account.getKsName());
  121. obj.put("accountId", account.getAccountId());
  122. obj.put("accountName", account.getAccountName());
  123. obj.put("createrId", account.getCreaterId());
  124. }
  125. if (Check.isNotNull(collaborators)) {
  126. accountPartMapper.replaceBatch(collaborators);
  127. }
  128. }
  129. account.setId(UUID.randomUUID().toString().replace("-", ""));
  130. account.setProjectId(getAndInsertProject(account.getKsId(), account.getKsName()));
  131. account.setAccountStatus("审核通过");
  132. int num = kuaishouLiveUserAccountMapper.insertKuaishouLiveUserAccount(account);
  133. if (num > 0) {
  134. return ResultResponse.success();
  135. }
  136. return ResultResponse.error("fail");
  137. }
  138. private Long getAndInsertProject(Long id, String projectName) {
  139. KuaishouLiveProject project = kuaishouLiveProjectMapper.getOne(id, projectName);
  140. if (Check.isNull(project)) {
  141. KuaishouLiveProject kuaishouLiveProject = new KuaishouLiveProject();
  142. kuaishouLiveProject.setId(id);
  143. kuaishouLiveProject.setProjectName(projectName);
  144. kuaishouLiveProjectMapper.insertKuaishouLiveProject(kuaishouLiveProject);
  145. }
  146. return id;
  147. }
  148. @Override
  149. public ResultResponse insertByExcel(MultipartFile file, Long createrId) {
  150. List<KuaishouLiveUserAccount> accountList = new ArrayList<>();
  151. try {
  152. String fileName = file.getOriginalFilename();
  153. List<JSONObject> list = LiveDataExcelUtils.analysisFile(file, fileName);
  154. for (JSONObject object : list) {
  155. KuaishouLiveUserAccount account = new KuaishouLiveUserAccount(object, createrId);
  156. account.setOperatorLeaderId(getUserIdByName(account.getOperatorLeader()));
  157. account.setSaleLeaderId(getUserIdByName(account.getSaleLeader()));
  158. account.setProjectId(getAndInsertProject(account.getKsId(), account.getProjectName()));
  159. insertAccountPart(account);
  160. accountList.add(account);
  161. }
  162. } catch (Exception e) {
  163. e.printStackTrace();
  164. return ResultResponse.error("导入异常");
  165. }
  166. if (Check.isNotNull(accountList)) {
  167. kuaishouLiveUserAccountMapper.replaceBatch(accountList);
  168. }
  169. return ResultResponse.success();
  170. }
  171. private void insertAccountPart(KuaishouLiveUserAccount account) {
  172. String collaborators = account.getCollaboratorList();
  173. if (Check.isNotNull(collaborators)) {
  174. List<String> users = Arrays.asList(collaborators.split(";"));
  175. List<JSONObject> list = new ArrayList<>();
  176. for (String user : users) {
  177. JSONObject obj = new JSONObject();
  178. obj.put("ksId", account.getKsId());
  179. obj.put("ksName", account.getKsName());
  180. obj.put("accountId", account.getAccountId());
  181. obj.put("accountName", account.getAccountName());
  182. obj.put("createrId", account.getCreaterId());
  183. String idAndName = user.replace("(", "(").replace(")", ")").trim();
  184. String[] split = idAndName.split("(");
  185. obj.put("userId", getUserIdByName(split[0]));
  186. obj.put("nickName", split[0]);
  187. list.add(obj);
  188. }
  189. if (Check.isNotNull(collaborators)) {
  190. accountPartMapper.replaceBatch(list);
  191. }
  192. }
  193. }
  194. // 通过人员名字查询人员ID
  195. private String getUserIdByName(String name) {
  196. if (Check.isNull(name)) {
  197. return null;
  198. }
  199. SysUser user = sysUserService.selectUserByNickName(name);
  200. if (Check.isNotNull(user)) {
  201. return user.getUserId().toString();
  202. }
  203. return null;
  204. }
  205. /**
  206. * 修改快手直播账户
  207. */
  208. @Override
  209. public ResultResponse updateKuaishouLiveUserAccount(KuaishouLiveUserAccount account) {
  210. KuaishouLiveUserAccount one = kuaishouLiveUserAccountMapper.getOne(account);
  211. if (one == null) {
  212. return ResultResponse.error("未查询到账户");
  213. }
  214. int i = kuaishouLiveUserAccountMapper.updateKuaishouLiveUserAccount(account);
  215. if (i > 0) {
  216. String collaboratorList = account.getCollaboratorList();
  217. if (Check.isNotNull(collaboratorList) && !"[]".equals(collaboratorList)) {
  218. List<JSONObject> collaborators = JSONArray.parseArray(collaboratorList, JSONObject.class);
  219. for (JSONObject obj : collaborators) {
  220. obj.put("ksId", account.getKsId());
  221. obj.put("ksName", account.getKsName());
  222. obj.put("accountId", account.getAccountId());
  223. obj.put("accountName", account.getAccountName());
  224. obj.put("createrId", account.getCreaterId());
  225. }
  226. if (Check.isNotNull(collaborators)) {
  227. accountPartMapper.deleteByAccountId(account.getKsId(), account.getAccountId(), null);
  228. accountPartMapper.replaceBatch(collaborators);
  229. }
  230. } else {
  231. accountPartMapper.deleteByAccountId(account.getKsId(), account.getAccountId(), null);
  232. }
  233. return ResultResponse.success();
  234. }
  235. return ResultResponse.error("fail");
  236. }
  237. /**
  238. * 批量删除快手直播账户
  239. *
  240. * @param ids 需要删除的快手直播账户主键
  241. * @return 结果
  242. */
  243. @Override
  244. public int deleteKuaishouLiveUserAccountByIds(String[] ids) {
  245. return kuaishouLiveUserAccountMapper.deleteKuaishouLiveUserAccountByIds(ids);
  246. }
  247. /**
  248. * 删除快手直播账户信息
  249. *
  250. * @param id 快手直播账户主键
  251. * @return 结果
  252. */
  253. @Override
  254. public int deleteKuaishouLiveUserAccountById(String id) {
  255. return kuaishouLiveUserAccountMapper.deleteKuaishouLiveUserAccountById(id);
  256. }
  257. @Override
  258. public List<JSONObject> queryNicknameList() {
  259. return kuaishouLiveUserAccountMapper.queryNicknameList();
  260. }
  261. @Override
  262. public List<JSONObject> queryAccountListByKsId(Long ksId) {
  263. return kuaishouLiveUserAccountMapper.queryAccountListByKsId(ksId);
  264. }
  265. @Override
  266. public ResultResponse queryRoles(String type) {
  267. List<JSONObject> list = sysUserService.queryRoles(type);
  268. return ResultResponse.success(list);
  269. }
  270. @Override
  271. public ResultResponse queryAccountAllUsers(Long ksId, Long accountId) {
  272. List<JSONObject> collaborators = accountPartMapper.queryAccountCollaborators(ksId, accountId);
  273. KuaishouLiveUserAccount account = new KuaishouLiveUserAccount();
  274. account.setAccountId(accountId);
  275. account.setKsId(ksId);
  276. KuaishouLiveUserAccount one = kuaishouLiveUserAccountMapper.getOne(account);
  277. if (Check.isNotNull(one)) {
  278. if (Check.isNotNull(one.getSaleLeaderId())) {
  279. JSONObject obj = new JSONObject();
  280. obj.put("role", "saleLeader");
  281. obj.put("roleName", "销售负责人");
  282. obj.put("userId", one.getSaleLeaderId());
  283. obj.put("name", one.getSaleLeader());
  284. collaborators.add(obj);
  285. }
  286. if (Check.isNotNull(one.getOperatorLeaderId())) {
  287. JSONObject obj = new JSONObject();
  288. obj.put("role", "operatorLeader");
  289. obj.put("roleName", "运营负责人");
  290. obj.put("userId", one.getOperatorLeaderId());
  291. obj.put("name", one.getOperatorLeader());
  292. collaborators.add(obj);
  293. }
  294. if (Check.isNotNull(one.getDesignerLeaderId())) {
  295. JSONObject obj = new JSONObject();
  296. obj.put("role", "designerLeader");
  297. obj.put("roleName", "设计负责人");
  298. obj.put("userId", one.getDesignerLeaderId());
  299. obj.put("name", one.getDesignerLeader());
  300. collaborators.add(obj);
  301. }
  302. }
  303. return ResultResponse.success(collaborators);
  304. }
  305. @Override
  306. public ResultResponse deleteAccountAllUser(Long ksId, Long accountId, String role, Long userId) {
  307. if ("collaborator".equals(role)) {
  308. accountPartMapper.deleteByAccountId(ksId, accountId, userId);
  309. } else {
  310. KuaishouLiveUserAccount account = new KuaishouLiveUserAccount();
  311. account.setAccountId(accountId);
  312. account.setKsId(ksId);
  313. KuaishouLiveUserAccount one = kuaishouLiveUserAccountMapper.getOne(account);
  314. if ("saleLeader".equals(role)) {
  315. one.setSaleLeader("");
  316. one.setSaleLeaderId("");
  317. }
  318. if ("operatorLeader".equals(role)) {
  319. one.setOperatorLeader("");
  320. one.setOperatorLeaderId("");
  321. }
  322. if ("designerLeader".equals(role)) {
  323. one.setDesignerLeader("");
  324. one.setDesignerLeaderId("");
  325. }
  326. kuaishouLiveUserAccountMapper.updateKuaishouLiveUserAccount(one);
  327. }
  328. return ResultResponse.success();
  329. }
  330. }