/// <summary> /// 获取key /// </summary> public static string GetKey(AppPointPresentedWay @enum) { switch (@enum) { case AppPointPresentedWay.DailyCheckIn: return("0001"); case AppPointPresentedWay.ContinuousCheckInReward: return("0002"); case AppPointPresentedWay.PropertyArrearsPayment: return("0003"); case AppPointPresentedWay.PropertyPrestorePayment: return("0004"); case AppPointPresentedWay.StoreTrade: return("0005"); case AppPointPresentedWay.PropertyPresented: return("0099"); default: throw new ArgumentException("不存在该赠送方式"); } }
public static string GetValue(AppPointPresentedWay @enum) { switch (@enum) { case AppPointPresentedWay.DailyCheckIn: return("每日签到"); case AppPointPresentedWay.ContinuousCheckInReward: return("持续签到额外奖励"); case AppPointPresentedWay.PropertyArrearsPayment: return("物业欠费缴费赠送"); case AppPointPresentedWay.PropertyPrestorePayment: return("物业预存缴费赠送"); case AppPointPresentedWay.StoreTrade: return("购物赠送"); case AppPointPresentedWay.PropertyPresented: return("物业主动发放"); default: throw new ArgumentException("不存在该赠送方式"); } }
/// <summary> /// 每日签到 /// </summary> private string DailyCheckIn(DataRow row) { if (!row.Table.Columns.Contains("UserId") || string.IsNullOrEmpty(row["UserId"].ToString()) || row["UserId"].ToString() == "(null)") { return(new ApiResult(false, "UserId不能为空").toJson()); } if (!row.Table.Columns.Contains("CommunityId") || string.IsNullOrEmpty(row["CommunityId"].ToString())) { return(new ApiResult(false, "CommunityId不能为空").toJson()); } string UserId = row["UserId"].ToString(); string CommunityId = row["CommunityId"].ToString(); Tb_Community tb_Community = GetCommunity(CommunityId); if (null == tb_Community) { return(new ApiResult(false, "小区不存在").toJson()); } using (IDbConnection conn = new SqlConnection(PubConstant.UnifiedContionString)) { if (conn.State == ConnectionState.Closed) { conn.Open(); } var trans = conn.BeginTransaction(); // 查询是否已经签到 if (conn.Query(@"SELECT * FROM Tb_App_DailyCheckIn WHERE UserID=@UserID AND CheckInTime BETWEEN convert(varchar(10), getDate(), 120) + ' 00:00:00' AND convert(varchar(10), getDate(), 120) + ' 23:59:59'", new { UserID = UserId }, trans).Count() > 0) { return(new ApiResult(false, "今日已签到").toJson()); } // 查询该公司是否允许签到奖励 if (!conn.Query <bool>(@"SELECT AllowDailyCheckInAward FROM Tb_Control_AppPoint WHERE CorpID=@CorpID AND (CommunityID=@CommunityID OR CommunityID IS NULL) AND IsEnable=1", new { CommunityID = tb_Community.Id, CorpID = tb_Community.CorpID }, trans).FirstOrDefault()) { trans.Commit(); return(new ApiResult(false, "功能暂未开通").toJson()); } // 查询该公司是否设置了签到奖励规则 var controlInfo = conn.Query <Tb_Control_AppPoint_DailyCheckIn>(@"SELECT * FROM Tb_Control_AppPoint_DailyCheckIn WHERE CorpID=@CorpID AND (CommunityID=@CommunityID OR CommunityID IS NULL) AND IsEnable=1", new { CommunityID = tb_Community.Id, CorpID = tb_Community.CorpID }, trans).FirstOrDefault(); if (controlInfo == null || controlInfo.IsEnable == false) { controlInfo = Tb_Control_AppPoint_DailyCheckIn.DefaultControl; } try { // 用户积分记录 if (conn.Query("SELECT * FROM Tb_App_UserPoint WHERE UserID=@UserID", new { UserID = UserId }, trans).Count() == 0) { // 用户积分 conn.Execute(@"INSERT INTO Tb_App_UserPoint(UserID, PointBalance) VALUES(@UserID, @PointBalance)", new { UserID = UserId, PointBalance = controlInfo.BaseRewardPoints }, trans); // 签到记录 conn.Execute("INSERT INTO Tb_App_DailyCheckIn(UserID, RewardPoints) VALUES(@UserID, @RewardPoints)", new { UserID = UserId, RewardPoints = controlInfo.BaseRewardPoints }, trans); // 赠送历史 conn.Execute(@"INSERT INTO Tb_App_Point_PresentedHistory(UserID, PresentedWay, PresentedPoints, PointBalance, Remark) VALUES(@UserID, @PresentedWay, @PresentedPoints, @PointBalance, '每日签到')", new { UserID = UserId, PresentedWay = AppPointPresentedWayConverter.GetKey(AppPointPresentedWay.DailyCheckIn), PresentedPoints = controlInfo.BaseRewardPoints, PointBalance = controlInfo.BaseRewardPoints }, trans); trans.Commit(); return(new ApiResult(true, new { PointBalance = controlInfo.BaseRewardPoints, RewardPoint = controlInfo.BaseRewardPoints }).toJson()); } else { // 1、获取用户持续签到天数 string sql = $@"DECLARE @now DATETIME=getdate(); SELECT count(*) FROM ( SELECT datediff(DAY, CheckInTime, @now) a, /* 签到时间对比今天的差值 */ row_number() OVER (ORDER BY CheckInTime DESC) b /* 排序字段 */ FROM Tb_App_DailyCheckIn WHERE UserID=@UserID AND datediff(DAY, CheckInTime, @now)>0 /* 条件排除今天的签到记录 */ ) t WHERE a=b;"; int continuousDays = conn.Query <int>(sql, new { UserID = UserId }, trans).FirstOrDefault() + 1; // 赠送方式 AppPointPresentedWay way = (continuousDays == 1 ? AppPointPresentedWay.DailyCheckIn : AppPointPresentedWay.ContinuousCheckInReward); // 2、计算奖励的积分数量 int rewardPoints = CalcRewardPoints(continuousDays, controlInfo, out int additionalRewardPoints); int pointBalance = conn.Query <int>("SELECT PointBalance FROM Tb_App_UserPoint WHERE UserID=@UserID", new { UserID = UserId }, trans).FirstOrDefault(); // 3、签到记录 conn.Execute(@"INSERT INTO Tb_App_DailyCheckIn(UserID, RewardPoints, AdditionalRewardPoints, IsAdditionalReward) VALUES(@UserID, @RewardPoints, @AdditionalRewardPoints, @IsAdditionalReward)", new { UserID = UserId, RewardPoints = controlInfo.BaseRewardPoints, AdditionalRewardPoints = additionalRewardPoints, IsAdditionalReward = (additionalRewardPoints > 0 ? 1 : 0) }, trans); // 4、插入积分赠送历史 conn.Execute(@"INSERT INTO Tb_App_Point_PresentedHistory(UserID, PresentedWay, PresentedPoints, PointBalance, Remark) VALUES(@UserID, @PresentedWay, @PresentedPoints, @PointBalance, '每日签到')", new { UserID = UserId, PresentedWay = AppPointPresentedWayConverter.GetKey(way), PresentedPoints = (rewardPoints + additionalRewardPoints), PointBalance = (pointBalance + rewardPoints + additionalRewardPoints) }, trans); // 5、更新积分余额 conn.Execute("UPDATE Tb_App_UserPoint SET PointBalance=(PointBalance+@RewardPoint+@AdditionalRewardPoints) WHERE UserID=@UserID", new { RewardPoint = rewardPoints, AdditionalRewardPoints = additionalRewardPoints, UserID = UserId }, trans); trans.Commit(); return(new ApiResult(true, new { PointBalance = pointBalance, RewardPoint = rewardPoints }).toJson()); } } catch (Exception) { trans.Rollback(); return(new ApiResult(false, "签到异常").toJson()); } } }