KuaishouPromoterServiceImpl.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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, Long promoterId, String promoterNickName, 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, 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 promoterId, String promoterNickName, String status, String parameter, String orderBy) {
  111. if (Check.isNull(parameter)) {
  112. parameter = "promoterId";
  113. }
  114. if (Check.isNull(orderBy)) {
  115. orderBy = "desc";
  116. }
  117. PageUtils.startPage();
  118. List<JSONObject> list = kuaishouPromoterMapper.selectAllPromoterList(promoterId, promoterNickName, status, parameter, orderBy);
  119. return list;
  120. }
  121. /**
  122. * 查询快手达人 信息
  123. *
  124. * @param id 快手达人 信息主键
  125. * @return 快手达人 信息
  126. */
  127. @Override
  128. public JSONObject selectKuaishouPromoterById(Long id) {
  129. JSONObject data = new JSONObject();
  130. JSONObject result = null;
  131. try {
  132. KuaishouPromoter promoter = kuaishouPromoterMapper.selectKuaishouPromoterById(id);
  133. if (Check.isNotNull(promoter)) {
  134. Thread thread = new Thread() {
  135. @Override
  136. public void run() {
  137. try {
  138. editPromoter(promoter.getId(), promoter.getPromoterId(), true);
  139. } catch (Exception e) {
  140. e.printStackTrace();
  141. }
  142. }
  143. };
  144. thread.start();
  145. Long startTime = System.currentTimeMillis();
  146. String content = HttpUtil.httpGetRequest(internalIpPath + "kuaiShou/promoter/getPromoterInfo?promoterId=" + promoter.getPromoterId());
  147. result = JSONObject.parseObject(content);
  148. log.info("获取达人详情数据,用时:{}s", (System.currentTimeMillis() - startTime) / 1000);
  149. data.put("promoterInfo", promoter);
  150. data.put("result", result);
  151. //跟进记录
  152. JSONObject followUpRecord = kuaishouPromoterMapper.getFollowUpRecordsByUserId(promoter.getPromoterId(), promoter.getUserId());
  153. data.put("followUpRecord", followUpRecord);
  154. Long followUpRecordId = null;
  155. if (Check.isNotNull(followUpRecord)) {
  156. followUpRecordId = followUpRecord.getLong("id");
  157. }
  158. List<JSONObject> followUpRecordList = kuaishouPromoterMapper.getFollowUpRecordListByPromoterId(promoter.getPromoterId(), followUpRecordId);
  159. data.put("historyFollowUpRecord", followUpRecordList);
  160. //标签
  161. List<JSONObject> promoterLabelList = kuaishouPromoterMapper.getPromoterLabel(promoter.getPromoterId(), promoter.getUserId());
  162. data.put("promoterLabelList", promoterLabelList);
  163. }
  164. } catch (Exception e) {
  165. e.printStackTrace();
  166. log.error("获取达人详情数据异常,{}", result);
  167. }
  168. return data;
  169. }
  170. /**
  171. * 新增快手达人 信息
  172. *
  173. * @param kuaishouPromoter 快手达人 信息
  174. */
  175. @Override
  176. public JSONObject insertKuaishouPromoter(KuaishouPromoter promoter) {
  177. JSONObject result = new JSONObject();
  178. KuaishouPromoter one = kuaishouPromoterMapper.getOneByIdAndPromoterId(promoter.getUserId(), promoter.getPromoterId());
  179. if (Check.isNotNull(one)) {
  180. result.put("code", 500);
  181. result.put("msg", "该数据已存在,请重新输入");
  182. return result;
  183. }
  184. /*//同一达人ID 不能绑定到多个渠道角色用户上
  185. String roleKey = sysUserService.getRoleKeyByUserId(promoter.getUserId());
  186. if (roleKey.contains("bd")) {
  187. List<JSONObject> keys = kuaishouPromoterMapper.getBoundPromoterRoleKey(promoter.getPromoterId());
  188. if (Check.isNotNull(keys)) {
  189. for (JSONObject key : keys) {
  190. String rKey = key.getString("role_key");
  191. if ("bd".equals(rKey) || "bdManager".equals(rKey)) {
  192. result.put("code", 500);
  193. result.put("msg", "该达人已被其他渠道人员绑定,绑定人:" + key.getString("user_name"));
  194. return result;
  195. }
  196. }
  197. }
  198. }*/
  199. SysUser sysUser = sysUserService.selectUserById(promoter.getUserId());
  200. if (Check.isNotNull(sysUser)) {
  201. promoter.setUserName(sysUser.getNickName());
  202. }
  203. kuaishouPromoterMapper.insertKuaishouPromoter(promoter);
  204. editPromoter(promoter.getId(), promoter.getPromoterId(), true);
  205. result.put("code", 200);
  206. result.put("msg", "success");
  207. return result;
  208. }
  209. /**
  210. * 同步更新数据
  211. */
  212. private void editPromoter(Long id, Long promoterId, boolean flag) {
  213. try {
  214. if (flag) {
  215. /*缓存标记,每日第一次访问调用该接口,更新达人部分(昵称,省市,粉丝,总销售,头像等)数据*/
  216. String key = DateUtils.getDate() + "_" + id + "_" + promoterId;
  217. String value = (String) redisUtil.get(key);
  218. if (Check.isNotNull(value) && "1".equals(value)) {
  219. return;
  220. }
  221. redisUtil.set(key, "1", 60 * 60 * 24);
  222. }
  223. /*更新达人数据*/
  224. Map<String, Object> param = new HashMap<>();
  225. param.put("promoterId", promoterId);
  226. Long startTime = System.currentTimeMillis();
  227. String content = HttpUtil.httpPostRequest(startIpPath + "promoterInfo/getPromoterId", param, null);
  228. JSONObject result = JSONObject.parseObject(content);
  229. log.info("更新达人信息,用时:{}s,返回结果:{}", (System.currentTimeMillis() - startTime) / 1000, result);
  230. if (Check.isNotNull(result) && Check.isNotNull(result.getString("promoterNickName"))) {
  231. KuaishouPromoter kuaishouPromoter = new KuaishouPromoter(
  232. id,
  233. result.getString("promoterNickName"),
  234. result.getString("province").concat("-").concat(result.getString("city")),
  235. result.getString("promoterHeadImgUrl"),
  236. result.getString("fanNum"),
  237. result.getString("avgVideoSales"),
  238. result.getString("videoSales"),
  239. result.getString("totalSale")
  240. );
  241. kuaishouPromoterMapper.updateKuaishouPromoter(kuaishouPromoter);
  242. }
  243. } catch (Exception e) {
  244. e.printStackTrace();
  245. log.error("更新达人信息异常", e);
  246. }
  247. }
  248. @Override
  249. public String supplementInfo() {
  250. List<KuaishouPromoter> list = kuaishouPromoterMapper.getFailInfo();
  251. Thread thread = new Thread() {
  252. @Override
  253. public void run() {
  254. try {
  255. list.forEach(kuaishouPromoter -> editPromoter(kuaishouPromoter.getId(), kuaishouPromoter.getPromoterId(), false));
  256. } catch (Exception e) {
  257. e.printStackTrace();
  258. }
  259. }
  260. };
  261. thread.start();
  262. return "共计" + list.size() + "条,数据补充中...";
  263. }
  264. @Override
  265. public int addFollowUpRecords(JSONObject result) {
  266. return kuaishouPromoterMapper.addFollowUpRecords(result);
  267. }
  268. @Override
  269. public void updateFollowUpRecords(JSONObject result) {
  270. kuaishouPromoterMapper.updateFollowUpRecords(result);
  271. }
  272. @Override
  273. public Result updatePromoterLabel(JSONObject result) {
  274. Long promoterId = result.getLong("promoterId");
  275. Long userId = result.getLong("userId");
  276. kuaishouPromoterMapper.deletePromoterLabel(promoterId, userId);
  277. JSONArray ids = result.getJSONArray("ids");
  278. if (Check.isNotNull(ids)) {
  279. List<JSONObject> list = new ArrayList<>();
  280. for (int i = 0; i < ids.size(); i++) {
  281. JSONObject obj = new JSONObject();
  282. Long labelId = ids.getLong(i);
  283. obj.put("promoterId", promoterId);
  284. obj.put("labelId", labelId);
  285. obj.put("userId", userId);
  286. list.add(obj);
  287. }
  288. kuaishouPromoterMapper.insertPromoterLabels(list);
  289. }
  290. return Result.success();
  291. }
  292. /**
  293. * 修改快手达人 信息
  294. *
  295. * @param kuaishouPromoter 快手达人 信息
  296. * @return 结果
  297. */
  298. @Override
  299. public int updateKuaishouPromoter(KuaishouPromoter kuaishouPromoter) {
  300. return kuaishouPromoterMapper.updateKuaishouPromoter(kuaishouPromoter);
  301. }
  302. /**
  303. * 删除快手达人 信息信息
  304. *
  305. * @param id 快手达人 信息主键 follow_up_records
  306. * @return 结果
  307. */
  308. @Override
  309. public Result deleteKuaishouPromoterById(Long id, Long userId) {
  310. KuaishouPromoter promoter = kuaishouPromoterMapper.selectKuaishouPromoterById(id);
  311. List<KuaishouItemCollectSamples> list = kuaishouItemCollectSamplesService.getListByPromoterId(promoter.getPromoterId());
  312. for (KuaishouItemCollectSamples item : list) {
  313. if (item.getCollectSampleStatus() != 2 && item.getCollectSampleStatus() != 7) {
  314. return Result.error("该达人还有未完成的领样申请,请在完成后进行解绑!");
  315. }
  316. }
  317. if (Check.isNotNull(promoter)) {
  318. JSONObject record = new JSONObject();
  319. record.put("promoterId", promoter.getPromoterId());
  320. record.put("userId", promoter.getUserId());
  321. record.put("operatorId", userId);
  322. kuaishouPromoterMapper.insertKuaishouPromoterRecord(record);
  323. }
  324. kuaishouPromoterMapper.deleteKuaishouPromoterById(id);
  325. return Result.success();
  326. }
  327. @Override
  328. public AjaxResult getPromoterInfo(Long promoterId) {
  329. AjaxResult ajaxResult = null;
  330. try {
  331. KuaishouPromoter promoter = kuaishouPromoterMapper.getOneByIdAndPromoterId(null, promoterId);
  332. JSONObject data = new JSONObject();
  333. if (Check.isNotNull(promoter)) {
  334. Thread thread = new Thread() {
  335. @Override
  336. public void run() {
  337. try {
  338. editPromoter(promoter.getId(), promoter.getPromoterId(), true);
  339. } catch (Exception e) {
  340. e.printStackTrace();
  341. }
  342. }
  343. };
  344. thread.start();
  345. Long startTime = System.currentTimeMillis();
  346. String content = HttpUtil.httpGetRequest(internalIpPath + "kuaiShou/promoter/getPromoterVideoInfo?promoterId=" + promoter.getPromoterId());
  347. JSONObject result = JSONObject.parseObject(content);
  348. log.info("获取达人视频销售数据,用时:{}s", (System.currentTimeMillis() - startTime) / 1000);
  349. String start = DateUtils.getSubtractTime(new Date(), 31);
  350. String end = DateUtils.getDate();
  351. JSONObject info = kuaishouPromoterMapper.getNearlyMonthGmv(promoter.getPromoterId(), start, end);
  352. if (Check.isNotNull(info)) {
  353. data.put("orderAmount", info.getString("orderAmount"));
  354. data.put("operationCompletionRate", info.getString("operationCompletionRate"));
  355. }
  356. if (Check.isNotNull(result)) {
  357. data.put("avgVideoSales", result.getString("avgVideoSales"));
  358. data.put("videoSales", result.getString("videoSales"));
  359. }
  360. data.put("promoterId", promoter.getPromoterId());
  361. data.put("promoterUrl", promoter.getPromoterUrl());
  362. }
  363. ajaxResult = new AjaxResult(200, "查询成功", data);
  364. } catch (Exception e) {
  365. e.printStackTrace();
  366. log.error("获取达人详情数据异常");
  367. ajaxResult = new AjaxResult(500, "查询失败");
  368. }
  369. return ajaxResult;
  370. }
  371. @Override
  372. public KuaishouPromoter getOnlyPromoterInfo(Long promoterId) {
  373. return kuaishouPromoterMapper.getOneByIdAndPromoterId(null, promoterId);
  374. }
  375. @Override
  376. public List<Long> selectPromoterIdList(List<Long> userList) {
  377. return kuaishouPromoterMapper.selectPromoterIdList(userList);
  378. }
  379. @Override
  380. public JSONObject getgetMonthPromoterTotal(List<Long> userList, String start, String end) {
  381. return kuaishouPromoterMapper.getgetMonthPromoterTotal(userList, start, end);
  382. }
  383. }