mongodb插入失败异常处理

This commit is contained in:
XinWei 2024-07-23 17:59:22 +08:00
parent 1b7f9b73d7
commit f18d321415
2 changed files with 78 additions and 69 deletions

View File

@ -6,6 +6,7 @@ import cn.iocoder.yudao.module.ticket.controller.admin.checkticket.vo.vo.CheckTi
import cn.iocoder.yudao.module.ticket.controller.admin.checkticket.vo.vo.CheckTicketSaveReqVO;
import cn.iocoder.yudao.module.ticket.dal.dataobject.checkticket.CheckTicket;
import cn.iocoder.yudao.module.ticket.dal.mongodb.checkticket.CheckTicketRepository;
import com.mongodb.MongoException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -118,11 +119,13 @@ public class CheckTicketServiceImpl implements CheckTicketService {
@Override
public String createCheckTicketByMongoDB(CheckTicketSaveReqDTO checkTicketSaveReqDTO) {
CheckTicket checkTicket = BeanUtils.toBean(checkTicketSaveReqDTO, CheckTicket.class);
// TODO 插入失败会自动抛出异常需要对异常进行处理
// DuplicateKeyException当插入的文档违反唯一索引约束时会抛出此异常
// MongoWriteException在写入操作中发生的其他错误例如文档格式不正确或数据库连接问题
checkTicketRepository.insert(checkTicket);
return "添加成功";
try {
// 执行插入操作
checkTicketRepository.insert(checkTicket);
return "添加成功";
} catch (MongoException e){
return "Mongo数据库写入异常";
}
}
}

View File

@ -5,6 +5,7 @@ import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.ticket.api.saledata.dto.SaleDataSaveReqDTO;
import cn.iocoder.yudao.module.ticket.dal.dataobject.saledata.SaleData;
import cn.iocoder.yudao.module.ticket.dal.mongodb.saledata.SaleDataRepository;
import com.mongodb.MongoException;
import kotlin.jvm.internal.Lambda;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -92,11 +93,11 @@ public class SaleDataServiceImpl implements SaleDataService {
}
@Override
public List<Map<String,String>> findCheckticketcountBytime(String startTime) {
public List<Map<String, String>> findCheckticketcountBytime(String startTime) {
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
Date date1= null;
Date date1 = null;
try {
date1 = inputFormat.parse(startTime);
} catch (ParseException e) {
@ -104,11 +105,11 @@ public class SaleDataServiceImpl implements SaleDataService {
}
calendar.setTime(date1);
calendar.add(Calendar.DAY_OF_YEAR, -9);
List<Map<String,String>>map = new ArrayList<>();
while (!calendar.getTime().after(date1)){
Map<String,String>map1= new HashMap<>();
List<Map<String, String>> map = new ArrayList<>();
while (!calendar.getTime().after(date1)) {
Map<String, String> map1 = new HashMap<>();
String date = inputFormat.format(calendar.getTime());
map1.put("date",outputFormat.format(calendar.getTime()));
map1.put("date", outputFormat.format(calendar.getTime()));
map1.put("count", String.valueOf(this.countBySddate(date)));
map.add(map1);
calendar.add(Calendar.DAY_OF_YEAR, 1);
@ -120,14 +121,14 @@ public class SaleDataServiceImpl implements SaleDataService {
public List<Map<String, String>> findByGender() {
List<Map<String, String>> allByCertificateno = saleDataMapper.findByGender();
List<Map<String, String>> map = new ArrayList<>();
Map<String,String>map1=new HashMap<>();
Map<String,String>map2=new HashMap<>();
map1.put("sex","女生" );
map2.put("sex","男生" );
Map<String, String> map1 = new HashMap<>();
Map<String, String> map2 = new HashMap<>();
map1.put("sex", "女生");
map2.put("sex", "男生");
for (Map<String, String> stringMap : allByCertificateno) {
if("female".equals(stringMap.get("gender"))){
map1.put("count",stringMap.get("count"));
}else if ("male".equals(stringMap.get("gender"))){
if ("female".equals(stringMap.get("gender"))) {
map1.put("count", stringMap.get("count"));
} else if ("male".equals(stringMap.get("gender"))) {
map2.put("count", stringMap.get("count"));
}
}
@ -139,33 +140,33 @@ public class SaleDataServiceImpl implements SaleDataService {
@Override
public List<Map<String, String>> findByAge() {
List<Map<String, String>> ageRangeList = saleDataMapper.findByAge();
List<Map<String,String>>map=new ArrayList<>();
Map<String,String>map1=new LinkedHashMap<>();
Map<String,String>map2=new LinkedHashMap<>();
Map<String,String>map3=new LinkedHashMap<>();
Map<String,String>map4=new LinkedHashMap<>();
Map<String,String>map5=new LinkedHashMap<>();
Map<String,String>map6=new LinkedHashMap<>();
List<Map<String, String>> map = new ArrayList<>();
Map<String, String> map1 = new LinkedHashMap<>();
Map<String, String> map2 = new LinkedHashMap<>();
Map<String, String> map3 = new LinkedHashMap<>();
Map<String, String> map4 = new LinkedHashMap<>();
Map<String, String> map5 = new LinkedHashMap<>();
Map<String, String> map6 = new LinkedHashMap<>();
map1.put("age","20以下"); // (~, 20)
map2.put("age","20-30"); // [20, 30)
map3.put("age","30-40"); // [30, 40)
map4.put("age","40-50"); // [40, 50)
map5.put("age","50-60"); // [50, 60)
map6.put("age","60以上"); // [60, ~)
map1.put("age", "20以下"); // (~, 20)
map2.put("age", "20-30"); // [20, 30)
map3.put("age", "30-40"); // [30, 40)
map4.put("age", "40-50"); // [40, 50)
map5.put("age", "50-60"); // [50, 60)
map6.put("age", "60以上"); // [60, ~)
for (Map<String, String> stringStringMap : ageRangeList) {
if ("one".equals(stringStringMap.get("age_group"))){
map1.put("count",stringStringMap.get("count"));
}else if("two".equals(stringStringMap.get("age_group"))){
map2.put("count",stringStringMap.get("count"));
}else if("three".equals(stringStringMap.get("age_group"))){
map3.put("count",stringStringMap.get("count"));
}else if("four".equals(stringStringMap.get("age_group"))){
map4.put("count",stringStringMap.get("count"));
}else if("five".equals(stringStringMap.get("age_group"))){
map5.put("count",stringStringMap.get("count"));
}else if("six".equals(stringStringMap.get("age_group"))){
map6.put("count",stringStringMap.get("count"));
if ("one".equals(stringStringMap.get("age_group"))) {
map1.put("count", stringStringMap.get("count"));
} else if ("two".equals(stringStringMap.get("age_group"))) {
map2.put("count", stringStringMap.get("count"));
} else if ("three".equals(stringStringMap.get("age_group"))) {
map3.put("count", stringStringMap.get("count"));
} else if ("four".equals(stringStringMap.get("age_group"))) {
map4.put("count", stringStringMap.get("count"));
} else if ("five".equals(stringStringMap.get("age_group"))) {
map5.put("count", stringStringMap.get("count"));
} else if ("six".equals(stringStringMap.get("age_group"))) {
map6.put("count", stringStringMap.get("count"));
}
}
map.add(map1);
@ -189,6 +190,7 @@ public class SaleDataServiceImpl implements SaleDataService {
wrapperX.between(SaleDataDO::getSddate, start, end).select(SaleDataDO::getCertificateno, SaleDataDO::getAmount);
return saleDataMapper.selectList(wrapperX);
}
@Override
public List<SaleDataDO> findEventsThisYear() {
DateTimeFormatter date = DateTimeFormatter.ofPattern("yyyyMMdd");
@ -242,15 +244,14 @@ public class SaleDataServiceImpl implements SaleDataService {
}
@Override
public List<Map<String, String>> findBySaleMethod() {
List<SaleDataDO> saleDataList = saleDataMapper.selectList(new LambdaQueryWrapperX<SaleDataDO>().select(SaleDataDO::getTransactiontypeno));
int[] saleMethodCounts = new int[5];
Map<String,String>map=new LinkedHashMap<>();
Map<String, String> map = new LinkedHashMap<>();
for (SaleDataDO saleData : saleDataList) {
String transactiontypeno = saleData.getTransactiontypeno();
switch (transactiontypeno){
switch (transactiontypeno) {
case "SD01":
saleMethodCounts[0]++;
break;
@ -275,11 +276,11 @@ public class SaleDataServiceImpl implements SaleDataService {
map.put("特殊退票", String.valueOf(saleMethodCounts[2]));
map.put("预约单", String.valueOf(saleMethodCounts[3]));
map.put("快速购票", String.valueOf(saleMethodCounts[4]));
List<Map<String,String>>mapList=new ArrayList<>();
List<Map<String, String>> mapList = new ArrayList<>();
for (Map.Entry<String, String> stringStringEntry : map.entrySet()) {
Map<String, String> map1=new LinkedHashMap<>();
map1.put("transactiontypeno",stringStringEntry.getKey());
map1.put("count",stringStringEntry.getValue());
Map<String, String> map1 = new LinkedHashMap<>();
map1.put("transactiontypeno", stringStringEntry.getKey());
map1.put("count", stringStringEntry.getValue());
mapList.add(map1);
}
return mapList;
@ -301,7 +302,7 @@ public class SaleDataServiceImpl implements SaleDataService {
for (SaleDataDO saleData : saleDataList) {
String sddate = saleData.getSddate();
String substring = sddate.substring(6, 8);
switch (substring){
switch (substring) {
case "01":
Counts[0]++;
break;
@ -321,16 +322,16 @@ public class SaleDataServiceImpl implements SaleDataService {
break;
}
}
List<Map<String,String>>mapList=new ArrayList<>();
Map<String,String> map=new LinkedHashMap<>();
List<Map<String, String>> mapList = new ArrayList<>();
Map<String, String> map = new LinkedHashMap<>();
map.put("第一天", String.valueOf(Counts[0]));
map.put("第二天", String.valueOf(Counts[1]));
map.put("第三天", String.valueOf(Counts[2]));
map.put("第四天", String.valueOf(Counts[3]));
map.put("第五天", String.valueOf(Counts[4]));
for (Map.Entry<String, String> stringStringEntry : map.entrySet()) {
Map<String, String> map1=new LinkedHashMap<>();
map1.put("day", stringStringEntry.getKey());
Map<String, String> map1 = new LinkedHashMap<>();
map1.put("day", stringStringEntry.getKey());
map1.put("count", stringStringEntry.getValue());
mapList.add(map1);
}
@ -353,7 +354,7 @@ public class SaleDataServiceImpl implements SaleDataService {
for (SaleDataDO saleData : saleDataList) {
String sddate = saleData.getSddate();
String substring = sddate.substring(6, 8);
switch (substring){
switch (substring) {
case "01":
Counts[0]++;
break;
@ -379,8 +380,8 @@ public class SaleDataServiceImpl implements SaleDataService {
break;
}
}
List<Map<String,String>>mapList=new ArrayList<>();
Map<String,String> map=new LinkedHashMap<>();
List<Map<String, String>> mapList = new ArrayList<>();
Map<String, String> map = new LinkedHashMap<>();
map.put("第一天", String.valueOf(Counts[0]));
map.put("第二天", String.valueOf(Counts[1]));
map.put("第三天", String.valueOf(Counts[2]));
@ -389,9 +390,9 @@ public class SaleDataServiceImpl implements SaleDataService {
map.put("第六天", String.valueOf(Counts[5]));
map.put("第七天", String.valueOf(Counts[6]));
for (Map.Entry<String, String> stringStringEntry : map.entrySet()) {
Map<String, String> map1=new LinkedHashMap<>();
map1.put("day",stringStringEntry.getKey());
map1.put("count",stringStringEntry.getValue());
Map<String, String> map1 = new LinkedHashMap<>();
map1.put("day", stringStringEntry.getKey());
map1.put("count", stringStringEntry.getValue());
mapList.add(map1);
}
return mapList;
@ -411,23 +412,23 @@ public class SaleDataServiceImpl implements SaleDataService {
assert date1 != null;
calendar.setTime(date1);
calendar.add(Calendar.DAY_OF_YEAR, -29);
Map<String,String>map = new LinkedHashMap<>();
Map<String, String> map = new LinkedHashMap<>();
int count = 0;
while (!calendar.getTime().after(date1)){
while (!calendar.getTime().after(date1)) {
String date = inputFormat.format(calendar.getTime());
long i = this.countBySddate(date);
count+=i;
count += i;
calendar.add(Calendar.DAY_OF_YEAR, 1);
}
return String.valueOf(count/30);
return String.valueOf(count / 30);
}
@Override
public BigDecimal findyearJun(List<SaleDataDO> list) {
BigDecimal total=new BigDecimal(0);
BigDecimal total = new BigDecimal(0);
for (SaleDataDO saleData : list) {
BigDecimal amount = saleData.getAmount();
total=total.add(amount);
total = total.add(amount);
}
return total;
}
@ -435,8 +436,13 @@ public class SaleDataServiceImpl implements SaleDataService {
@Override
public String createSaleDataByMongoDB(SaleDataSaveReqDTO saleDataSaveReqDTO) {
SaleData saleData = BeanUtils.toBean(saleDataSaveReqDTO, SaleData.class);
saleDataRepository.insert(saleData);
return "添加成功";
try {
saleDataRepository.insert(saleData);
return "添加成功";
} catch (MongoException e) {
return "Mongo数据库写入异常";
}
}