public bool SignOut(SignOutInDto inDto)
{
try
{
//判断学生是否存在
var student = CheckStudent(inDto.StudentId);
log(student.Account + " " + student.Name + "开始签退", "StuSignLog");
//判断活动是否存在
var activity = CheckActivity(inDto.ActivityId);
//获取活动报名信息
var activityApply = GetActivityApplyList(activity.Id).FirstOrDefault(x => x.Type == 0 && x.UserId == student.Id && x is {IsPass: 1, IsCancel: 0});
if (activityApply == null)
{
log(student.Account + " " + student.Name + "您没有报名该课程,无法签退", "StuSignLog");
throw new ApiException("您没有报名该课程,无法签退");
}
if (activityApply.IsSign == 0)
{
log(student.Account + " " + student.Name + "您还没有签到,无法签退", "StuSignLog");
throw new ApiException("您还没有签到,无法签退");
}
if (activityApply is {IsSign: 1, IsSignOut: 1})
{
log(student.Account + " " + student.Name + "您已经签退成功", "StuSignLog");
throw new ApiException("您已经签退成功");
}
//签退时间为活动结束前半小时至结束后5分钟
if (activity.ActivityEndTime.Value.AddMinutes(-30) > DateTime.Now)
{
log(student.Account + " " + student.Name + "课程结束前半小时才能签退", "StuSignLog");
throw new ApiException("课程结束前半小时才能签退");
}
//
if (activity.ActivityEndTime.Value.AddMinutes(5) < DateTime.Now)
{
log(student.Account + " " + student.Name + "该课程已结束超过5分钟,无法签退", "StuSignLog");
throw new ApiException("该课程已结束超过5分钟,无法签退");
}
//开启了限制定位100米内
if (activity.RestrictedLocation == 1)
{
if (string.IsNullOrEmpty(inDto.Coordinates))
{
log(student.Account + " " + student.Name + "定位信息不可为空", "StuSignLog");
throw new ApiException("定位信息不可为空");
}
//判断定位是否在100米内
var activityCoordinates = activity.Coordinates.Split(",");
var coordinates = inDto.Coordinates.Split(",");
var lat1 = double.Parse(activityCoordinates[0]);
var lng1 = double.Parse(activityCoordinates[1]);
var lat2 = double.Parse(coordinates[0]);
var lng2 = double.Parse(coordinates[1]);
var distance = MapHelper.GetDistance(lat1, lng1, lat2, lng2);
if (distance > 100)
{
log(student.Account + " " + student.Name + "您不在课程现场,无法签退", "StuSignLog");
throw new ApiException("您不在课程现场,无法签退");
}
}
//执行签退
activityApply.IsSignOut = 1;
activityApply.SignOutTime = DateTime.Now;
//签退成功获得学时、学分
//获取我的活动课程
var myCourse = _myDb.Sets<MyCourse>()
.FirstOrDefault(x => x.StudentId == student.Id && x.Type == 3 && x.RelationId == activity.Id);
if (myCourse != null)
{
//更新我的学时
myCourse.IsFinish = true;
myCourse.GetScore = activity.Score;
myCourse.GetScoreTime = DateTime.Now;
}
else
{
//添加我的课程
var addCourse = new MyCourse
{
StudentId = student.Id,
Type = 3,
RelationId = activity.Id,
JoinTime = DateTime.Now,
IsFinish = true,
GetScore = activity.Score,
GetScoreTime = DateTime.Now
};
_myDb.Add(addCourse);
}
_myDb.SaveChanges();
log(student.Account + " " + student.Name + "签退完成", "StuSignLog");
return true;
}
catch (Exception ex)
{
log(inDto.ToJson() + "签退失败,失败原因:" + ex.Message , "StuSignLog");
throw new ApiException($"{ex.Message}");
}
}
数据库类型:腾讯云数据库 SQL server
框架:.Net6.0
语言:C#
代码功能:一个活动签退接口
问题描述:活动人数在1000人以内,活动开始签到或者签退时,会正常一会(几十个人能签到、签退成功),然后数据库就直接挂掉了,无法连接。同一个项目下也有其他的功能,比如活动报名,那个参与人数在3000人左右,那个就很正常,没有导致数据库挂掉的情况出现。这个签到、签退的数据交互用的事Linq的方式,没有用到事务。活动报名的用的是ADO.net的方式写SQL语句来进行数据交互的,不知道会不会和这个有关。