using Infrastructure;
using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.LiveForum.Model.Liveforum;
using ZR.LiveForum.Model.Liveforum.Dto;
using ZR.Repository;
using ZR.Service.Liveforum.ILiveforumService;
namespace ZR.Service.Liveforum
{
///
/// 认证申请记录Service业务层处理
///
[AppService(ServiceType = typeof(IT_UserCertificationsService), ServiceLifetime = LifeTime.Transient)]
public class T_UserCertificationsService : BaseService, IT_UserCertificationsService
{
private readonly IT_UsersService _usersService;
private readonly IT_MessagesService _messagesService;
public T_UserCertificationsService(IT_UsersService usersService, IT_MessagesService messagesService)
{
_usersService = usersService;
_messagesService = messagesService;
}
///
/// 查询认证申请记录列表
///
///
///
public PagedInfo GetList(T_UserCertificationsQueryDto parm)
{
var predicate = QueryExp(parm);
var response = Queryable()
//.OrderBy("Id desc")
.Where(predicate.ToExpression())
.ToPage(parm);
return response;
}
///
/// 获取详情
///
///
///
public T_UserCertifications GetInfo(long Id)
{
var response = Queryable()
.Where(x => x.Id == Id)
.First();
return response;
}
///
/// 添加认证申请记录
///
///
///
public T_UserCertifications AddT_UserCertifications(T_UserCertifications model)
{
return Insertable(model).ExecuteReturnEntity();
}
///
/// 修改认证申请记录
///
///
///
public int UpdateT_UserCertifications(T_UserCertifications model)
{
return Update(model, true);
}
///
/// 审核认证申请
///
/// 审核参数
/// 审核人用户ID
///
public int ReviewCertification(ReviewCertificationDto parm, long reviewerId)
{
// 验证审核状态
if (parm.Status != 1 && parm.Status != 2)
{
throw new CustomException(ResultCode.CUSTOM_ERROR, "审核状态无效,只能为1(通过)或2(拒绝)");
}
// 验证拒绝时拒绝原因必填
if (parm.Status == 2 && string.IsNullOrWhiteSpace(parm.RejectReason))
{
throw new CustomException(ResultCode.CUSTOM_ERROR, "审核拒绝时,拒绝原因不能为空");
}
// 获取认证记录
var certification = GetById(parm.Id);
if (certification == null)
{
throw new CustomException(ResultCode.CUSTOM_ERROR, "认证申请记录不存在");
}
// 验证状态为待审核
if (certification.Status != 0)
{
throw new CustomException(ResultCode.CUSTOM_ERROR, "该认证申请记录已审核,不能重复审核");
}
// 使用事务处理
return Context.Ado.UseTran(() =>
{
// 更新认证申请记录
certification.Status = parm.Status;
certification.ReviewerId = reviewerId;
certification.ReviewedAt = DateTime.Now;
if (parm.Status == 2)
{
certification.RejectReason = parm.RejectReason;
}
Update(certification, false);
// 如果审核通过,更新用户表
if (parm.Status == 1)
{
var user = _usersService.GetById(certification.UserId);
if (user == null)
{
throw new CustomException(ResultCode.CUSTOM_ERROR, "用户不存在");
}
user.CertifiedType = 1;
user.IsCertified = true;
_usersService.UpdateT_Users(user);
}
// 创建消息记录(审核通过或拒绝都需要发送消息)
var messageContent = parm.Status == 1
? "您的认证申请已通过审核"
: $"您的认证申请未通过审核,原因:{parm.RejectReason}";
_messagesService.SendSystemNotification(certification.UserId, messageContent, certification.Id);
return 1;
}).IsSuccess ? 1 : 0;
}
///
/// 导出认证申请记录
///
///
///
public PagedInfo ExportList(T_UserCertificationsQueryDto parm)
{
parm.PageNum = 1;
parm.PageSize = 100000;
var predicate = QueryExp(parm);
var response = Queryable()
.Where(predicate.ToExpression())
.Select((it) => new T_UserCertificationsDto()
{
CertificationTypeLabel = it.CertificationType.GetConfigValue("liveforum_sk"),
StatusLabel = it.Status.GetConfigValue("liveforum_sk_review"),
}, true)
.ToPage(parm);
return response;
}
///
/// 查询导出表达式
///
///
///
private static Expressionable QueryExp(T_UserCertificationsQueryDto parm)
{
var predicate = Expressionable.Create();
predicate = predicate.AndIF(parm.UserId != null, it => it.UserId == parm.UserId);
predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.CertificationType), it => it.CertificationType == parm.CertificationType);
predicate = predicate.AndIF(parm.Status != null, it => it.Status == parm.Status);
return predicate;
}
}
}