package com.ruixuan.isc.service.impl; 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.http.HttpUtil; import com.ruixuan.isc.entity.KuaishouPromoter; import com.ruixuan.isc.mapper.KuaishouPromoterMapper; 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.Arrays; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 快手达人 信息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; /** * 查询快手达人 信息列表 * * @param kuaishouPromoter 快手达人 信息 * @return 快手达人 信息 */ @Override public List selectKuaishouPromoterList(Long userId, Long promoterId, String promoterNickName, 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"; } if (Check.isNull(orderBy)) { orderBy = "desc"; } PageUtils.startPage(); List list = kuaishouPromoterMapper.selectKuaishouPromoterList(userList, promoterId, promoterNickName, parameter, orderBy); return list; } @Override public List selectKuaishouPromoterList2(Long promoterId, String promoterNickName, String parameter, String orderBy) { if (Check.isNull(parameter)) { parameter = "t1.create_time"; } if (Check.isNull(orderBy)) { orderBy = "desc"; } PageUtils.startPage(); List list = kuaishouPromoterMapper.selectAllPromoterList(promoterId, promoterNickName, 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.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); log.info("获取达人详情数据,用时:{}s", (System.currentTimeMillis() - startTime) / 1000); data.put("promoterInfo", promoter); data.put("result", result); } } catch (Exception e) { e.printStackTrace(); log.error("获取达人详情数据异常,{}", result); } return data; } /** * 新增快手达人 信息 * * @param kuaishouPromoter 快手达人 信息 */ @Override public JSONObject insertKuaishouPromoter(KuaishouPromoter promoter) { JSONObject result = new JSONObject(); KuaishouPromoter one = kuaishouPromoterMapper.getOneByIdAndPromoterId(promoter.getUserId(), promoter.getPromoterId()); if (Check.isNotNull(one)) { result.put("code", 500); result.put("msg", "该数据已存在,请重新输入"); return result; } /*//同一达人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; } } } }*/ SysUser sysUser = sysUserService.selectUserById(promoter.getUserId()); if (Check.isNotNull(sysUser)) { promoter.setUserName(sysUser.getNickName()); } kuaishouPromoterMapper.insertKuaishouPromoter(promoter); editPromoter(promoter.getId(), promoter.getPromoterId(), true); result.put("code", 200); result.put("msg", "success"); return result; } /** * 同步更新数据 */ private void editPromoter(Long id, Long 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); Long startTime = System.currentTimeMillis(); String content = HttpUtil.httpPostRequest(startIpPath + "promoterInfo/getPromoterId", param, null); JSONObject result = JSONObject.parseObject(content); log.info("更新达人信息,用时:{}s,返回结果:{}", (System.currentTimeMillis() - startTime) / 1000, result); if (Check.isNotNull(result) && Check.isNotNull(result.getString("promoterNickName"))) { KuaishouPromoter kuaishouPromoter = new KuaishouPromoter( id, result.getString("promoterNickName"), result.getString("province").concat("-").concat(result.getString("city")), result.getString("promoterHeadImgUrl"), result.getString("fanNum"), result.getString("avgVideoSales"), result.getString("videoSales"), result.getString("totalSale") ); kuaishouPromoterMapper.updateKuaishouPromoter(kuaishouPromoter); } } catch (Exception e) { e.printStackTrace(); log.error("更新达人信息异常", e); } } @Override public String supplementInfo() { List list = kuaishouPromoterMapper.getFailInfo(); Thread thread = new Thread() { @Override public void run() { try { list.forEach(kuaishouPromoter -> editPromoter(kuaishouPromoter.getId(), kuaishouPromoter.getPromoterId(), false)); } catch (Exception e) { e.printStackTrace(); } } }; thread.start(); return "共计" + list.size() + "条,数据补充中..."; } /** * 修改快手达人 信息 * * @param kuaishouPromoter 快手达人 信息 * @return 结果 */ @Override public int updateKuaishouPromoter(KuaishouPromoter kuaishouPromoter) { return kuaishouPromoterMapper.updateKuaishouPromoter(kuaishouPromoter); } /** * 删除快手达人 信息信息 * * @param id 快手达人 信息主键 * @return 结果 */ @Override public int deleteKuaishouPromoterById(Long id, Long userId) { KuaishouPromoter promoter = kuaishouPromoterMapper.selectKuaishouPromoterById(id); 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); } return kuaishouPromoterMapper.deleteKuaishouPromoterById(id); } @Override public AjaxResult getPromoterInfo(Long 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.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(Long 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); } }