Merge pull request '票务抽数改造' (#55) from zzw-one into master

Reviewed-on: #55
This commit is contained in:
root 2024-08-28 17:33:29 +08:00
commit 1dc70860a6
15 changed files with 331 additions and 28 deletions

View File

@ -0,0 +1,21 @@
package cn.iocoder.yudao.module.datacenter.dal.dataobject.checkticket;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;
@Document("check_ticket_today")
//@Document("checktickettest")
@Data
public class CheckTicketToday {
private String id;
private String dataId;
private String checkstation;
private String checkstationname;
private String ticket;
private String checkticketdate;
private String checktickettime;
private Integer personcount;
private String sdshipping;
private String salepropetyvaluename;
}

View File

@ -0,0 +1,27 @@
package cn.iocoder.yudao.module.datacenter.dal.dataobject.saledata;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;
import java.math.BigDecimal;
@Document("sale_data_today")
@Data
public class SaleDataToday {
private String id;
private String dataId;
private String sdno;
private String transactiontypeno;
private String sddate;
private String sdtime;
private BigDecimal amount;
private Integer quantity;
private String certificatetype;
private String certificateno;
private String itemtype;
private String itemtypename;
private String item;
private String itemname;
private String productbatchno;
private String salepropetyvaluename;
}

View File

@ -0,0 +1,28 @@
package cn.iocoder.yudao.module.datacenter.dal.mongodb.checkticket;
import cn.iocoder.yudao.module.datacenter.dal.dataobject.checkticket.CheckTicket;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface CheckTicketTodayRepository extends MongoRepository<CheckTicket,String> {
@Query("{'name': {'$regex': '?0', '$options': 'i'}}")
Page<CheckTicket> findByName(String q, PageRequest pageable);
@Query("{'checkticketdate':{'$gte': ?0, '$lt': ?1}}")
List<CheckTicket> countByCheckticketdateBetween(String starTime, String endTime);
@Query("{'checktickettime':{'$gte': ?0, '$lt': ?1},'checkticketdate':{'$dt': ?2}}")
List<CheckTicket> findByTimestampBetweenAndCheckticketdate(String startTime, String endTime,String day);
List<CheckTicket> findByCheckticketdateAndChecktickettimeBetween(String day,String startTime, String endTime);
@Query("{'checkticketdate':{'$gte': ?0, '$lt': ?1} , 'checktickettime':{'$gte': ?2, '$lt': ?3}}")
List<CheckTicket> findByCheckticketdateBetweenAndChecktickettimeBetween(String startTime, String endTime,String beingTime, String resultTime);
}

View File

@ -0,0 +1,68 @@
package cn.iocoder.yudao.module.datacenter.dal.mongodb.saledata;
import cn.iocoder.yudao.module.datacenter.controller.app.saledata.vo.AgeVo;
import cn.iocoder.yudao.module.datacenter.controller.app.saledata.vo.AggregationVO;
import cn.iocoder.yudao.module.datacenter.controller.app.saledata.vo.SaleDataItemTypeVo;
import cn.iocoder.yudao.module.datacenter.dal.dataobject.saledata.SaleData;
import cn.iocoder.yudao.module.datacenter.dal.dataobject.saledata.SaleDataToday;
import org.springframework.data.mongodb.repository.Aggregation;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface SaleDataTodayRepository extends MongoRepository<SaleDataToday,String> {
/*@Query("{'name': {'$regex': '?0', '$options': 'i'}}")
Page<SaleData> findByName(String q, PageRequest pageable);*/
int countBySddate(String day);
@Query(value = "{'sddate': { $gte: ?0, $lt: ?1 }}", fields = "{ 'sddate': 1 }")
List<String> countBySddateToTime(String starTime, String endTime);
// @Query("{ 'sddate' : { $gte: ?0, $lte: ?1 } }")
@Query(value = "{ 'sddate': { $gte: ?0, $lte: ?1 } }", fields = "{ 'certificateno': 1, 'amount': 1}")
List<SaleData>findBySddateBetween(String begin,String end);
@Aggregation(pipeline = {
"{$project: {certLength: {$strLenCP: '$certificateno'}, secondLastChar: {$substrCP: ['$certificateno', {$subtract: [{$strLenCP: '$certificateno'}, 2]}, 1]}}}",
"{$group: {_id: null, maleCount: {$sum: {$cond: {if: {$eq: [{$mod: [{$toInt: '$secondLastChar'}, 2]}, 1]}, then: 1, else: 0}}}, femaleCount: {$sum: {$cond: {if: {$eq: [{$mod: [{$toInt: '$secondLastChar'}, 2]}, 0]}, then: 1, else: 0}}}, totalCount: {$sum: 1}}}",
"{$project: {_id: 0, maleCount: 1, femaleCount: 1, maleRatio: {$divide: ['$maleCount', '$totalCount']}}}"
})
AggregationVO findAllByCertificateno();
@Aggregation(pipeline = {
"{$match: {sddate: { $gte: ?0, $lte: ?1 }}}",
"{$project: {certLength: {$strLenCP: '$certificateno'}, secondLastChar: {$substrCP: ['$certificateno', {$subtract: [{$strLenCP: '$certificateno'}, 2]}, 1]}}}",
"{$group: {_id: null, maleCount: {$sum: {$cond: {if: {$eq: [{$mod: [{$toInt: '$secondLastChar'}, 2]}, 1]}, then: 1, else: 0}}}, femaleCount: {$sum: {$cond: {if: {$eq: [{$mod: [{$toInt: '$secondLastChar'}, 2]}, 0]}, then: 1, else: 0}}}, totalCount: {$sum: 1}}}",
"{$project: {_id: 0, maleCount: 1, femaleCount: 1, maleRatio: {$divide: ['$maleCount', '$totalCount']}}}"
})
AggregationVO findAllByCertificatenoSDTime(String starTime, String endTime);
@Aggregation(pipeline = {
"{$match: {sddate: { $gte: ?0, $lte: ?1 }}}",
"{$match: {certificateno: {$regex: '^.{18}$'}}}",
"{$project: {birthYear: {$toInt: {$substr: ['$certificateno', 6, 4]}}, currentYear: {$year: '$$NOW'}}}",
"{$addFields: {age: {$subtract: ['$currentYear', '$birthYear']}}}",
"{$group: {_id: null, one: {$sum: {$cond: [{$lte: ['$age', 20]}, 1, 0]}}, " +
"two: {$sum: {$cond: [{$and: [{$gt: ['$age', 20]}, {$lte: ['$age', 30]}]}, 1, 0]}}, " +
"three: {$sum: {$cond: [{$and: [{$gt: ['$age', 30]}, {$lte: ['$age', 40]}]}, 1, 0]}}, " +
"four: {$sum: {$cond: [{$and: [{$gt: ['$age', 40]}, {$lte: ['$age', 50]}]}, 1, 0]}}, " +
"five: {$sum: {$cond: [{$and: [{$gt: ['$age', 50]}, {$lte: ['$age', 60]}]}, 1, 0]}}, " +
"six: {$sum: {$cond: [{$gt: ['$age', 60]}, 1, 0]}}}}"
})
AgeVo findByAge(String starTime, String endTime);
@Query(value = "{}", fields = "{ 'itemtypename': 1 }")
List<SaleDataItemTypeVo> findAllByItemtypename();
@Query(value = "{'sddate': { $gte: ?0, $lt: ?1 }}", fields = "{ 'itemname': 1 }")
List<SaleDataItemTypeVo> findAllByItemtypenameTime(String starTime, String endTime);
@Query(value = "{'sddate': { $gte: ?0, $lt: ?1 }}", fields = "{ 'transactiontypeno': 1 }")
List<SaleData> findAllByTransactiontypenoTime(String starTime, String endTime);
}

View File

@ -1,10 +1,8 @@
package cn.iocoder.yudao.module.datacenter.service.checkticket;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.datacenter.controller.app.saledata.vo.AggregationVO;
import cn.iocoder.yudao.module.datacenter.dal.dataobject.checkticket.CheckTicket;
import cn.iocoder.yudao.module.datacenter.dal.mongodb.checkticket.CheckTicketRepository;
import cn.iocoder.yudao.module.datacenter.dal.mongodb.checkticket.CheckTicketTodayRepository;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
@ -17,7 +15,6 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
/**
* 检票 Service 实现类
@ -29,7 +26,7 @@ import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionU
public class CheckTicketServiceImpl implements CheckTicketService {
@Resource
private CheckTicketRepository checkTicketRepository;
private CheckTicketTodayRepository checkTicketRepository;
@Override

View File

@ -8,7 +8,7 @@ import cn.iocoder.yudao.module.datacenter.controller.app.saledata.vo.AgeVo;
import cn.iocoder.yudao.module.datacenter.controller.app.saledata.vo.AggregationVO;
import cn.iocoder.yudao.module.datacenter.controller.app.saledata.vo.SaleDataItemTypeVo;
import cn.iocoder.yudao.module.datacenter.dal.dataobject.saledata.SaleData;
import cn.iocoder.yudao.module.datacenter.dal.mongodb.saledata.SaleDataRepository;
import cn.iocoder.yudao.module.datacenter.dal.mongodb.saledata.SaleDataTodayRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -16,8 +16,6 @@ import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
@ -32,7 +30,7 @@ import java.util.stream.Collectors;
public class SaleDataServiceImpl implements SaleDataService {
@Autowired
private SaleDataRepository saleDataRepository;
private SaleDataTodayRepository saleDataRepository;
public String publicMethod(String endTime){
// 创建 DateTimeFormatter 实例以解析指定日期的格式

View File

@ -0,0 +1,21 @@
package cn.iocoder.yudao.module.infra.dal.dataobject.checkticket;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;
@Document("checkticket")
//@Document("checktickettest")
@Data
public class CheckTicketToday {
private String id;
private String dataId;
private String checkstation;
private String checkstationname;
private String ticket;
private String checkticketdate;
private String checktickettime;
private Integer personcount;
private String sdshipping;
private String salepropetyvaluename;
}

View File

@ -0,0 +1,27 @@
package cn.iocoder.yudao.module.infra.dal.dataobject.saledata;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;
import java.math.BigDecimal;
@Document("sale_data_today")
@Data
public class SaleDataToday {
private String id;
private String dataId;
private String sdno;
private String transactiontypeno;
private String sddate;
private String sdtime;
private BigDecimal amount;
private Integer quantity;
private String certificatetype;
private String certificateno;
private String itemtype;
private String itemtypename;
private String item;
private String itemname;
private String productbatchno;
private String salepropetyvaluename;
}

View File

@ -0,0 +1,12 @@
package cn.iocoder.yudao.module.infra.dal.mongodb.checkticket;
import cn.iocoder.yudao.module.infra.dal.dataobject.checkticket.CheckTicketToday;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CheckTicketTodayRepository extends MongoRepository<CheckTicketToday,String> {
}

View File

@ -0,0 +1,65 @@
package cn.iocoder.yudao.module.infra.dal.mongodb.saledata;
import cn.iocoder.yudao.module.infra.dal.dataobject.saledata.SaleData;
import cn.iocoder.yudao.module.infra.dal.dataobject.saledata.SaleDataToday;
import org.springframework.data.mongodb.repository.Aggregation;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface SaleDataTodayRepository extends MongoRepository<SaleDataToday,String> {
// /*@Query("{'name': {'$regex': '?0', '$options': 'i'}}")
// Page<SaleData> findByName(String q, PageRequest pageable);*/
// int countBySddate(String day);
//
// @Query(value = "{'sddate': { $gte: ?0, $lt: ?1 }}", fields = "{ 'sddate': 1 }")
// List<String> countBySddateToTime(String starTime, String endTime);
//
//
//// @Query("{ 'sddate' : { $gte: ?0, $lte: ?1 } }")
// @Query(value = "{ 'sddate': { $gte: ?0, $lte: ?1 } }", fields = "{ 'certificateno': 1, 'amount': 1}")
// List<SaleData>findBySddateBetween(String begin, String end);
//
// @Aggregation(pipeline = {
// "{$project: {certLength: {$strLenCP: '$certificateno'}, secondLastChar: {$substrCP: ['$certificateno', {$subtract: [{$strLenCP: '$certificateno'}, 2]}, 1]}}}",
// "{$group: {_id: null, maleCount: {$sum: {$cond: {if: {$eq: [{$mod: [{$toInt: '$secondLastChar'}, 2]}, 1]}, then: 1, else: 0}}}, femaleCount: {$sum: {$cond: {if: {$eq: [{$mod: [{$toInt: '$secondLastChar'}, 2]}, 0]}, then: 1, else: 0}}}, totalCount: {$sum: 1}}}",
// "{$project: {_id: 0, maleCount: 1, femaleCount: 1, maleRatio: {$divide: ['$maleCount', '$totalCount']}}}"
// })
// AggregationVO findAllByCertificateno();
//
// @Aggregation(pipeline = {
// "{$match: {sddate: { $gte: ?0, $lte: ?1 }}}",
// "{$project: {certLength: {$strLenCP: '$certificateno'}, secondLastChar: {$substrCP: ['$certificateno', {$subtract: [{$strLenCP: '$certificateno'}, 2]}, 1]}}}",
// "{$group: {_id: null, maleCount: {$sum: {$cond: {if: {$eq: [{$mod: [{$toInt: '$secondLastChar'}, 2]}, 1]}, then: 1, else: 0}}}, femaleCount: {$sum: {$cond: {if: {$eq: [{$mod: [{$toInt: '$secondLastChar'}, 2]}, 0]}, then: 1, else: 0}}}, totalCount: {$sum: 1}}}",
// "{$project: {_id: 0, maleCount: 1, femaleCount: 1, maleRatio: {$divide: ['$maleCount', '$totalCount']}}}"
// })
// AggregationVO findAllByCertificatenoSDTime(String starTime, String endTime);
//
// @Aggregation(pipeline = {
// "{$match: {sddate: { $gte: ?0, $lte: ?1 }}}",
// "{$match: {certificateno: {$regex: '^.{18}$'}}}",
// "{$project: {birthYear: {$toInt: {$substr: ['$certificateno', 6, 4]}}, currentYear: {$year: '$$NOW'}}}",
// "{$addFields: {age: {$subtract: ['$currentYear', '$birthYear']}}}",
// "{$group: {_id: null, one: {$sum: {$cond: [{$lte: ['$age', 20]}, 1, 0]}}, " +
// "two: {$sum: {$cond: [{$and: [{$gt: ['$age', 20]}, {$lte: ['$age', 30]}]}, 1, 0]}}, " +
// "three: {$sum: {$cond: [{$and: [{$gt: ['$age', 30]}, {$lte: ['$age', 40]}]}, 1, 0]}}, " +
// "four: {$sum: {$cond: [{$and: [{$gt: ['$age', 40]}, {$lte: ['$age', 50]}]}, 1, 0]}}, " +
// "five: {$sum: {$cond: [{$and: [{$gt: ['$age', 50]}, {$lte: ['$age', 60]}]}, 1, 0]}}, " +
// "six: {$sum: {$cond: [{$gt: ['$age', 60]}, 1, 0]}}}}"
// })
// AgeVo findByAge(String starTime, String endTime);
//
// @Query(value = "{}", fields = "{ 'itemtypename': 1 }")
// List<SaleDataItemTypeVo> findAllByItemtypename();
//
// @Query(value = "{'sddate': { $gte: ?0, $lt: ?1 }}", fields = "{ 'itemname': 1 }")
// List<SaleDataItemTypeVo> findAllByItemtypenameTime(String starTime, String endTime);
//
// @Query(value = "{'sddate': { $gte: ?0, $lt: ?1 }}", fields = "{ 'transactiontypeno': 1 }")
// List<SaleData> findAllByTransactiontypenoTime(String starTime, String endTime);
}

View File

@ -7,8 +7,10 @@ import cn.iocoder.yudao.framework.common.util.date.TickingDateUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.infra.dal.dataobject.checkticket.CheckTicket;
import cn.iocoder.yudao.module.infra.dal.dataobject.checkticket.CheckTicketDO;
import cn.iocoder.yudao.module.infra.dal.dataobject.checkticket.CheckTicketToday;
import cn.iocoder.yudao.module.infra.dal.dataobject.saledata.SaleData;
import cn.iocoder.yudao.module.infra.dal.dataobject.saledata.SaleDataDO;
import cn.iocoder.yudao.module.infra.dal.dataobject.saledata.SaleDataToday;
import cn.iocoder.yudao.module.infra.dal.mongodb.checkticket.CheckTicketRepository;
import cn.iocoder.yudao.module.infra.dal.mongodb.saledata.SaleDataRepository;
import cn.iocoder.yudao.module.infra.dal.mysql.checkticket.CheckTicketMapper;
@ -148,7 +150,7 @@ public class TicketDataMigration {
for (; pageNumber <= totalPages; pageNumber++) {
ticketingSamplingRespVO = this.getUrlResponseData(SALE_DATA_SERVICE, SALE_DATA_METHOD, TickingDateUtils.getNowDayFormat(), pageNumber);
mapList = ticketingSamplingRespVO.getDataMapList();
List<Integer> resultNum = saleDataService.insertByMongoDB(BeanUtils.toBean(mapList, SaleData.class));
List<Integer> resultNum = saleDataService.insertTodayByMongoDB(BeanUtils.toBean(mapList, SaleDataToday.class));
if (resultNum == null) {
XxlJobHelper.log("数据批量插入异常!", pageNumber);
} else {
@ -179,7 +181,7 @@ public class TicketDataMigration {
for (; pageNumber <= totalPages; pageNumber++) {
ticketingSamplingRespVO = this.getUrlResponseData(CHECK_TICKET_SERVICE, CHECK_TICKET_METHOD, date, pageNumber);
mapList = ticketingSamplingRespVO.getDataMapList();
List<Integer> resultNum = checkTicketService.insertByMongoDB(BeanUtils.toBean(mapList, CheckTicket.class));
List<Integer> resultNum = checkTicketService.insertTodayByMongoDB(BeanUtils.toBean(mapList, CheckTicketToday.class));
if (resultNum == null) {
XxlJobHelper.log("数据批量插入异常!", pageNumber);
} else {

View File

@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.infra.controller.checkticket.vo.CheckTicketSaveReqVO;
import cn.iocoder.yudao.module.infra.dal.dataobject.checkticket.CheckTicket;
import cn.iocoder.yudao.module.infra.dal.dataobject.checkticket.CheckTicketDO;
import cn.iocoder.yudao.module.infra.dal.dataobject.checkticket.CheckTicketToday;
import javax.validation.Valid;
import java.util.List;
@ -37,4 +38,5 @@ public interface CheckTicketService {
* @return java.util.List<java.lang.Integer> 插入数据和重复数据
*/
List<Integer> insertByMongoDB(List<CheckTicket> checkTicketList);
List<Integer> insertTodayByMongoDB(List<CheckTicketToday> checkTicketList);
}

View File

@ -5,7 +5,9 @@ import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.infra.dal.dataobject.checkticket.CheckTicket;
import cn.iocoder.yudao.module.infra.dal.dataobject.checkticket.CheckTicketDO;
import cn.iocoder.yudao.module.infra.dal.dataobject.checkticket.CheckTicketToday;
import cn.iocoder.yudao.module.infra.dal.mongodb.checkticket.CheckTicketRepository;
import cn.iocoder.yudao.module.infra.dal.mongodb.checkticket.CheckTicketTodayRepository;
import cn.iocoder.yudao.module.infra.dal.mysql.checkticket.CheckTicketMapper;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.Slave;
@ -38,6 +40,8 @@ public class CheckTicketServiceImpl implements CheckTicketService {
private CheckTicketMapper checkTicketMapper;
@Resource
private CheckTicketRepository checkTicketRepository;
@Resource
private CheckTicketTodayRepository checkTicketTodayRepository;
@Override
public Long createCheckTicket(CheckTicketDO checkTicketDO) {
@ -75,4 +79,24 @@ public class CheckTicketServiceImpl implements CheckTicketService {
return resultNum;
}
@Override
public List<Integer> insertTodayByMongoDB(List<CheckTicketToday> checkTicketList) {
List<Integer> resultNum = new ArrayList<>(2);
// 把检票数据的dataId提取出来成数组
List<String> dataIdList = checkTicketList.stream().map(CheckTicketToday::getDataId).collect(Collectors.toList());
// 获取重复的dataId
List<String> duplicatesDataId = this.checkDuplicatesByMongoDB(dataIdList);
// 对checkTicketList过滤掉重复的dataId对应的数据得到需要插入的不重复数据
List<CheckTicketToday> insertCheckTicketList = checkTicketList.stream().filter(ticket -> !duplicatesDataId.contains(ticket.getDataId())).collect(Collectors.toList());
try {
// 批量插入
checkTicketTodayRepository.insert(insertCheckTicketList);
} catch (MongoException e){
return null;
}
resultNum.add(insertCheckTicketList.size());
resultNum.add(duplicatesDataId.size());
return resultNum;
}
}

View File

@ -6,6 +6,7 @@ import cn.iocoder.yudao.module.infra.controller.saledata.vo.SaleDataSaveReqVO;
import cn.iocoder.yudao.module.infra.dal.dataobject.checkticket.CheckTicket;
import cn.iocoder.yudao.module.infra.dal.dataobject.saledata.SaleData;
import cn.iocoder.yudao.module.infra.dal.dataobject.saledata.SaleDataDO;
import cn.iocoder.yudao.module.infra.dal.dataobject.saledata.SaleDataToday;
import javax.validation.Valid;
import java.math.BigDecimal;
@ -40,4 +41,5 @@ public interface SaleDataService {
* @return java.util.List<java.lang.Integer>
*/
List<Integer> insertByMongoDB(List<SaleData> saleDataList);
List<Integer> insertTodayByMongoDB(List<SaleDataToday> saleDataList);
}

View File

@ -1,35 +1,20 @@
package cn.iocoder.yudao.module.infra.service.saledata;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.common.util.ticket.IdCardUtil;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.infra.controller.saledata.vo.SaleDataSaveReqVO;
import cn.iocoder.yudao.module.infra.dal.dataobject.checkticket.CheckTicket;
import cn.iocoder.yudao.module.infra.dal.dataobject.saledata.SaleData;
import cn.iocoder.yudao.module.infra.dal.dataobject.saledata.SaleDataDO;
import cn.iocoder.yudao.module.infra.dal.dataobject.saledata.SaleDataToday;
import cn.iocoder.yudao.module.infra.dal.mongodb.saledata.SaleDataRepository;
import cn.iocoder.yudao.module.infra.dal.mongodb.saledata.SaleDataTodayRepository;
import cn.iocoder.yudao.module.infra.dal.mysql.saledata.SaleDataMapper;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.Slave;
import com.mongodb.DuplicateKeyException;
import com.mongodb.MongoException;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.exact;
/**
* 售票 Service 实现类
@ -41,6 +26,10 @@ import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatc
public class SaleDataServiceImpl implements SaleDataService {
@Resource
private SaleDataRepository saleDataRepository;
@Resource
private SaleDataTodayRepository saleDataTodayRepository;
@Resource
private SaleDataMapper saleDataMapper;
@ -81,5 +70,25 @@ public class SaleDataServiceImpl implements SaleDataService {
return resultNum;
}
@Override
public List<Integer> insertTodayByMongoDB(List<SaleDataToday> saleDataList) {
List<Integer> resultNum = new ArrayList<>(2);
// 把检票数据的dataId提取出来成数组
List<String> dataIdList = saleDataList.stream().map(SaleDataToday::getDataId).collect(Collectors.toList());
// 获取重复的dataId
List<String> duplicatesDataId = this.checkDuplicatesByMongoDB(dataIdList);
// 对checkTicketList过滤掉重复的dataId对应的数据得到需要插入的不重复数据
List<SaleDataToday> insertSaleDataList = saleDataList.stream().filter(saleData -> !duplicatesDataId.contains(saleData.getDataId())).collect(Collectors.toList());
try {
// 批量插入
saleDataTodayRepository.insert(insertSaleDataList);
} catch (MongoException e){
return null;
}
resultNum.add(insertSaleDataList.size());
resultNum.add(duplicatesDataId.size());
return resultNum;
}
}