public ActionResult CreateActivity(RegionVehicleIdActivityConfig model)
        {
            MJsonResult json = new MJsonResult();

            if (string.IsNullOrEmpty(model.ActivityName))
            {
                json.Status = false;
                json.Msg    = "活动名不能为空";
            }
            else if (string.IsNullOrEmpty(model.ActivityType))
            {
                json.Status = false;
                json.Msg    = "活动类型不合法";
            }
            else if (model.StartTime == null)
            {
                json.Status = false;
                json.Msg    = "开始时间不能为空";
            }
            else if (model.EndTime == null)
            {
                json.Status = false;
                json.Msg    = "结束时间不能为空";
            }
            else
            {
                model.ActivityName = model.ActivityName.Trim();
                string userno = User.Identity.Name;
                if (string.IsNullOrEmpty(userno))
                {
                    json.Status = false;
                    json.Msg    = "获取用户名异常";
                }
                else
                {
                    var activityId = Guid.NewGuid();
                    model.ActivityId = activityId;
                    model.CreateUser = model.UpdateUser = userno;
                    var result = cityActivityConfig.CreateActivity(model);
                    if (result)
                    {
                        int id         = cityActivityConfig.GetActivityPKIDByActivityId(activityId);
                        var AfterValue = $"活动名:{model.ActivityName},活动类型:{model.ActivityType},开始时间:{model.StartTime.ToString()},结束时间:{model.EndTime.ToString()},是否启用:{model.IsEnabled}";
                        LoggerManager.InsertOplog(new ConfigHistory()
                        {
                            AfterValue = AfterValue, Author = User.Identity.Name, Operation = $"{activityId.ToString()}创建活动", ObjectType = "CVPage", ObjectID = id.ToString()
                        });
                    }

                    return(Json(new { Status = result, Msg = result ? "OK" : "创建失败", Data = activityId }));
                }
            }
            return(Json(json));
        }
        /// <summary>
        /// 更新活动
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateActivity(RegionVehicleIdActivityConfig model)
        {
            bool result = false;

            try
            {
                result = ConfigurationDb.Execute(conn => DalCityActivityPageConfig.UpdateActivity(conn, model));
            }
            catch (Exception ex)
            {
                Logger.Error("UpdateActivity", ex);
            }
            return(result);
        }
        /// <summary>
        /// 创建活动
        /// </summary>
        /// <param name="activity"></param>
        /// <returns></returns>
        public bool CreateActivity(RegionVehicleIdActivityConfig activity)
        {
            bool result;

            try
            {
                result = ConfigurationDb.Execute(conn => DalCityActivityPageConfig.CreateActivity(conn, activity));
            }
            catch (Exception ex)
            {
                Logger.Error("CreateActivity", ex);
                result = false;
            }
            return(result);
        }
        public ActionResult UpdateActivity(RegionVehicleIdActivityConfig model)
        {
            MJsonResult json = new MJsonResult();

            if (model.ActivityId == Guid.Empty)
            {
                json.Status = false;
                json.Msg    = "活动ID不合法";
            }
            else if (string.IsNullOrEmpty(model.ActivityName))
            {
                json.Status = false;
                json.Msg    = "活动名不能为空";
            }
            else if (model.StartTime == null || model.EndTime == null)
            {
                json.Status = false;
                json.Msg    = "活动开始时间或结束时间不合法";
            }
            else if (model.IsEnabled != 0 && model.IsEnabled != 1)
            {
                json.Status = false;
                json.Msg    = "是否启用字段不合法";
            }
            string userno = User.Identity.Name;

            if (string.IsNullOrEmpty(userno))
            {
                json.Status = false;
                json.Msg    = "用户登录信息获取失败";
            }
            else
            {
                model.UpdateUser = userno;
                json.Status      = cityActivityConfig.UpdateActivity(model);
                if (json.Status)
                {
                    int id         = cityActivityConfig.GetActivityPKIDByActivityId(model.ActivityId);
                    var AfterValue = $"活动名:{ model.ActivityName },开始时间:{model.StartTime.ToString()},结束时间:{model.EndTime.ToString()},是否启用:{model.IsEnabled}";
                    LoggerManager.InsertOplog(new ConfigHistory()
                    {
                        AfterValue = AfterValue, Author = userno, Operation = $"{model.ActivityId.ToString()}更新活动", ObjectType = "CVPage", ObjectID = id.ToString()
                    });
                }
                json.Msg = json.Status ? "OK" : "更新失败";
            }
            return(Json(json));
        }
        public List <RegionVehicleIdActivityConfig> GetAllActivity(RegionVehicleIdActivityConfig filter, int pageIndex, int pageSize, out int totalItem)
        {
            List <RegionVehicleIdActivityConfig> activityList = null;

            totalItem = 0;
            try
            {
                activityList = ConfigurationReadDb.Execute(conn => DalCityActivityPageConfig.SelectAllActivity(conn, filter, pageIndex, pageSize));
                totalItem    = ConfigurationReadDb.Execute(conn => DalCityActivityPageConfig.SelectActivityCount(conn, filter));
            }
            catch (Exception ex)
            {
                Logger.Error("GetActivity", ex);
            }
            return(activityList);
        }
        public static int SelectActivityCount(SqlConnection conn, RegionVehicleIdActivityConfig filter)
        {
            var sql        = @"SELECT  COUNT(1)
                        FROM    Configuration..RegionVehicleIdActivityConfig WITH ( NOLOCK )
                        WHERE   ( @ActivityName IS NULL 
                                    OR ActivityName LIKE N'%' + @ActivityName + N'%' )
                                AND ( @Status IS NULL  
                                    OR  @Status = N'所有'
                                    OR ( @Status = N'未开始'
                                        AND StartTime >= GETDATE()
                                        )
                                    OR ( @Status = N'进行中'
                                        AND StartTime <= GETDATE()
                                        AND EndTime >= GETDATE()
                                        )
                                    OR ( @Status = N'已结束'
                                        AND EndTime <= GETDATE()
                                        )
                                    )
                                AND ( @StartTime IS NULL
                                      OR  EndTime > @StartTime
                                    )
                                AND ( @EndTime IS NULL
                                    OR StartTime < @EndTime
                                    )
                                AND ( @CreateUser IS NULL
                                    OR CreateUser = @CreateUser
                                    )
                                AND ( @UpdateUser IS NULL
                                    OR UpdateUser = @UpdateUser
                                    )";
            var parameters = new SqlParameter[]
            {
                new SqlParameter("@Status", filter.Status),
                new SqlParameter("@StartTime", filter.StartTime),
                new SqlParameter("@EndTime", filter.EndTime),
                new SqlParameter("@CreateUser", filter.CreateUser),
                new SqlParameter("@UpdateUser", filter.UpdateUser),
                new SqlParameter("@ActivityName", filter.ActivityName)
            };
            var activityCount = SqlHelper.ExecuteScalar(conn, CommandType.Text, sql, parameters);

            return((int)activityCount);
        }
        public static bool CreateActivity(SqlConnection conn, RegionVehicleIdActivityConfig model)
        {
            var sql        = @"INSERT  INTO Configuration..RegionVehicleIdActivityConfig
                                ( ActivityId ,
                                ActivityName ,
                                ActivityType ,
                                StartTime ,
                                EndTime ,
                                IsEnabled ,
                                CreateUser ,
                                UpdateUser ,
                                CreateDateTime ,
                                LastUpdateDateTime
                                )
                        VALUES  ( @ActivityId ,
                                @ActivityName ,
                                @ActivityType ,
                                @StartTime ,
                                @EndTime ,
                                @IsEnabled ,
                                @CreateUser ,
                                @UpdateUser ,
                                GETDATE() ,
                                GETDATE()
                                );";
            var parameters = new SqlParameter[]
            {
                new SqlParameter("@ActivityId", model.ActivityId),
                new SqlParameter("@ActivityName", model.ActivityName),
                new SqlParameter("@ActivityType", model.ActivityType),
                new SqlParameter("@StartTime", model.StartTime),
                new SqlParameter("@EndTime", model.EndTime),
                new SqlParameter("@IsEnabled", model.IsEnabled),
                new SqlParameter("@CreateUser", model.CreateUser),
                new SqlParameter("@UpdateUser", model.UpdateUser)
            };
            var count = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql, parameters);

            return(count > 0);
        }
        public static bool UpdateActivity(SqlConnection conn, RegionVehicleIdActivityConfig model)
        {
            var sql        = @"UPDATE  Configuration..RegionVehicleIdActivityConfig
                        SET     ActivityName = @ActivityName ,
                                StartTime = @StartTime ,
                                EndTime = @EndTime ,
                                IsEnabled = @IsEnabled,
                                UpdateUser = @UpdateUser,
                                LastUpdateDateTime = GETDATE()
                        WHERE   ActivityId = @ActivityId;";
            var parameters = new SqlParameter[]
            {
                new SqlParameter("@ActivityId", model.ActivityId),
                new SqlParameter("@ActivityName", model.ActivityName),
                new SqlParameter("@StartTime", model.StartTime),
                new SqlParameter("@EndTime", model.EndTime),
                new SqlParameter("@UpdateUser", model.UpdateUser),
                new SqlParameter("@IsEnabled", model.IsEnabled)
            };
            var count = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql, parameters);

            return(count > 0);
        }
        // GET: CityActivityDisplayConfig
        public ActionResult Index(RegionVehicleIdActivityConfig filter, int pageIndex = 1)
        {
            if (filter.ActivityName != null)
            {
                filter.ActivityName = filter.ActivityName.Trim();
            }
            if (filter.CreateUser != null)
            {
                filter.CreateUser = filter.CreateUser.Trim();
            }
            if (filter.UpdateUser != null)
            {
                filter.UpdateUser = filter.UpdateUser.Trim();
            }
            int totalItem;
            int pageSize     = 12;
            var activityList = cityActivityConfig.GetAllActivity(filter, pageIndex, pageSize, out totalItem);

            ViewBag.pageIndex    = pageIndex;
            ViewBag.totalRecords = totalItem;
            ViewBag.totalPage    = Math.Ceiling(totalItem / (pageSize + 0.0));
            ViewBag.activityList = activityList;
            return(View());
        }
        public static List <RegionVehicleIdActivityConfig> SelectAllActivity(SqlConnection conn, RegionVehicleIdActivityConfig filter, int pageIndex, int pageSize)
        {
            var sql        = @"SELECT  PKID ,
                                ActivityId ,
                                ActivityName ,
                                ActivityType ,
                                StartTime ,
                                EndTime ,
                                IsEnabled ,
                                CreateUser ,
                                UpdateUser
                        FROM    Configuration..RegionVehicleIdActivityConfig WITH ( NOLOCK )
                        WHERE   ( @ActivityName IS NULL 
                                    OR ActivityName LIKE N'%' + @ActivityName + N'%' )
                                AND ( @Status IS NULL  
                                    OR  @Status = N'所有'
                                    OR ( @Status = N'未开始'
                                        AND StartTime >= GETDATE()
                                        )
                                    OR ( @Status = N'进行中'
                                        AND StartTime <= GETDATE()
                                        AND EndTime >= GETDATE()
                                        )
                                    OR ( @Status = N'已结束'
                                        AND EndTime <= GETDATE()
                                        )
                                    )
                                AND ( @StartTime IS NULL
                                      OR  EndTime > @StartTime
                                    )
                                AND ( @EndTime IS NULL
                                    OR StartTime < @EndTime
                                    )
                                AND ( @CreateUser IS NULL
                                    OR CreateUser = @CreateUser
                                    )
                                AND ( @UpdateUser IS NULL
                                    OR UpdateUser = @UpdateUser
                                    )
                         ORDER BY PKID DESC
                                OFFSET ( @PageIndex - 1 ) * @PageSize ROWS  FETCH NEXT @PageSize ROWS
                                ONLY;";
            var parameters = new SqlParameter[]
            {
                new SqlParameter("@Status", filter.Status),
                new SqlParameter("@StartTime", filter.StartTime),
                new SqlParameter("@EndTime", filter.EndTime),
                new SqlParameter("@CreateUser", filter.CreateUser),
                new SqlParameter("@UpdateUser", filter.UpdateUser),
                new SqlParameter("@ActivityName", filter.ActivityName),
                new SqlParameter("@PageIndex", pageIndex),
                new SqlParameter("@PageSize", pageSize)
            };
            var activityList = SqlHelper.ExecuteDataTable(conn, CommandType.Text, sql, parameters).ConvertTo <RegionVehicleIdActivityConfig>().ToList();

            return(activityList);
        }