/// <summary> /// 计算赠送积分 /// </summary> private void PresentedPoint(string communityId, string orderId, string userId, string useHistoryId, decimal paidAmount) { using (var appConn = new SqlConnection(PubConstant.UnifiedContionString)) { if (appConn.State == ConnectionState.Closed) { appConn.Open(); } var trans = appConn.BeginTransaction(); short corpId = 0; try { decimal propertyMaxDiscountsAmount = 0.0m; // 物管费最大可抵用金额 decimal parkingMaxDiscountsAmount = 0.0m; // 车位费最大可抵用金额 var deductionObject = new List <string>(); // 积分可抵扣对象 using (var erpConn = new SqlConnection(PubInfo.GetConnectionStr(PubInfo.GetCommunity(communityId)))) { var feesIds = erpConn.Query <long>(@"SELECT FeesId FROM Tb_OL_AlipayDetail WHERE PayOrderId= (SELECT Id FROM Tb_OL_AlipayOrder WHERE out_trade_no=@out_trade_no)", new { out_trade_no = orderId }); if (feesIds.Count() == 0) { return; } #region 获取积分抵扣规则 // 企业编号 corpId = appConn.Query <short>("SELECT CorpID FROM Tb_Community WHERE Id=@CommunityId", new { CommunityId = communityId }, trans).FirstOrDefault(); // 积分权限控制 var controlInfo = appConn.Query <Tb_Control_AppPoint>(@"SELECT * FROM Tb_Control_AppPoint WHERE CorpID=@CorpID AND IsEnable=1 AND (CommunityID=@CommunityId OR CommunityID IS NULL) ORDER BY CommunityID DESC", new { CorpID = corpId, CommunityId = communityId }, trans).FirstOrDefault(); if (controlInfo == null || controlInfo.IsEnable == false) { controlInfo = Tb_Control_AppPoint.DefaultControl; controlInfo.CommunityID = Guid.Empty.ToString(); } // 允许抵用物业费 if (controlInfo.AllowDeductionPropertyFees) { deductionObject.Add($@"'{AppPointUsableObjectConverter.GetKey(AppPointUsableObject.PropertyFee)}'"); } // 允许抵用车位费 if (controlInfo.AllowDeductionParkingFees) { deductionObject.Add($@"'{AppPointUsableObjectConverter.GetKey(AppPointUsableObject.ParkingFee)}'"); } #endregion // 不计算违约金 string sql = $@"SELECT isnull(sum(isnull(DebtsAmount,0)),0) FROM view_HSPR_Fees_Filter WHERE FeesID IN({string.Join(",", feesIds)}) AND SysCostSign = 'B0001'; SELECT isnull(sum(isnull(DebtsAmount,0)),0) FROM view_HSPR_Fees_Filter WHERE FeesID IN({string.Join(",", feesIds)}) AND SysCostSign = 'B0002';"; var reader = erpConn.QueryMultiple(sql); // 物业费、车位费实际欠费总额 decimal propertyAmount = reader.Read <decimal>().FirstOrDefault(); decimal parkingAmount = reader.Read <decimal>().FirstOrDefault(); // 积分抵用了部分金额 if (!string.IsNullOrEmpty(useHistoryId)) { // 积分抵用的金额数量 var deductionAmount = appConn.Query <decimal>(@"SELECT isnull(DeductionAmount,0) FROM Tb_App_Point_UseHistory WHERE IID=@IID;", new { IID = useHistoryId }, trans).FirstOrDefault(); // 计算相关抵用的金额 if (deductionAmount != 0) { // 积分抵用规则 var ruleInfo = appConn.Query($@"SELECT IID,ConditionAmount,DiscountsAmount,DeductionObject,b.Remark AS SysCostSign,StartTime,EndTime FROM Tb_App_Point_PropertyDeductionRule a LEFT JOIN Tb_Dictionary_Point_UsableObject b ON a.DeductionObject=b.[Key] WHERE CommunityID=@CommunityId AND DeductionObject IN({string.Join(", ", deductionObject) }) AND getdate() BETWEEN StartTime AND EndTime AND a.IsDelete=0 ORDER BY ConditionAmount,DiscountsAmount", new { CommunityId = communityId }, trans); if (ruleInfo.Count() == 0) { Business.Alipay.Log("支付宝下账:计算赠送积分失败,相关积分抵用规则已被禁用"); return; } // 确定物管费可抵用金额 if (propertyAmount > 0) { string key = AppPointUsableObjectConverter.GetKey(AppPointUsableObject.PropertyFee); foreach (var item in ruleInfo) { if (item.DeductionObject == key && propertyAmount >= item.ConditionAmount) { propertyMaxDiscountsAmount = item.DiscountsAmount; } } } // 确定车位费可抵用金额 if (parkingAmount > 0) { string key = AppPointUsableObjectConverter.GetKey(AppPointUsableObject.ParkingFee); foreach (var item in ruleInfo) { if (item.DeductionObject == key && parkingAmount >= item.ConditionAmount) { parkingMaxDiscountsAmount = item.DiscountsAmount; } } } // 积分数量不正常 if (deductionAmount > (parkingMaxDiscountsAmount + propertyMaxDiscountsAmount)) { Business.Alipay.Log("支付宝下账:计算赠送积分失败,积分实际抵用金额超出可抵用金额"); return; } else { decimal tmp = deductionAmount; // 部分抵扣物业费 if (propertyMaxDiscountsAmount != 0) { if (tmp <= propertyMaxDiscountsAmount) { propertyMaxDiscountsAmount = tmp; tmp = 0; } else { tmp -= propertyMaxDiscountsAmount; } } // 部分抵扣车位费 if (parkingMaxDiscountsAmount != 0 && tmp > 0) { if (tmp <= parkingMaxDiscountsAmount) { parkingMaxDiscountsAmount = tmp; tmp = 0; } else { tmp -= parkingMaxDiscountsAmount; } } if (tmp != 0) { Business.Alipay.Log("支付宝下账:计算赠送积分失败,积分实际抵用金额与可抵用金额不相等,可能是更改了积分抵用规则"); return; } } } } // 计算要赠送的积分数量 new AppPoint().CalcPresentedPointForPropertyFees(communityId, propertyAmount - propertyMaxDiscountsAmount, parkingAmount - parkingMaxDiscountsAmount, out int p1, out int p2); if (p1 == 0 && p2 == 0) { return; } int presentedPoints = p1 + p2; var userPoint = appConn.Query("SELECT * FROM Tb_App_UserPoint WHERE UserID=@UserID", new { UserID = userId }, trans).FirstOrDefault(); int balance = 0; if (userPoint == null) { balance = 0; appConn.Execute(@"INSERT INTO Tb_App_UserPoint(UserID, PointBalance) VALUES(@UserID, @PointBalance)", new { UserID = userId, PointBalance = presentedPoints }, trans); } else { balance = userPoint.PointBalance; appConn.Execute("UPDATE Tb_App_UserPoint SET PointBalance=(PointBalance+@PresentedPoints) WHERE UserID=@UserID", new { PresentedPoints = presentedPoints, UserID = userId }, trans); } // 赠送历史 appConn.Execute(@"INSERT INTO Tb_App_Point_PresentedHistory(UserID, PresentedWay, PresentedPoints, PointBalance, Remark) VALUES(@UserID, @PresentedWay, @PresentedPoints, @PointBalance, @Remark)", new { UserID = userId, PresentedWay = AppPointPresentedWayConverter.GetKey(AppPointPresentedWay.PropertyArrearsPayment), PresentedPoints = presentedPoints, PointBalance = balance + presentedPoints, Remark = AppPointPresentedWayConverter.GetValue(AppPointPresentedWay.PropertyArrearsPayment), }, trans); Business.Alipay.Log("支付宝下账:赠送积分=" + presentedPoints); // 力帆,缴清,额外赠送 if (corpId == 2015) { // 查询CustID、RoomID var custInfo = erpConn.Query(@"SELECT CustID,RoomID FROM Tb_HSPR_Fees WHERE FeesID= (SELECT TOP 1 FeesId FROM Tb_OL_AlipayDetail WHERE PayOrderId IN (SELECT Id FROM Tb_OL_AlipayOrder WHERE out_trade_no=@out_trade_no))", new { out_trade_no = orderId }).FirstOrDefault(); if (custInfo != null && custInfo.CustID != 0 && custInfo.RoomID != 0) { // 无欠费 if (erpConn.Query(@"SELECT * FROM Tb_HSPR_Fees WHERE CustID=@CustID AND RoomID=@RoomID AND isnull(IsCharge,0)=0", new { CustID = custInfo.CustID, RoomID = custInfo.RoomID }).Count() == 0) { int extraPoints = new AppPoint().CalcPresentedPointForPayAll(); if (extraPoints > 0) { presentedPoints += extraPoints; // 赠送积分 appConn.Execute(@"UPDATE Tb_App_UserPoint SET PointBalance=(PointBalance+@PresentedPoints) WHERE UserID=@UserID; INSERT INTO Tb_App_Point_PresentedHistory(UserID, PresentedWay, PresentedPoints, PointBalance, Remark) VALUES(@UserID, @PresentedWay, @PresentedPoints, @PointBalance, @Remark);", new { UserID = userId, PresentedWay = AppPointPresentedWayConverter.GetKey(AppPointPresentedWay.PropertyArrearsPayment), PresentedPoints = extraPoints, PointBalance = balance + presentedPoints, Remark = "物业欠费缴清赠送", }, trans); } } } } } trans.Commit(); } catch (Exception ex) { trans.Rollback(); Business.Alipay.Log("支付宝下账:计算赠送积分异常," + ex.Message + Environment.NewLine + ex.StackTrace); } } }
/// <summary> /// 赠送积分 /// </summary> public void PresentedPoint(string UserId, int Months) { int presentedPoint = new AppPoint().CalcPresentedPointForPrec_lf(Months); if (presentedPoint == 0) { return; } using (IDbConnection conn = new SqlConnection(PubConstant.UnifiedContionString)) { if (conn.State == ConnectionState.Closed) { conn.Open(); } var trans = conn.BeginTransaction(); try { int pointBalance = 0; var resultSet = conn.Query("SELECT * FROM Tb_App_UserPoint WHERE UserID=@UserID", new { UserID = UserId }, trans); // 用户积分记录 if (resultSet.Count() == 0) { // 用户积分 conn.Execute(@"INSERT INTO Tb_App_UserPoint(UserID, PointBalance) VALUES(@UserID, @PointBalance)", new { UserID = UserId, PointBalance = presentedPoint }, trans); } else { pointBalance = resultSet.FirstOrDefault().PointBalance + presentedPoint; // 5、更新积分余额 conn.Execute("UPDATE Tb_App_UserPoint SET PointBalance=(PointBalance+@PresentedPoint) WHERE UserID=@UserID", new { PresentedPoint = presentedPoint, UserID = UserId }, trans); } // 赠送历史 conn.Execute(@"INSERT INTO Tb_App_Point_PresentedHistory(UserID, PresentedWay, PresentedPoints, PointBalance, Remark) VALUES(@UserID, @PresentedWay, @PresentedPoints, @PointBalance, @PresentedName)", new { UserID = UserId, PresentedWay = AppPointPresentedWayConverter.GetKey(AppPointPresentedWay.PropertyPrestorePayment), PresentedPoints = presentedPoint, PointBalance = pointBalance, PresentedName = AppPointPresentedWayConverter.GetValue(AppPointPresentedWay.PropertyPrestorePayment) }, trans); trans.Commit(); Business.Alipay.Log("支付宝预存下账:赠送积分=" + presentedPoint); } catch (Exception ex) { trans.Rollback(); Business.Alipay.Log("支付宝预存下账:赠送积分异常," + ex.Message + Environment.NewLine + ex.StackTrace); } } }
/// <summary> /// 获取绘制图表的数据点 /// </summary> /// <param name="deviceData"></param> /// <param name="result"></param> public void GetDataPoints(ItemPoint deviceData, INetworkResult result, string datastream_id, string limit, string start, string end, string cursor, string duration) { // client.Authenticator = new HttpBasicAuthenticator(username, password); var request = new RestRequest("devices/{device_id}/datapoints", Method.GET); request.AddUrlSegment("device_id", deviceData.deviceId); // replaces matching token in request.Resource // add parameters for all properties on an object //request.AddObject(object); // or just whitelisted properties //request.AddObject(object, "PersonId", "Name", ...); // easily add HTTP Headers request.AddHeader("api-key", "VtaeS4yK3Fk6xiOljgw69lYcH9k="); if (!string.IsNullOrEmpty(datastream_id)) { request.AddParameter("datastream_id", datastream_id); } if (!string.IsNullOrEmpty(limit)) { request.AddParameter("limit", limit); } if (!string.IsNullOrEmpty(start)) { request.AddParameter("start", start); } if (!string.IsNullOrEmpty(end)) { request.AddParameter("end", end); } if (!string.IsNullOrEmpty(cursor)) { request.AddParameter("cursor", cursor); } if (!string.IsNullOrEmpty(duration)) { request.AddParameter("duration", duration); } IRestResponse response = client.Execute(request); var content = response.Content; // raw content as string if (response.ResponseStatus == ResponseStatus.Completed) { AppPoint point = JsonHelper.DeserializeJsonToObject <AppPoint>(content); if (point.error.Equals("succ")) { ItemPoint item = new ItemPoint { deviceId = deviceData.deviceId, title = deviceData.title }; StringBuilder builder = new StringBuilder(); foreach (DataStreams dataStream in point.data.datastreams) { RemarksID remarksID = RemarksDao.Instance.GetDeviceDataById(item.deviceId + dataStream.id); foreach (DataPoints dataPoints in dataStream.datapoints) { dataPoints.at = dataPoints.at.Substring(0, dataPoints.at.Length - 4); } if (dataStream.id.Equals("P") || dataStream.id.Equals("T") || dataStream.id.Equals("H")) { //只获取最新的一个点在列表里显示 bool m = false; foreach (DataPoints dataPoints in dataStream.datapoints) { if (m) { break; } if (remarksID == null || string.IsNullOrEmpty(remarksID.remarks)) { builder.Append(dataStream.id + "=" + dataPoints.value); } else { builder.Append(remarksID.remarks + "=" + dataPoints.value); } foreach (DeviceDataStreams stream in deviceData.deviceDatastreams) { if (dataStream.id.Equals(stream.id)) { builder.Append(stream.unit + " "); m = true; break; } } } } } item.dataStreamsList = point.data.datastreams; item.deviceDatastreams = deviceData.deviceDatastreams; item.dataStrams = builder.ToString(); result.OnSuccess(item); } else { result.OnFailure(point.error); } } else { result.OnFailure("获取" + deviceData.title + "的数据失败"); } }