KuaishouPromoterServiceImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. package com.ruixuan.isc.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.ruixuan.common.core.domain.AjaxResult;
  4. import com.ruixuan.common.core.domain.entity.SysUser;
  5. import com.ruixuan.common.utils.Check;
  6. import com.ruixuan.common.utils.DateUtils;
  7. import com.ruixuan.common.utils.PageUtils;
  8. import com.ruixuan.common.utils.RedisUtil;
  9. import com.ruixuan.common.utils.http.HttpUtil;
  10. import com.ruixuan.isc.entity.KuaishouPromoter;
  11. import com.ruixuan.isc.mapper.KuaishouPromoterMapper;
  12. import com.ruixuan.isc.service.IKuaishouPromoterService;
  13. import com.ruixuan.system.service.ISysDeptService;
  14. import com.ruixuan.system.service.ISysRoleService;
  15. import com.ruixuan.system.service.ISysUserService;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.beans.factory.annotation.Value;
  19. import org.springframework.stereotype.Service;
  20. import java.util.Arrays;
  21. import java.util.Collections;
  22. import java.util.Date;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. /**
  27. * 快手达人 信息Service业务层处理
  28. *
  29. * @author ruoyi
  30. * @date 2023-02-03
  31. */
  32. @Slf4j
  33. @Service
  34. public class KuaishouPromoterServiceImpl implements IKuaishouPromoterService {
  35. @Value("${promoter.start_ip_path}")
  36. private String startIpPath;
  37. @Value("${promoter.internal_ip_path}")
  38. private String internalIpPath;
  39. @Autowired
  40. private ISysUserService sysUserService;
  41. @Autowired
  42. private KuaishouPromoterMapper kuaishouPromoterMapper;
  43. @Autowired
  44. private ISysDeptService deptService;
  45. @Autowired
  46. private ISysRoleService roleService;
  47. @Autowired
  48. private RedisUtil redisUtil;
  49. /**
  50. * 查询快手达人 信息列表
  51. *
  52. * @param kuaishouPromoter 快手达人 信息
  53. * @return 快手达人 信息
  54. */
  55. @Override
  56. public List<KuaishouPromoter> selectKuaishouPromoterList(Long userId, Long promoterId, String promoterNickName, String parameter, String orderBy) {
  57. String roleKey = sysUserService.getRoleKeyByUserId(userId);
  58. List<Long> userList = null;
  59. //角色:供应链管理员、招商、招商经理可以看到所有的达人信息
  60. if (roleKey.contains("courtship") || "supplyChainAdmin".equals(roleKey) || "admin".equals(roleKey)) {
  61. } else if ("bdManager".equals(roleKey) || "associationManager".equals(roleKey)) {
  62. // 渠道经理、社群经理 可以看到自己创建的以及自己部门人员的达人信息
  63. Long deptId = deptService.getDeptIdByUserId(userId);
  64. userList = deptService.getDeptUserListByDeptId(deptId);
  65. } else if ("bd".equals(roleKey)) {
  66. // 渠道可以看到自己创建的达人信息
  67. userList = Arrays.asList(userId);
  68. } else if ("association".equals(roleKey)) {
  69. // 社群可以看到所有社群的达人信息
  70. userList = sysUserService.getUserIdByRoleKey(roleKey);
  71. } else {
  72. return Collections.emptyList();
  73. }
  74. if (Check.isNull(parameter)) {
  75. parameter = "t1.create_time";
  76. }
  77. if (Check.isNull(orderBy)) {
  78. orderBy = "desc";
  79. }
  80. PageUtils.startPage();
  81. List<KuaishouPromoter> list = kuaishouPromoterMapper.selectKuaishouPromoterList(userList, promoterId, promoterNickName, parameter, orderBy);
  82. return list;
  83. }
  84. @Override
  85. public List<KuaishouPromoter> selectKuaishouPromoterList2(Long promoterId, String promoterNickName, String parameter, String orderBy) {
  86. if (Check.isNull(parameter)) {
  87. parameter = "t1.create_time";
  88. }
  89. if (Check.isNull(orderBy)) {
  90. orderBy = "desc";
  91. }
  92. PageUtils.startPage();
  93. List<KuaishouPromoter> list = kuaishouPromoterMapper.selectAllPromoterList(promoterId, promoterNickName, parameter, orderBy);
  94. return list;
  95. }
  96. /**
  97. * 查询快手达人 信息
  98. *
  99. * @param id 快手达人 信息主键
  100. * @return 快手达人 信息
  101. */
  102. @Override
  103. public JSONObject selectKuaishouPromoterById(Long id) {
  104. JSONObject data = new JSONObject();
  105. JSONObject result = null;
  106. try {
  107. KuaishouPromoter promoter = kuaishouPromoterMapper.selectKuaishouPromoterById(id);
  108. if (Check.isNotNull(promoter)) {
  109. Thread thread = new Thread() {
  110. @Override
  111. public void run() {
  112. try {
  113. editPromoter(promoter.getId(), promoter.getPromoterId(), true);
  114. } catch (Exception e) {
  115. e.printStackTrace();
  116. }
  117. }
  118. };
  119. thread.start();
  120. Long startTime = System.currentTimeMillis();
  121. String content = HttpUtil.httpGetRequest(internalIpPath + "kuaiShou/promoter/getPromoterInfo?promoterId=" + promoter.getPromoterId());
  122. result = JSONObject.parseObject(content);
  123. log.info("获取达人详情数据,用时:{}s", (System.currentTimeMillis() - startTime) / 1000);
  124. data.put("promoterInfo", promoter);
  125. data.put("result", result);
  126. }
  127. } catch (Exception e) {
  128. e.printStackTrace();
  129. log.error("获取达人详情数据异常,{}", result);
  130. }
  131. return data;
  132. }
  133. /**
  134. * 新增快手达人 信息
  135. *
  136. * @param kuaishouPromoter 快手达人 信息
  137. */
  138. @Override
  139. public JSONObject insertKuaishouPromoter(KuaishouPromoter promoter) {
  140. JSONObject result = new JSONObject();
  141. KuaishouPromoter one = kuaishouPromoterMapper.getOneByIdAndPromoterId(promoter.getUserId(), promoter.getPromoterId());
  142. if (Check.isNotNull(one)) {
  143. result.put("code", 500);
  144. result.put("msg", "该数据已存在,请重新输入");
  145. return result;
  146. }
  147. /*//同一达人ID 不能绑定到多个渠道角色用户上
  148. String roleKey = sysUserService.getRoleKeyByUserId(promoter.getUserId());
  149. if (roleKey.contains("bd")) {
  150. List<JSONObject> keys = kuaishouPromoterMapper.getBoundPromoterRoleKey(promoter.getPromoterId());
  151. if (Check.isNotNull(keys)) {
  152. for (JSONObject key : keys) {
  153. String rKey = key.getString("role_key");
  154. if ("bd".equals(rKey) || "bdManager".equals(rKey)) {
  155. result.put("code", 500);
  156. result.put("msg", "该达人已被其他渠道人员绑定,绑定人:" + key.getString("user_name"));
  157. return result;
  158. }
  159. }
  160. }
  161. }*/
  162. SysUser sysUser = sysUserService.selectUserById(promoter.getUserId());
  163. if (Check.isNotNull(sysUser)) {
  164. promoter.setUserName(sysUser.getNickName());
  165. }
  166. kuaishouPromoterMapper.insertKuaishouPromoter(promoter);
  167. editPromoter(promoter.getId(), promoter.getPromoterId(), true);
  168. result.put("code", 200);
  169. result.put("msg", "success");
  170. return result;
  171. }
  172. /**
  173. * 同步更新数据
  174. */
  175. private void editPromoter(Long id, Long promoterId, boolean flag) {
  176. try {
  177. if (flag) {
  178. /*缓存标记,每日第一次访问调用该接口,更新达人部分(昵称,省市,粉丝,总销售,头像等)数据*/
  179. String key = DateUtils.getDate() + "_" + id + "_" + promoterId;
  180. String value = (String) redisUtil.get(key);
  181. if (Check.isNotNull(value) && "1".equals(value)) {
  182. return;
  183. }
  184. redisUtil.set(key, "1", 60 * 60 * 24);
  185. }
  186. /*更新达人数据*/
  187. Map<String, Object> param = new HashMap<>();
  188. param.put("promoterId", promoterId);
  189. Long startTime = System.currentTimeMillis();
  190. String content = HttpUtil.httpPostRequest(startIpPath + "promoterInfo/getPromoterId", param, null);
  191. JSONObject result = JSONObject.parseObject(content);
  192. log.info("更新达人信息,用时:{}s,返回结果:{}", (System.currentTimeMillis() - startTime) / 1000, result);
  193. if (Check.isNotNull(result) && Check.isNotNull(result.getString("promoterNickName"))) {
  194. KuaishouPromoter kuaishouPromoter = new KuaishouPromoter(
  195. id,
  196. result.getString("promoterNickName"),
  197. result.getString("province").concat("-").concat(result.getString("city")),
  198. result.getString("promoterHeadImgUrl"),
  199. result.getString("fanNum"),
  200. result.getString("avgVideoSales"),
  201. result.getString("videoSales"),
  202. result.getString("totalSale")
  203. );
  204. kuaishouPromoterMapper.updateKuaishouPromoter(kuaishouPromoter);
  205. }
  206. } catch (Exception e) {
  207. e.printStackTrace();
  208. log.error("更新达人信息异常", e);
  209. }
  210. }
  211. @Override
  212. public String supplementInfo() {
  213. List<KuaishouPromoter> list = kuaishouPromoterMapper.getFailInfo();
  214. Thread thread = new Thread() {
  215. @Override
  216. public void run() {
  217. try {
  218. list.forEach(kuaishouPromoter -> editPromoter(kuaishouPromoter.getId(), kuaishouPromoter.getPromoterId(), false));
  219. } catch (Exception e) {
  220. e.printStackTrace();
  221. }
  222. }
  223. };
  224. thread.start();
  225. return "共计" + list.size() + "条,数据补充中...";
  226. }
  227. /**
  228. * 修改快手达人 信息
  229. *
  230. * @param kuaishouPromoter 快手达人 信息
  231. * @return 结果
  232. */
  233. @Override
  234. public int updateKuaishouPromoter(KuaishouPromoter kuaishouPromoter) {
  235. return kuaishouPromoterMapper.updateKuaishouPromoter(kuaishouPromoter);
  236. }
  237. /**
  238. * 删除快手达人 信息信息
  239. *
  240. * @param id 快手达人 信息主键
  241. * @return 结果
  242. */
  243. @Override
  244. public int deleteKuaishouPromoterById(Long id, Long userId) {
  245. KuaishouPromoter promoter = kuaishouPromoterMapper.selectKuaishouPromoterById(id);
  246. if (Check.isNotNull(promoter)) {
  247. JSONObject record = new JSONObject();
  248. record.put("promoterId", promoter.getPromoterId());
  249. record.put("userId", promoter.getUserId());
  250. record.put("operatorId", userId);
  251. kuaishouPromoterMapper.insertKuaishouPromoterRecord(record);
  252. }
  253. return kuaishouPromoterMapper.deleteKuaishouPromoterById(id);
  254. }
  255. @Override
  256. public AjaxResult getPromoterInfo(Long promoterId) {
  257. AjaxResult ajaxResult = null;
  258. try {
  259. KuaishouPromoter promoter = kuaishouPromoterMapper.getOneByIdAndPromoterId(null, promoterId);
  260. JSONObject data = new JSONObject();
  261. if (Check.isNotNull(promoter)) {
  262. Thread thread = new Thread() {
  263. @Override
  264. public void run() {
  265. try {
  266. editPromoter(promoter.getId(), promoter.getPromoterId(), true);
  267. } catch (Exception e) {
  268. e.printStackTrace();
  269. }
  270. }
  271. };
  272. thread.start();
  273. Long startTime = System.currentTimeMillis();
  274. String content = HttpUtil.httpGetRequest(internalIpPath + "kuaiShou/promoter/getPromoterVideoInfo?promoterId=" + promoter.getPromoterId());
  275. JSONObject result = JSONObject.parseObject(content);
  276. log.info("获取达人视频销售数据,用时:{}s", (System.currentTimeMillis() - startTime) / 1000);
  277. String start = DateUtils.getSubtractTime(new Date(), 31);
  278. String end = DateUtils.getDate();
  279. JSONObject info = kuaishouPromoterMapper.getNearlyMonthGmv(promoter.getPromoterId(), start, end);
  280. if (Check.isNotNull(info)) {
  281. data.put("orderAmount", info.getString("orderAmount"));
  282. data.put("operationCompletionRate", info.getString("operationCompletionRate"));
  283. }
  284. if (Check.isNotNull(result)) {
  285. data.put("avgVideoSales", result.getString("avgVideoSales"));
  286. data.put("videoSales", result.getString("videoSales"));
  287. }
  288. data.put("promoterId", promoter.getPromoterId());
  289. data.put("promoterUrl", promoter.getPromoterUrl());
  290. }
  291. ajaxResult = new AjaxResult(200, "查询成功", data);
  292. } catch (Exception e) {
  293. e.printStackTrace();
  294. log.error("获取达人详情数据异常");
  295. ajaxResult = new AjaxResult(500, "查询失败");
  296. }
  297. return ajaxResult;
  298. }
  299. @Override
  300. public KuaishouPromoter getOnlyPromoterInfo(Long promoterId) {
  301. return kuaishouPromoterMapper.getOneByIdAndPromoterId(null, promoterId);
  302. }
  303. @Override
  304. public List<Long> selectPromoterIdList(List<Long> userList) {
  305. return kuaishouPromoterMapper.selectPromoterIdList(userList);
  306. }
  307. @Override
  308. public JSONObject getgetMonthPromoterTotal(List<Long> userList, String start, String end) {
  309. return kuaishouPromoterMapper.getgetMonthPromoterTotal(userList, start, end);
  310. }
  311. }