Esempio n. 1
0
        public ViewResult Edit(string id)
        {
            var     user    = OperatorProvider.Provider.Current();
            UserBLL userBLL = new UserBLL();
            var     users   = userBLL.GetDeptUsers(user.DeptId).ToList();
            //任务类型
            var itemdetialbll = new DataItemDetailBLL();
            var itembll       = new DataItemBLL();
            var type          = itembll.GetEntityByName("任务库任务类型");
            var content       = itemdetialbll.GetList(type.ItemId).ToList();
            var jobtype       = content.Select(x => new SelectListItem()
            {
                Value = x.ItemId, Text = x.ItemName
            }).ToList();
            WorkSettingBLL settingbll = new WorkSettingBLL();
            var            setting    = settingbll.GetList("");
            //var pDeptid = dept.GetParent(deptid,"部门");
            var setupid      = string.Empty;
            var createuserid = string.Empty;

            //WorkOrderBLL orderbll = new WorkOrderBLL();
            //var banci = orderbll.getSetValue(user.DeptId).Select(x => new SelectListItem() { Text = x.Value, Value = x.Key }).ToList();
            //orderbll.GetWorkSettingByDept(user.DeptId, out setupid, out createuserid);
            //var getbanci = setting.Where(x => x.WorkSetupId == setupid && x.CreateUserId == createuserid);
            //var banci = getbanci.OrderBy(x => x.StartTime).Select(x => new SelectListItem() { Text = x.Name, Value = x.WorkSettingId }).ToList();
            //ViewData["banci"] = banci;
            ViewData["users"]   = users;
            ViewData["jobtype"] = jobtype;
            var job = workmeetingbll.GetJobTemplate(id);

            if (job == null)
            {
                job = new JobTemplateEntity();
                job.JobStartTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 9, 0, 0);
                job.JobEndTime   = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 15, 30, 0);
                job.Cycle        = "";
            }
            ViewBag.JobTime = string.Format("{0} - {1}", job.JobStartTime.HasValue ? job.JobStartTime.Value.ToString("HH:mm") : string.Empty, job.JobEndTime.HasValue ? job.JobEndTime.Value.ToString("HH:mm") : string.Empty);

            return(View(job));
        }
Esempio n. 2
0
        public JsonResult DoImport()
        {
            var success = true;
            var message = string.Empty;

            try
            {
                if (this.Request.Files.Count == 0)
                {
                    throw new Exception("请上传文件");
                }
                if (!this.Request.Files[0].FileName.EndsWith(".xlsx"))
                {
                    throw new Exception("请上传 Excel 文件");
                }

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

                var     templates = new List <JobTemplateEntity>();
                var     date      = DateTime.Now;
                UserBLL userBLL   = new UserBLL();
                var     users     = userBLL.GetDeptUsers(user.DeptId).ToList();
                for (int i = 2; i <= sheet.Cells.MaxDataRow; i++)
                {
                    var entity = new JobTemplateEntity();
                    entity.JobId      = Guid.NewGuid().ToString();
                    entity.JobContent = sheet.Cells[i, 0].StringValue;
                    if (string.IsNullOrEmpty(entity.JobContent))
                    {
                        if (templates.Count > 0 && string.IsNullOrEmpty(sheet.Cells[i, 1].StringValue))
                        {
                            break;
                        }
                        success  = false;
                        message += "第" + (i + 1) + "行,未填写工作任务</br>";
                        continue;
                        //return Json(new { success, message });
                    }
                    var sss = sheet.Cells[i, 1].StringValue;
                    if (!string.IsNullOrEmpty(sheet.Cells[i, 1].StringValue))
                    {
                        var itemdetialbll = new DataItemDetailBLL();
                        var itembll       = new DataItemBLL();
                        var type          = itembll.GetEntityByName("任务库任务类型");
                        var content       = itemdetialbll.GetList(type.ItemId).ToList();
                        var typename      = sheet.Cells[i, 1].StringValue.Trim();
                        var gettype       = content.FirstOrDefault(row => row.ItemName == typename);
                        if (gettype == null)
                        {
                            success  = false;
                            message += "第" + (i + 1) + "行,不存在该类型</br>";
                            continue;
                            //return Json(new { success, message });
                        }
                        entity.jobplantype   = gettype.ItemName;
                        entity.jobplantypeid = gettype.ItemId;
                    }
                    else
                    {
                        success  = false;
                        message += "第" + (i + 1) + "行,任务类型不能为空</br>";
                        continue;
                        //return Json(new { success, message });
                    }
                    entity.RiskLevel = sheet.Cells[i, 2].StringValue;
                    if (string.IsNullOrEmpty(entity.RiskLevel))
                    {
                        success  = false;
                        message += "第" + (i + 1) + "行,未填写风险等级</br>";
                        continue;
                        // return Json(new { success, message });
                    }

                    var jobperson = sheet.Cells[i, 3].StringValue;
                    if (jobperson.Contains(","))
                    {
                        var person     = string.Empty;
                        var personid   = string.Empty;
                        var personList = jobperson.Split(',');
                        for (int j = 0; j < personList.Length; j++)
                        {
                            var ckjobperson = users.FirstOrDefault(row => row.RealName == personList[j]);
                            if (ckjobperson == null)
                            {
                                success  = false;
                                message += "第" + (i + 1) + "行," + personList[j] + "作业人错误</br>";
                                continue;
                                //return Json(new { success, message });
                            }
                            if (j >= personList.Length - 1)
                            {
                                person   += personList[j];
                                personid += ckjobperson.DepartmentId;
                            }
                            else
                            {
                                person   += personList[j] + ",";
                                personid += ckjobperson.DepartmentId + ",";
                            }
                        }

                        entity.JobPerson   = person;
                        entity.JobPersonId = personid;
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(jobperson))
                        {
                            var ckjobperson = users.FirstOrDefault(row => row.RealName == jobperson);
                            if (ckjobperson == null)
                            {
                                success  = false;
                                message += "第" + (i + 1) + "行," + jobperson + "作业人错误</br>";
                                continue;
                                // return Json(new { success, message });
                            }
                            entity.JobPerson   = jobperson;
                            entity.JobPersonId = ckjobperson == null ? null : ckjobperson.UserId;
                        }
                    }
                    //string otherperson = sheet.Cells[i, 3].StringValue;
                    //if (!string.IsNullOrEmpty(otherperson))
                    //{
                    //    var ckotherperson = users.FirstOrDefault(row => row.RealName == otherperson);
                    //    if (ckotherperson == null)
                    //    {
                    //        success = false;
                    //        message = "选填作业人错误";
                    //        return Json(new { success, message });
                    //    }
                    //}
                    //if (otherperson == jobperson)
                    //{
                    //    success = false;
                    //    message = "作业人不能相同";
                    //    return Json(new { success, message });
                    //}
                    //entity.otherperson = otherperson;
                    entity.Device = sheet.Cells[i, 4].StringValue;
                    var getNow = DateTime.Now.ToString("yyyy-MM-dd");
                    //entity.JobStartTime = sheet.Cells[i, 5].StringValue == "" ? Convert.ToDateTime(getNow + " 08:30") : Convert.ToDateTime(getNow + " " + sheet.Cells[i, 5].StringValue);
                    //entity.JobEndTime = sheet.Cells[i, 6].StringValue == "" ? Convert.ToDateTime(getNow + " 17:30") : Convert.ToDateTime(getNow + " " + sheet.Cells[i, 6].StringValue);
                    entity.JobStartTime = Convert.ToDateTime(getNow + " 08:30");
                    entity.JobEndTime   = Convert.ToDateTime(getNow + " 17:30");
                    if (!string.IsNullOrEmpty(sheet.Cells[i, 5].StringValue))
                    {
                        //每年,二月、九月,15日,白班  每月,第一个、第三个,星期五,白班
                        var Cycle = sheet.Cells[i, 5].StringValue.Trim();

                        var cycleType = Cycle.Split(',');
                        if (cycleType[0] != "每天" && cycleType[0] != "每周" && cycleType[0] != "每月" && cycleType[0] != "每年")
                        {
                            success  = false;
                            message += "第" + (i + 1) + "行,周期规则错误</br>";
                            continue;
                            // return Json(new { success, message });
                        }

                        entity.Cycle = cycleType[0];
                        var ck   = false;
                        var data = string.Empty;
                        for (int j = 1; j < cycleType.Length; j++)
                        {
                            //if (cycleType[j] == "白班" || cycleType[j] == "夜班")
                            //{
                            //    entity.worksetname = cycleType[j];

                            //}
                            //else
                            if (cycleType[j] == "截止")
                            {
                                entity.isend = true;
                            }
                            else
                            if (cycleType[j] == "最后一天")
                            {
                                if ((Cycle.Contains("每月") || Cycle.Contains("每年")) && Cycle.Contains("日"))
                                {
                                    entity.islastday = true;
                                }
                                else
                                {
                                    success  = false;
                                    message += "第" + (i + 1) + "行,周期规则错误</br>";
                                    continue;
                                    //return Json(new { success, message });
                                }
                            }
                            else
                            if (cycleType[j].Contains("双休"))
                            {
                                if ((Cycle.Contains("每月") || Cycle.Contains("每年")) && Cycle.Contains("日"))
                                {
                                    entity.isweek = true;
                                }
                                else if (Cycle.Contains("每天"))
                                {
                                    entity.isweek = true;
                                }
                                else
                                {
                                    success  = false;
                                    message += "第" + (i + 1) + "行,周期规则错误</br>";
                                    continue;
                                    //return Json(new { success, message });
                                }
                            }
                            else
                            {
                                data += cycleType[j].Replace('日', ' ').Trim().Replace('、', ',') + ";";
                                ck    = true;
                            }
                        }

                        if (ck)
                        {
                            data             = data.Substring(0, data.Length - 1);
                            entity.CycleDate = data;
                        }
                        else
                        {
                            entity.CycleDate = data;
                        }
                    }
                    else
                    {
                        if (entity.jobplantype != "临时任务")
                        {
                            success  = false;
                            message += "第" + (i + 1) + "行,周期不能为空</br>";
                            continue;
                            //return Json(new { success, message });
                        }
                    }
                    entity.Dangerous = sheet.Cells[i, 6].StringValue;
                    entity.Measure   = sheet.Cells[i, 7].StringValue;
                    var EnableTraining = sheet.Cells[i, 8].StringValue;
                    entity.EnableTraining = EnableTraining == "是";
                    //entity.EnableTraining = false;
                    entity.worksetname = sheet.Cells[i, 9].StringValue;
                    if (entity.jobplantype == "设备巡回检查")
                    {
                        entity.TaskType = "巡回检查";
                    }
                    else if (entity.jobplantype == "定期工作")
                    {
                        entity.TaskType = "定期工作";
                    }
                    else
                    {
                        entity.TaskType = "日常工作";
                    }
                    var          setupid      = string.Empty;
                    var          createuserid = string.Empty;
                    WorkOrderBLL orderbll     = new WorkOrderBLL();
                    orderbll.GetWorkSettingByDept(user.DeptId, out setupid, out createuserid);
                    WorkSettingBLL settingbll = new WorkSettingBLL();
                    var            setting    = settingbll.GetList("");
                    var            getbanci   = setting.Where(x => x.WorkSetupId == setupid && x.CreateUserId == createuserid);
                    if (entity.worksetname.Contains(","))
                    {
                        var setname   = string.Empty;
                        var setnameid = string.Empty;
                        var setList   = entity.worksetname.Split(',');
                        for (int j = 0; j < setList.Length; j++)
                        {
                            var ckset = getbanci.FirstOrDefault(x => x.Name == entity.worksetname);
                            if (ckset == null)
                            {
                                success  = false;
                                message += "第" + (i + 1) + "行,不存在该班次" + setList[j] + "</br>";
                                continue;
                                //return Json(new { success, message });
                            }
                            if (j >= setList.Length - 1)
                            {
                                setname   += setList[j];
                                setnameid += ckset.WorkSettingId;
                            }
                            else
                            {
                                setname   += setList[j] + ",";
                                setnameid += ckset.WorkSettingId + ",";
                            }
                        }

                        entity.worksetname = setname;
                        entity.worksetid   = setnameid;
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(entity.worksetname))
                        {
                            if (entity.jobplantype != "临时任务")
                            {
                                success  = false;
                                message += "第" + (i + 1) + "行,班次不能为空</br>";
                                continue;
                                //return Json(new { success, message });
                            }
                        }
                        else
                        {
                            var ckset = getbanci.FirstOrDefault(x => x.Name == entity.worksetname);
                            if (ckset == null)
                            {
                                success  = false;
                                message += "第" + (i + 1) + "行,不存在该班次" + entity.worksetname + "</br>";
                                continue;
                                //return Json(new { success, message });
                            }
                            entity.worksetname = ckset.Name;
                            entity.worksetid   = ckset.WorkSettingId;
                        }
                    }
                    entity.DeptId     = user.DeptId;
                    entity.CreateDate = date.AddMinutes(i);
                    entity.DangerType = "job";
                    templates.Add(entity);
                }
                if (!success)
                {
                    return(Json(new { success, message }));
                }
                foreach (var item in templates)
                {
                    if (item.DangerousList == null)
                    {
                        item.DangerousList = new List <JobDangerousEntity>();
                    }
                    if (!string.IsNullOrEmpty(item.Dangerous))
                    {
                        var dangerArray        = item.Dangerous.Split('。');
                        var dangerMeasureArray = item.Measure?.Split('。');
                        for (int i = 0; i < dangerArray.Length; i++)
                        {
                            var danger = dangerArray[i];
                            if (string.IsNullOrEmpty(danger))
                            {
                                continue;
                            }
                            var templateDangerousEntity = new JobDangerousEntity {
                                Content = danger
                            };
                            if (dangerMeasureArray != null && dangerMeasureArray.Length > i)
                            {
                                templateDangerousEntity.MeasureList = new List <JobMeasureEntity>();
                                var dangerMeasure = dangerMeasureArray[i];
                                if (!string.IsNullOrEmpty(dangerMeasure))
                                {
                                    var measureArray = dangerMeasure.Split(';');
                                    foreach (var measure in measureArray)
                                    {
                                        if (string.IsNullOrEmpty(measure))
                                        {
                                            continue;
                                        }
                                        templateDangerousEntity.MeasureList.Add(new JobMeasureEntity {
                                            Content = measure
                                        });
                                    }
                                }
                            }
                            item.DangerousList.Add(templateDangerousEntity);
                        }
                    }
                    foreach (var item1 in item.DangerousList)
                    {
                        if (string.IsNullOrEmpty(item1.JobDangerousId))
                        {
                            item1.JobDangerousId = Guid.NewGuid().ToString();
                        }
                        item1.CreateTime = DateTime.Now;
                        item1.JobId      = item.JobId;
                        if (item1.MeasureList == null)
                        {
                            item1.MeasureList = new List <JobMeasureEntity>();
                        }
                        foreach (var item2 in item1.MeasureList)
                        {
                            if (string.IsNullOrEmpty(item2.JobMeasureId))
                            {
                                item2.JobMeasureId = Guid.NewGuid().ToString();
                            }
                            item2.CreateTime     = DateTime.Now;
                            item2.JobDangerousId = item1.JobDangerousId;
                        }
                    }

                    item.CreateUserId = user.UserId;
                    item.CreateUser   = user.UserName;
                    workmeetingbll.UpdateJobTemplate(item);
                }
                // workmeetingbll.AddJobTemplates(templates);
            }
            catch (Exception ex)
            {
                success = false;
                message = HttpUtility.JavaScriptStringEncode(ex.Message);
            }

            return(Json(new { success, message }));
        }