1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Data;
- using System.Threading;
- using MySystem.PxcModels;
- using Library;
- using LitJson;
- namespace MySystem
- {
- public class BalancePayBackService
- {
- public readonly static BalancePayBackService Instance = new BalancePayBackService();
- private BalancePayBackService()
- { }
- public void Start()
- {
- Thread th = new Thread(dosomething);
- th.IsBackground = true;
- th.Start();
- }
-
- private void dosomething()
- {
- while (true)
- {
- try
- {
- string content = RedisDbconn.Instance.RPop<string>("BalancePayQueue");
- if (!string.IsNullOrEmpty(content))
- {
- sloveAlipayCallBack(content);
- }
- else
- {
- Thread.Sleep(2000);
- }
- }
- catch (Exception ex)
- {
- function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "商城订单余额支付异常");
- Thread.Sleep(2000);
- }
- }
- }
- public void sloveAlipayCallBack(string content)
- {
- int OrderId = int.Parse(function.CheckInt(content));
- WebCMSEntities db = new WebCMSEntities();
- Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId && m.PayMode == 3 && m.PayStatus == 0);
- if (order != null)
- {
- decimal TotalPrice = order.TotalPrice;
- if (order.UserId == 1)
- {
- TotalPrice = 0.01M;
- }
- UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId);
- if (account != null)
- {
- if(account.BalanceAmount >= TotalPrice)
- {
- decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
- decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
- decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
- account.BalanceAmount -= TotalPrice;
- decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
- decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
- decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
- db.SaveChanges();
- UserAccountRecord accountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
- {
- CreateDate = DateTime.Now,
- UpdateDate = DateTime.Now,
- UserId = order.UserId, //创客
- ChangeType = 20, //变动类型
- ChangeAmount = TotalPrice, //变更金额
- BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
- AfterTotalAmount = AfterTotalAmount, //变更后总金额
- BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
- AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
- BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
- AfterBalanceAmount = AfterBalanceAmount, //变更后余额
- TransRecordNo = order.OrderNo, //交易流水编号
- Remark = "",
- }).Entity;
- db.SaveChanges();
- AlipayPayBack2Service.Instance.DoOrder(db, OrderId);
- }
- }
- }
- db.Dispose();
- }
- }
- }
|