KuaishouPromoterServiceImpl.java 21 KB

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