package com.ruixuan.isc.service.impl; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Lists; import com.monitorjbl.xlsx.StreamingReader; 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.LoadFileUtil; import com.ruixuan.common.utils.PageUtils; import com.ruixuan.common.utils.RedisUtil; import com.ruixuan.common.utils.Result; import com.ruixuan.common.utils.file.FileUtil; import com.ruixuan.common.utils.http.HttpUtil; import com.ruixuan.data.utils.ExportExcelUtils; import com.ruixuan.isc.entity.FirstDeliveryValidMaterials; import com.ruixuan.isc.entity.JYKuaishouPromoter; import com.ruixuan.isc.entity.KuaishouItemCollectSamples; import com.ruixuan.isc.entity.KuaishouPromoter; import com.ruixuan.isc.mapper.FirstDeliveryValidMapper; 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.apache.commons.io.IOUtils; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; 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.UUID; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; 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 FirstDeliveryValidMapper deliveryValidMapper; @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, Long bdUserId, Integer searchPhone) { String roleKey = sysUserService.getRoleKeyByUserId(userId); List userList = new ArrayList<>(); //角色:供应链管理员、招商、招商经理可以看到所有的达人信息 if (roleKey.contains("courtship") || "supplyChainAdmin".equals(roleKey) || "admin".equals(roleKey)) { if (!Check.isNull(bdUserId)) { userList.add(bdUserId); } } else if ("bdManager".equals(roleKey) || "z".equals(roleKey)) { // 渠道经理、社群经理 可以看到自己创建的以及自己部门人员的达人信息 if (!Check.isNull(bdUserId)) { userList.add(bdUserId); } else { 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 = "create_time"; } else { switch (parameter) { case "avg_video_sales": parameter = "avgVideoSalesOrder"; break; case "video_sales": parameter = "videoSalesOrder"; break; case "total_sale": parameter = "totalSaleOrder"; break; } } if (Check.isNull(orderBy)) { orderBy = "desc"; } PageUtils.startPage(); List list = kuaishouPromoterMapper.selectKuaishouPromoterList(userList, promoterId, promoterNickName, mediaId, parameter, orderBy, bdUserId, searchPhone); 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 selectKuaishouPromoterList3(Long userId, String promoterId, String promoterNickName, String mediaId, String type, String parameter, String orderBy) { String roleKey = sysUserService.getRoleKeyByUserId(userId); List userList = new ArrayList<>(); //角色:管理员和骄阳管理员可以看到所有的达人信息 Long expertMentorId = null; Long expertBusinessId = null; if ("admin".equals(roleKey) || "jy_admnin".equals(roleKey)) { userList = null; } else if ("jy_bd_manage".equals(roleKey) || "jy_scissors_manage".equals(roleKey) || "jy_out_operation_manager".equals(roleKey)) { // 骄阳渠道经理、骄阳剪手经理 可以看到自己创建的以及自己部门人员的达人信息 Long deptId = deptService.getDeptIdByUserId(userId); userList = deptService.getDeptUserListByDeptId(deptId); } else if ("jy_bd".equals(roleKey) || "jy_scissors".equals(roleKey) || "jy_out_operation".equals(roleKey)) { // 骄阳渠道、骄阳-剪手 可以看到自己创建的达人信息 userList = Arrays.asList(userId); } else if ("jy_expert_mentor".equals(roleKey) || "jy_expert_business".equals(roleKey)) { if ("jy_expert_mentor".equals(roleKey)) { expertMentorId = userId; } if ("jy_expert_business".equals(roleKey)) { expertBusinessId = userId; } } else { return Collections.emptyList(); } if (Check.isNull(parameter)) { parameter = "create_time"; } else { switch (parameter) { case "avg_video_sales": parameter = "avgVideoSalesOrder"; break; case "video_sales": parameter = "videoSalesOrder"; break; case "total_sale": parameter = "totalSaleOrder"; break; } } if (Check.isNull(orderBy)) { orderBy = "desc"; } PageUtils.startPage(); List list = kuaishouPromoterMapper.selectKuaishouPromoterList3(userList, promoterId, promoterNickName, mediaId, type, parameter, orderBy, expertMentorId, expertBusinessId); 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 = "avgVideoSalesOrder"; } else if ("videoSales".equals(parameter)) { parameter = "videoSalesOrder"; } 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; } /** * 新增快手达人 信息 */ @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) || "supplyChainAdmin".equals(rKey)) { result.put("code", 500); result.put("msg", "该达人已被其他渠道人员绑定,绑定人:" + key.getString("user_name")); return result; } } } } // } //查询 设置可绑定达人数 Integer count = kuaishouPromoterMapper.getUserPromotersCount(promoter.getUserId(), promoter.getMediaId()); 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)) { //睡眠: 随机数3的倍数 3、6、9 ...33s Thread.sleep(1000 * (int) (1 + Math.random() * 11) * 3); content = HttpUtil.httpPostRequest(startIpPath + "promoterInfo/getPromoterId", param, null); } else { Thread.sleep(60000); content = HttpUtil.httpPostRequest(startIpPath + "promoterInfo/getPromoterId", param, null); } JSONObject result = JSONObject.parseObject(content); if (Check.isNotNull(result) && Check.isNotNull(result.getString("promoterNickName"))) { log.info("--------------------RuiXuan达人更新\n【入参】:{}\n【返回结果】:{}", param, result.toJSONString()); 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); } } /** * 同步更新数据 */ private void editPromoter3(Long id, String mediaId, String promoterId, boolean flag) { try { if (flag) { /*缓存标记,每日第一次访问调用该接口,更新达人部分(昵称,省市,粉丝,总销售,头像等)数据*/ String key = DateUtils.getDate() + "_" + promoterId + "_" + id; String value = (String) redisUtil.get(key); if (Check.isNotNull(value) && "1".equals(value)) { return; } redisUtil.set(key, "1", 60 * 60 * 24); } //睡眠: 随机数3的倍数 3、6、9 ...33s Thread.sleep(1000 * (int) (1 + Math.random() * 11) * 3); /*更新达人数据*/ Map param = new HashMap<>(); param.put("promoterId", promoterId); param.put("mediaId", mediaId); Long startTime = System.currentTimeMillis(); String content = HttpUtil.httpPostRequest(startIpPath + "promoterInfo/getPromoterId", param, null); JSONObject result = JSONObject.parseObject(content); if (Check.isNotNull(result) && Check.isNotNull(result.getString("promoterNickName"))) { log.info("--------------------JY达人更新\n【入参】:{}\n【返回结果】:{}", param, result.toJSONString()); String province = ""; if (Check.isNotNull(result.getString("province"))) { province = result.getString("province").concat("-").concat(result.getString("city")); } JYKuaishouPromoter kuaishouPromoter = new JYKuaishouPromoter( id, result.getString("promoterNickName"), province, result.getString("promoterHeadImgUrl"), result.getString("fanNum"), result.getString("avgVideoSales"), result.getString("videoSales"), result.getString("totalSale"), promoterId ); kuaishouPromoterMapper.updateKuaishouPromoterByPromoterId3(kuaishouPromoter); } else { log.error("更新达人失败,达人ID:{} 【返回结果】:{}", promoterId, result); } } catch (Exception e) { e.printStackTrace(); log.error("更新达人信息异常", e); } } /** * Miaogousi同步更新数据 */ private void editMiaogousiPromoter(Long id, String mediaId, String promoterId, boolean flag) { try { if (flag) { /*缓存标记,每日第一次访问调用该接口,更新达人部分(昵称,省市,粉丝,总销售,头像等)数据*/ String key = DateUtils.getDate() + "mgs_" + promoterId + "_" + id; String value = (String) redisUtil.get(key); if (Check.isNotNull(value) && "1".equals(value)) { return; } redisUtil.set(key, "1", 60 * 60 * 24); } //睡眠: 随机数3的倍数 3、6、9 ...33s Thread.sleep(1000 * (int) (1 + Math.random() * 11) * 3); /*更新达人数据*/ Map param = new HashMap<>(); param.put("promoterId", promoterId); param.put("mediaId", mediaId); Long startTime = System.currentTimeMillis(); String content = HttpUtil.httpPostRequest(startIpPath + "promoterInfo/getPromoterId", param, null); JSONObject result = JSONObject.parseObject(content); if (Check.isNotNull(result) && Check.isNotNull(result.getString("promoterNickName"))) { log.info("--------------------Miaogousi达人更新\n【入参】:{}\n【返回结果】:{}", param, result.toJSONString()); 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 ); kuaishouPromoterMapper.updateMiaogousiKuaishouPromoterByPromoterId(kuaishouPromoter); } else { log.error("更新达人失败,达人ID:{} 【返回结果】:{}", promoterId, result); } } catch (Exception e) { e.printStackTrace(); log.error("更新达人信息异常", e); } } /** * Rocket同步更新数据 */ private void editRocketPromoter(Long id, String mediaId, String promoterId, boolean flag) { try { if (flag) { /*缓存标记,每日第一次访问调用该接口,更新达人部分(昵称,省市,粉丝,总销售,头像等)数据*/ String key = DateUtils.getDate() + "rocket_" + promoterId + "_" + id; String value = (String) redisUtil.get(key); if (Check.isNotNull(value) && "1".equals(value)) { return; } redisUtil.set(key, "1", 60 * 60 * 24); } //睡眠: 随机数3的倍数 3、6、9 ...33s Thread.sleep(1000 * (int) (1 + Math.random() * 11) * 3); /*更新达人数据*/ Map param = new HashMap<>(); param.put("promoterId", promoterId); param.put("mediaId", mediaId); Long startTime = System.currentTimeMillis(); String content = HttpUtil.httpPostRequest(startIpPath + "promoterInfo/getPromoterId", param, null); JSONObject result = JSONObject.parseObject(content); if (Check.isNotNull(result) && Check.isNotNull(result.getString("promoterNickName"))) { log.info("--------------------Rocket达人更新\n【入参】:{}\n【返回结果】:{}", param, result.toJSONString()); 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 ); kuaishouPromoterMapper.updateRocketKuaishouPromoterByPromoterId(kuaishouPromoter); } else { log.error("更新达人失败,达人ID:{} 【返回结果】:{}", promoterId, result); } } catch (Exception e) { e.printStackTrace(); log.error("更新达人信息异常", e); } } /** * Yufu同步更新数据 */ private void editYufuPromoter(Long id, String mediaId, String promoterId, boolean flag) { try { if (flag) { /*缓存标记,每日第一次访问调用该接口,更新达人部分(昵称,省市,粉丝,总销售,头像等)数据*/ String key = DateUtils.getDate() + "yufu_" + promoterId + "_" + id; String value = (String) redisUtil.get(key); if (Check.isNotNull(value) && "1".equals(value)) { return; } redisUtil.set(key, "1", 60 * 60 * 24); } //睡眠: 随机数3的倍数 3、6、9 ...33s Thread.sleep(1000 * (int) (1 + Math.random() * 11) * 3); /*更新达人数据*/ Map param = new HashMap<>(); param.put("promoterId", promoterId); param.put("mediaId", mediaId); Long startTime = System.currentTimeMillis(); String content = HttpUtil.httpPostRequest(startIpPath + "promoterInfo/getPromoterId", param, null); JSONObject result = JSONObject.parseObject(content); if (Check.isNotNull(result) && Check.isNotNull(result.getString("promoterNickName"))) { log.info("--------------------Yufu达人更新\n【入参】:{}\n【返回结果】:{}", param, result.toJSONString()); 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 ); kuaishouPromoterMapper.updateYufuKuaishouPromoterByPromoterId(kuaishouPromoter); } else { log.error("更新达人失败,达人ID:{} 【返回结果】:{}", promoterId, result); } } catch (Exception e) { e.printStackTrace(); log.error("更新达人信息异常", e); } } /** * Yuxiu同步更新数据 */ private void editYuxiuPromoter(Long id, String mediaId, String promoterId, boolean flag) { try { if (flag) { /*缓存标记,每日第一次访问调用该接口,更新达人部分(昵称,省市,粉丝,总销售,头像等)数据*/ String key = DateUtils.getDate() + "yuxiu_" + promoterId + "_" + id; String value = (String) redisUtil.get(key); if (Check.isNotNull(value) && "1".equals(value)) { return; } redisUtil.set(key, "1", 60 * 60 * 24); } //睡眠: 随机数3的倍数 3、6、9 ...33s Thread.sleep(1000 * (int) (1 + Math.random() * 11) * 3); /*更新达人数据*/ Map param = new HashMap<>(); param.put("promoterId", promoterId); param.put("mediaId", mediaId); Long startTime = System.currentTimeMillis(); String content = HttpUtil.httpPostRequest(startIpPath + "promoterInfo/getPromoterId", param, null); JSONObject result = JSONObject.parseObject(content); if (Check.isNotNull(result) && Check.isNotNull(result.getString("promoterNickName"))) { log.info("--------------------Yuxiu达人更新\n【入参】:{}\n【返回结果】:{}", param, result.toJSONString()); 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 ); kuaishouPromoterMapper.updateYuxiuKuaishouPromoterByPromoterId(kuaishouPromoter); } else { log.error("更新达人失败,达人ID:{} 【返回结果】:{}", promoterId, result); } } catch (Exception e) { e.printStackTrace(); log.error("更新达人信息异常", e); } } /** * zhishui同步更新数据 */ private void editZhishuiPromoter(Long id, String mediaId, String promoterId, boolean flag) { try { if (flag) { /*缓存标记,每日第一次访问调用该接口,更新达人部分(昵称,省市,粉丝,总销售,头像等)数据*/ String key = DateUtils.getDate() + "zhishui_" + promoterId + "_" + id; String value = (String) redisUtil.get(key); if (Check.isNotNull(value) && "1".equals(value)) { return; } redisUtil.set(key, "1", 60 * 60 * 24); } //睡眠: 随机数3的倍数 3、6、9 ...33s Thread.sleep(1000 * (int) (1 + Math.random() * 11) * 3); /*更新达人数据*/ Map param = new HashMap<>(); param.put("promoterId", promoterId); param.put("mediaId", mediaId); Long startTime = System.currentTimeMillis(); String content = HttpUtil.httpPostRequest(startIpPath + "promoterInfo/getPromoterId", param, null); JSONObject result = JSONObject.parseObject(content); if (Check.isNotNull(result) && Check.isNotNull(result.getString("promoterNickName"))) { log.info("--------------------Zhishui达人更新\n【入参】:{}\n【返回结果】:{}", param, result.toJSONString()); 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 ); kuaishouPromoterMapper.updateZhishuiKuaishouPromoterByPromoterId(kuaishouPromoter); } else { log.error("更新达人失败,达人ID:{} 【返回结果】:{}", promoterId, result); } } catch (Exception e) { e.printStackTrace(); log.error("更新达人信息异常", e); } } @Override public JSONObject insertKuaishouPromoter3(JYKuaishouPromoter promoter) { JSONObject result = new JSONObject(); promoter.setPromoterId(promoter.getPromoterId().trim()); KuaishouPromoter one = kuaishouPromoterMapper.getOneByIdAndPromoterId3(null, promoter.getPromoterId()); if (Check.isNotNull(one)) { result.put("code", 500); result.put("msg", "达人已被绑定,请重新选择 (绑定人:" + one.getUserName() + ")"); return result; } SysUser sysUser = sysUserService.selectUserById(promoter.getUserId()); if (Check.isNotNull(sysUser)) { promoter.setUserName(sysUser.getNickName()); } if (!Check.isNull(promoter.getExpertBusinessId())) { SysUser businessUser = sysUserService.selectUserById(promoter.getExpertBusinessId()); if (!Check.isNull(businessUser)) { promoter.setExpertBusinessName(businessUser.getNickName()); } } if (!Check.isNull(promoter.getExpertMentorId())) { SysUser mentorUser = sysUserService.selectUserById(promoter.getExpertMentorId()); if (!Check.isNull(mentorUser)) { promoter.setExpertMentorName(mentorUser.getNickName()); } } kuaishouPromoterMapper.insertKuaishouPromoter3(promoter); if (promoter.getMediaId().equals("2")) { addAuth(Long.valueOf(promoter.getPromoterId())); } editPromoter3(promoter.getId(), promoter.getMediaId(), promoter.getPromoterId(), true); result.put("code", 200); result.put("msg", "success"); return result; } @Override public JSONObject addAuth(Long promoterId) { String cookie = kuaishouPromoterMapper.getAuthCookie(); Map param = new HashMap<>(); param.put("userId", promoterId); param.put("appKey", "ks690458118961229051"); param.put("type", 2); Map headers = new HashMap<>(); headers.put("Cookie", cookie); String url = "https://open.kwaixiaodian.com/rest/open/platform/oauth/add"; String s = HttpUtil.httpPostRequest(url, param, headers); JSONObject jsonObject = JSONObject.parseObject(s); log.info("爬虫增加授权账户返回信息:{}", jsonObject); return jsonObject; } static ExecutorService editPromoterService = Executors.newFixedThreadPool(6); @Override public Result supplementInfo() { List promoters = new ArrayList<>(); List list = kuaishouPromoterMapper.getFailInfo(); if (!Check.isNull(list)) { list.forEach(kuaishouPromoter -> editPromoterService.submit(() -> { try { editPromoter(kuaishouPromoter.getId(), kuaishouPromoter.getMediaId(), kuaishouPromoter.getPromoterId(), false); } catch (Exception e) { e.printStackTrace(); } })); list.forEach(kuaishouPromoter -> promoters.add(kuaishouPromoter.getPromoterId() + "[" + ("2".equals(kuaishouPromoter.getMediaId()) ? "快手" : "头条") + "]")); } promoters.add(supplementInfo3()); promoters.add(miaogousiInfo()); promoters.add(rocketInfo()); promoters.add(yufuInfo()); promoters.add(yuxiuInfo()); promoters.add(zhishuiInfo()); return Result.success("达人数据更新 -------------<睿选>共计:" + list.size() + "条", promoters); } //骄阳补全 public String supplementInfo3() { List promoters = new ArrayList<>(); List list = kuaishouPromoterMapper.getFailInfo3(); if (!Check.isNull(list)) { list.forEach(kuaishouPromoter -> editPromoterService.submit(() -> { try { editPromoter3(kuaishouPromoter.getId(), kuaishouPromoter.getMediaId(), kuaishouPromoter.getPromoterId(), false); } catch (Exception e) { e.printStackTrace(); } })); list.forEach(kuaishouPromoter -> promoters.add(kuaishouPromoter.getPromoterId())); } return "-------------骄阳共计" + list.size() + "条,达人id: " + promoters.toString(); } //miaogousi public String miaogousiInfo() { List promoters = new ArrayList<>(); List list = kuaishouPromoterMapper.getMiaogousiInfoFailInfo(); if (!Check.isNull(list)) { list.forEach(kuaishouPromoter -> editPromoterService.submit(() -> { try { editMiaogousiPromoter(kuaishouPromoter.getId(), kuaishouPromoter.getMediaId(), kuaishouPromoter.getPromoterId(), false); } catch (Exception e) { e.printStackTrace(); } })); list.forEach(kuaishouPromoter -> promoters.add(kuaishouPromoter.getPromoterId())); } return "-------------miaogousi共计" + list.size() + "条,达人id: " + promoters.toString(); } //rocket public String rocketInfo() { List promoters = new ArrayList<>(); List list = kuaishouPromoterMapper.getRocketFailInfo(); if (!Check.isNull(list)) { list.forEach(kuaishouPromoter -> editPromoterService.submit(() -> { try { editRocketPromoter(kuaishouPromoter.getId(), kuaishouPromoter.getMediaId(), kuaishouPromoter.getPromoterId(), false); } catch (Exception e) { e.printStackTrace(); } })); list.forEach(kuaishouPromoter -> promoters.add(kuaishouPromoter.getPromoterId())); } return "-------------rocket共计" + list.size() + "条,达人id: " + promoters.toString(); } //yufu public String yufuInfo() { List promoters = new ArrayList<>(); List list = kuaishouPromoterMapper.getYufuFailInfo(); if (!Check.isNull(list)) { list.forEach(kuaishouPromoter -> editPromoterService.submit(() -> { try { editYufuPromoter(kuaishouPromoter.getId(), kuaishouPromoter.getMediaId(), kuaishouPromoter.getPromoterId(), false); } catch (Exception e) { e.printStackTrace(); } })); list.forEach(kuaishouPromoter -> promoters.add(kuaishouPromoter.getPromoterId())); } return "-------------yufu共计" + list.size() + "条,达人id: " + promoters.toString(); } //yuxiu public String yuxiuInfo() { List promoters = new ArrayList<>(); List list = kuaishouPromoterMapper.getYuxiuFailInfo(); if (!Check.isNull(list)) { list.forEach(kuaishouPromoter -> editPromoterService.submit(() -> { try { editYuxiuPromoter(kuaishouPromoter.getId(), kuaishouPromoter.getMediaId(), kuaishouPromoter.getPromoterId(), false); } catch (Exception e) { e.printStackTrace(); } })); list.forEach(kuaishouPromoter -> promoters.add(kuaishouPromoter.getPromoterId())); } return "-------------yuxiu共计" + list.size() + "条,达人id: " + promoters.toString(); } //zhishui public String zhishuiInfo() { List promoters = new ArrayList<>(); List list = kuaishouPromoterMapper.getZhishuiFailInfo(); if (!Check.isNull(list)) { list.forEach(kuaishouPromoter -> editPromoterService.submit(() -> { try { editZhishuiPromoter(kuaishouPromoter.getId(), kuaishouPromoter.getMediaId(), kuaishouPromoter.getPromoterId(), false); } catch (Exception e) { e.printStackTrace(); } })); list.forEach(kuaishouPromoter -> promoters.add(kuaishouPromoter.getPromoterId())); } return "-------------zhishui共计" + list.size() + "条,达人id: " + promoters.toString(); } @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 String getRuleName(Long ruleId) { return kuaishouPromoterMapper.getRuleName(ruleId); } @Override public int addRule(String promoterId, Long ruleId, String ruleName) { return kuaishouPromoterMapper.addRule(promoterId, ruleId, ruleName); } @Override public List selectPromoterIdListByTime(List userList, Long startDate, Long endDate) { return kuaishouPromoterMapper.selectPromoterIdListByTime(userList, startDate, endDate); } @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 kuaishouPromoter 快手达人 信息 * @return 结果 */ @Override public int updateKuaishouPromoter3(JYKuaishouPromoter promoter) { if (!Check.isNull(promoter.getExpertBusinessId())) { SysUser businessUser = sysUserService.selectUserById(promoter.getExpertBusinessId()); if (!Check.isNull(businessUser)) { promoter.setExpertBusinessName(businessUser.getNickName()); } } if (!Check.isNull(promoter.getExpertMentorId())) { SysUser mentorUser = sysUserService.selectUserById(promoter.getExpertMentorId()); if (!Check.isNull(mentorUser)) { promoter.setExpertMentorName(mentorUser.getNickName()); } } return kuaishouPromoterMapper.updateKuaishouPromoter3(promoter); } /** * 删除快手达人 信息信息 * * @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); List roles = sysUserService.getUserRoleByUserId(userId); if (Check.isNotNull(roles)) { record.put("roleName", roles.get(0).getString("role_name")); record.put("roleKey", roles.get(0).getString("role_key")); } kuaishouPromoterMapper.insertKuaishouPromoterRecord(record); } kuaishouPromoterMapper.deleteKuaishouPromoterById(id); return Result.success(); } /** * 删除快手达人 信息信息 * * @param id 快手达人 信息主键 follow_up_records * @return 结果 */ @Override public Result deleteKuaishouPromoterById3(Long id, Long userId) { JYKuaishouPromoter promoter = kuaishouPromoterMapper.selectKuaishouPromoterById3(id); if (Check.isNotNull(promoter)) { JSONObject record = new JSONObject(); record.put("promoterId", promoter.getPromoterId()); record.put("userId", promoter.getUserId()); record.put("operatorId", userId); List roles = sysUserService.getUserRoleByUserId(userId); if (Check.isNotNull(roles)) { record.put("roleName", roles.get(0).getString("role_name") + " (解绑骄阳)"); record.put("roleKey", roles.get(0).getString("role_key")); } kuaishouPromoterMapper.insertKuaishouPromoterRecord(record); } kuaishouPromoterMapper.deleteKuaishouPromoterById3(id); return Result.success(); } @Override public JSONObject selectKuaishouRulePromoterById(Long promoterId) { return kuaishouPromoterMapper.selectKuaishouRulePromoterById(promoterId); } @Override public List jyExpertMentorList() { return kuaishouPromoterMapper.jyExpertMentorList(); } @Override public List jyExpertBusiness() { return kuaishouPromoterMapper.jyExpertBusiness(); } @Value("${pcm.local-path}") private String downloadPath; @Override public void importExcelMaterials(MultipartFile file) throws InterruptedException { ExecutorService executorService = Executors.newFixedThreadPool(8); long t1 = System.currentTimeMillis(); List dataList = null; StringBuffer path = new StringBuffer(); try { path.append(downloadPath).append(DateUtils.getNowDateStr()).append("/").append(UUID.randomUUID().toString()).append("/"); log.info("-------下载到服务器地址:{}", path.toString()); dataList = this.analysisBIGBIGFile(file, path.toString()); } catch (Exception e) { e.printStackTrace(); throw new InterruptedException("解析excel失败"); } finally { LoadFileUtil.delFile(path.toString()); } if (!Check.isNull(dataList)) { //根据当前年份和月份,删除旧数据 FirstDeliveryValidMaterials material = dataList.get(0); deliveryValidMapper.delMaterials(material.getDeliveryYear(), material.getDeliveryMonth()); //插入 数据量大于5Q,分批插入 if (dataList.size() > 5000) { List> splitList = Lists.newArrayList(Lists.partition(dataList, 5000)); splitList.forEach(materials -> executorService.submit(() -> { deliveryValidMapper.replaceBatchMaterials(materials); })); } else { deliveryValidMapper.replaceBatchMaterials(dataList); } } executorService.shutdown(); while (true) { if (executorService.isTerminated()) { log.info("所有的子线程都结束了!"); //执行宇鹏提供的sql deliveryValidMapper.replaceEtl(); break; } Thread.sleep(1000L);//休眠1秒钟 } log.info("------共用时" + (System.currentTimeMillis() - t1) / 1000 + "秒"); } @Override public List getAllJyPromoterList() { return kuaishouPromoterMapper.getAllJyPromoterList(); } public List analysisBIGBIGFile(MultipartFile mfile, String path) throws Exception { InputStream is = null; String newFileUrl = FileUtil.approvalFile(mfile, path); List dataList = null; try { File file = new File(newFileUrl); is = new FileInputStream(file); Workbook workbook = StreamingReader.builder() .rowCacheSize(20000) .bufferSize(4096) .open(is); ExportExcelUtils eeu = new ExportExcelUtils(); //最后一列(列数) int lastCellNum = 0; // list存储数据集 dataList = new ArrayList<>(); for (Sheet sheet : workbook) { // sheet.getLastRowNum()获取最后一个非空行的行下标,比如总行数为n,则返回的为n-1 int sumPageNum = (sheet.getLastRowNum() + 1) / 20000 + 1; //计算分片数 for (int i = 1; i <= sumPageNum; i++) { //读取每片范围内的内容 int minRowNum = 20000 * (i - 1) + 1; //计算每片读取的行数范围 int maxRowNum = 20000 * i; for (Row row : sheet) { if (lastCellNum == 0) { lastCellNum = row.getLastCellNum(); } if (row != null) { Map columnValueMap = new HashMap<>(); if (row.getRowNum() >= minRowNum && row.getRowNum() <= maxRowNum) {//控制每片读取的行 //每行数据对象,按照顺序从0到(lastCellNum-1)记录 JSONObject cellEntity = new JSONObject(); for (int k = 0; k < lastCellNum; k++) { cellEntity.put(String.valueOf(k), eeu.getCellValue(row.getCell(k))); } FirstDeliveryValidMaterials material = new FirstDeliveryValidMaterials(cellEntity); if ("走量".equals(material.getOperationLabel())) { continue; } if (!Check.isNull(material.getStatDate())) { material.setDeliveryYear(material.getStatDate().substring(0, 4)); } dataList.add(material); if (row.getRowNum() == maxRowNum) {//读取到每片最大行数限制,跳出,执行数据写入 break; } } } } } } workbook.close(); } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(is); } return dataList; } @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(), DateUtils.strDateToInt(start), DateUtils.strDateToInt(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, String mediaId) { return kuaishouPromoterMapper.selectPromoterIdList(userList, mediaId); } @Override public JSONObject getgetMonthPromoterTotal(List userList, String start, String end, String mediaId) { return kuaishouPromoterMapper.getgetMonthPromoterTotal(userList, start, end, mediaId); } /** * 判断是否为数字 */ public static boolean isNumeric(String str) { Pattern pattern = Pattern.compile("[0-9]*"); return pattern.matcher(str).matches(); } }