Ejemplo n.º 1
0
        /// <summary>
        /// 批量新增或更新保养活动地区配置
        /// </summary>
        /// <param name="regionIds"></param>
        /// <param name="activityId"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public Tuple <bool, bool> MultiAddOrEditBaoYangActivityRegion(List <int> regionIds, string activityId, string user)
        {
            var result      = false;
            var cacheResult = false;

            try
            {
                if (regionIds != null && regionIds.Any() && !string.IsNullOrWhiteSpace(activityId))
                {
                    var insertModels = new List <BaoYangActivityVehicleAndRegionModel>();
                    var updateModels = new List <BaoYangActivityVehicleAndRegionModel>();
                    var logData      = new List <BaoYangOprLog>();
                    foreach (var regionId in regionIds)
                    {
                        var oldValue = GetBaoYangActivityRegionByRegionId(regionId);
                        if (oldValue == null)
                        {
                            var insertModel = new BaoYangActivityVehicleAndRegionModel
                            {
                                VehicleId          = null,
                                ActivityId         = activityId,
                                RegionId           = regionId,
                                Type               = "Region",
                                CreateDateTime     = DateTime.Now,
                                LastUpdateDateTime = DateTime.Now
                            };
                            insertModels.Add(insertModel);
                        }
                        else
                        {
                            updateModels.Add(oldValue);
                        }
                    }
                    dbScopeManagerGungnir.CreateTransaction(conn =>
                    {
                        foreach (var insertModel in insertModels)
                        {
                            var pkid = DalBaoYangActivitySetting.AddBaoYangActivityRegion(conn, insertModel);
                            if (pkid < 1)
                            {
                                throw new Exception($"AddBaoYangActivityRegion失败,{JsonConvert.SerializeObject(insertModel)}");
                            }
                            insertModel.PKID = pkid;
                            var log          = new BaoYangOprLog
                            {
                                LogType     = "BaoYangActivityRegion",
                                IdentityID  = insertModel.RegionId.ToString(),
                                OldValue    = null,
                                NewValue    = JsonConvert.SerializeObject(insertModel),
                                Remarks     = "Add",
                                OperateUser = user,
                            };
                            logData.Add(log);
                        }
                        foreach (var oldValue in updateModels)
                        {
                            var updateModel = new BaoYangActivityVehicleAndRegionModel
                            {
                                PKID               = oldValue.PKID,
                                VehicleId          = oldValue.VehicleId,
                                ActivityId         = activityId,
                                RegionId           = oldValue.RegionId,
                                Type               = oldValue.Type,
                                CreateDateTime     = oldValue.CreateDateTime,
                                LastUpdateDateTime = DateTime.Now
                            };
                            var updateResult = DalBaoYangActivitySetting.UpdateBaoYangActivityRegion(conn, updateModel);
                            if (!updateResult)
                            {
                                throw new Exception($"UpdateBaoYangActivityRegion失败,{JsonConvert.SerializeObject(updateModel)}");
                            }

                            var log = new BaoYangOprLog
                            {
                                LogType     = "BaoYangActivityRegion",
                                IdentityID  = updateModel.RegionId.ToString(),
                                OldValue    = JsonConvert.SerializeObject(oldValue),
                                NewValue    = JsonConvert.SerializeObject(updateModel),
                                Remarks     = "Update",
                                OperateUser = user,
                            };
                            logData.Add(log);
                        }
                        result = true;
                    });
                    foreach (var log in logData)
                    {
                        LoggerManager.InsertLog("BYOprLog", log);
                    }
                    cacheResult = CleanBaoYangActivityRegionCache(regionIds);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("MultiAddOrEditBaoYangActivityRegion", ex);
            }
            return(Tuple.Create(result, cacheResult));
        }