KuaishouPromoterServiceImpl.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. package com.ruixuan.isc.service.impl;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.ruixuan.common.core.domain.AjaxResult;
  5. import com.ruixuan.common.core.domain.entity.SysUser;
  6. import com.ruixuan.common.utils.Check;
  7. import com.ruixuan.common.utils.DateUtils;
  8. import com.ruixuan.common.utils.PageUtils;
  9. import com.ruixuan.common.utils.RedisUtil;
  10. import com.ruixuan.common.utils.Result;
  11. import com.ruixuan.common.utils.http.HttpUtil;
  12. import com.ruixuan.isc.entity.KuaishouItemCollectSamples;
  13. import com.ruixuan.isc.entity.KuaishouPromoter;
  14. import com.ruixuan.isc.mapper.KuaishouPromoterMapper;
  15. import com.ruixuan.isc.service.IKuaishouItemCollectSamplesService;
  16. import com.ruixuan.isc.service.IKuaishouPromoterService;
  17. import com.ruixuan.system.service.ISysDeptService;
  18. import com.ruixuan.system.service.ISysRoleService;
  19. import com.ruixuan.system.service.ISysUserService;
  20. import lombok.extern.slf4j.Slf4j;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.beans.factory.annotation.Value;
  23. import org.springframework.stereotype.Service;
  24. import java.util.ArrayList;
  25. import java.util.Arrays;
  26. import java.util.Collections;
  27. import java.util.Date;
  28. import java.util.HashMap;
  29. import java.util.List;
  30. import java.util.Map;
  31. /**
  32. * 快手达人 信息Service业务层处理
  33. *
  34. * @author ruoyi
  35. * @date 2023-02-03
  36. */
  37. @Slf4j
  38. @Service
  39. public class KuaishouPromoterServiceImpl implements IKuaishouPromoterService {
  40. @Value("${promoter.start_ip_path}")
  41. private String startIpPath;
  42. @Value("${promoter.internal_ip_path}")
  43. private String internalIpPath;
  44. @Autowired
  45. private ISysUserService sysUserService;
  46. @Autowired
  47. private KuaishouPromoterMapper kuaishouPromoterMapper;
  48. @Autowired
  49. private ISysDeptService deptService;
  50. @Autowired
  51. private ISysRoleService roleService;
  52. @Autowired
  53. private RedisUtil redisUtil;
  54. @Autowired
  55. private IKuaishouItemCollectSamplesService kuaishouItemCollectSamplesService;
  56. /**
  57. * 查询快手达人 信息列表
  58. *
  59. * @param kuaishouPromoter 快手达人 信息
  60. * @return 快手达人 信息
  61. */
  62. @Override
  63. public List<KuaishouPromoter> selectKuaishouPromoterList(Long userId, String promoterId, String promoterNickName, String mediaId, String parameter, String orderBy) {
  64. String roleKey = sysUserService.getRoleKeyByUserId(userId);
  65. List<Long> userList = null;
  66. //角色:供应链管理员、招商、招商经理可以看到所有的达人信息
  67. if (roleKey.contains("courtship") || "supplyChainAdmin".equals(roleKey) || "admin".equals(roleKey)) {
  68. } else if ("bdManager".equals(roleKey) || "associationManager".equals(roleKey)) {
  69. // 渠道经理、社群经理 可以看到自己创建的以及自己部门人员的达人信息
  70. Long deptId = deptService.getDeptIdByUserId(userId);
  71. userList = deptService.getDeptUserListByDeptId(deptId);
  72. } else if ("bd".equals(roleKey)) {
  73. // 渠道可以看到自己创建的达人信息
  74. userList = Arrays.asList(userId);
  75. } else if ("association".equals(roleKey)) {
  76. // 社群可以看到所有社群的达人信息
  77. userList = sysUserService.getUserIdByRoleKey(roleKey);
  78. } else {
  79. return Collections.emptyList();
  80. }
  81. if (Check.isNull(parameter)) {
  82. parameter = "t1.create_time";
  83. } else {
  84. switch (parameter) {
  85. case "avg_video_sales":
  86. parameter = "SUBSTRING_INDEX(t1.avg_video_sales,'-',-1)+0";
  87. break;
  88. case "video_sales":
  89. parameter = "SUBSTRING_INDEX(t1.video_sales,'-',1)+0";
  90. break;
  91. case "total_sale":
  92. parameter = "SUBSTRING_INDEX(t1.total_sale,'-',-1)+0";
  93. break;
  94. }
  95. }
  96. if (Check.isNull(orderBy)) {
  97. orderBy = "desc";
  98. }
  99. PageUtils.startPage();
  100. List<KuaishouPromoter> list = kuaishouPromoterMapper.selectKuaishouPromoterList(userList, promoterId, promoterNickName, mediaId, parameter, orderBy);
  101. list.forEach(ent -> {
  102. JSONObject follow = kuaishouPromoterMapper.getFollowUpRecordsByUserId(ent.getPromoterId(), ent.getUserId());
  103. ent.setFollowUpRecord(Check.isNull(follow) ? "-" : follow.getString("txt"));
  104. List<JSONObject> promoterLabelList = kuaishouPromoterMapper.getPromoterLabel(ent.getPromoterId(), ent.getUserId());
  105. ent.setLabels(promoterLabelList);
  106. });
  107. return list;
  108. }
  109. @Override
  110. public List<JSONObject> selectKuaishouPromoterList2(Long userId, String promoterId, String promoterNickName, String status, String parameter, String orderBy) {
  111. if (Check.isNull(parameter)) {
  112. parameter = "promoterId";
  113. } else if ("avgVideoSales".equals(parameter)) {
  114. parameter = "SUBSTRING_INDEX(avgVideoSales,'-',-1)+0";
  115. } else if ("videoSales".equals(parameter)) {
  116. parameter = "SUBSTRING_INDEX(videoSales,'-',-1)+0";
  117. }
  118. if (Check.isNull(orderBy)) {
  119. orderBy = "desc";
  120. }
  121. PageUtils.startPage();
  122. List<JSONObject> list = kuaishouPromoterMapper.selectAllPromoterList(userId, promoterId, promoterNickName, status, parameter, orderBy);
  123. return list;
  124. }
  125. /**
  126. * 查询快手达人 信息
  127. *
  128. * @param id 快手达人 信息主键
  129. * @return 快手达人 信息
  130. */
  131. @Override
  132. public JSONObject selectKuaishouPromoterById(Long id) {
  133. JSONObject data = new JSONObject();
  134. JSONObject result = null;
  135. try {
  136. KuaishouPromoter promoter = kuaishouPromoterMapper.selectKuaishouPromoterById(id);
  137. if (Check.isNotNull(promoter)) {
  138. Thread thread = new Thread() {
  139. @Override
  140. public void run() {
  141. try {
  142. editPromoter(promoter.getId(), promoter.getMediaId(), promoter.getPromoterId(), true);
  143. } catch (Exception e) {
  144. e.printStackTrace();
  145. }
  146. }
  147. };
  148. thread.start();
  149. Long startTime = System.currentTimeMillis();
  150. String content = HttpUtil.httpGetRequest(internalIpPath + "kuaiShou/promoter/getPromoterInfo?promoterId=" + promoter.getPromoterId());
  151. result = JSONObject.parseObject(content);
  152. data.put("promoterInfo", promoter);
  153. data.put("result", result);
  154. //跟进记录
  155. JSONObject followUpRecord = kuaishouPromoterMapper.getFollowUpRecordsByUserId(promoter.getPromoterId(), promoter.getUserId());
  156. data.put("followUpRecord", followUpRecord);
  157. Long followUpRecordId = null;
  158. if (Check.isNotNull(followUpRecord)) {
  159. followUpRecordId = followUpRecord.getLong("id");
  160. }
  161. List<JSONObject> followUpRecordList = kuaishouPromoterMapper.getFollowUpRecordListByPromoterId(promoter.getPromoterId(), followUpRecordId);
  162. data.put("historyFollowUpRecord", followUpRecordList);
  163. //标签
  164. List<JSONObject> promoterLabelList = kuaishouPromoterMapper.getPromoterLabel(promoter.getPromoterId(), promoter.getUserId());
  165. data.put("promoterLabelList", promoterLabelList);
  166. }
  167. } catch (Exception e) {
  168. e.printStackTrace();
  169. log.error("获取达人详情数据异常,{}", result);
  170. }
  171. return data;
  172. }
  173. /**
  174. * 新增快手达人 信息
  175. *
  176. * @param kuaishouPromoter 快手达人 信息
  177. */
  178. @Override
  179. public JSONObject insertKuaishouPromoter(KuaishouPromoter promoter) {
  180. JSONObject result = new JSONObject();
  181. promoter.setPromoterId(promoter.getPromoterId().trim());
  182. KuaishouPromoter one = kuaishouPromoterMapper.getOneByIdAndPromoterId(promoter.getUserId(), promoter.getPromoterId());
  183. if (Check.isNotNull(one)) {
  184. result.put("code", 500);
  185. result.put("msg", "该数据已存在,请重新输入");
  186. return result;
  187. }
  188. if ("2".equals(promoter.getMediaId())) {
  189. // 快手 同一达人ID 不能绑定到多个渠道角色用户上
  190. String roleKey = sysUserService.getRoleKeyByUserId(promoter.getUserId());
  191. if (roleKey.contains("bd")) {
  192. List<JSONObject> keys = kuaishouPromoterMapper.getBoundPromoterRoleKey(promoter.getPromoterId());
  193. if (Check.isNotNull(keys)) {
  194. for (JSONObject key : keys) {
  195. String rKey = key.getString("role_key");
  196. if ("bd".equals(rKey) || "bdManager".equals(rKey)) {
  197. result.put("code", 500);
  198. result.put("msg", "该达人已被其他渠道人员绑定,绑定人:" + key.getString("user_name"));
  199. return result;
  200. }
  201. }
  202. }
  203. }
  204. }
  205. //查询 设置可绑定达人数
  206. Integer count = kuaishouPromoterMapper.getUserPromotersCount(promoter.getUserId());
  207. if (Check.isNotNull(count)) {
  208. //查询 已绑定达人数
  209. Integer hCount = kuaishouPromoterMapper.getBoundCount(promoter.getUserId());
  210. if (Check.isNotNull(hCount) && hCount >= count) {
  211. result.put("code", 500);
  212. result.put("msg", "绑定达人数过多,暂无法绑定!");
  213. return result;
  214. }
  215. }
  216. SysUser sysUser = sysUserService.selectUserById(promoter.getUserId());
  217. if (Check.isNotNull(sysUser)) {
  218. promoter.setUserName(sysUser.getNickName());
  219. }
  220. kuaishouPromoterMapper.insertKuaishouPromoter(promoter);
  221. editPromoter(promoter.getId(), promoter.getMediaId(), promoter.getPromoterId(), true);
  222. result.put("code", 200);
  223. result.put("msg", "success");
  224. return result;
  225. }
  226. /**
  227. * 同步更新数据
  228. */
  229. private void editPromoter(Long id, String mediaId, String promoterId, boolean flag) {
  230. try {
  231. if (flag) {
  232. /*缓存标记,每日第一次访问调用该接口,更新达人部分(昵称,省市,粉丝,总销售,头像等)数据*/
  233. String key = DateUtils.getDate() + "_" + id + "_" + promoterId;
  234. String value = (String) redisUtil.get(key);
  235. if (Check.isNotNull(value) && "1".equals(value)) {
  236. return;
  237. }
  238. redisUtil.set(key, "1", 60 * 60 * 24);
  239. }
  240. /*更新达人数据*/
  241. Map<String, Object> param = new HashMap<>();
  242. param.put("promoterId", promoterId);
  243. param.put("mediaId", mediaId);
  244. Long startTime = System.currentTimeMillis();
  245. String content = "";
  246. if ("2".equals(mediaId)) {
  247. content = HttpUtil.httpPostRequest(startIpPath + "promoterInfo/getPromoterId", param, null);
  248. Thread.sleep(3000);
  249. } else {
  250. content = HttpUtil.httpPostRequest(startIpPath + "promoterInfo/getPromoterId", param, null);
  251. Thread.sleep(60000);
  252. }
  253. JSONObject result = JSONObject.parseObject(content);
  254. if (Check.isNotNull(result) && Check.isNotNull(result.getString("promoterNickName"))) {
  255. String province = "";
  256. if (Check.isNotNull(result.getString("province"))) {
  257. province = result.getString("province").concat("-").concat(result.getString("city"));
  258. }
  259. KuaishouPromoter kuaishouPromoter = new KuaishouPromoter(
  260. id,
  261. result.getString("promoterNickName"),
  262. province,
  263. result.getString("promoterHeadImgUrl"),
  264. result.getString("fanNum"),
  265. result.getString("avgVideoSales"),
  266. result.getString("videoSales"),
  267. result.getString("totalSale"),
  268. promoterId
  269. );
  270. if ("1".equals(mediaId)) {
  271. String followerincr = result.getString("follower_incr");
  272. if (Check.isNotNull(followerincr) && !"-".equals(followerincr)) {
  273. kuaishouPromoter.setFollowerIncr(result.getLong("follower_incr"));
  274. } else {
  275. kuaishouPromoter.setFollowerIncr(0l);
  276. }
  277. kuaishouPromoter.setLiveCount30(result.getInteger("live_count_30"));
  278. kuaishouPromoter.setLiveAverageUser30(result.getString("live_average_user_30"));
  279. kuaishouPromoter.setAwemeDiggFollowerRation(result.getString("aweme_digg_follower_ration"));
  280. kuaishouPromoter.setAwemeAvgDiggCount30(result.getLong("aweme_avg_digg_count_30"));
  281. }
  282. kuaishouPromoterMapper.updateKuaishouPromoterByPromoterId(kuaishouPromoter);
  283. } else {
  284. log.error("更新达人失败,达人ID:{} 【返回结果】:{}", promoterId, result);
  285. }
  286. } catch (Exception e) {
  287. e.printStackTrace();
  288. log.error("更新达人信息异常", e);
  289. }
  290. }
  291. @Override
  292. public Result supplementInfo() {
  293. List<KuaishouPromoter> list = kuaishouPromoterMapper.getFailInfo();
  294. Thread thread = new Thread() {
  295. @Override
  296. public void run() {
  297. try {
  298. list.forEach(kuaishouPromoter -> editPromoter(kuaishouPromoter.getId(), kuaishouPromoter.getMediaId(), kuaishouPromoter.getPromoterId(), false));
  299. } catch (Exception e) {
  300. e.printStackTrace();
  301. }
  302. }
  303. };
  304. thread.start();
  305. List<String> promoters = new ArrayList<>();
  306. list.forEach(kuaishouPromoter -> promoters.add(kuaishouPromoter.getPromoterId() + "【" + kuaishouPromoter.getMediaId() + "】"));
  307. return Result.success("共计" + list.size() + "条,数据补充中...", promoters);
  308. }
  309. @Override
  310. public int addFollowUpRecords(JSONObject result) {
  311. return kuaishouPromoterMapper.addFollowUpRecords(result);
  312. }
  313. @Override
  314. public void updateFollowUpRecords(JSONObject result) {
  315. kuaishouPromoterMapper.updateFollowUpRecords(result);
  316. }
  317. @Override
  318. public Result getGongHaiVideoSales(String promoterId) {
  319. Map<String, Object> param = new HashMap<>();
  320. param.put("promoterId", promoterId);
  321. String url = startIpPath + "promoterInfo/updatePromoter";
  322. String content = HttpUtil.httpPostRequest(url, param, null);
  323. if ("0".equals(content)) {
  324. return Result.success();
  325. } else {
  326. log.error("查询达人公海销售额失败,达人ID:{}\nurl:{}", promoterId, url);
  327. return Result.error("失败");
  328. }
  329. }
  330. @Override
  331. public KuaishouPromoter getOnlyPromoterInfoByUserId(String promoterId, Long userId) {
  332. return kuaishouPromoterMapper.getOnlyPromoterInfoByUserId(promoterId, userId);
  333. }
  334. @Override
  335. public JSONObject addPromotersCount(Long userId, Long promotersCount) {
  336. kuaishouPromoterMapper.addPromotersCount(userId, promotersCount);
  337. JSONObject result = new JSONObject();
  338. result.put("code", 200);
  339. result.put("msg", "success");
  340. return result;
  341. }
  342. @Override
  343. public Result updatePromoterLabel(JSONObject result) {
  344. String promoterId = result.getString("promoterId");
  345. Long userId = result.getLong("userId");
  346. kuaishouPromoterMapper.deletePromoterLabel(promoterId, userId);
  347. JSONArray ids = result.getJSONArray("ids");
  348. if (Check.isNotNull(ids)) {
  349. List<JSONObject> list = new ArrayList<>();
  350. for (int i = 0; i < ids.size(); i++) {
  351. JSONObject obj = new JSONObject();
  352. Long labelId = ids.getLong(i);
  353. obj.put("promoterId", promoterId);
  354. obj.put("labelId", labelId);
  355. obj.put("userId", userId);
  356. list.add(obj);
  357. }
  358. kuaishouPromoterMapper.insertPromoterLabels(list);
  359. }
  360. return Result.success();
  361. }
  362. /**
  363. * 修改快手达人 信息
  364. *
  365. * @param kuaishouPromoter 快手达人 信息
  366. * @return 结果
  367. */
  368. @Override
  369. public int updateKuaishouPromoter(KuaishouPromoter kuaishouPromoter) {
  370. return kuaishouPromoterMapper.updateKuaishouPromoter(kuaishouPromoter);
  371. }
  372. /**
  373. * 删除快手达人 信息信息
  374. *
  375. * @param id 快手达人 信息主键 follow_up_records
  376. * @return 结果
  377. */
  378. @Override
  379. public Result deleteKuaishouPromoterById(Long id, Long userId) {
  380. KuaishouPromoter promoter = kuaishouPromoterMapper.selectKuaishouPromoterById(id);
  381. List<KuaishouItemCollectSamples> list = kuaishouItemCollectSamplesService.getListByPromoterId(promoter.getPromoterId(), userId);
  382. for (KuaishouItemCollectSamples item : list) {
  383. if (item.getCollectSampleStatus() != 2 && item.getCollectSampleStatus() != 7) {
  384. return Result.error("该达人还有未完成的领样申请,请在完成后进行解绑!");
  385. }
  386. }
  387. if (Check.isNotNull(promoter)) {
  388. JSONObject record = new JSONObject();
  389. record.put("promoterId", promoter.getPromoterId());
  390. record.put("userId", promoter.getUserId());
  391. record.put("operatorId", userId);
  392. kuaishouPromoterMapper.insertKuaishouPromoterRecord(record);
  393. }
  394. kuaishouPromoterMapper.deleteKuaishouPromoterById(id);
  395. return Result.success();
  396. }
  397. @Override
  398. public AjaxResult getPromoterInfo(String promoterId) {
  399. AjaxResult ajaxResult = null;
  400. try {
  401. KuaishouPromoter promoter = kuaishouPromoterMapper.getOneByIdAndPromoterId(null, promoterId);
  402. JSONObject data = new JSONObject();
  403. if (Check.isNotNull(promoter)) {
  404. Thread thread = new Thread() {
  405. @Override
  406. public void run() {
  407. try {
  408. editPromoter(promoter.getId(), promoter.getMediaId(), promoter.getPromoterId(), true);
  409. } catch (Exception e) {
  410. e.printStackTrace();
  411. }
  412. }
  413. };
  414. thread.start();
  415. Long startTime = System.currentTimeMillis();
  416. String content = HttpUtil.httpGetRequest(internalIpPath + "kuaiShou/promoter/getPromoterVideoInfo?promoterId=" + promoter.getPromoterId());
  417. JSONObject result = JSONObject.parseObject(content);
  418. log.info("获取达人视频销售数据,用时:{}s", (System.currentTimeMillis() - startTime) / 1000);
  419. String start = DateUtils.getSubtractTime(new Date(), 31);
  420. String end = DateUtils.getDate();
  421. JSONObject info = kuaishouPromoterMapper.getNearlyMonthGmv(promoter.getPromoterId(), start, end);
  422. if (Check.isNotNull(info)) {
  423. data.put("orderAmount", info.getString("orderAmount"));
  424. data.put("operationCompletionRate", info.getString("operationCompletionRate"));
  425. }
  426. if (Check.isNotNull(result)) {
  427. data.put("avgVideoSales", result.getString("avgVideoSales"));
  428. data.put("videoSales", result.getString("videoSales"));
  429. }
  430. data.put("promoterId", promoter.getPromoterId());
  431. data.put("promoterUrl", promoter.getPromoterUrl());
  432. }
  433. ajaxResult = new AjaxResult(200, "查询成功", data);
  434. } catch (Exception e) {
  435. e.printStackTrace();
  436. log.error("获取达人详情数据异常");
  437. ajaxResult = new AjaxResult(500, "查询失败");
  438. }
  439. return ajaxResult;
  440. }
  441. @Override
  442. public KuaishouPromoter getOnlyPromoterInfo(String promoterId) {
  443. return kuaishouPromoterMapper.getOneByIdAndPromoterId(null, promoterId);
  444. }
  445. @Override
  446. public List<String> selectPromoterIdList(List<Long> userList) {
  447. return kuaishouPromoterMapper.selectPromoterIdList(userList);
  448. }
  449. @Override
  450. public JSONObject getgetMonthPromoterTotal(List<Long> userList, String start, String end) {
  451. return kuaishouPromoterMapper.getgetMonthPromoterTotal(userList, start, end);
  452. }
  453. }