コード例 #1
0
        public ActionResult btnSubmit_Click(JArray Grid1_fields, JArray Grid1_modifiedData, int Grid1_pageIndex,
                                            int Grid1_pageSize)
        {
            if (!Grid1_modifiedData.Any())
            {
                ShowNotify("无修改数据!");
                return(UIHelper.Result());
            }
            foreach (var jToken in Grid1_modifiedData)
            {
                var    modifiedRow = (JObject)jToken;
                string status      = modifiedRow.Value <string>("status");
                var    rowId       = modifiedRow.Value <string>("id");

                if (status == "newadded")
                {
                    var rowDic = modifiedRow.Value <JObject>("values").ToObject <Dictionary <string, object> >();
                    var model  = new ScreenRepairs();
                    model._id = (int)(_repDetail.Max(t => t._id) ?? 0) + 1;
                    foreach (var p in rowDic)
                    {
                        var prop = typeof(ScreenRepairs).GetProperty(p.Key);
                        if (prop.PropertyType == typeof(int))
                        {
                            prop.SetValue(model, Convert.ToInt32(p.Value));
                        }
                        else if (prop.PropertyType == typeof(DateTime?) || prop.PropertyType == typeof(DateTime))
                        {
                            prop.SetValue(model, Convert.ToDateTime(p.Value));
                        }
                        else if (prop.PropertyType == typeof(double))
                        {
                            double d;
                            if (double.TryParse(p.Value.ToString(), out d))
                            {
                                prop.SetValue(model, d);
                            }
                        }
                        else if (prop.PropertyType != typeof(int?))
                        {
                            prop.SetValue(model, p.Value);
                        }
                    }
                    _rep.Add(model);
                }
                else if (status == "modified")
                {
                    var rowDic = modifiedRow.Value <JObject>("values").ToObject <Dictionary <string, object> >();
                    foreach (var p in rowDic)
                    {
                        var param  = Expression.Parameter(typeof(ScreenRepairs), "x");
                        var body   = Expression.Property(param, typeof(ScreenRepairs), p.Key);
                        var lambda =
                            Expression.Lambda <Func <ScreenRepairs, object> >(Expression.Convert(body, typeof(object)), param);
                        _rep.Update(t => t._id == int.Parse(rowId), Builders <ScreenRepairs> .Update.Set(lambda, p.Value));
                    }
                }
                else if (status == "deleted")
                {
                    _rep.Delete(t => t._id == int.Parse(rowId));
                }
            }
            ShowNotify("数据保存成功!");
            int count;
            var source = _rep.QueryByPage(Grid1_pageIndex, Grid1_pageSize, out count);
            var grid1  = UIHelper.Grid("Grid1");

            grid1.RecordCount(count);
            grid1.DataSource(source, Grid1_fields);
            return(UIHelper.Result());
        }
コード例 #2
0
        public ActionResult AddOrEdit(ScreenRepairs model)
        {
            try
            {
                var isStatusInput = Request["Status_isUserInput"];
                if (!string.IsNullOrEmpty(isStatusInput))
                {
                    model.Status = (HitchStatusEnum)Enum.Parse(typeof(HitchStatusEnum), Request["Status_text"]);
                }
                var isTypeInput = Request["HitchType_isUserInput"];
                if (!string.IsNullOrEmpty(isTypeInput))
                {
                    if (bool.Parse(isTypeInput))
                    {
                        model.HitchType = Request["HitchType_text"];
                    }
                }

                var isAccInput  = Request["Accepter_isUserInput"];
                var isHandInput = Request["Handler_isUserInput"];
                if (!string.IsNullOrEmpty(isAccInput))
                {
                    model.Accepter = Request["Accepter_text"];
                }
                if (!string.IsNullOrEmpty(isHandInput))
                {
                    model.Handler = Request["Handler_text"];
                }
                //处理上传附件
                var file = Request.Files["file"];
                if (file != null && file.ContentLength != 0)
                {
                    var fileName = file.FileName;
                    var fileType = GetFileType(fileName);
                    if (!ValidFileTypes.Contains(fileType))
                    {
                        // 清空文件上传组件
                        UIHelper.FileUpload("file").Reset();
                        ShowNotify("无效的文件类型!");
                    }
                    else
                    {
                        var md5         = file.InputStream.GetMd5();
                        var repairModel = _rep.Get(t => t.FileMd5 == md5);
                        if (repairModel != null)
                        {
                            model.FileName = repairModel.FileName;
                            model.FilePath = repairModel.FilePath;
                        }
                        else
                        {
                            var name = file.FileName;
                            if (_rep.Get(t => t.FileName == file.FileName) != null)
                            {
                                name = file.FileName + "_" + Guid.NewGuid();
                            }
                            var path = Server.MapPath("~/attachments");
                            if (!Directory.Exists(path))
                            {
                                Directory.CreateDirectory(path);
                            }
                            var attachPath = Path.Combine(path, name);
                            file.SaveAs(attachPath);
                            model.FileName = name;
                            model.FilePath = attachPath;
                        }
                        model.FileMd5 = md5;
                    }
                }

                if (model._id == 0)
                {
                    model._id = (int)(_rep.Max(t => t._id) ?? 0) + 1;
                    _rep.Add(model);
                    // 关闭本窗体(触发窗体的关闭事件)
                    PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
                }
                else
                {
                    _rep.Update(t => t._id == model._id,
                                Builders <ScreenRepairs> .Update.Set(t => t.RepairsDate, model.RepairsDate)
                                .Set(t => t.LineName, model.LineName)
                                .Set(t => t.Station, model.Station)
                                .Set(t => t.Owner, model.Owner)
                                .Set(t => t.RepairsSource, model.RepairsSource)
                                .Set(t => t.Accepter, model.Accepter)
                                .Set(t => t.Handler, model.Handler)
                                .Set(t => t.HitchType, model.HitchType)
                                .Set(t => t.Status, model.Status)
                                .Set(t => t.HitchContent, model.HitchContent)
                                .Set(t => t.Solution, model.Solution)
                                .Set(t => t.FileName, model.FileName)
                                .Set(t => t.FileMd5, model.FileMd5)
                                .Set(t => t.FilePath, model.FilePath));
                    // 关闭本窗体(触发窗体的关闭事件)
                    PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
                }
            }
            catch (Exception ex)
            {
                Alert.Show(ex.Message, MessageBoxIcon.Warning);
            }
            return(UIHelper.Result());
        }
コード例 #3
0
        public ActionResult RepairsImport(HttpPostedFileBase file)
        {
            if (file == null || file.ContentLength == 0)
            {
                return(UIHelper.Result());
            }
            var fileName = file.FileName;
            var fileType = GetFileType(fileName);

            if (!ValidFileTypes.Contains(fileType))
            {
                // 清空文件上传组件
                UIHelper.FileUpload("file").Reset();
                ShowNotify("无效的文件类型!");
            }
            else
            {
                try
                {
                    var       list    = new List <ScreenRepairs>();
                    var       listLog = new List <ScreenRecDetail>();
                    IWorkbook wb;
                    if (fileType == "xls")
                    {
                        wb = new HSSFWorkbook(file.InputStream);
                    }
                    else
                    {
                        wb = new XSSFWorkbook(file.InputStream);
                    }
                    var sheet    = wb.GetSheetAt(0);
                    var maxId    = (int)(_rep.Max(t => t._id) ?? 0) + 1;
                    var maxLogId = (int)(_repDetail.Max(t => t._id) ?? 0) + 1;
                    var logs     = _repDetail.Find(t => !t.IsLog).ToList();
                    for (var i = 1; i <= sheet.LastRowNum; i++)
                    {
                        var row   = sheet.GetRow(i);
                        var model = new ScreenRepairs();
                        model._id = maxId + i - 1;
                        row.GetCell(1).SetCellType(CellType.Numeric);
                        model.RepairsDate = row.GetCell(1).DateCellValue;
                        row.GetCell(2).SetCellType(CellType.String);
                        model.LineName = row.GetCell(2).StringCellValue;
                        model.Station  = row.GetCell(3).StringCellValue;
                        model.Owner    = row.GetCell(4).StringCellValue;
                        row.GetCell(5).SetCellType(CellType.String);
                        model.RepairsSource = row.GetCell(5).StringCellValue;
                        model.Accepter      = row.GetCell(6).StringCellValue;
                        model.Handler       = row.GetCell(7).StringCellValue;
                        model.HitchType     = row.GetCell(8).StringCellValue;
                        HitchStatusEnum e;
                        if (Enum.TryParse(row.GetCell(9).StringCellValue, out e))
                        {
                            model.Status = e;
                        }
                        //if (string.IsNullOrWhiteSpace(model.Status))
                        //{
                        //    model.Status = "未解决";
                        //}
                        model.HitchContent = row.GetCell(10).StringCellValue;
                        model.Solution     = row.GetCell(11).StringCellValue;


                        var lines = model.LineName.Replace("、", "/")
                                    .Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                        //var filter =
                        //    Builders<ScreenRecDetail>.Filter.And(
                        //        Builders<ScreenRecDetail>.Filter.Eq(t => t.InstallStation, model.Station),
                        //        Builders<ScreenRecDetail>.Filter.Or(
                        //            Builders<ScreenRecDetail>.Filter.In(t => t.LineName, lines),
                        //            Builders<ScreenRecDetail>.Filter.Eq(t => t.LineName, model.LineName),Builders<ScreenRecDetail>.Filter.Regex(t=>t.LineName,new BsonRegularExpression())));
                        if (model.LineName == null)
                        {
                            model.LineName = "";
                        }
                        var devs =
                            logs.Where(
                                t =>
                                (t.InstallStation == model.Station || model.Station.Contains(t.InstallStation) ||
                                 t.InstallStation.Contains(model.Station ?? "")) &&
                                (model.LineName.Contains(t.LineName) || t.LineName == model.LineName)).ToList();
                        if (devs.Any())
                        {
                            var devNums = new HashSet <string>();
                            foreach (var dev in devs)
                            {
                                if (!devNums.Add(dev.DeviceNum))
                                {
                                    continue;
                                }
                                //var log = new ScreenRecDetail();
                                //log._id = maxLogId;
                                //maxLogId++;
                                //log.DeviceNum = dev.DeviceNum;
                                //log.LogType = "服务类型";
                                //log.HandlingType = "维修";
                                //log.Date = model.RepairsDate;
                                //log.IsLog = true;
                                //log.Materials.Remark = string.Format("故障问题:{0};解决方法:{1}", model.HitchContent,
                                //    model.Solution);
                                //listLog.Add(log);

                                model.DeviceNum = dev.DeviceNum;
                            }
                        }
                        if (!string.IsNullOrEmpty(model.LineName) && !string.IsNullOrEmpty(model.Station) && !string.IsNullOrEmpty(model.Owner))
                        {
                            list.Add(model);
                        }
                    }
                    if (list.Any())
                    {
                        _rep.BulkInsert(list);
                    }
                    if (listLog.Any())
                    {
                        _repDetail.BulkInsert(listLog);
                    }
                    // 关闭本窗体(触发窗体的关闭事件)
                    PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
                    ShowNotify("导入成功");
                }
                //catch (Exception e)
                //{
                //    Alert.Show(e.Message, MessageBoxIcon.Warning);
                //    return UIHelper.Result();
                //}
                finally
                {
                    file.InputStream.Close();
                }
            }
            return(UIHelper.Result());
        }