博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#定时任务
阅读量:5023 次
发布时间:2019-06-12

本文共 4966 字,大约阅读时间需要 16 分钟。

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");
                }
            }
        }
      
    }
}

转载于:https://www.cnblogs.com/weimingxin/p/8950177.html

你可能感兴趣的文章
系统管理玩玩Windows Azure
查看>>
c#匿名方法
查看>>
如何判断链表是否有环
查看>>
【小程序】缓存
查看>>
ssh无密码登陆屌丝指南
查看>>
MySQL锁之三:MySQL的共享锁与排它锁编码演示
查看>>
docker常用命令详解
查看>>
jQuery技巧大放送
查看>>
字符串转换成JSON的三种方式
查看>>
Hive时间函数笔记
查看>>
clojure-emacs-autocomplete
查看>>
一个自己写的判断2个相同对象的属性值差异的工具类
查看>>
10 华电内部文档搜索系统 search03
查看>>
[HIHO1149]回文字符序列(dp)
查看>>
[HDU1402]A * B Problem Plus(FFT)
查看>>
[CF803C] Maximal GCD(gcd,贪心,构造)
查看>>
逆时针旋转的矩阵变换
查看>>
第10周15/16/17
查看>>
【数据库】SQL两表之间:根据一个表的字段更新另一个表的字段
查看>>
四六级作文常见错误解析(转载)
查看>>