Example #1
0
        public string ImportLabor()
        {
            LaborprotectionBLL laborprotectionbll = new LaborprotectionBLL();
            PostCache          postCache          = new PostCache();
            PostBLL            postBLL            = new PostBLL();
            DepartmentBLL      departmentBLL      = new DepartmentBLL();
            //获取到已选数据
            List <LaborprotectionEntity> laborlist = laborprotectionbll.GetLaborList();
            var    currUser     = OperatorProvider.Provider.Current();
            string orgId        = OperatorProvider.Provider.Current().OrganizeId;//所属公司
            int    error        = 0;
            string message      = "请选择格式正确的文件再导入!";
            string falseMessage = "";
            int    count        = HttpContext.Request.Files.Count;

            if (count > 0)
            {
                HttpPostedFileBase file = HttpContext.Request.Files[0];
                if (string.IsNullOrEmpty(file.FileName))
                {
                    return(message);
                }
                if (!(file.FileName.Substring(file.FileName.IndexOf('.')).Contains("xls") || file.FileName.Substring(file.FileName.IndexOf('.')).Contains("xlsx")))
                {
                    return(message);
                }
                string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + System.IO.Path.GetExtension(file.FileName);
                file.SaveAs(Server.MapPath("~/Resource/temp/" + fileName));
                Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook();
                wb.Open(Server.MapPath("~/Resource/temp/" + fileName));
                Aspose.Cells.Cells cells = wb.Worksheets[0].Cells;
                if (cells.MaxDataRow == 0)
                {
                    message = "没有数据,请选择先填写模板在进行导入!";
                    return(message);
                }
                DataTable dt    = cells.ExportDataTable(0, 0, cells.MaxDataRow + 1, cells.MaxColumn + 1, true);
                int       order = 1;

                IList <LaborprotectionEntity> LaborList = new List <LaborprotectionEntity>();

                IEnumerable <DepartmentEntity> deptlist = new DepartmentBLL().GetList();
                OrganizeBLL orgbll = new OrganizeBLL();
                //先获取到原始的一个编号
                string no   = laborprotectionbll.GetNo();
                int    ysno = Convert.ToInt32(no);

                DataItemDetailBLL dataItemDetailBLL = new DataItemDetailBLL();
                var dataitem = dataItemDetailBLL.GetDataItemListByItemCode("'LaborName'").ToList();
                List <LaborprotectionEntity> insertpro  = new List <LaborprotectionEntity>();
                List <LaborinfoEntity>       insertinfo = new List <LaborinfoEntity>();
                //先获取人员
                List <UserEntity> userlist =
                    new UserBLL().GetListForCon(it => it.IsPresence == "1" && it.Account != "System").ToList();
                List <LaborequipmentinfoEntity> eqlist = new List <LaborequipmentinfoEntity>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    order = i;
                    string Name = dt.Rows[i]["名称"].ToString();
                    string Model = dt.Rows[i]["型号"].ToString();
                    string Type = dt.Rows[i]["类型"].ToString();
                    string DeptName = dt.Rows[i]["使用部门"].ToString();
                    string OrgName = dt.Rows[i]["使用单位"].ToString();
                    string PostName = dt.Rows[i]["使用岗位"].ToString().Trim();
                    string Unit = dt.Rows[i]["劳动防护用品单位"].ToString().Trim();
                    string Time = dt.Rows[i]["使用期限"].ToString().Trim();
                    string TimeType = dt.Rows[i]["使用期限单位"].ToString().Trim();
                    string deptId = "", deptCode = "", PostId = "";
                    //---****值存在空验证*****--
                    if (string.IsNullOrEmpty(Name) || string.IsNullOrEmpty(Unit) || string.IsNullOrEmpty(DeptName) || string.IsNullOrEmpty(OrgName) || string.IsNullOrEmpty(PostName))
                    {
                        falseMessage += "</br>" + "第" + (i + 2) + "行值存在空,未能导入.";
                        error++;
                        continue;
                    }

                    //验证机构是不是和自己一个机构
                    DepartmentEntity org = deptlist.Where(it => it.FullName == OrgName).FirstOrDefault();
                    if (org == null)
                    {
                        falseMessage += "</br>" + "第" + (i + 2) + "行使用单位名称不存在,未能导入.";
                        error++;
                        continue;
                    }
                    //如果导入的机构id和本人的机构id不一致
                    if (org.DepartmentId != currUser.OrganizeId)
                    {
                        falseMessage += "</br>" + "第" + (i + 2) + "行使用单位不是导入者的单位,未能导入.";
                        error++;
                        continue;
                    }

                    //验证所填部门是否存在
                    var deptFlag = false;
                    var entity1  = deptlist.Where(x => x.OrganizeId == currUser.OrganizeId && x.FullName == DeptName).FirstOrDefault();
                    if (entity1 == null)
                    {
                        falseMessage += "</br>" + "第" + (i + 2) + "行使用部门不存在,未能导入.";
                        error++;
                        deptFlag = true;
                        break;
                    }
                    else
                    {
                        deptId   = entity1.DepartmentId;
                        deptCode = entity1.EnCode;
                    }

                    //var deptFlag = false;
                    //var array = DeptName.Split('/');
                    //for (int j = 0; j < array.Length; j++)
                    //{
                    //    if (j == 0)
                    //    {
                    //        if (currUser.RoleName.Contains("省级"))
                    //        {
                    //            var entity1 = deptlist.Where(x => x.OrganizeId == currUser.OrganizeId && x.FullName == array[j].ToString()).FirstOrDefault();
                    //            if (entity1 == null)
                    //            {
                    //                falseMessage += "</br>" + "第" + (i + 2) + "行部门不存在,未能导入.";
                    //                error++;
                    //                deptFlag = true;
                    //                break;
                    //            }
                    //            else
                    //            {
                    //                deptId = entity1.DepartmentId;
                    //                deptCode = entity1.EnCode;
                    //            }
                    //        }
                    //        else
                    //        {
                    //            var entity = deptlist.Where(x => x.OrganizeId == currUser.OrganizeId && x.Nature == "厂级" && x.FullName == array[j].ToString()).FirstOrDefault();
                    //            if (entity == null)
                    //            {
                    //                entity = deptlist.Where(x => x.OrganizeId == currUser.OrganizeId && x.Nature == "部门" && x.FullName == array[j].ToString()).FirstOrDefault();
                    //                if (entity == null)
                    //                {
                    //                    entity = deptlist.Where(x => x.OrganizeId == currUser.OrganizeId && x.Nature == "承包商" && x.FullName == array[j].ToString()).FirstOrDefault();
                    //                    if (entity == null)
                    //                    {
                    //                        falseMessage += "</br>" + "第" + (i + 2) + "行部门不存在,未能导入.";
                    //                        error++;
                    //                        deptFlag = true;
                    //                        break;
                    //                    }
                    //                    else
                    //                    {
                    //                        deptId = entity.DepartmentId;
                    //                        deptCode = entity.EnCode;
                    //                    }
                    //                }
                    //                else
                    //                {
                    //                    deptId = entity.DepartmentId;
                    //                    deptCode = entity.EnCode;
                    //                }
                    //            }
                    //            else
                    //            {
                    //                deptId = entity.DepartmentId;
                    //                deptCode = entity.EnCode;
                    //            }
                    //        }
                    //    }
                    //    else if (j == 1)
                    //    {
                    //        var entity1 = deptlist.Where(x => x.OrganizeId == currUser.OrganizeId && x.Nature == "专业" && x.FullName == array[j].ToString()).FirstOrDefault();
                    //        if (entity1 == null)
                    //        {
                    //            entity1 = deptlist.Where(x => x.OrganizeId == currUser.OrganizeId && x.Nature == "班组" && x.FullName == array[j].ToString()).FirstOrDefault();
                    //            if (entity1 == null)
                    //            {
                    //                falseMessage += "</br>" + "第" + (i + 2) + "行专业/班组不存在,未能导入.";
                    //                error++;
                    //                deptFlag = true;
                    //                break;
                    //            }
                    //            else
                    //            {
                    //                deptId = entity1.DepartmentId;
                    //                deptCode = entity1.EnCode;
                    //            }
                    //        }
                    //        else
                    //        {
                    //            deptId = entity1.DepartmentId;
                    //            deptCode = entity1.EnCode;
                    //        }

                    //    }
                    //    else
                    //    {
                    //        var entity1 = deptlist.Where(x => x.OrganizeId == currUser.OrganizeId && x.Nature == "班组" && x.FullName == array[j].ToString()).FirstOrDefault();
                    //        if (entity1 == null)
                    //        {
                    //            falseMessage += "</br>" + "第" + (i + 2) + "行班组不存在,未能导入.";
                    //            error++;
                    //            deptFlag = true;
                    //            break;
                    //        }
                    //        else
                    //        {
                    //            deptId = entity1.DepartmentId;
                    //            deptCode = entity1.EnCode;
                    //        }
                    //    }
                    //}
                    if (deptFlag)
                    {
                        continue;
                    }

                    //检验所填岗位是否属于其公司或者部门
                    if (string.IsNullOrEmpty(deptId) || deptId == "undefined")
                    {
                        //所属公司
                        RoleEntity data = postCache.GetList(orgId, "true").OrderBy(x => x.SortCode).Where(a => a.FullName == PostName).FirstOrDefault();
                        if (data == null)
                        {
                            falseMessage += "</br>" + "第" + (i + 2) + "行岗位不属于该公司,未能导入.";
                            error++;
                            continue;
                        }
                    }
                    else
                    {
                        //所属部门
                        //所属公司
                        RoleEntity data = postCache.GetList(orgId, deptId).OrderBy(x => x.SortCode).Where(a => a.FullName == PostName).FirstOrDefault();
                        if (data == null)
                        {
                            falseMessage += "</br>" + "第" + (i + 2) + "行岗位不属于该部门,未能导入.";
                            error++;
                            continue;
                        }
                    }
                    //--**验证岗位是否存在**--


                    RoleEntity re = postBLL.GetList().Where(a => a.FullName == PostName && a.OrganizeId == orgId).FirstOrDefault();
                    if (!(string.IsNullOrEmpty(deptId) || deptId == "undefined"))
                    {
                        re = postBLL.GetList().Where(a => a.FullName == PostName && a.OrganizeId == orgId && a.DeptId == deptId).FirstOrDefault();
                        if (re == null)
                        {
                            re = postBLL.GetList().Where(a =>
                                                         a.FullName == PostName && a.OrganizeId == orgId &&
                                                         a.Nature == departmentBLL.GetEntity(deptId).Nature).FirstOrDefault();
                        }
                    }
                    if (re == null)
                    {
                        falseMessage += "</br>" + "第" + (i + 2) + "行岗位有误,未能导入.";
                        error++;
                        continue;
                    }
                    else
                    {
                        PostId = re.RoleId;
                    }

                    LaborinfoEntity linfo = new LaborinfoEntity();
                    linfo.PostId                 = PostId;
                    linfo.DeptCode               = deptCode;
                    linfo.DeptId                 = deptId;
                    linfo.DeptName               = DeptName;
                    linfo.LaboroPerationTime     = DateTime.Now;
                    linfo.LaboroPerationUserName = currUser.UserName;
                    linfo.Model   = Model;
                    linfo.Name    = Name;
                    linfo.OrgCode = currUser.OrganizeCode;
                    linfo.OrgId   = currUser.OrganizeId;
                    linfo.OrgName = currUser.OrganizeName;
                    linfo.Type    = Type;
                    if (Time == "" || !isInt(Time))
                    {
                        linfo.TimeNum = null;
                    }
                    else
                    {
                        linfo.TimeNum  = Convert.ToInt32(Time);
                        linfo.TimeType = TimeType;
                    }

                    linfo.PostName = PostName;
                    linfo.Unit     = Unit;
                    linfo.Create();
                    //如果已存在物品库中
                    LaborprotectionEntity lp = laborlist.Where(it => it.Name == Name).FirstOrDefault();
                    if (lp != null)
                    {
                        linfo.No  = lp.No;
                        linfo.LId = lp.ID;
                        //如果库里有值 则使用库里的值
                        linfo.Type     = linfo.Type;
                        linfo.TimeNum  = lp.TimeNum;
                        linfo.TimeType = lp.TimeType;
                    }
                    else
                    {
                        LaborprotectionEntity newlp = new LaborprotectionEntity();
                        newlp.Create();
                        newlp.Name = Name;
                        newlp.No   = ysno.ToString();
                        newlp.LaborOperationTime     = DateTime.Now;
                        newlp.LaborOperationUserName = currUser.UserName;
                        newlp.Model    = Model;
                        newlp.Type     = Type;
                        newlp.Unit     = Unit;
                        newlp.TimeNum  = linfo.TimeNum;
                        newlp.TimeType = TimeType;
                        linfo.No       = ysno.ToString();
                        linfo.LId      = newlp.ID;
                        ysno++;
                        insertpro.Add(newlp);
                    }

                    int num = 0;
                    List <UserEntity> ulist = userlist.Where(it => it.DepartmentId == deptId && it.DutyId == PostId).ToList();
                    for (int j = 0; j < ulist.Count; j++)
                    {
                        //添加岗位关联人员
                        LaborequipmentinfoEntity eq = new LaborequipmentinfoEntity();
                        eq.UserName  = ulist[j].RealName;
                        eq.AssId     = linfo.ID;
                        eq.LaborType = 0;
                        eq.ShouldNum = 1;
                        num++;
                        eq.UserId = ulist[j].UserId;
                        if (linfo.Type == "衣服")
                        {
                            eq.Size = "L";
                        }
                        else if (linfo.Type == "鞋子")
                        {
                            eq.Size = "40";
                        }
                        else
                        {
                            eq.Size = "";
                        }
                        eq.Create();
                        eqlist.Add(eq);
                    }

                    linfo.ShouldNum = num;
                    insertinfo.Add(linfo);
                }



                laborinfobll.ImportSaveForm(insertinfo, insertpro, eqlist);

                count    = dt.Rows.Count;
                message  = "共有" + count + "条记录,成功导入" + (count - error) + "条,失败" + error + "条";
                message += "</br>" + falseMessage;
            }

            return(message);
        }