Commit 95065426 950654268405dcb732c92f7cc8383bc0ecea5bed by xianghan

1.添加消费成长报告mq

1 parent fd7258cd
package com.topdraw.business.module.user.iptv.growreport.domain;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
/**
* @author XiangHan
* @date 2022-07-07
*/
@Entity
@Data
@EntityListeners(AuditingEntityListener.class)
@Accessors(chain = true)
@Table(name="uc_growth_report")
public class GrowthReport implements Serializable {
@Id
@Column(name = "id")
private Long id;
// 用户id
@Column(name = "user_id")
private Long userId;
// 会员id
@Column(name = "member_id")
private Long memberId;
// 会员code
@Column(name = "member_code")
private String memberCode;
// 大屏账号
@Column(name = "platform_account")
private String platformAccount;
// 开始日期
@Column(name = "start_date")
private String startDate;
// 结束时间
@Column(name = "end_date")
private String endDate;
// 栏目播放时长数据
@Column(name = "data")
private String data;
// 创建时间
@CreatedDate
@Column(name = "create_time")
private Timestamp createTime;
// 修改时间
@LastModifiedDate
@Column(name = "update_time")
private Timestamp updateTime;
public void copy(GrowthReport source){
BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
}
}
package com.topdraw.business.module.user.iptv.growreport.repository;
import com.topdraw.business.module.user.iptv.growreport.domain.GrowthReport;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import java.util.Optional;
/**
* @author XiangHan
* @date 2022-07-07
*/
public interface GrowthReportRepository extends JpaRepository<GrowthReport, Long>, JpaSpecificationExecutor<GrowthReport> {
Optional<GrowthReport> findByPlatformAccountAndStartDateAndEndDate(String platformAccount, String startDate, String endDate);
@Modifying
@Query(value = "UPDATE `uc_growth_report` SET `data` = ?2, `update_time` = now() WHERE `id` =?1", nativeQuery = true)
Integer updateGrowthReportData(Long id, String data);
}
package com.topdraw.business.module.user.iptv.growreport.service;
import com.topdraw.business.module.user.iptv.growreport.domain.GrowthReport;
import com.topdraw.business.module.user.iptv.growreport.service.dto.GrowthReportDTO;
/**
* @author XiangHan
* @date 2022-07-07
*/
public interface GrowthReportService {
/**
* 根据ID查询
* @param id ID
* @return GrowthReportDTO
*/
GrowthReportDTO findById(Long id);
void create(GrowthReport resources);
void update(GrowthReport resources);
void delete(Long id);
GrowthReportDTO findByPlatformAccountAndStartDateAndEndDate(String platformAccount, String weekFirstDay, String weekLastDay);
Integer updateGrowthReportData(Long id, String data);
}
package com.topdraw.business.module.user.iptv.growreport.service.dto;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Data;
import java.io.Serializable;
import java.sql.Timestamp;
/**
* @author XiangHan
* @date 2022-07-07
*/
@Data
public class GrowthReportDTO implements Serializable {
// 处理精度丢失问题
@JsonSerialize(using= ToStringSerializer.class)
private Long id;
// 用户id
private Long userId;
// 会员id
private Long memberId;
// 会员code
private String memberCode;
// 大屏账号
private String platformAccount;
// 开始日期
private String startDate;
// 结束时间
private String endDate;
// 栏目播放时长数据
private String data;
// 创建时间
private Timestamp createTime;
// 修改时间
private Timestamp updateTime;
}
package com.topdraw.business.module.user.iptv.growreport.service.impl;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.topdraw.business.module.user.iptv.growreport.domain.GrowthReport;
import com.topdraw.business.module.user.iptv.growreport.repository.GrowthReportRepository;
import com.topdraw.business.module.user.iptv.growreport.service.GrowthReportService;
import com.topdraw.business.module.user.iptv.growreport.service.dto.GrowthReportDTO;
import com.topdraw.business.module.user.iptv.growreport.service.mapper.GrowthReportMapper;
import com.topdraw.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
/**
* @author XiangHan
* @date 2022-07-07
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class GrowthReportServiceImpl implements GrowthReportService {
@Autowired
private GrowthReportRepository growthReportRepository;
@Autowired
private GrowthReportMapper growthReportMapper;
@Override
public GrowthReportDTO findById(Long id) {
GrowthReport growthReport = this.growthReportRepository.findById(id).orElseGet(GrowthReport::new);
ValidationUtil.isNull(growthReport.getId(),"GrowthReport","id",id);
return this.growthReportMapper.toDto(growthReport);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void create(GrowthReport resources) {
Snowflake snowflake = IdUtil.createSnowflake(1, 1);
resources.setId(snowflake.nextId());
this.growthReportRepository.save(resources);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void update(GrowthReport resources) {
GrowthReport growthReport = this.growthReportRepository.findById(resources.getId()).orElseGet(GrowthReport::new);
ValidationUtil.isNull( growthReport.getId(),"GrowthReport","id",resources.getId());
growthReport.copy(resources);
this.growthReportRepository.save(growthReport);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void delete(Long id) {
Assert.notNull(id, "The given id must not be null!");
GrowthReport growthReport = this.growthReportRepository.findById(id).orElseThrow(
() -> new EmptyResultDataAccessException(String.format("No %s entity " + "with id %s " + "exists!", GrowthReport.class, id), 1));
this.growthReportRepository.delete(growthReport);
}
@Override
public GrowthReportDTO findByPlatformAccountAndStartDateAndEndDate(String platformAccount, String weekFirstDay, String weekLastDay) {
GrowthReport growthReport = this.growthReportRepository.findByPlatformAccountAndStartDateAndEndDate(platformAccount, weekFirstDay, weekLastDay).orElseGet(GrowthReport::new);
return this.growthReportMapper.toDto(growthReport);
}
@Override
@Transactional(rollbackFor = Exception.class)
public Integer updateGrowthReportData(Long id, String data) {
return this.growthReportRepository.updateGrowthReportData(id, data);
}
}
package com.topdraw.business.module.user.iptv.growreport.service.mapper;
import com.topdraw.base.BaseMapper;
import com.topdraw.business.module.user.iptv.growreport.domain.GrowthReport;
import com.topdraw.business.module.user.iptv.growreport.service.dto.GrowthReportDTO;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
/**
* @author XiangHan
* @date 2022-07-07
*/
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface GrowthReportMapper extends BaseMapper<GrowthReportDTO, GrowthReport> {
}
......@@ -8,6 +8,9 @@ import com.topdraw.business.module.member.profile.service.dto.MemberProfileDTO;
import com.topdraw.business.module.member.service.MemberService;
import com.topdraw.business.module.member.service.dto.MemberDTO;
import com.topdraw.business.module.user.iptv.domain.UserTv;
import com.topdraw.business.module.user.iptv.growreport.domain.GrowthReport;
import com.topdraw.business.module.user.iptv.growreport.service.GrowthReportService;
import com.topdraw.business.module.user.iptv.growreport.service.dto.GrowthReportDTO;
import com.topdraw.business.module.user.iptv.service.UserTvService;
import com.topdraw.business.module.user.iptv.service.dto.UserTvDTO;
import com.topdraw.business.module.user.weixin.domain.UserWeixin;
......@@ -18,6 +21,7 @@ import com.topdraw.business.process.service.dto.MemberAndUserTvDTO;
import com.topdraw.business.process.service.dto.MemberAndWeixinUserDTO;
import com.topdraw.exception.EntityNotFoundException;
import com.topdraw.exception.GlobeExceptionMsg;
import com.topdraw.util.TimestampUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
......@@ -42,25 +46,40 @@ public class UserOperationServiceImpl implements UserOperationService {
private UserWeixinService userWeixinService;
@Autowired
private MemberProfileService memberProfileService;
@Autowired
private GrowthReportService growthReportService;
public void asyncsaveGrowthReport(GrowthReport growthReport) {
String platformAccount = growthReport.getPlatformAccount();
UserTvDTO userTvDTO = this.userTvService.findByPlatformAccount(platformAccount);
if (Objects.isNull(userTvDTO.getId())){
log.error("保存成长报告失败,大屏信息不存在[asyncsaveGrowthReport#]");
return;
}
String weekFirstDay = com.topdraw.util.DateUtil.getWeekFirstDay();
String weekLastDay = com.topdraw.util.DateUtil.getWeekLastDay();
GrowthReportDTO growthReportDTO = this.growthReportService.findByPlatformAccountAndStartDateAndEndDate(platformAccount, weekFirstDay, weekLastDay);
if (Objects.isNull(growthReportDTO.getId())) {
Long id = userTvDTO.getId();
Long memberId = userTvDTO.getMemberId();
growthReport.setUserId(id);
growthReport.setMemberId(memberId);
growthReport.setStartDate(weekFirstDay);
growthReport.setEndDate(weekLastDay);
/* {"entityBody":"{\"memberDTO\":{\"birthday\":\"1900-01-01\",\"blackStatus\":0,\"code\":\"1540299597507502080\"," +
"\"couponAmount\":0,\"createTime\":1656071016149,\"dueCouponAmount\":0,\"duePoints\":0,\"exp\":0,\"gender\":-1," +
"\"id\":62515,\"level\":1,\"points\":0,\"status\":1," +
"\"type\":2,\"updateTime\":1656071016149,\"vip\":0}," +
"" +
"\"userWeixinDTO\":{\"accessToken\":\"\",\"appid\":\"wx0f7db04bbc5aa004\"," +
"\"city\":\"\",\"country\":\"\",\"createBy\":\"system\"," +
"\"createTime\":1656071016625,\"description\":\"\"," +
"\"headimgurl\":\"\",\"id\":127987," +
"\"memberCode\":\"1540299597507502080\"," +
"\"memberId\":62515,\"nickname\":\"\"," +
"\"openid\":\"oJ4Pl4rRiLHLfPx2Zey0YUC-89T0\"," +
"\"privilege\":\"\",\"province\":\"\"," +
"\"refreshToken\":\"\",\"sex\":-1,\"sourceDesc\":\"\",\"sourceEntity\":\"\"," +
"\"sourceId\":\"\",\"sourceType\":\"\",\"sourceUser\":0,\"status\":0,\"syncStatus\":0," +
"\"unionid\":\"oqDha5lxMuXYMGgT6gyLIFL7VumM\",\"updateBy\":\"system\",\"updateTime\":1656071016625}}*/
this.growthReportService.create(growthReport);
} else {
this.growthReportService.updateGrowthReportData(growthReportDTO.getId(), growthReport.getData());
}
}
@Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
public void asyncMemberAndUserWeixin4Iptv(MemberAndWeixinUserDTO memberAndWeixinUserDTO) {
......
......@@ -16,6 +16,26 @@ public class RabbitMqCustomConfig {
private List<Map<String, String>> list;
/**
* growthReport
* @return
*/
public Map<String, String> ucgGrowthReportInfo() {
if (CollectionUtils.isNotEmpty(list)) {
for (Map<String, String> map : list) {
String type = map.get("source");
if (type.equalsIgnoreCase("growthReport")) {
return map;
}
}
}
return null;
}
/**
* viewRecord
* @return
*/
......
......@@ -160,6 +160,53 @@ public class RabbitMqSourceConfig {
/**************************************************uc-getaway 2 uc-consumer*************************************************************/
@Value("#{rabbitMqCustomConfig.ucgGrowthReportInfo()}")
private Map<String, String> ucgGrowthReportInfo;
public static final String GROWTH_REPORT_EXCHANGE = "growthReport.exchange";
public static final String GROWTH_REPORT_QUEUE = "growthReport.queue";
public String getGrowthReportQueue(){
if (Objects.nonNull(ucgGrowthReportInfo)) {
if (MapUtils.isNotEmpty(ucgGrowthReportInfo)) {
String queue = ucgGrowthReportInfo.get("queue");
return queue;
}
}
return GROWTH_REPORT_QUEUE;
}
public String getGrowthReportSource(){
if (Objects.nonNull(ucgGrowthReportInfo)) {
if (MapUtils.isNotEmpty(ucgGrowthReportInfo)) {
String source = ucgGrowthReportInfo.get("active");
if (StringUtils.isNotBlank(source)) {
return this.chargeSource(source);
} else {
return SERVICE_;
}
}
}
return SERVICE_;
}
public String getGrowthReportStartUp(){
if (Objects.nonNull(ucgGrowthReportInfo)) {
if (MapUtils.isNotEmpty(ucgGrowthReportInfo)) {
String source = ucgGrowthReportInfo.get("active");
if (StringUtils.isNotBlank(source)) {
return "true";
}
}
}
return "false";
}
public static final String EVENT_EXCHANGE = "event.exchange";
public static final String EVENT_QUEUE = "event.queue";
......
......@@ -78,6 +78,47 @@ public class UcGatewayIptv2IptvConsumer {
}
/**
* @description 删除全部收藏记录
* @param content 消息内容
*/
@RabbitHandler
@RabbitListener(queues = "#{rabbitMqSourceConfig.getGrowthReportQueue()}",
containerFactory = "#{rabbitMqSourceConfig.getGrowthReportSource()}",
autoStartup = "#{rabbitMqSourceConfig.getGrowthReportStartUp()}",
ackMode = "AUTO")
public void dealGrowthReport(Channel channel, Message message, String content) throws IOException {
log.info("receive dealGrowthReport add message, content {}", content);
try {
JSONObject jsonObject = JSON.parseObject(content, JSONObject.class);
if (Objects.nonNull(content)) {
Object msgData = jsonObject.get("msgData");
JSONObject response = this.restTemplateClient.saveGrowthReport(JSON.toJSONString(msgData));
if (Objects.isNull(response)) {
log.error("同步大屏成长报告失败,uce接口响应超时");
}
}
} catch (Exception e) {
log.error("同步大屏成长报告失败,cause ==>> {}", e.getMessage());
if (MapUtils.isNotEmpty(error)) {
String errorStart = this.error.get("start");
if (errorStart.equalsIgnoreCase("true")) {
String fileName = this.error.get("fileName")+"_"+ LocalDate.now() +".log";
String filePath = this.error.get("filePath");
String filePath1 = filePath+fileName;
FileUtil.writeStringToFile2(filePath1, content, e.getMessage());
}
}
}
}
/**
* @description 添加收藏记录
* @param content 消息内容
......@@ -348,4 +389,10 @@ public class UcGatewayIptv2IptvConsumer {
}
}
}
}
......
......@@ -6,12 +6,14 @@ import com.topdraw.config.ResponseStatus;
import com.topdraw.mq.consumer.UcEventBusIptv2ManagementUcEngine;
import com.topdraw.mq.domain.DataSyncMsg;
import com.topdraw.mq.domain.SubscribeBean;
import com.topdraw.util.Base64Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;
import org.springframework.web.client.RestTemplate;
import javax.annotation.PostConstruct;
......@@ -153,6 +155,23 @@ public class RestTemplateClient {
return null;
}
public JSONObject saveGrowthReport(String content) {
try {
String url = BASE_URL + "/uce/userOperation/saveGrowthReport";
String encode = Base64Utils.encodeToString(content.getBytes());
log.info("request url is ==>> {} || param is ==>> {} ", url, encode);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, encode, String.class);
log.info("response ==>> {}", responseEntity);
return getParseResponseResult(responseEntity);
} catch (Exception e) {
log.error("删除所有观影记录(ApiUti.deleteAllCollection)信息时出现异常,cause ==>> {}", e.getMessage());
}
return null;
}
public JSONObject dealViewRecord(String content) {
try {
String url = BASE_URL + "/uce/userOperation/addCollection";
......
......@@ -2,6 +2,7 @@ package com.topdraw.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
......@@ -27,4 +28,28 @@ public class DateUtil {
return null;
}
public static String getWeekFirstDay() {
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar1=Calendar.getInstance();
calendar1.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
System.out.println("本周日: "+sdf.format(calendar1.getTime()));
return sdf.format(calendar1.getTime());
}
public static String getWeekLastDay() {
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar1=Calendar.getInstance();
calendar1.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
System.out.println("本周六: "+sdf.format(calendar1.getTime()));
return sdf.format(calendar1.getTime());
}
}
......
......@@ -99,6 +99,12 @@ mutil-mq:
service:
mq:
list:
- source: growthReport
exchange: growthReport.exchange
queue: growthReport.queue
exchange-type: direct
routing-key:
active: service
- source: event
exchange: event.exchange
queue: event.queue
......