Пример #1
0
        /// <summary>
        /// 巡检表详情页
        /// <param name="keyValue">主键</param>
        /// <param name="action">add 新增;edit修改 </param>
        /// </summary>
        /// <returns></returns>
        public ActionResult InspectionAdd(string keyValue)
        {
            DeviceInspectionEntity entity = _bll.GetEntity(keyValue);

            if (entity == null)
            {
                entity = new DeviceInspectionEntity();
                entity.Create();
            }
            List <DeviceInspectionItemEntity> items = _bll.GetDeviceInspectionItems(keyValue);

            ViewBag.Items = HttpUtility.JavaScriptStringEncode(JsonConvert.SerializeObject(items));
            return(View(entity));
        }
Пример #2
0
        public ActionResult Import()
        {
            Operator user = OperatorProvider.Provider.Current();

            try
            {
                if (this.Request.Files.Count == 0)
                {
                    throw new Exception("请上传文件");
                }
                if (!this.Request.Files[0].FileName.EndsWith(".xlsx"))
                {
                    return(Json(new { success = false, message = "请上传excel文件!" }));
                }

                var book  = new Workbook(this.Request.Files[0].InputStream);
                var sheet = book.Worksheets[0];

                if (sheet.Cells[1, 0].StringValue != "检查表名称*" || sheet.Cells[1, 1].StringValue != "设备系统*" || sheet.Cells[1, 2].StringValue != "检查项目*" || sheet.Cells[1, 3].StringValue != "方法*" || sheet.Cells[1, 4].StringValue != "标准*" || sheet.Cells[1, 5].StringValue != "班组*")
                {
                    return(Json(new { success = false, message = "请使用正确的模板导入!" }));
                }

                bool   success = true;
                string message = "导入成功";
                List <ImportInspectionModel> importList = new List <ImportInspectionModel>();
                for (int i = 2; i <= sheet.Cells.MaxDataRow; i++)
                {
                    if (string.IsNullOrEmpty(sheet.Cells[i, 0].StringValue))
                    {
                        success = false; message = "检查表名称不能为空"; break;
                    }
                    if (string.IsNullOrEmpty(sheet.Cells[i, 1].StringValue))
                    {
                        success = false; message = "设备系统不能为空"; break;
                    }
                    if (string.IsNullOrEmpty(sheet.Cells[i, 2].StringValue))
                    {
                        success = false; message = "检查项目不能为空"; break;
                    }
                    if (string.IsNullOrEmpty(sheet.Cells[i, 3].StringValue))
                    {
                        success = false; message = "方法不能为空"; break;
                    }
                    if (string.IsNullOrEmpty(sheet.Cells[i, 4].StringValue))
                    {
                        success = false; message = "标准不能为空"; break;
                    }
                    if (string.IsNullOrEmpty(sheet.Cells[i, 5].StringValue))
                    {
                        success = false; message = "班组不能为空"; break;
                    }

                    ImportInspectionModel import = new ImportInspectionModel()
                    {
                        InspectionName = sheet.Cells[i, 0].StringValue,
                        DeviceSystem   = sheet.Cells[i, 0].StringValue,
                        ItemName       = sheet.Cells[i, 2].StringValue,
                        Method         = sheet.Cells[i, 3].StringValue,
                        Standard       = sheet.Cells[i, 4].StringValue,
                        DeptName       = sheet.Cells[i, 5].StringValue,
                    };
                    importList.Add(import);
                }


                //检查表名称、设备系统、班组相同的数据进行合并
                List <InspectionGroupingModel> groupdata = new List <InspectionGroupingModel>();
                foreach (var item in importList)
                {
                    //判断该数据是否是要合并的数据
                    if (groupdata.Any(x => x.InspectionName.Equals(item.InspectionName) && x.DeivceSystem.Equals(item.DeviceSystem) && x.DeptName.Equals(item.DeptName)))
                    {
                        //合并
                        groupdata.FirstOrDefault(x => x.InspectionName.Equals(item.InspectionName) && x.DeivceSystem.Equals(item.DeviceSystem) && x.DeptName.Equals(item.DeptName)).ItemList.Add(item);
                        continue;
                    }
                    //创建新的合并数据
                    groupdata.Add(new InspectionGroupingModel()
                    {
                        DeivceSystem   = item.DeviceSystem,
                        DeptName       = item.DeptName,
                        InspectionName = item.InspectionName,
                        ItemList       = new List <ImportInspectionModel>()
                        {
                            item
                        }
                    });
                }

                //组装数据
                if (groupdata != null && groupdata.Count > 0)
                {
                    List <DepartmentEntity>           deptList           = new DepartmentBLL().GetAll();            //所有的部门
                    List <DeviceInspectionEntity>     inspectionEntities = new List <DeviceInspectionEntity>();     //要新增的检查表的数据
                    List <DeviceInspectionItemEntity> itemEntities       = new List <DeviceInspectionItemEntity>(); //要新增的检查项的数据
                    groupdata.ForEach(groupItem => {
                        //先组装检查表的数据
                        DepartmentEntity dept = deptList.First(p => p.FullName.Equals(groupItem.DeptName));
                        if (dept == null)
                        {
                            success = false; message = $"班组\"{groupItem.DeptName}\"不存在,请更改后再上传";
                        }
                        DeviceInspectionEntity inspectionEntity = new DeviceInspectionEntity()
                        {
                            InspectionName = groupItem.InspectionName,
                            DeviceSystem   = groupItem.DeivceSystem,
                            DeptId         = dept.DepartmentId,
                            DeptCode       = dept.EnCode,
                            DeptName       = dept.FullName
                        };
                        inspectionEntity.Create();
                        inspectionEntities.Add(inspectionEntity);
                        //后组装检查项的数据
                        groupItem.ItemList.ForEach(item => {
                            itemEntities.Add(new DeviceInspectionItemEntity()
                            {
                                Id       = Guid.NewGuid().ToString(),
                                DeviceId = inspectionEntity.Id,
                                ItemName = item.ItemName,
                                Method   = item.Method,
                                Standard = item.Standard
                            });
                        });
                    });

                    //新增到数据库
                    _bll.Import(inspectionEntities, itemEntities);
                }
                else
                {
                    success = false; message = "导入数据为空";
                }
                return(Json(new { success, message }));
            }
            catch (Exception ex)
            {
                return(Json(new { success = false, message = ex.Message }));
            }
        }