package com.ruixuan.isc.service.impl; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.ruixuan.common.core.domain.AjaxResult; import com.ruixuan.common.core.domain.entity.SysUser; import com.ruixuan.common.utils.Check; import com.ruixuan.common.utils.DateUtils; import com.ruixuan.common.utils.PageUtils; import com.ruixuan.common.utils.RedisUtil; import com.ruixuan.common.utils.Result; import com.ruixuan.common.utils.http.HttpUtil; import com.ruixuan.isc.entity.KuaishouItemCollectSamples; import com.ruixuan.isc.entity.KuaishouPromoter; import com.ruixuan.isc.mapper.KuaishouPromoterMapper; import com.ruixuan.isc.service.IKuaishouItemCollectSamplesService; import com.ruixuan.isc.service.IKuaishouPromoterService; import com.ruixuan.system.service.ISysDeptService; import com.ruixuan.system.service.ISysRoleService; import com.ruixuan.system.service.ISysUserService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Pattern; /** * 快手达人 信息Service业务层处理 * * @author ruoyi * @date 2023-02-03 */ @Slf4j @Service public class KuaishouPromoterServiceImpl implements IKuaishouPromoterService { @Value("${promoter.start_ip_path}") private String startIpPath; @Value("${promoter.internal_ip_path}") private String internalIpPath; @Autowired private ISysUserService sysUserService; @Autowired private KuaishouPromoterMapper kuaishouPromoterMapper; @Autowired private ISysDeptService deptService; @Autowired private ISysRoleService roleService; @Autowired private RedisUtil redisUtil; @Autowired private IKuaishouItemCollectSamplesService kuaishouItemCollectSamplesService; /** * 查询快手达人 信息列表 * * @param kuaishouPromoter 快手达人 信息 * @return 快手达人 信息 */ @Override public List selectKuaishouPromoterList(Long userId, String promoterId, String promoterNickName, String mediaId, String parameter, String orderBy) { String roleKey = sysUserService.getRoleKeyByUserId(userId); List userList = null; //角色:供应链管理员、招商、招商经理可以看到所有的达人信息 if (roleKey.contains("courtship") || "supplyChainAdmin".equals(roleKey) || "admin".equals(roleKey)) { } else if ("bdManager".equals(roleKey) || "associationManager".equals(roleKey)) { // 渠道经理、社群经理 可以看到自己创建的以及自己部门人员的达人信息 Long deptId = deptService.getDeptIdByUserId(userId); userList = deptService.getDeptUserListByDeptId(deptId); } else if ("bd".equals(roleKey)) { // 渠道可以看到自己创建的达人信息 userList = Arrays.asList(userId); } else if ("association".equals(roleKey)) { // 社群可以看到所有社群的达人信息 userList = sysUserService.getUserIdByRoleKey(roleKey); } else { return Collections.emptyList(); } if (Check.isNull(parameter)) { parameter = "t1.create_time"; } else { switch (parameter) { case "avg_video_sales": parameter = "SUBSTRING_INDEX(t1.avg_video_sales,'-',-1)+0"; break; case "video_sales": parameter = "SUBSTRING_INDEX(t1.video_sales,'-',1)+0"; break; case "total_sale": parameter = "SUBSTRING_INDEX(t1.total_sale,'-',-1)+0"; break; } } if (Check.isNull(orderBy)) { orderBy = "desc"; } PageUtils.startPage(); List list = kuaishouPromoterMapper.selectKuaishouPromoterList(userList, promoterId, promoterNickName, mediaId, parameter, orderBy); list.forEach(ent -> { JSONObject follow = kuaishouPromoterMapper.getFollowUpRecordsByUserId(ent.getPromoterId(), ent.getUserId()); ent.setFollowUpRecord(Check.isNull(follow) ? "-" : follow.getString("txt")); List promoterLabelList = kuaishouPromoterMapper.getPromoterLabel(ent.getPromoterId(), ent.getUserId()); ent.setLabels(promoterLabelList); }); return list; } @Override public List selectKuaishouPromoterList2(Long userId, String promoterId, String promoterNickName, String status, String parameter, String orderBy) { if (Check.isNull(parameter)) { parameter = "promoterId"; } else if ("avgVideoSales".equals(parameter)) { parameter = "SUBSTRING_INDEX(avgVideoSales,'-',-1)+0"; } else if ("videoSales".equals(parameter)) { parameter = "SUBSTRING_INDEX(videoSales,'-',-1)+0"; } if (Check.isNull(orderBy)) { orderBy = "desc"; } PageUtils.startPage(); List list = kuaishouPromoterMapper.selectAllPromoterList(userId, promoterId, promoterNickName, status, parameter, orderBy); return list; } /** * 查询快手达人 信息 * * @param id 快手达人 信息主键 * @return 快手达人 信息 */ @Override public JSONObject selectKuaishouPromoterById(Long id) { JSONObject data = new JSONObject(); JSONObject result = null; try { KuaishouPromoter promoter = kuaishouPromoterMapper.selectKuaishouPromoterById(id); if (Check.isNotNull(promoter)) { Thread thread = new Thread() { @Override public void run() { try { editPromoter(promoter.getId(), promoter.getMediaId(), promoter.getPromoterId(), true); } catch (Exception e) { e.printStackTrace(); } } }; thread.start(); Long startTime = System.currentTimeMillis(); String content = HttpUtil.httpGetRequest(internalIpPath + "kuaiShou/promoter/getPromoterInfo?promoterId=" + promoter.getPromoterId()); result = JSONObject.parseObject(content); data.put("promoterInfo", promoter); data.put("result", result); //跟进记录 JSONObject followUpRecord = kuaishouPromoterMapper.getFollowUpRecordsByUserId(promoter.getPromoterId(), promoter.getUserId()); data.put("followUpRecord", followUpRecord); Long followUpRecordId = null; if (Check.isNotNull(followUpRecord)) { followUpRecordId = followUpRecord.getLong("id"); } List followUpRecordList = kuaishouPromoterMapper.getFollowUpRecordListByPromoterId(promoter.getPromoterId(), followUpRecordId); data.put("historyFollowUpRecord", followUpRecordList); //标签 List promoterLabelList = kuaishouPromoterMapper.getPromoterLabel(promoter.getPromoterId(), promoter.getUserId()); data.put("promoterLabelList", promoterLabelList); } } catch (Exception e) { e.printStackTrace(); log.error("获取达人详情数据异常,{}", result); } return data; } /** * 新增快手达人 信息 * * @param kuaishouPromoter 快手达人 信息 */ @Override public JSONObject insertKuaishouPromoter(KuaishouPromoter promoter) { JSONObject result = new JSONObject(); promoter.setPromoterId(promoter.getPromoterId().trim()); KuaishouPromoter one = kuaishouPromoterMapper.getOneByIdAndPromoterId(promoter.getUserId(), promoter.getPromoterId()); if (Check.isNotNull(one)) { result.put("code", 500); result.put("msg", "该数据已存在,请重新输入"); return result; } // 如果 达人ID是字母 并且 媒体是快手,说明操作失误,媒体改为抖音 if (!isNumeric(promoter.getPromoterId()) && "2".equals(promoter.getMediaId())) { promoter.setMediaId("1"); } if ("2".equals(promoter.getMediaId())) { // 快手 同一达人ID 不能绑定到多个渠道角色用户上 String roleKey = sysUserService.getRoleKeyByUserId(promoter.getUserId()); if (roleKey.contains("bd")) { List keys = kuaishouPromoterMapper.getBoundPromoterRoleKey(promoter.getPromoterId()); if (Check.isNotNull(keys)) { for (JSONObject key : keys) { String rKey = key.getString("role_key"); if ("bd".equals(rKey) || "bdManager".equals(rKey)) { result.put("code", 500); result.put("msg", "该达人已被其他渠道人员绑定,绑定人:" + key.getString("user_name")); return result; } } } } } //查询 设置可绑定达人数 Integer count = kuaishouPromoterMapper.getUserPromotersCount(promoter.getUserId()); if (Check.isNotNull(count)) { //查询 已绑定达人数 Integer hCount = kuaishouPromoterMapper.getBoundCount(promoter.getUserId()); if (Check.isNotNull(hCount) && hCount >= count) { result.put("code", 500); result.put("msg", "绑定达人数过多,暂无法绑定!"); return result; } } SysUser sysUser = sysUserService.selectUserById(promoter.getUserId()); if (Check.isNotNull(sysUser)) { promoter.setUserName(sysUser.getNickName()); } kuaishouPromoterMapper.insertKuaishouPromoter(promoter); editPromoter(promoter.getId(), promoter.getMediaId(), promoter.getPromoterId(), true); result.put("code", 200); result.put("msg", "success"); return result; } /** * 同步更新数据 */ private void editPromoter(Long id, String mediaId, String promoterId, boolean flag) { try { if (flag) { /*缓存标记,每日第一次访问调用该接口,更新达人部分(昵称,省市,粉丝,总销售,头像等)数据*/ String key = DateUtils.getDate() + "_" + id + "_" + promoterId; String value = (String) redisUtil.get(key); if (Check.isNotNull(value) && "1".equals(value)) { return; } redisUtil.set(key, "1", 60 * 60 * 24); } /*更新达人数据*/ Map param = new HashMap<>(); param.put("promoterId", promoterId); param.put("mediaId", mediaId); Long startTime = System.currentTimeMillis(); String content = ""; if ("2".equals(mediaId)) { content = HttpUtil.httpPostRequest(startIpPath + "promoterInfo/getPromoterId", param, null); Thread.sleep(3000); } else { content = HttpUtil.httpPostRequest(startIpPath + "promoterInfo/getPromoterId", param, null); Thread.sleep(60000); } JSONObject result = JSONObject.parseObject(content); if (Check.isNotNull(result) && Check.isNotNull(result.getString("promoterNickName"))) { String province = ""; if (Check.isNotNull(result.getString("province"))) { province = result.getString("province").concat("-").concat(result.getString("city")); } KuaishouPromoter kuaishouPromoter = new KuaishouPromoter( id, result.getString("promoterNickName"), province, result.getString("promoterHeadImgUrl"), result.getString("fanNum"), result.getString("avgVideoSales"), result.getString("videoSales"), result.getString("totalSale"), promoterId ); if ("1".equals(mediaId)) { String followerincr = result.getString("follower_incr"); if (Check.isNotNull(followerincr) && !"-".equals(followerincr)) { kuaishouPromoter.setFollowerIncr(result.getLong("follower_incr")); } else { kuaishouPromoter.setFollowerIncr(0l); } kuaishouPromoter.setLiveCount30(result.getInteger("live_count_30")); kuaishouPromoter.setLiveAverageUser30(result.getString("live_average_user_30")); kuaishouPromoter.setAwemeDiggFollowerRation(result.getString("aweme_digg_follower_ration")); kuaishouPromoter.setAwemeAvgDiggCount30(result.getLong("aweme_avg_digg_count_30")); } kuaishouPromoterMapper.updateKuaishouPromoterByPromoterId(kuaishouPromoter); } else { log.error("更新达人失败,达人ID:{} 【返回结果】:{}", promoterId, result); } } catch (Exception e) { e.printStackTrace(); log.error("更新达人信息异常", e); } } @Override public Result supplementInfo() { List list = kuaishouPromoterMapper.getFailInfo(); Thread thread = new Thread() { @Override public void run() { try { list.forEach(kuaishouPromoter -> editPromoter(kuaishouPromoter.getId(), kuaishouPromoter.getMediaId(), kuaishouPromoter.getPromoterId(), false)); } catch (Exception e) { e.printStackTrace(); } } }; thread.start(); List promoters = new ArrayList<>(); list.forEach(kuaishouPromoter -> promoters.add(kuaishouPromoter.getPromoterId() + "【" + kuaishouPromoter.getMediaId() + "】")); return Result.success("共计" + list.size() + "条,数据补充中...", promoters); } @Override public int addFollowUpRecords(JSONObject result) { return kuaishouPromoterMapper.addFollowUpRecords(result); } @Override public void updateFollowUpRecords(JSONObject result) { kuaishouPromoterMapper.updateFollowUpRecords(result); } @Override public Result getGongHaiVideoSales(String promoterId) { Map param = new HashMap<>(); param.put("promoterId", promoterId); String url = startIpPath + "promoterInfo/updatePromoter"; String content = HttpUtil.httpPostRequest(url, param, null); if ("0".equals(content)) { return Result.success(); } else { log.error("查询达人公海销售额失败,达人ID:{}\nurl:{}", promoterId, url); return Result.error("失败"); } } @Override public KuaishouPromoter getOnlyPromoterInfoByUserId(String promoterId, Long userId) { return kuaishouPromoterMapper.getOnlyPromoterInfoByUserId(promoterId, userId); } @Override public JSONObject addPromotersCount(Long userId, Long promotersCount, String mediaId) { kuaishouPromoterMapper.addPromotersCount(userId, promotersCount, mediaId); JSONObject result = new JSONObject(); result.put("code", 200); result.put("msg", "success"); return result; } @Override public Result updatePromoterLabel(JSONObject result) { String promoterId = result.getString("promoterId"); Long userId = result.getLong("userId"); kuaishouPromoterMapper.deletePromoterLabel(promoterId, userId); JSONArray ids = result.getJSONArray("ids"); if (Check.isNotNull(ids)) { List list = new ArrayList<>(); for (int i = 0; i < ids.size(); i++) { JSONObject obj = new JSONObject(); Long labelId = ids.getLong(i); obj.put("promoterId", promoterId); obj.put("labelId", labelId); obj.put("userId", userId); list.add(obj); } kuaishouPromoterMapper.insertPromoterLabels(list); } return Result.success(); } /** * 修改快手达人 信息 * * @param kuaishouPromoter 快手达人 信息 * @return 结果 */ @Override public int updateKuaishouPromoter(KuaishouPromoter kuaishouPromoter) { return kuaishouPromoterMapper.updateKuaishouPromoter(kuaishouPromoter); } /** * 删除快手达人 信息信息 * * @param id 快手达人 信息主键 follow_up_records * @return 结果 */ @Override public Result deleteKuaishouPromoterById(Long id, Long userId) { KuaishouPromoter promoter = kuaishouPromoterMapper.selectKuaishouPromoterById(id); List list = kuaishouItemCollectSamplesService.getListByPromoterId(promoter.getPromoterId(), userId); for (KuaishouItemCollectSamples item : list) { if (item.getCollectSampleStatus() != 2 && item.getCollectSampleStatus() != 7) { return Result.error("该达人还有未完成的领样申请,请在完成后进行解绑!"); } } if (Check.isNotNull(promoter)) { JSONObject record = new JSONObject(); record.put("promoterId", promoter.getPromoterId()); record.put("userId", promoter.getUserId()); record.put("operatorId", userId); kuaishouPromoterMapper.insertKuaishouPromoterRecord(record); } kuaishouPromoterMapper.deleteKuaishouPromoterById(id); return Result.success(); } @Override public AjaxResult getPromoterInfo(String promoterId) { AjaxResult ajaxResult = null; try { KuaishouPromoter promoter = kuaishouPromoterMapper.getOneByIdAndPromoterId(null, promoterId); JSONObject data = new JSONObject(); if (Check.isNotNull(promoter)) { Thread thread = new Thread() { @Override public void run() { try { editPromoter(promoter.getId(), promoter.getMediaId(), promoter.getPromoterId(), true); } catch (Exception e) { e.printStackTrace(); } } }; thread.start(); Long startTime = System.currentTimeMillis(); String content = HttpUtil.httpGetRequest(internalIpPath + "kuaiShou/promoter/getPromoterVideoInfo?promoterId=" + promoter.getPromoterId()); JSONObject result = JSONObject.parseObject(content); log.info("获取达人视频销售数据,用时:{}s", (System.currentTimeMillis() - startTime) / 1000); String start = DateUtils.getSubtractTime(new Date(), 31); String end = DateUtils.getDate(); JSONObject info = kuaishouPromoterMapper.getNearlyMonthGmv(promoter.getPromoterId(), start, end); if (Check.isNotNull(info)) { data.put("orderAmount", info.getString("orderAmount")); data.put("operationCompletionRate", info.getString("operationCompletionRate")); } if (Check.isNotNull(result)) { data.put("avgVideoSales", result.getString("avgVideoSales")); data.put("videoSales", result.getString("videoSales")); } data.put("promoterId", promoter.getPromoterId()); data.put("promoterUrl", promoter.getPromoterUrl()); } ajaxResult = new AjaxResult(200, "查询成功", data); } catch (Exception e) { e.printStackTrace(); log.error("获取达人详情数据异常"); ajaxResult = new AjaxResult(500, "查询失败"); } return ajaxResult; } @Override public KuaishouPromoter getOnlyPromoterInfo(String promoterId) { return kuaishouPromoterMapper.getOneByIdAndPromoterId(null, promoterId); } @Override public List selectPromoterIdList(List userList) { return kuaishouPromoterMapper.selectPromoterIdList(userList); } @Override public JSONObject getgetMonthPromoterTotal(List userList, String start, String end) { return kuaishouPromoterMapper.getgetMonthPromoterTotal(userList, start, end); } /** * 判断是否为数字 */ public static boolean isNumeric(String str) { Pattern pattern = Pattern.compile("[0-9]*"); return pattern.matcher(str).matches(); } }