/// <summary>
        /// 验证报表参数
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="parameter"></param>
        private void ValidReportParameter(MissionskyOAEntities dbContext, ReportParameterModel parameter)
        {
            var existedReport =
                dbContext.ReportParameters.FirstOrDefault(
                    it =>
                    !string.IsNullOrEmpty(it.Name) && it.ReportId == parameter.Id &&
                    it.Name.Equals(parameter.Name, StringComparison.InvariantCultureIgnoreCase) && parameter.Id != it.Id);

            if (existedReport != null)
            {
                Log.Error(string.Format("报表参数已经存在,报表: {0}, 报表参数: {1}", parameter.Id, parameter.Name));
                throw new InvalidOperationException(string.Format("报表参数已经存在,报表: {0}, 报表参数: {1}", parameter.Id, parameter.Name));
            }
        }
        /// <summary>
        /// 更新报表参数
        /// </summary>
        /// <param name="param">报表参数</param>
        /// <returns>true or exception</returns>
        public bool UpdateParameter(ReportParameterModel param)
        {
            if (param == null)
            {
                Log.Error("更新报表参数异常。");
                throw new InvalidOperationException("更新报表参数异常。");
            }

            using (var dbContext = new MissionskyOAEntities())
            {
                ValidReportParameter(dbContext, param);

                var oldParameter = dbContext.ReportParameters.FirstOrDefault(it => it.Id == param.Id);
                if (oldParameter == null)
                {
                    Log.Error(string.Format("找不到报表参数,报表参数Id: {0}", param.Id));
                    throw new InvalidOperationException(string.Format("找不到报表参数,报表参数Id: {0}", param.Id));
                }

                //更新报表参数
                if (!oldParameter.Name.Equals(param.Name, StringComparison.InvariantCultureIgnoreCase))
                {
                    oldParameter.Name = param.Name;
                }

                if (string.IsNullOrEmpty(oldParameter.Desc) || !oldParameter.Desc.Equals(param.Desc, StringComparison.InvariantCultureIgnoreCase))
                {
                    oldParameter.Desc = param.Desc;
                }

                if (!oldParameter.Type.Equals(param.Type, StringComparison.InvariantCultureIgnoreCase))
                {
                    oldParameter.Type = param.Type;
                }

                if (string.IsNullOrEmpty(oldParameter.DataSource) || !oldParameter.DataSource.Equals(param.DataTable, StringComparison.InvariantCultureIgnoreCase))
                {
                    oldParameter.DataSource = param.DataTable;
                }

                if (!oldParameter.Nullable.HasValue || oldParameter.Nullable != param.Nullable)
                {
                    oldParameter.Nullable = param.Nullable;
                }

                dbContext.SaveChanges(); //更新数据库

                return(true);
            }
        }
        public static ReportParameter ToEntity(this ReportParameterModel model)
        {
            var entity = new ReportParameter()
            {
                Id          = model.Id,
                ReportId    = model.ReportId,
                Name        = model.Name,
                Desc        = model.Desc,
                Type        = model.Type,
                DataSource  = model.DataTable,
                Nullable    = model.Nullable,
                CreatedTime = model.CreatedTime
            };

            return(entity);
        }
        public static ReportParameterModel ToModel(this ReportParameter entity)
        {
            var model = new ReportParameterModel()
            {
                Id          = entity.Id,
                ReportId    = entity.ReportId,
                Name        = entity.Name ?? string.Empty,
                Desc        = entity.Desc ?? string.Empty,
                Type        = entity.Type ?? string.Empty,
                DataTable   = entity.DataSource ?? string.Empty,
                Nullable    = entity.Nullable.HasValue ? entity.Nullable.Value : true,
                CreatedTime = entity.CreatedTime
            };

            return(model);
        }
Beispiel #5
0
        public ActionResult EditParameter(ReportParameterModel model)
        {
            if (string.IsNullOrEmpty(model.Name))
            {
                ModelState.AddModelError("Name", "请输入报表参数名称.");
            }

            if (string.IsNullOrEmpty(model.Type))
            {
                ModelState.AddModelError("Type", "请选择报表参数类型.");
            }

            if (model.Type.Equals(Constant.REPORT_PARAM_TYPE_DROPDOWNLIST, StringComparison.InvariantCultureIgnoreCase) &&
                string.IsNullOrEmpty(model.DataTable))
            {
                ModelState.AddModelError("DataTable", "请输入参数数据源.");
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (model.Id <= 0)
                    {
                        ReportService.AddParameter(model);
                    }
                    else
                    {
                        ReportService.UpdateParameter(model);
                    }

                    return(RedirectToAction("Edit", new { id = model.ReportId }));
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    ViewBag.Message = ex.Message;
                }
            }

            EditParameter(model.ReportId, model.Id);
            return(View(model));
        }
        /// <summary>
        /// 添加报表参数
        /// </summary>
        /// <param name="param">报表参数</param>
        /// <returns>report id</returns>
        public int AddParameter(ReportParameterModel param)
        {
            if (param == null)
            {
                Log.Error("添加报表参数异常。");
                throw new InvalidOperationException("添加报表参数异常。");
            }

            using (var dbContext = new MissionskyOAEntities())
            {
                ValidReportParameter(dbContext, param);

                //添加报表参数
                var entity = param.ToEntity();
                entity.CreatedTime = DateTime.Now;
                dbContext.ReportParameters.Add(entity);
                dbContext.SaveChanges(); //更新数据库

                return(entity.Id);
            }
        }
Beispiel #7
0
        //
        // GET: /Report/EditParameter/1
        public ActionResult EditParameter(int rId, int?pId)
        {
            if (rId < 1)
            {
                return(RedirectToAction("Edit"));
            }

            var         selectedType = string.Empty;
            ReportModel report       = ReportService.GetReportDetail(rId);

            if (report == null)
            {
                Log.Error("找不到报表。");
                throw new KeyNotFoundException("找不到报表。");
            }

            ReportParameterModel parameter = new ReportParameterModel();

            if (pId.HasValue && pId.Value > 0) //修改
            {
                parameter = ReportService.GetParameterDetail(pId.Value);

                if (parameter == null || parameter.ReportId != rId)
                {
                    Log.Error("找不到参数。");
                    throw new KeyNotFoundException("找不到参数。");
                }

                //参数类型
                selectedType = parameter.Type;
            }

            InitParameterTypes(selectedType);
            ViewData["Report"] = report;
            parameter.ReportId = rId;

            return(View(parameter));
        }