/// <summary>
        /// 导入985年度预算设备
        /// </summary>
        /// <param name="httpContext"></param>
        /// <param name="postedFile"></param>
        /// <param name="request"></param>
        /// <param name="deviceReportID"></param>
        /// <returns></returns>
        public static string ImportConstructPlanDevice(this HttpContext httpContext, HttpPostedFile postedFile, HttpRequest request, int deviceReportID)
        {
            //return string.Empty;
            IDatabase database = Database.New();
            string logName = "ConstructPlanDeviceImportLog" + DateTime.Now.ToLongDateString().ToString() + Guid.NewGuid().ToString();
            var writer = httpContext.GetLogWriter(logName);

            string fileName = "ConstructPlanDevice";
            DataSet ds = postedFile.GetDataFromUploadFile(httpContext, fileName);

            StringBuilder deviceImportLog = new StringBuilder();
            StringBuilder deviceImportId = new StringBuilder();

            int importDeviceCount = 0;//导入设备个数

            foreach (DataRow row in ds.Tables[fileName].Rows)
            {
                string memberApplyItemID = row[0].ToString().Trim();

                database = Database.New();
                var reportItem = new DeviceReportItem();
                try
                {
                    /*
                     一个页面对应一个IDatabase,对于Linq来说,就是一个页面对应一个DataContext,这保证页面生存周期中的所有业务实体都来源或依附于同一个DataContext,避免了跨DataContext传递实体的问题。在Linq中,一个DataContext产生的Entity交由另一个DataContext中使用是一件非常麻烦的事情,必须保证实体必须使用Attach方法附加到新的DataContext上,如果不附加,则新的DataContext会认为该Entity是new出来的,这会导致再数据库中插入一条新的记录,而不是与现存记录建立关联,解决办法是不保存实体,而保存实体ID
                     */
                    //保存设备条目的基本信息
                    reportItem.DeviceReport = database.DeviceReports.SingleOrDefault(dr => dr.ID == deviceReportID);
                    var item = database.MemberApplyItems.SingleOrDefault(mai => mai.ID == Convert.ToInt16(memberApplyItemID));// request.GetEntity<MemberApplyItem>(database.MemberApplyItems, memberApplyItemID);
                    if (item != null)
                        reportItem.MemberApplyItem = item;

                    reportItem.Save(database);

                    importDeviceCount++;
                }
                catch (Exception e)
                {
                    writer.WriteLine(string.Format("导入项目发生错误,项目名称为{0},错误信息为:{1}", memberApplyItemID, e.Message));
                }
                deviceImportId.AppendFormat("{0},", reportItem.ID);
            }

            deviceImportLog.AppendFormat("共导入设备{0}个, 导入设备Id为:\n{1}\n", importDeviceCount, deviceImportId.ToString());
            //projectImportLog.AppendFormat("项目成员匹配情况如下:\n{0}\n", expertMatchString.ToString());

            writer.WriteLine(deviceImportLog.ToString());
            httpContext.DeleteFile(postedFile.FileName);
            writer.Close();

            return logName;
        }
Example #2
0
        /// <summary>
        /// 导入专家
        /// </summary>
        /// <param name="httpContext"></param>
        /// <param name="postedFile"></param>
        /// <param name="request"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public static string ImportExpert(this HttpContext httpContext, HttpPostedFile postedFile, HttpRequest request, User user)
        {
            IDatabase database = Database.New();
            string logName = "ExpertImportLog" + DateTime.Now.ToLongDateString().ToString() + Guid.NewGuid().ToString();

            var writer = httpContext.GetLogWriter(logName);

            string fileName = "Expert";
            DataSet ds = postedFile.GetDataFromUploadFile(httpContext, fileName);

            StringBuilder ExpertImportLog = new StringBuilder();
            StringBuilder ExpertImportId = new StringBuilder();
            StringBuilder ExpertUpdateLog = new StringBuilder();
            StringBuilder ExpertDepartmentNotMatchLog = new StringBuilder();
            StringBuilder ExpertNumberAndNameNotMatchLog = new StringBuilder();

            int importExpertCount = 0;//导入专家个数
            int updateExpertCount = 0;//更新专家个数

            foreach (DataRow row in ds.Tables[fileName].Rows)
            {
                database = Database.New();

                string expertNumber = row[0].ToString().Trim();
                string expertName = row[1].ToString().Trim();
                string expertNameSpell = row[2].ToString().Trim();
                string expertSex = row[3].ToString().Trim();
                string expertIDCardNumber = row[4].ToString().Trim();
                string expertCollege = row[5].ToString().Trim();
                string expertPost = row[6].ToString().Trim();
                string IsDoctorDirector = row[7].ToString().Trim();
                string IsAcademician = row[8].ToString().Trim();
                string expertMobilePhone = row[9].ToString().Trim();

                try
                {
                    //保存专家信息
                    Expert expert = database.Experts.FirstOrDefault(q => q.ExpertNumber == expertNumber);
                    if (expert == null)
                        expert = new Expert();

                    bool expertIsNew = expert.IsNew;

                    if (!expert.IsNew)
                    {
                        if (expert.Name != expertName)
                        {
                            ExpertNumberAndNameNotMatchLog.AppendFormat("专家姓名和工作证号不一致,工作证号为:{0},原专家姓名为:{1},现在专家姓名为:{2}\n", expert.ExpertNumber, expert.Name, expertName);

                            //continue;
                        }
                    }

                    expert.ExpertNumber = expertNumber;
                    expert.Name = expertName;
                    expert.NameSpell = expertNameSpell;

                    expert.Sex = expert.IsNew ? expertSex.GetSexType() : string.IsNullOrEmpty(expertSex) ? expert.Sex : expertSex.GetSexType();
                    expert.IDCardNumber = expertIDCardNumber.ConfirmValue(expert.IDCardNumber, expert.IsNew);

                    expert.College = expertCollege.ConfirmValue(expert.College == null ? string.Empty : expert.College.Name, expert.IsNew).GetCollege(database);
                    expert.Post = expertPost;
                    expert.IsAcademician = IsAcademician.GetIsTrueOrFalse();
                    expert.IsDoctorDirector = IsDoctorDirector.GetIsTrueOrFalse();
                    expert.MobilePhone = expertMobilePhone == null ? expert.MobilePhone : expertMobilePhone;

                    //保存专家
                    expert.Save(database);

                    if (!string.IsNullOrEmpty(expert.ExpertNumber) && expertIsNew)
                    {
                        var importUser = database.Users.FirstOrDefault(u => u.LoginID == expert.ExpertNumber);
                        if (importUser == null)
                        {
                            var newUser = new User();
                            newUser.Name = expert.Name;
                            newUser.NameSpell = expert.NameSpell == null ? expert.Name : expert.NameSpell;
                            newUser.LoginID = expert.ExpertNumber;
                            newUser.Password = "******";
                            newUser.CreateTime=DateTime.Now;
                            newUser.ModifyTime = DateTime.Now;
                            //保存用户
                            newUser.Save(database);

                            expert.User = newUser;
                            expert.Save(database);

                            var userRole = database.UserRoles.FirstOrDefault(ur => ur.User == newUser);
                            if (userRole == null)
                                userRole = new UserRole();

                                userRole.User = newUser;
                                var role = database.Roles.FirstOrDefault(r => r.TypeCode == "Expert");
                                userRole.Role = role;
                                userRole.College = expert.College;
                                userRole.CreateTime = DateTime.Now;
                                userRole.ModifyTime = DateTime.Now;
                                //保存用户角色
                                userRole.Save(database);
                        }
                    }

                    if (expertIsNew)
                    {
                        importExpertCount++;
                        ExpertImportId.AppendFormat("Id为:{0},职工号为:{1}\n;", expert.ID, expert.ExpertNumber);
                    }
                    else
                    {
                        updateExpertCount++;
                        ExpertUpdateLog.AppendFormat("更新专家职工号为:{0}。\n", expert.ExpertNumber);
                    }
                }
                catch (Exception e)
                {
                    writer.WriteLine(string.Format("导入专家发生错误,专家职工号为{0},错误信息为:{1}", expertNumber, e.Message));
                }
            }
            ExpertImportLog.AppendFormat("成功更新和导入专家{0}位:其中更新专家{1}位;添加专家{2}位。\n", importExpertCount + updateExpertCount, updateExpertCount, importExpertCount);
            ExpertImportLog.AppendFormat("添加专家ID为:\n{0}\n", ExpertImportId.ToString());
            ExpertImportLog.AppendFormat("更新专家详细信息为:\n{0}", ExpertUpdateLog.ToString());
            ExpertImportLog.AppendFormat("以下专家对应所属部门不匹配,请检查写法是否规范。\n{0}", ExpertDepartmentNotMatchLog.ToString());
            ExpertImportLog.AppendFormat("以下专家工作证号和姓名不匹配的信息\n{0}", ExpertNumberAndNameNotMatchLog.ToString());

            //注释 写入导入日志
            //Log.Write(user.Name, (int)(LogType.ExpertImport), ExpertImportLog.ToString(), request.UserHostAddress, "专家导入", database);

            writer.WriteLine("成功更新和导入专家{0}位:其中更新专家{1}位;添加专家{2}位。\n", importExpertCount + updateExpertCount, updateExpertCount, importExpertCount);
            writer.WriteLine();
            writer.WriteLine();
            writer.WriteLine("添加专家ID为:\n{0}\n", ExpertImportId.ToString());
            writer.WriteLine();
            writer.WriteLine();
            writer.WriteLine("更新专家详细信息为:\n{0}", ExpertUpdateLog.ToString());
            writer.WriteLine("以下专家对应所属部门不匹配,请检查写法是否规范。\n{0}", ExpertDepartmentNotMatchLog.ToString());
            writer.WriteLine("以下专家工作证号和姓名不匹配的信息。\n{0}", ExpertNumberAndNameNotMatchLog.ToString());

            httpContext.DeleteFile(postedFile.FileName);
            writer.Close();

            return logName;
        }
Example #3
0
        /// <summary>
        /// 导入专家
        /// </summary>
        /// <param name="httpContext"></param>
        /// <param name="postedFile"></param>
        /// <param name="request"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public static string ImportDevice(this HttpContext httpContext, HttpPostedFile postedFile, HttpRequest request, User user)
        {
            IDatabase database = Database.New();
            string logName = "DeviceImportLog" + DateTime.Now.ToLongDateString().ToString() + Guid.NewGuid().ToString();

            var writer = httpContext.GetLogWriter(logName);

            string fileName = "Device";
            DataSet ds = postedFile.GetDataFromUploadFile(httpContext, fileName);

            StringBuilder DeviceImportLog = new StringBuilder();
            StringBuilder DeviceImportId = new StringBuilder();
            StringBuilder DeviceUpdateLog = new StringBuilder();
            StringBuilder DeviceDepartmentNotMatchLog = new StringBuilder();
            StringBuilder DeviceNumberAndNameNotMatchLog = new StringBuilder();

            int importDeviceCount = 0;//导入设备个数
            int updateDeviceCount = 0;//更新设备个数

            foreach (DataRow row in ds.Tables[fileName].Rows)
            {
                database = Database.New();

                string academicID = row[0].ToString().Trim();
                string academicName = row[1].ToString().Trim();
                string deviceNum = row[2].ToString().Trim();
                string deviceName = row[3].ToString().Trim();
                string deviceType = row[4].ToString().Trim();
                string deviceStarderd = row[5].ToString().Trim();
                string deviceTypeNum = row[6].ToString().Trim();
                string deviceCountry = row[7].ToString().Trim();
                string deviceFactory = row[8].ToString().Trim();
                string devicePurchaseTime = row[9].ToString().Trim();
                string deviceManager = row[10].ToString().Trim();
                try
                {
                    //保存专家信息
                    DeviceIntramural Device = database.DeviceIntramurals.FirstOrDefault(q => q.DeviceNumber == deviceNum);
                    if (Device == null)
                        Device = new DeviceIntramural();

                    bool expertIsNew = Device.IsNew;

                    Device.CollegeCode = academicID;
                    Device.CollegeName = academicName;
                    Device.DeviceNumber = deviceNum;
                    Device.DeviceName = deviceName;
                    Device.Model = deviceType;
                    Device.Standard = deviceStarderd;
                    Device.TypeCode = deviceTypeNum;
                    Device.Nation = deviceCountry;
                    Device.Manufacture = deviceFactory;
                    Device.BuyTime = DateTime.Parse(devicePurchaseTime);
                    Device.ManagerName = deviceManager;
                    //保存专家
                    Device.Save(database);
                    if (expertIsNew)
                    {
                        importDeviceCount++;
                        DeviceImportId.AppendFormat("Id为:{0},设备编号为:{1}\n;", Device.ID, Device.DeviceNumber);
                    }
                    else
                    {
                        updateDeviceCount++;
                        DeviceUpdateLog.AppendFormat("更新设备编号为:{0}。\n", Device.DeviceNumber);
                    }

                }
                catch (Exception e)
                {
                    writer.WriteLine(string.Format("导入设备发生错误,设备编号号为{0},错误信息为:{1}", deviceNum, e.Message));
                }
            }
            DeviceImportLog.AppendFormat("成功更新和导入设备{0}个:其中更新设备{1}个;添加设备{2}个。\n", importDeviceCount + updateDeviceCount, updateDeviceCount, importDeviceCount);
            //  DeviceImportLog.AppendFormat("添加设备ID为:\n{0}\n", ExpertImportId.ToString());
            //  DeviceImportLog.AppendFormat("更新专家详细信息为:\n{0}", ExpertUpdateLog.ToString());

            //注释 写入导入日志
            //Log.Write(user.Name, (int)(LogType.ExpertImport), ExpertImportLog.ToString(), request.UserHostAddress, "专家导入", database);

            writer.WriteLine("成功更新和导入专家{0}位:其中更新专家{1}位;添加专家{2}位。\n", importDeviceCount + updateDeviceCount, updateDeviceCount, importDeviceCount);
            writer.WriteLine();
            writer.WriteLine();
            writer.WriteLine("添加专家ID为:\n{0}\n", DeviceImportId.ToString());
            writer.WriteLine();
            writer.WriteLine();
            writer.WriteLine("更新专家详细信息为:\n{0}", DeviceUpdateLog.ToString());
            // writer.WriteLine("以下专家对应所属部门不匹配,请检查写法是否规范。\n{0}", ExpertDepartmentNotMatchLog.ToString());
            //  writer.WriteLine("以下专家工作证号和姓名不匹配的信息。\n{0}", ExpertNumberAndNameNotMatchLog.ToString());

            httpContext.DeleteFile(postedFile.FileName);
            writer.Close();

            return logName;
        }