C# timed tasks

Global file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;


namespace OrangeMall
{
    public class Global : System.Web.HttpApplication
    {
        static readonly object lock1 = new object(), lock2 = new object();
        protected void Application_Start(object sender, EventArgs e)
        {
            var timer = new System.Timers.Timer(30 * 1000);//30 seconds
            timer.AutoReset = true; //When the AutoReset property is true, it loops every specified time; if it is false, it executes only once.
            timer.Enabled = true;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(CancelSekillOrder);

            //var timer2 = new System.Timers.Timer( 60 * 60 * 1000);//1 hour//
            timer2.AutoReset = true;//When the AutoReset property is true, it loops every specified time; if it is false, is executed only once.
            //timer2.Enabled = true;
            //timer2.Elapsed += new System.Timers.ElapsedEventHandler(User_Upgrade);

            var timer3 = new System.Timers.Timer(60 * 60 * 1000);//1 hour
            timer3.AutoReset = true;//When the AutoReset property is true, it will loop once every specified time; if it is false, it will only be executed once.
            timer3.Enabled = true;
            timer3.Elapsed += new System.Timers.ElapsedEventHandler(Execute);

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
        protected void CancelSekillOrder(object sender, EventArgs e)
        {
            #region===超过10分钟的未付款秒杀订单取消===
            lock (lock2)
            {
                try
                {

                    var dt = new BLL.t_order().GetList(" GroupSign=3 and OrderState =1  and datediff(mi, AddTime, getdate()) > 10").Tables[0];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        new BLL.t_order().Delete(Common.Utils.ToGuid(dt.Rows[i]["ID"].ToString()));
                    }
                }
                catch (Exception ex)
                {
                    Common.Utils.WriteLogger(ex.Message, "Seckill order cancellation exception");
                }
            }
            #endregion === Cancellation of unpaid instant kill orders over 10 minutes ===
        }
        protected void Execute(object sender, EventArgs e)
        {
            lock (lock1)
            {
                try
                {
                    var now = DateTime.Now;
                    var configdic = new BLL.t_config().GetConfigDictionary();
                    #region === unused and expired coupon handling ===
                    new BLL.t_couponlogger().ExpireCoupon ();
                    #endregion ===Unused and expired coupon processing===
                    #region===Over 7 days unreceived order processing===
                    var orderlist = new BLL.t_order().GetModelList(" OrderState =3 and DeliverTime<>'' and datediff(dd, DeliverTime, getdate())>7");
                    if (orderlist.Count > 0)
                    {
                        BLL.t_user blluser = new BLL.t_user();
                        for (int i = 0; i < orderlist.Count; i++)
                        {
                            orderlist[i].OrderState = 4;
                            orderlist[i].ReceiptTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            new BLL.t_order().Update(orderlist[i]);

                            //三级佣金发放到三级用户的可提现余额
                            var lucrlist = new BLL.t_orderlucre().GetModelList(" GroupSign =1  and  OrderID='" + orderlist[i].ID + "'");
                            for (int j = 0; j < lucrlist.Count; j++)
                            {
                                var user = blluser.GetModel(lucrlist[j].UserID);
                                if (user.FreezeBalance >= lucrlist[j].Amount)
                                {
                                    user.FreezeBalance -= lucrlist[j].Amount;
                                    user.Balance += lucrlist[j].Amount;
                                }
                                else
                                {
                                    user.FreezeBalance = 0;
                                    user.Balance += user.FreezeBalance;
                                    Common.Utils.WriteLogger(user.NickName + "Insufficient frozen balance to deduct");
                                }
                                blluser.Update(user);
                            }
                        }
                    }

                    #endregion === Processing of orders not received for more than 7 days ===
                    #region ===Distribute dividends on the 28th of this month and clear this month's sales ===
                    var end = DateTime.Parse(now.ToString("yyyy-MM") + "-" + 28);//28th of this month
                    var begin = DateTime.Parse(now.AddMonths(-1).ToString("yyyy-MM") + "-" + 28);//28th of last month
                    if (DateTime.Compare(now.Date, end.Date) == 0)
                    {
                        BLL.t_user blluser = new BLL.t_user();
                        BLL.t_orderlucre bllorderlucre = new BLL.t_orderlucre();
                        var reader = new BLL.t_user().GetReader("");
                        while (reader.Read())
                        {
                            var model = new BLL.t_user().GetModel(Common.Utils.ToInt(reader["ID"]));
                            decimal renwu_amount = 0;
                            //本月的分红
                            decimal shareMoney = new BLL.t_orderlucre().GetAmountSum(" UserID=" + reader["ID"] + " and GroupSign=2 and datediff(d,'" + begin + "',AddTime)>0 and datediff(d,AddTime,'" + end + "')>=0  ");

                            switch (model.UserGrade)
                            {
                                case 2://见习股东
                                    {
                                        if (configdic.ContainsKey("JianXi"))
                                        {
                                            var arr = configdic["JianXi"].Split(',');
                                            if (arr.Length > 0)
                                                renwu_amount = Common.Utils.ToDecimal(arr[0]);
                                        }
                                    }
                                    break;
                                case 3://高级股东
                                    {
                                        if (configdic.ContainsKey("GaoJi"))
                                        {
                                            var arr = configdic["GaoJi"].Split(',');
                                            if (arr.Length > 0)
                                                renwu_amount = Common.Utils.ToDecimal(arr[0]);
                                        }
                                    }
                                    break;
                                case 4://首席股东
                                    {
                                        if (configdic.ContainsKey("ShouXi"))
                                        {
                                            var arr = configdic["ShouXi"].Split(',');
                                            if (arr.Length > 0)
                                                renwu_amount = Common.Utils.ToDecimal(arr[0]);
                                        }
                                    }
                                    break;
                            }
                            //Achieving the standard
                            if (model.SaleAmount >= renwu_amount)
                            {
                                //Distribute this month's dividend to the withdrawable balance
                                if (shareMoney > 0)
                                {
                                    if (model.FreezeBalance >= shareMoney)
                                    {
                                        model.Balance += shareMoney;
                                        model. FreezeBalance -= shareMoney;
                                        blluser.Update(model);
                                    }
                                    else
                                    {
                                        model.Balance += model.FreezeBalance;
                                        model.FreezeBalance = 0;
                                    }
                                    blluser.Update(model);
                                }

                            }
                            else
                            { //Not up to standard

                                if (shareMoney > 0)
                                { //Clear this month's dividend and sales
                                    if (model.FreezeBalance >= shareMoney)
                                    {
                                        model.FreezeBalance -= shareMoney;
                                    }
                                    else
                                    {
                                        model.FreezeBalance = 0;
                                    }
                                }
                                model.SaleAmount = 0;//Clear this month's sales
                                blluser.Update(model);
                                //Generate record
                                var orderlucre = new Model.t_orderlucre ();
                                orderlucre.Amount = shareMoney;
                                orderlucre.GroupSign = 4;
                                orderlucre.UserID = model.ID;
                                orderlucre.VState = 2;
                                orderlucre.Intro = "销售任务未达标";
                                orderlucre.AddTime = DateTime.Now;
                                bllorderlucre.Add(orderlucre);
                            }
                        }
                        reader.Close();


                     
                    }
                    #endregion
                }
                catch (Exception ex)
                {
                    Common.Utils.WriteLogger(ex.Message, "timer");
                }
            }
        }
      
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324919466&siteId=291194637