C#定时任务

Global文件:

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秒
            timer.AutoReset = true;//AutoReset 属性为 true 时,每隔指定时间循环一次;如果为 false,则只执行一次。
            timer.Enabled = true;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(CancelSekillOrder);

            //var timer2 = new System.Timers.Timer( 60 * 60 * 1000);//1小时
            //timer2.AutoReset = true;//AutoReset 属性为 true 时,每隔指定时间循环一次;如果为 false,则只执行一次。
            //timer2.Enabled = true;
            //timer2.Elapsed += new System.Timers.ElapsedEventHandler(User_Upgrade);

            var timer3 = new System.Timers.Timer(60 * 60 * 1000);//1小时
            timer3.AutoReset = true;//AutoReset 属性为 true 时,每隔指定时间循环一次;如果为 false,则只执行一次。
            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, "秒杀订单取消异常");
                }
            }
            #endregion ===超过10分钟的未付款秒杀订单取消===
        }
        protected void Execute(object sender, EventArgs e)
        {
            lock (lock1)
            {
                try
                {
                    var now = DateTime.Now;
                    var configdic = new BLL.t_config().GetConfigDictionary();
                    #region ===未使用且过期优惠券处理===
                    new BLL.t_couponlogger().ExpireCoupon();
                    #endregion ===未使用且过期优惠券处理===
                    #region===超7天未收货订单处理===
                    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 + "的冻结余额不足扣除");
                                }
                                blluser.Update(user);
                            }
                        }
                    }

                    #endregion ===超7天未收货订单处理===
                    #region ===本月28号发放分红、清零本月销售额===
                    var end = DateTime.Parse(now.ToString("yyyy-MM") + "-" + 28);//本月28号
                    var begin = DateTime.Parse(now.AddMonths(-1).ToString("yyyy-MM") + "-" + 28);//上月28号
                    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;
                            }
                            //达标
                            if (model.SaleAmount >= renwu_amount)
                            {
                                //发放本月的分红到可提现余额
                                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
                            { //未达标

                                if (shareMoney > 0)
                                { //清零本月分红和销售额
                                    if (model.FreezeBalance >= shareMoney)
                                    {
                                        model.FreezeBalance -= shareMoney;
                                    }
                                    else
                                    {
                                        model.FreezeBalance = 0;
                                    }
                                }
                                model.SaleAmount = 0;//清零本月销售额
                                blluser.Update(model);
                                //生成记录
                                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");
                }
            }
        }
      
    }
}

猜你喜欢

转载自www.cnblogs.com/weimingxin/p/8950177.html