/// <summary> /// 新建实体时使用 /// </summary> public static RatingRecord New() { RatingRecord ratingRecord = new RatingRecord() { DateCreated = DateTime.UtcNow, RateNumber = 1, TenantTypeId = string.Empty }; return(ratingRecord); }
/// <summary> /// 对操作对象进行星级评价操作 /// </summary> /// <param name="objectId">操作对象Id</param> /// <param name="tenantTypeId">租户类型Id</param> /// <param name="userId">用户的UserId</param> /// <param name="rateNumber">星级类型</param> /// <param name="ownerId">拥有者Id</param> public bool Rated(long objectId, string tenantTypeId, long userId, int rateNumber, long ownerId = 0) { RatingSettings ratingSettings = ratingSettingsManager.Get(); //执行Sql语句的集合 List <Sql> sqls = new List <Sql>(); //判断输入的等级数是否符合标准 if (rateNumber < 1 || rateNumber > 5) { return(false); } PetaPocoDatabase dao = CreateDAO(); dao.OpenSharedConnection(); //先判断是否存在评价信息 var sql = Sql.Builder; sql.Select("Count(*)") .From("tn_Ratings") .Where("ObjectId = @0 AND TenantTypeId = @1", objectId, tenantTypeId); if (ownerId > 0) { sql.Where("OwnerId = @0", ownerId); } int affectCount = dao.ExecuteScalar <int>(sql); //如果记录为0则不存在则先创建评价信息 if (affectCount == 0) { //创建 评价信息 sqls.Add(Sql.Builder.Append("INSERT INTO tn_Ratings (ObjectId, TenantTypeId, OwnerId, RateCount, Comprehensive , RateSum ) VALUES (@0, @1, @2,1,@3,@4)", objectId, tenantTypeId, ownerId, rateNumber, rateNumber)); //创建时直接添加新记录数据 for (int i = 1; i <= 5; i++) { if (rateNumber == i) { //如果等级是用户所选择的等级则直接插入数据 并且创建记录 sqls.Add(Sql.Builder.Append("INSERT INTO tn_RatingGrades (ObjectId, TenantTypeId, RateNumber, RateCount) VALUES (@0,@1,@2,1)", objectId, tenantTypeId, i)); sqls.Add(Sql.Builder.Append("INSERT INTO tn_RatingRecords (ObjectId, TenantTypeId, RateNumber, UserId, DateCreated) VALUES (@0,@1,@2,@3,@4)", objectId, tenantTypeId, i, userId, DateTime.UtcNow)); continue; } sqls.Add(Sql.Builder.Append("INSERT INTO tn_RatingGrades (ObjectId, TenantTypeId, RateNumber, RateCount) VALUES (@0,@1,@2,@3)", objectId, tenantTypeId, i, 0)); } } else { //判断是都有过评价记录 sql = Sql.Builder; sql.Select("*") .From("tn_RatingRecords") .Where("ObjectId = @0 AND TenantTypeId = @1 AND UserId = @2", objectId, tenantTypeId, userId); //获取评价的记录 RatingRecord ratingRecord = dao.FirstOrDefault <RatingRecord>(sql); if (ratingRecord == null) { sqls.Add(Sql.Builder.Append("INSERT INTO tn_RatingRecords (ObjectId, TenantTypeId, RateNumber, UserId, DateCreated) VALUES (@0,@1,@2,@3,@4)", objectId, tenantTypeId, rateNumber, userId, DateTime.UtcNow)); //更新信息评价数据 sqls.Add(Sql.Builder.Append("UPDATE tn_Ratings SET RateCount = RateCount + 1, Comprehensive=(RateSum + @0)/(RateCount + 1.0) , RateSum=RateSum + @0 where ObjectId = @1 and TenantTypeId = @2 and OwnerId = @3", rateNumber, objectId, tenantTypeId, ownerId)); //更新评价等级统计 sqls.Add(Sql.Builder.Append("UPDATE tn_RatingGrades SET RateCount = RateCount + 1 where ObjectId = @0 and TenantTypeId = @1 and RateNumber = @2", objectId, tenantTypeId, rateNumber)); } else { //用户 再次评价 先判断是否可修改 if (ratingSettings.IsModify) { //先检测是否是之前选过的等级 if (ratingRecord.RateNumber != rateNumber) { sqls.Add(Sql.Builder.Append("UPDATE tn_RatingRecords SET RateNumber = @0 WHERE ObjectId = @1 AND TenantTypeId = @2 AND UserId = @3", rateNumber, objectId, tenantTypeId, userId)); //先更新之前的等级统计 sqls.Add(Sql.Builder.Append("UPDATE tn_RatingGrades SET RateCount = RateCount - 1 where ObjectId = @0 and TenantTypeId = @1 and RateNumber = @2", objectId, tenantTypeId, ratingRecord.RateNumber)); //然后更新现在的最新等级统计 sqls.Add(Sql.Builder.Append("UPDATE tn_RatingGrades SET RateCount = RateCount + 1 where ObjectId = @0 and TenantTypeId = @1 and RateNumber = @2", objectId, tenantTypeId, rateNumber)); //之后更新评价信息表 sqls.Add(Sql.Builder.Append("UPDATE tn_Ratings SET Comprehensive = (RateSum + @0 - @1)/RateCount , RateSum=RateSum + @0 - @1 where ObjectId=@2 and TenantTypeId = @3 and OwnerId = @4", (float)rateNumber, (float)ratingRecord.RateNumber, objectId, tenantTypeId, ownerId)); } } } } if (sqls == null) { return(false); } using (var transaction = dao.GetTransaction()) { affectCount = dao.Execute(sqls); transaction.Complete(); } if (affectCount > 0) { if (RealTimeCacheHelper.EnableCache) { EntityData.ForType(typeof(RatingRecord)).RealTimeCacheHelper.IncreaseAreaVersion("ObjectId", objectId); EntityData.ForType(typeof(RatingGrade)).RealTimeCacheHelper.IncreaseAreaVersion("ObjectId", objectId); RealTimeCacheHelper.IncreaseAreaVersion("ObjectId", objectId); } } else { return(false); } dao.CloseSharedConnection(); return(true); }
/// <summary> /// 新建实体时使用 /// </summary> public static RatingRecord New() { RatingRecord ratingRecord = new RatingRecord() { DateCreated = DateTime.UtcNow, RateNumber = 1, TenantTypeId = string.Empty }; return ratingRecord; }