/// <summary> /// 后台手动修改用户积分信息 /// </summary> /// <param name="exchangeUserIntegral"></param> /// <param name="aid"></param> /// <param name="points"></param> /// <returns></returns> public bool ChangeUserPoints(ExchangeUserIntegral exchangeUserIntegral, int aid, int points) { TransactionModel tran = new TransactionModel(); if (points > 0) { //表示赠送 exchangeUserIntegral.integral += points; } else { //表示减少 if (Math.Abs(points) > exchangeUserIntegral.integral) { //表示减少的积分大于当前现有积分,不够减少直接变为0 exchangeUserIntegral.integral = 0; } else { exchangeUserIntegral.integral -= Math.Abs(points); } } exchangeUserIntegral.UpdateDate = DateTime.Now; tran.Add(base.BuildUpdateSql(exchangeUserIntegral, "integral,UpdateDate")); #region 插入积分变动日志 ExchangeUserIntegralLog userIntegralLog = new ExchangeUserIntegralLog { ruleId = 0, appId = aid, integral = points > 0?points:-1,//这里表示赠送的积分 -1 表示扣除 price = 0, ruleType = -1, goodids = "-1", orderId = -1, usegoodids = "-1", userId = exchangeUserIntegral.UserId, actiontype = points > 0 ? 0 : -1, curintegral = Math.Abs(points), AddTime = DateTime.Now, UpdateDate = DateTime.Now, ordertype = points > 0?4:5, buyPrice = 0 }; tran.Add(ExchangeUserIntegralLogBLL.SingleModel.BuildAddSql(userIntegralLog)); #endregion return(base.ExecuteTransactionDataCorect(tran.sqlArray)); }
/// <summary> /// 根据userId appId 更新用户在对应小程序里消费获赠的积分 /// 直接到店使用储值支付消费 线上没有显示的商品 消费则传入userId appId price 否则线上买单的传入userId appId orderId /// </summary> /// <param name="userId"></param> /// <param name="appId"></param> /// <param name="price">直接到店使用储值支付消费的金额</param> /// <param name="orderId">线上买单产生的订单Id EntGoodsOrder 里的Id</param> /// <returns></returns> public bool AddUserIntegral(int userId, int appId, int price = 0, int orderId = 0, int fromType = 0) { bool result = false; int curSumIntegral = 0; //本次消费获赠总积分 int curIntegral = 0; //本条规则消费获赠积分 List <ExchangeRule> listRule = new List <ExchangeRule>(); var TranModel = new TransactionModel(); List <string> listSql = new List <string>(); try { if (orderId == 0) { int ordertype = 3; //表示储值支付(潇哥那个储值支付)过来的(直接到店消费储余额) int ruleType = 0; //表示储值支付(潇哥那个储值支付)过来的(直接到店消费储余额) 直接按照全场商品积分规则计算 if (fromType == 1) { ruleType = 2;//表示充钱储值过来的 ordertype = 2; } listRule = ExchangeRuleBLL.SingleModel.GetList($"appId={appId} and state=0 and ruleType={ruleType}"); if (listRule != null && listRule.Count > 0) { foreach (var rule in listRule) //遍历全场赠送积分规则 { if (price >= rule.price) //消费金额大于设置的金额才进行积分是否赠送判断 { curIntegral = (price / rule.price) * rule.integral; curSumIntegral += curIntegral; #region 积分变动日志记录 ExchangeUserIntegralLog integralLog = ExchangeUserIntegralLogBLL.SingleModel.GetAddUserIntegralLog(rule, userId, orderId, "0", curIntegral, ordertype, price); if (integralLog.Id == 0) { listSql.Add(ExchangeUserIntegralLogBLL.SingleModel.BuildAddSql(integralLog)); } #endregion } } } } else { var entOrder = EntGoodsOrderBLL.SingleModel.GetModel(orderId); if (entOrder != null) { // bool isAddFreightPrice = false; //表示有订单 listRule = ExchangeRuleBLL.SingleModel.GetList($"appId={appId} and state=0"); if (listRule != null && listRule.Count > 0) { foreach (var rule in listRule)//遍历积分规则 { if (rule.ruleType == 1) {//部分商品赠送积分 int curTotalPrice = 0; curTotalPrice += entOrder.FreightPrice; List <string> listusegoodis = new List <string>(); //部分商品获赠积分 //通过订单获取购物车然后取得订单的商品Id var cartModelList = EntGoodsCartBLL.SingleModel.GetList($" GoodsOrderId = {orderId} "); foreach (var item in cartModelList) { if (rule.goodids.Contains(item.FoodGoodsId.ToString())) { //表示当前订单所包含的商品属于赠送积分商品内,计算出这些商品的价钱 curTotalPrice += item.Price * item.Count; listusegoodis.Add(item.FoodGoodsId.ToString()); } } if (curTotalPrice >= rule.price) { curIntegral = (curTotalPrice / rule.price) * rule.integral; curSumIntegral += curIntegral; #region 记录日志 ExchangeUserIntegralLog integralLog = ExchangeUserIntegralLogBLL.SingleModel.GetAddUserIntegralLog(rule, userId, orderId, string.Join(",", listusegoodis), curIntegral, 0, curTotalPrice); if (integralLog.Id == 0) { listSql.Add(ExchangeUserIntegralLogBLL.SingleModel.BuildAddSql(integralLog)); } #endregion } else { log4net.LogHelper.WriteInfo(this.GetType(), $"订单Id={orderId}全场商品赠送积分记录日志,【ruleprice={rule.price}】【BuyPrice={entOrder.BuyPrice}】curIntegral={curSumIntegral}"); } } else if (rule.ruleType == 0) { //全场商品送积分规则 if (entOrder.BuyPrice >= rule.price) { curIntegral = (entOrder.BuyPrice / rule.price) * rule.integral; curSumIntegral += curIntegral; #region 记录日志 ExchangeUserIntegralLog integralLog = ExchangeUserIntegralLogBLL.SingleModel.GetAddUserIntegralLog(rule, userId, orderId, rule.goodids, curIntegral, 0, entOrder.BuyPrice); if (integralLog.Id == 0) { listSql.Add(ExchangeUserIntegralLogBLL.SingleModel.BuildAddSql(integralLog)); } #endregion } else { log4net.LogHelper.WriteInfo(this.GetType(), $"订单Id={orderId}全场商品赠送积分记录日志,【ruleprice={rule.price}】【BuyPrice={entOrder.BuyPrice}】curIntegral={curSumIntegral}"); } } } } } } ExchangeUserIntegral exchangeUserIntegral = GetModel($"userId={userId}"); if (exchangeUserIntegral != null) { exchangeUserIntegral.integral = exchangeUserIntegral.integral + curSumIntegral; exchangeUserIntegral.UpdateDate = DateTime.Now; listSql.Add(this.BuildUpdateSql(exchangeUserIntegral, "integral,UpdateDate")); } else { //表示新增 exchangeUserIntegral = new ExchangeUserIntegral { UserId = userId, integral = curSumIntegral, AddTime = DateTime.Now, UpdateDate = DateTime.Now }; listSql.Add(BuildAddSql(exchangeUserIntegral)); } //执行事务 积分变动日志与积分表都执行成功才正确 if (listSql.Count > 0) { TranModel.Add(listSql.ToArray()); result = ExecuteTransactionDataCorect(TranModel.sqlArray, TranModel.ParameterArray); } } catch (Exception ex) { log4net.LogHelper.WriteInfo(this.GetType(), "积分处理异常:" + ex.Message); } return(result); }
/// <summary> /// 积分兑换商品 只有返回为2才表示成功 /// </summary> /// <param name="exchangeActivity">兑换商品</param> /// <param name="userId"></param> /// <param name="appId"></param> /// <param name="address">收货地址</param> /// <returns>0→积分不足,1→生成订单失败,2→成功,3→系统异常</returns> public int SubUserIntegral(ExchangeActivity exchangeActivity, int userId, int appId, string address, int way = 0) { //.先生成订单,生成成功后拿到 订单Id //1.根据订单Id更新订单状态 2.扣除积分以及3.插入积分记录日志 放在事务里执行 ExchangeUserIntegral exchangeUserIntegral = GetModel($"userId={userId}"); if (exchangeUserIntegral == null || exchangeUserIntegral.integral < exchangeActivity.integral) { return(-1);//积分不足(请先进行消费获得积分)! } ExchangeActivityOrder exchangeActivityOrder = new ExchangeActivityOrder { appId = appId, UserId = userId, ActivityId = exchangeActivity.id, integral = exchangeActivity.integral, PayWay = 0, BuyCount = 1, address = address, state = 0, AddTime = DateTime.Now, activityImg = exchangeActivity.activityimg, activityName = exchangeActivity.activityname, originalPrice = exchangeActivity.originalPrice, Way = way }; int orderId = Convert.ToInt32(ExchangeActivityOrderBLL.SingleModel.Add(exchangeActivityOrder)); if (orderId <= 0) { return(-2);//支付失败(生成订单失败) } var TranModel = new TransactionModel(); List <string> listSql = new List <string>(); //减积分商品库存 exchangeActivity.stock--; listSql.Add(ExchangeActivityBLL.SingleModel.BuildUpdateSql(exchangeActivity, "stock")); #region 生成积分兑换商品订单操作 //对外订单号规则:年月日时分 + 商品ID最后3位数字 var idStr = exchangeActivity.id.ToString(); if (idStr.Length >= 3) { idStr = idStr.Substring(idStr.Length - 3, 3); } else { idStr.PadLeft(3, '0'); } idStr = $"{DateTime.Now.ToString("yyyyMMddHHmm")}{idStr}"; exchangeActivityOrder.Id = orderId; exchangeActivityOrder.OrderNum = idStr; exchangeActivityOrder.state = 2; exchangeActivityOrder.PayTime = DateTime.Now; listSql.Add(ExchangeActivityOrderBLL.SingleModel.BuildUpdateSql(exchangeActivityOrder, "state,PayTime,OrderNum")); #endregion #region 扣除用户积分操作 exchangeUserIntegral.integral = exchangeUserIntegral.integral - exchangeActivity.integral; exchangeUserIntegral.UpdateDate = DateTime.Now; listSql.Add(BuildUpdateSql(exchangeUserIntegral, "integral,UpdateDate")); #endregion #region 插入积分兑换商品后扣除积分的日志 ExchangeUserIntegralLog userIntegralLog = new ExchangeUserIntegralLog { ruleId = 0, appId = appId, integral = -1, price = 0, ruleType = -1, goodids = "-1", orderId = orderId, usegoodids = "-1", userId = userId, actiontype = -1, curintegral = exchangeActivity.integral, AddTime = DateTime.Now, UpdateDate = DateTime.Now, ordertype = 1, buyPrice = exchangeActivity.price }; listSql.Add(ExchangeUserIntegralLogBLL.SingleModel.BuildAddSql(userIntegralLog)); #endregion if (listSql.Count > 0) { TranModel.Add(listSql.ToArray()); if (ExchangeActivityOrderBLL.SingleModel.ExecuteTransactionDataCorect(TranModel.sqlArray, TranModel.ParameterArray)) { return(orderId); } } return(-3); }