public IActionResult CpcCreate(dynamic model)
        {
            var response = ResponseModelFactory.CreateInstance;

            using (_dbContext)
            {
                var    entity    = new HaikanSmartTownCockpit.Api.Entities.Userinfoty();
                string residence = model.residence;
                string town      = model.town;
                if (!residence.Contains(town))
                {
                    response.SetFailed("住址不属于该村镇");
                    return(Ok(response));
                }
                entity.UserInfoUuid = Guid.NewGuid();
                entity.RealName     = model.realName;
                entity.Sex          = model.sex;
                entity.Birth        = model.birth;
                entity.Education    = model.education;
                entity.Category     = model.category;
                entity.Partybranch  = model.partybranch;
                entity.JoinDate     = model.joinDate;
                entity.Phone        = model.phone;
                entity.Residence    = model.residence;
                entity.Work         = model.work;
                entity.DyStaues     = "1";
                DateTime now   = DateTime.Now;
                DateTime birth = model.birth;
                int      age   = now.Year - birth.Year;
                if (now.Month < birth.Month || (now.Month == birth.Month && now.Day < birth.Day))
                {
                    age--;
                }
                entity.Age       = age < 0 ? 0 : age;
                entity.AddTime   = DateTime.Now.ToString("yyyy-MM-dd");
                entity.AddPeople = AuthContextService.CurrentUser.DisplayName;
                entity.IsDeleted = 0;
                _dbContext.Userinfoty.Add(entity);
                int res = _dbContext.SaveChanges();
                if (res > 0)
                {
                    ToLog.AddLog("添加", "成功:添加:党员信息一条数据", _dbContext);
                }
                response.SetSuccess("添加成功");
                return(Ok(response));
            }
        }
Example #2
0
        public IActionResult Import(IFormFile excelfile)
        {
            var response = ResponseModelFactory.CreateInstance;

            using (_dbContext)
            {
                DateTime beginTime = DateTime.Now;

                string sWebRootFolder = _hostingEnvironment.WebRootPath + "\\UploadFiles\\ImportBuidingExcel";


                //var schoolinfo = _dbContext.SchoolInforManagement.AsQueryable();
                string   uploadtitle = "适龄青年信息导入" + DateTime.Now.ToString("yyyyMMddHHmmss");
                string   sFileName   = $"{uploadtitle}.xlsx";
                FileInfo file        = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
                //string conStr = ConnectionStrings.DefaultConnection;
                string responsemsgsuccess = "";
                string responsemsgrepeat  = "";
                string responsemsgdefault = "";
                int    successcount       = 0;
                int    repeatcount        = 0;
                int    defaultcount       = 0;
                string today = DateTime.Now.ToString("yyyy-MM-dd");
                try
                {
                    //把excelfile中的数据复制到file中
                    using (FileStream fs = new FileStream(file.ToString(), FileMode.Create)) //初始化一个指定路径和创建模式的FileStream
                    {
                        excelfile.CopyTo(fs);
                        fs.Flush();  //清空stream的缓存,并且把缓存中的数据输出到file
                    }
                    DataTable dt = Haikan3.Utils.ExcelTools.ExcelToDataTable(file.ToString(), "Sheet1", true);

                    if (dt == null || dt.Rows.Count == 0)
                    {
                        response.SetFailed("表格无数据");
                        return(Ok(response));
                    }
                    else
                    {
                        if (!dt.Columns.Contains("姓名"))
                        {
                            response.SetFailed("无‘姓名’列");
                            return(Ok(response));
                        }
                        if (!dt.Columns.Contains("性别"))
                        {
                            response.SetFailed("无‘性别’列");
                            return(Ok(response));
                        }
                        if (!dt.Columns.Contains("出生日期"))
                        {
                            response.SetFailed("无‘出生日期’列");
                            return(Ok(response));
                        }
                        if (!dt.Columns.Contains("身份证号"))
                        {
                            response.SetFailed("无‘身份证号’列");
                            return(Ok(response));
                        }
                        if (!dt.Columns.Contains("户籍地"))
                        {
                            response.SetFailed("无‘户籍地’列");
                            return(Ok(response));
                        }
                        if (!dt.Columns.Contains("手机号"))
                        {
                            response.SetFailed("无‘手机号’列");
                            return(Ok(response));
                        }
                        if (!dt.Columns.Contains("民族"))
                        {
                            response.SetFailed("无‘民族’列");
                            return(Ok(response));
                        }
                        if (!dt.Columns.Contains("学历"))
                        {
                            response.SetFailed("无‘学历’列");
                            return(Ok(response));
                        }
                        if (!dt.Columns.Contains("参军意愿"))
                        {
                            response.SetFailed("无‘参军意愿’列");
                            return(Ok(response));
                        }
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            if (!string.IsNullOrEmpty(dt.Rows[i]["姓名"].ToString()))
                            {
                                var entity = new HaikanSmartTownCockpit.Api.Entities.Userinfoty();
                                entity.UserInfoUuid = Guid.NewGuid();
                                if (!string.IsNullOrEmpty(dt.Rows[i]["姓名"].ToString()))
                                {
                                    Regex reg = new Regex("^([\\u4e00-\\u9fa5]){2,7}$");
                                    if (reg.IsMatch(dt.Rows[i]["姓名"].ToString()))
                                    {
                                        entity.RealName = dt.Rows[i]["姓名"].ToString();
                                    }
                                    else
                                    {
                                        responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行姓名格式不正确" + "</p></br>";
                                        defaultcount++;
                                        continue;
                                    }
                                }
                                else
                                {
                                    responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行姓名为空" + "</p></br>";
                                    defaultcount++;
                                    continue;
                                }
                                if (!string.IsNullOrEmpty(dt.Rows[i]["性别"].ToString()))
                                {
                                    if (dt.Rows[i]["性别"].ToString() != "男" && dt.Rows[i]["性别"].ToString() != "女")
                                    {
                                        responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行性别不正确" + "</p></br>";
                                        defaultcount++;
                                        continue;
                                    }
                                    else
                                    {
                                        entity.Sex = dt.Rows[i]["性别"].ToString();
                                    }
                                }
                                if (!string.IsNullOrEmpty(dt.Rows[i]["出生日期"].ToString()))
                                {
                                    Regex reg = new Regex("^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
                                    if (reg.IsMatch(dt.Rows[i]["出生日期"].ToString()))
                                    {
                                        entity.Birth = dt.Rows[i]["出生日期"].ToString();
                                    }
                                    else
                                    {
                                        responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行出生日期不正确" + "</p></br>";
                                        defaultcount++;
                                        continue;
                                    }
                                }
                                if (!string.IsNullOrEmpty(dt.Rows[i]["身份证号"].ToString()))
                                {
                                    Regex reg = new Regex("^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$");
                                    if (reg.IsMatch(dt.Rows[i]["身份证号"].ToString()))
                                    {
                                        entity.IdentityCard = dt.Rows[i]["身份证号"].ToString();
                                    }
                                    else
                                    {
                                        responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行身份证号格式不正确" + "</p></br>";
                                        defaultcount++;
                                        continue;
                                    }
                                }
                                if (!string.IsNullOrEmpty(dt.Rows[i]["户籍地"].ToString()))
                                {
                                    entity.Domicile = dt.Rows[i]["户籍地"].ToString();
                                }
                                if (!string.IsNullOrEmpty(dt.Rows[i]["手机号"].ToString()))
                                {
                                    Regex reg = new Regex("^[1][3,4,5,7,8][0-9]{9}$");
                                    if (reg.IsMatch(dt.Rows[i]["手机号"].ToString()))
                                    {
                                        entity.Phone = dt.Rows[i]["手机号"].ToString();
                                    }
                                    else
                                    {
                                        responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行手机号格式不正确" + "</p></br>";
                                        defaultcount++;
                                        continue;
                                    }
                                }
                                if (!string.IsNullOrEmpty(dt.Rows[i]["民族"].ToString()))
                                {
                                    Regex reg = new Regex("^(汉族|蒙古族|回族|藏族|维吾尔族|苗族|彝族|壮族|布依族|朝鲜族|满族|侗族|瑶族|白族|土家族|哈尼族|哈萨克族|傣族|黎族|僳僳族|佤族|畲族|高山族|拉祜族|水族|东乡族|纳西族|景颇族|柯尔克孜族|土族|达斡尔族|仫佬族|羌族|布朗族|撒拉族|毛南族|仡佬族|锡伯族|阿昌族|普米族|塔吉克族|怒族|乌孜别克族|俄罗斯族|鄂温克族|德昂族|保安族|京族|独龙族|鄂伦春族|赫哲族|裕固族|门巴族|珞巴族|基诺族)$");
                                    if (reg.IsMatch(dt.Rows[i]["民族"].ToString()))
                                    {
                                        entity.Nation = dt.Rows[i]["民族"].ToString();
                                    }
                                    else
                                    {
                                        responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行民族不正确" + "</p></br>";
                                        defaultcount++;
                                        continue;
                                    }
                                }
                                if (!string.IsNullOrEmpty(dt.Rows[i]["学历"].ToString()))
                                {
                                    Regex reg = new Regex("^(小学|初中|中专|大专|本科|硕士|博士)$");
                                    if (reg.IsMatch(dt.Rows[i]["学历"].ToString()))
                                    {
                                        entity.Education = dt.Rows[i]["学历"].ToString();
                                    }
                                    else
                                    {
                                        responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行学历类别不正确" + "</p></br>";
                                        defaultcount++;
                                        continue;
                                    }
                                }
                                if (!string.IsNullOrEmpty(dt.Rows[i]["参军意愿"].ToString()))
                                {
                                    entity.JoinArmy = dt.Rows[i]["参军意愿"].ToString();
                                }
                                entity.Defense   = "适龄青年";
                                entity.AddTime   = DateTime.Now.ToString("yyyy-MM-dd");
                                entity.AddPeople = AuthContextService.CurrentUser.DisplayName;
                                entity.IsDeleted = 0;
                                _dbContext.Userinfoty.Add(entity);
                                _dbContext.SaveChanges();
                                successcount++;
                            }
                        }
                    }
                    responsemsgsuccess = "<p style='color:green'>导入成功:" + successcount + "条</p></br>" + responsemsgsuccess;
                    responsemsgrepeat  = "<p style='color:orange'>重复需手动修改数据:" + repeatcount + "条</p></br>" + responsemsgrepeat;
                    responsemsgdefault = "<p style='color:red'>导入失败:" + defaultcount + "条</p></br>" + responsemsgdefault;

                    ToLog.AddLog("导入", "成功:导入:统一人员信息数据", _dbContext);
                    DateTime endTime  = DateTime.Now;
                    TimeSpan useTime  = endTime - beginTime;
                    string   taketime = "导入时间" + useTime.TotalSeconds.ToString() + "秒  ";
                    response.SetData(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(new
                    {
                        time       = taketime,
                        successmsg = responsemsgsuccess
                        ,
                        repeatmsg  = responsemsgrepeat,
                        defaultmsg = responsemsgdefault
                    })));
                    return(Ok(response));
                }
                catch (Exception ex)
                {
                    response.SetFailed(ex.Message);
                    return(Ok(response));
                }
            }
        }
Example #3
0
        public IActionResult Import(IFormFile excelfile)
        {
            var response = ResponseModelFactory.CreateInstance;

            using (_dbContext)
            {
                DateTime beginTime      = DateTime.Now;
                string   sWebRootFolder = _hostingEnvironment.WebRootPath + "\\UploadFiles\\ImportBuidingExcel";
                string   uploadtitle    = "党员考评信息导入" + DateTime.Now.ToString("yyyyMMddHHmmss");
                string   sFileName      = $"{uploadtitle}.xlsx";
                FileInfo file           = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
                //string conStr = ConnectionStrings.DefaultConnection;
                string responsemsgsuccess = "";
                string responsemsgrepeat  = "";
                string responsemsgdefault = "";
                int    successcount       = 0;
                int    repeatcount        = 0;
                int    defaultcount       = 0;
                string today = DateTime.Now.ToString("yyyy-MM-dd");
                try
                {
                    //把excelfile中的数据复制到file中
                    using (FileStream fs = new FileStream(file.ToString(), FileMode.Create)) //初始化一个指定路径和创建模式的FileStream
                    {
                        excelfile.CopyTo(fs);
                        fs.Flush();  //清空stream的缓存,并且把缓存中的数据输出到file
                    }
                    DataTable dt = Haikan3.Utils.ExcelTools.ExcelToDataTable(file.ToString(), "Sheet1", true);

                    if (dt == null || dt.Rows.Count == 0)
                    {
                        response.SetFailed("表格无数据");
                        return(Ok(response));
                    }
                    else
                    {
                        if (!dt.Columns.Contains("姓名"))
                        {
                            response.SetFailed("无‘姓名’列");
                            return(Ok(response));
                        }
                        if (!dt.Columns.Contains("性别"))
                        {
                            response.SetFailed("无‘性别’列");
                            return(Ok(response));
                        }
                        if (!dt.Columns.Contains("出生日期"))
                        {
                            response.SetFailed("无‘出生日期’列");
                            return(Ok(response));
                        }
                        if (!dt.Columns.Contains("学历"))
                        {
                            response.SetFailed("无‘学历’列");
                            return(Ok(response));
                        }
                        if (!dt.Columns.Contains("人员类别"))
                        {
                            response.SetFailed("无‘人员类别’列");
                            return(Ok(response));
                        }
                        if (!dt.Columns.Contains("所在党支部"))
                        {
                            response.SetFailed("无‘所在党支部’列");
                            return(Ok(response));
                        }
                        if (!dt.Columns.Contains("加入党组织时间"))
                        {
                            response.SetFailed("无‘加入党组织时间’列");
                            return(Ok(response));
                        }
                        if (!dt.Columns.Contains("联系方式"))
                        {
                            response.SetFailed("无‘联系方式’列");
                            return(Ok(response));
                        }
                        if (!dt.Columns.Contains("家庭住址"))
                        {
                            response.SetFailed("无‘家庭住址’列");
                            return(Ok(response));
                        }
                        if (!dt.Columns.Contains("工作单位"))
                        {
                            response.SetFailed("无‘工作单位’列");
                            return(Ok(response));
                        }
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            var entity = new HaikanSmartTownCockpit.Api.Entities.Userinfoty();
                            entity.UserInfoUuid = Guid.NewGuid();
                            if (!string.IsNullOrEmpty(dt.Rows[i]["姓名"].ToString()))
                            {
                                Regex reg = new Regex("^([\\u4e00-\\u9fa5]){2,7}$");
                                if (reg.IsMatch(dt.Rows[i]["姓名"].ToString()))
                                {
                                    entity.RealName = dt.Rows[i]["姓名"].ToString();
                                }
                                else
                                {
                                    responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行姓名格式不正确" + "</p></br>";
                                    defaultcount++;
                                    continue;
                                }
                            }
                            else
                            {
                                responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行姓名为空" + "</p></br>";
                                defaultcount++;
                                continue;
                            }
                            if (!string.IsNullOrEmpty(dt.Rows[i]["性别"].ToString()))
                            {
                                if (dt.Rows[i]["性别"].ToString() != "男" && dt.Rows[i]["性别"].ToString() != "女")
                                {
                                    responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行性别不正确" + "</p></br>";
                                    defaultcount++;
                                    continue;
                                }
                                else
                                {
                                    entity.Sex = dt.Rows[i]["性别"].ToString();
                                }
                            }
                            if (!string.IsNullOrEmpty(dt.Rows[i]["出生日期"].ToString()))
                            {
                                Regex reg = new Regex("^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
                                if (reg.IsMatch(dt.Rows[i]["出生日期"].ToString()))
                                {
                                    entity.Birth = dt.Rows[i]["出生日期"].ToString();
                                }
                                else
                                {
                                    responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行出生日期不正确" + "</p></br>";
                                    defaultcount++;
                                    continue;
                                }
                            }
                            else
                            {
                                responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行出生日期为空" + "</p></br>";
                                defaultcount++;
                                continue;
                            }
                            if (!string.IsNullOrEmpty(dt.Rows[i]["学历"].ToString()))
                            {
                                Regex reg = new Regex("^(小学|初中|高中|中专|大专|本科|硕士|博士)$");
                                if (reg.IsMatch(dt.Rows[i]["学历"].ToString()))
                                {
                                    entity.Education = dt.Rows[i]["学历"].ToString();
                                }
                                else
                                {
                                    responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行学历类别不正确" + "</p></br>";
                                    defaultcount++;
                                    continue;
                                }
                            }
                            if (!string.IsNullOrEmpty(dt.Rows[i]["人员类别"].ToString().Trim()))
                            {
                                Regex reg = new Regex("^(正式党员|预备党员|发展对象|积极分子)$");
                                if (reg.IsMatch(dt.Rows[i]["人员类别"].ToString().Trim()))
                                {
                                    entity.Category = dt.Rows[i]["人员类别"].ToString().Trim();
                                }
                                else
                                {
                                    responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行人员类别不正确" + "</p></br>";
                                    defaultcount++;
                                    continue;
                                }
                            }
                            if (!string.IsNullOrEmpty(dt.Rows[i]["所在党支部"].ToString()))
                            {
                                Regex reg = new Regex("^(机关党组织|村级党支部|企业党支部)$");
                                if (reg.IsMatch(dt.Rows[i]["所在党支部"].ToString()))
                                {
                                    entity.Partybranch = dt.Rows[i]["所在党支部"].ToString();
                                }
                                else
                                {
                                    responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行所在党支部类别不正确" + "</p></br>";
                                    defaultcount++;
                                    continue;
                                }
                            }
                            if (!string.IsNullOrEmpty(dt.Rows[i]["加入党组织时间"].ToString()))
                            {
                                Regex reg = new Regex("^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
                                if (reg.IsMatch(dt.Rows[i]["迁移时间"].ToString()))
                                {
                                    entity.JoinDate = dt.Rows[i]["加入党组织时间"].ToString();
                                }
                                else
                                {
                                    responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行加入党组织时间不正确" + "</p></br>";
                                    defaultcount++;
                                    continue;
                                }
                            }
                            if (!string.IsNullOrEmpty(dt.Rows[i]["联系方式"].ToString()))
                            {
                                Regex reg = new Regex("^[1][3,4,5,7,8][0-9]{9}$");
                                if (reg.IsMatch(dt.Rows[i]["联系方式"].ToString()))
                                {
                                    entity.Phone = dt.Rows[i]["联系方式"].ToString();
                                }
                                else
                                {
                                    responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行联系方式格式不正确" + "</p></br>";
                                    defaultcount++;
                                    continue;
                                }
                            }
                            if (!string.IsNullOrEmpty(dt.Rows[i]["家庭住址"].ToString().Trim()))
                            {
                                entity.Residence = dt.Rows[i]["家庭住址"].ToString().Trim();
                            }
                            else
                            {
                                responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行家庭住址为空" + "</p></br>";
                                defaultcount++;
                                continue;
                            }
                            if (!string.IsNullOrEmpty(dt.Rows[i]["工作单位"].ToString()))
                            {
                                entity.Occupation = dt.Rows[i]["工作单位"].ToString();
                            }
                            if (!string.IsNullOrEmpty(dt.Rows[i]["考评级别"].ToString()))
                            {
                                entity.Evaluate = dt.Rows[i]["考评级别"].ToString();
                            }
                            entity.DyStaues  = "1";
                            entity.AddTime   = DateTime.Now.ToString("yyyy-MM-dd");
                            entity.AddPeople = AuthContextService.CurrentUser.DisplayName;
                            entity.IsDeleted = 0;
                            _dbContext.Userinfoty.Add(entity);
                            _dbContext.SaveChanges();
                            successcount++;
                        }
                    }
                    responsemsgsuccess = "<p style='color:green'>导入成功:" + successcount + "条</p></br>" + responsemsgsuccess;
                    responsemsgrepeat  = "<p style='color:orange'>重复需手动修改数据:" + repeatcount + "条</p></br>" + responsemsgrepeat;
                    responsemsgdefault = "<p style='color:red'>导入失败:" + defaultcount + "条</p></br>" + responsemsgdefault;
                    ToLog.AddLog("导入", "成功:导入:统一人员信息数据", _dbContext);
                    DateTime endTime  = DateTime.Now;
                    TimeSpan useTime  = endTime - beginTime;
                    string   taketime = "导入时间" + useTime.TotalSeconds.ToString() + "秒";
                    response.SetData(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(new
                    {
                        time       = taketime,
                        successmsg = responsemsgsuccess,
                        repeatmsg  = responsemsgrepeat,
                        defaultmsg = responsemsgdefault
                    })));
                    return(Ok(response));
                }
                catch (Exception ex)
                {
                    response.SetFailed(ex.Message);
                    return(Ok(response));
                }
            }
        }
Example #4
0
        public IActionResult UserInfoImport(IFormFile excelfile)
        {
            var response = ResponseModelFactory.CreateInstance;

            using (_dbContext)
            {
                DateTime beginTime = DateTime.Now;

                string sWebRootFolder = _hostingEnvironment.WebRootPath + "\\UploadFiles\\ImportUserInfoExcel";


                //var schoolinfo = _dbContext.SchoolInforManagement.AsQueryable();
                string   uploadtitle = " 人员信息导入" + DateTime.Now.ToString("yyyyMMddHHmmss");
                string   sFileName   = $"{uploadtitle}.xlsx";
                FileInfo file        = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
                //string conStr = ConnectionStrings.DefaultConnection;
                string responsemsgsuccess = "";
                string responsemsgrepeat  = "";
                string responsemsgdefault = "";
                int    successcount       = 0;
                int    repeatcount        = 0;
                int    defaultcount       = 0;
                string today = DateTime.Now.ToString("yyyy-MM-dd");
                try
                {
                    //把excelfile中的数据复制到file中
                    using (FileStream fs = new FileStream(file.ToString(), FileMode.Create)) //初始化一个指定路径和创建模式的FileStream
                    {
                        excelfile.CopyTo(fs);
                        fs.Flush();  //清空stream的缓存,并且把缓存中的数据输出到file
                    }
                    DataTable dt = Haikan3.Utils.ExcelTools.ExcelToDataTable(file.ToString(), "Sheet1", true);

                    if (dt == null || dt.Rows.Count == 0)
                    {
                        response.SetFailed("表格无数据");
                        return(Ok(response));
                    }
                    else
                    {
                        if (!dt.Columns.Contains("姓名"))
                        {
                            response.SetFailed("无‘姓名’列");
                            return(Ok(response));
                        }
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            var entity = new HaikanSmartTownCockpit.Api.Entities.Userinfoty();
                            entity.UserInfoUuid = Guid.NewGuid();
                            //Regex regxm = new Regex("^([\\u4e00-\\u9fa5]){2,7}$");
                            //if (regxm.IsMatch(dt.Rows[i]["姓名"].ToString()))
                            //{
                            if (string.IsNullOrEmpty(dt.Rows[i]["姓名"].ToString().Trim()))
                            {
                                continue;
                            }
                            else
                            {
                                entity.RealName = dt.Rows[i]["姓名"].ToString();
                            }


                            //}
                            //else
                            //{
                            //    responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行姓名格式不正确" + "</p></br>";
                            //    defaultcount++;
                            //    continue;
                            //}
                            //Regex regxb = new Regex("^(男|女)$");
                            //if (regxb.IsMatch(dt.Rows[i]["性别"].ToString()))
                            //{
                            //entity.Sex = dt.Rows[i]["性别"].ToString();
                            //}
                            //else
                            //{
                            //    responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行性别不正确" + "</p></br>";
                            //    defaultcount++;
                            //    continue;
                            //}
                            //Regex regbr = new Regex("^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");

                            //if (regbr.IsMatch(dt.Rows[i]["出生日期"].ToString()))
                            //{
                            //entity.Birth = dt.Rows[i]["出生日期"].ToString();
                            //DateTime now = DateTime.Now;
                            //DateTime birth = Convert.ToDateTime(entity.Birth);
                            //int age = now.Year - birth.Year;
                            //if (now.Month < birth.Month || (now.Month == birth.Month && now.Day < birth.Day))
                            //{
                            //    age--;
                            //}
                            //entity.Age = age < 0 ? 0 : age;
                            //}
                            //else
                            //{
                            //    responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行出生日期不正确,例:1999-09-09" + "</p></br>";
                            //    defaultcount++;
                            //    continue;
                            //}

                            Regex regsfz = new Regex("^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$");
                            if (regsfz.IsMatch(dt.Rows[i]["身份证号"].ToString()))
                            {
                                if (!string.IsNullOrEmpty(dt.Rows[i]["身份证号"].ToString()))
                                {
                                    string ddd = dt.Rows[i]["身份证号"].ToString();
                                    entity.IdentityCard = dt.Rows[i]["身份证号"].ToString();
                                    if (ddd != "" && ddd != null)
                                    {
                                        string sex = "";
                                        //判断身份证是否18位
                                        if (ddd.Length == 18)
                                        {
                                            entity.Birth = ddd.Substring(6, 4) + "-" + ddd.Substring(10, 2) + "-" + ddd.Substring(12, 2);
                                            sex          = ddd.Substring(14, 3);
                                            if (sex != "" && sex != null)
                                            {
                                                if (int.Parse(sex) % 2 == 0)
                                                {
                                                    entity.Sex = "女";
                                                }
                                                else
                                                {
                                                    entity.Sex = "男";
                                                }
                                            }
                                            var d1 = ddd.Substring(6, 4) + "-" + ddd.Substring(10, 2) + "-" + ddd.Substring(12, 2);
                                            sex          = ddd.Substring(14, 3);
                                            entity.Birth = DateTime.Parse(d1).ToString("yyyy-MM-dd");
                                            DateTime now   = DateTime.Now;
                                            DateTime birth = Convert.ToDateTime(d1);
                                            int      age   = now.Year - birth.Year;
                                            if (now.Month < birth.Month || (now.Month == birth.Month && now.Day < birth.Day))
                                            {
                                                age--;
                                            }
                                            entity.Age = age < 0 ? 0 : age;
                                        }
                                        //判断身份证是否15位
                                        if (ddd.Length == 15)
                                        {
                                            entity.Birth = "19" + ddd.Substring(6, 2) + "-" + ddd.Substring(8, 2) + "-" + ddd.Substring(10, 2);
                                            sex          = ddd.Substring(12, 3);
                                            if (sex != "" && sex != null)
                                            {
                                                if (int.Parse(sex) % 2 == 0)
                                                {
                                                    entity.Sex = "女";
                                                }
                                                else
                                                {
                                                    entity.Sex = "男";
                                                }
                                            }
                                            var d1 = "19" + ddd.Substring(6, 2) + "-" + ddd.Substring(8, 2) + "-" + ddd.Substring(10, 2);
                                            sex          = ddd.Substring(12, 3);
                                            entity.Birth = DateTime.Parse(d1).ToString("yyyy-MM-dd");
                                            DateTime now   = DateTime.Now;
                                            DateTime birth = Convert.ToDateTime(d1);
                                            int      age   = now.Year - birth.Year;
                                            if (now.Month < birth.Month || (now.Month == birth.Month && now.Day < birth.Day))
                                            {
                                                age--;
                                            }
                                            entity.Age = age < 0 ? 0 : age;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行身份证号格式不正确" + "</p></br>";
                                defaultcount++;
                                continue;
                            }
                            if (dt.Rows[i]["户籍地"] != null)
                            {
                                entity.Residence = dt.Rows[i]["户籍地"].ToString();
                            }
                            if (!string.IsNullOrEmpty(dt.Rows[i]["地址"].ToString()))
                            {
                                entity.Domicile = dt.Rows[i]["地址"].ToString();
                            }
                            //Regex reg = new Regex("^(汉族|蒙古族|回族|藏族|维吾尔族|苗族|彝族|壮族|布依族|朝鲜族|满族|侗族|瑶族|白族|土家族|哈尼族|哈萨克族|傣族|黎族|僳僳族|佤族|畲族|高山族|拉祜族|水族|东乡族|纳西族|景颇族|柯尔克孜族|土族|达斡尔族|仫佬族|羌族|布朗族|撒拉族|毛南族|仡佬族|锡伯族|阿昌族|普米族|塔吉克族|怒族|乌孜别克族|俄罗斯族|鄂温克族|德昂族|保安族|京族|独龙族|鄂伦春族|赫哲族|裕固族|门巴族|珞巴族|基诺族)$");
                            //if (reg.IsMatch(dt.Rows[i]["民族"].ToString()))
                            //{
                            //entity.Nation = dt.Rows[i]["民族"].ToString();
                            //}
                            //else
                            //{
                            //    responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行民族不正确" + "</p></br>";
                            //    defaultcount++;
                            //    continue;
                            //}
                            //Regex reg1 = new Regex("^(小学|初中|中专|大专|本科|硕士|博士)$");
                            //if (reg1.IsMatch(dt.Rows[i]["学历"].ToString()))
                            //{
                            //entity.Education = dt.Rows[i]["学历"].ToString();
                            //}
                            //else
                            //{
                            //    responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行学历类别不正确" + "</p></br>";
                            //    defaultcount++;
                            //    continue;
                            //}
                            if (!string.IsNullOrEmpty(dt.Rows[i]["户别"]?.ToString()))
                            {
                                entity.Household = dt.Rows[i]["户别"].ToString();
                            }
                            if (!string.IsNullOrEmpty(dt.Rows[i]["电话号码"]?.ToString()))
                            {
                                entity.Phone = dt.Rows[i]["电话号码"].ToString();
                            }
                            if (!string.IsNullOrEmpty(dt.Rows[i]["与户主的关系"]?.ToString()))
                            {
                                entity.Relation = dt.Rows[i]["与户主的关系"].ToString();
                            }
                            if (!string.IsNullOrEmpty(dt.Rows[i]["户主姓名"]?.ToString()))
                            {
                                entity.HouseholderName = dt.Rows[i]["户主姓名"].ToString();
                            }

                            if (!string.IsNullOrEmpty(dt.Rows[i]["迁入时间"].ToString()))
                            {
                                entity.QianYiStime = dt.Rows[i]["迁入时间"].ToString();
                            }
                            if (!string.IsNullOrEmpty(dt.Rows[i]["迁出时间"].ToString()))
                            {
                                entity.QianYiEtime = dt.Rows[i]["迁出时间"].ToString();
                            }
                            //if (!string.IsNullOrEmpty(dt.Rows[i]["职业"].ToString()))
                            //{
                            //    entity.Occupation = dt.Rows[i]["职业"].ToString();
                            //}
                            if (!string.IsNullOrEmpty(dt.Rows[i]["政治面貌"].ToString()))
                            {
                                entity.Politics = dt.Rows[i]["政治面貌"].ToString();
                                if (dt.Rows[i]["政治面貌"].ToString().Contains("党"))
                                {
                                    entity.DyStaues = "1";
                                }
                            }

                            //else
                            //{
                            //    responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行价格为空" + "</p></br>";
                            //    defaultcount++;
                            //    continue;
                            //}
                            entity.AddTime   = DateTime.Now.ToString("yyyy-MM-dd");
                            entity.AddPeople = AuthContextService.CurrentUser.DisplayName;
                            //entity.AddTime = DateTime.Now.ToString("yyyy-MM-dd");
                            //entity.AddPeople = AuthContextService.CurrentUser.DisplayName;
                            entity.IsDeleted = 0;
                            _dbContext.Userinfoty.Add(entity);
                            _dbContext.SaveChanges();
                            successcount++;
                        }
                    }
                    responsemsgsuccess = "<p style='color:green'>导入成功:" + successcount + "条</p></br>" + responsemsgsuccess;
                    responsemsgrepeat  = "<p style='color:orange'>重复需手动修改数据:" + repeatcount + "条</p></br>" + responsemsgrepeat;
                    responsemsgdefault = "<p style='color:red'>导入失败:" + defaultcount + "条</p></br>" + responsemsgdefault;

                    ToLog.AddLog("导入", "成功:导入:统一人员信息数据", _dbContext);
                    DateTime endTime  = DateTime.Now;
                    TimeSpan useTime  = endTime - beginTime;
                    string   taketime = "导入时间" + useTime.TotalSeconds.ToString() + "秒  ";
                    response.SetData(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(new
                    {
                        time       = taketime,
                        successmsg = responsemsgsuccess
                        ,
                        repeatmsg  = responsemsgrepeat,
                        defaultmsg = responsemsgdefault
                    })));
                    return(Ok(response));
                }
                catch (Exception ex)
                {
                    response.SetFailed(ex.Message);
                    return(Ok(response));
                }
            }
        }
Example #5
0
        public IActionResult UserInfoCreate(UsreInfoViewModel model)
        {
            var response = ResponseModelFactory.CreateInstance;

            using (_dbContext)
            {
                var entity = new HaikanSmartTownCockpit.Api.Entities.Userinfoty();
                entity.UserInfoUuid = Guid.NewGuid();
                entity.RealName     = model.RealName;
                entity.Sex          = model.Sex;
                entity.Education    = model.Education;
                entity.Politics     = model.Politics;
                entity.Nation       = model.Nation;
                entity.IdentityCard = model.IdentityCard;
                entity.Occupation   = model.Occupation;
                entity.Residence    = model.Residence;
                entity.Domicile     = model.Domicile;

                entity.Household       = model.Household;
                entity.Relation        = model.Relation;
                entity.HouseholderName = model.HouseholderName;
                entity.Phone           = model.Phone;
                if (model.Birth != null && model.Birth != "")
                {
                    entity.Birth = DateTime.Parse(model.Birth).ToString("yyyy-MM-dd");
                    DateTime now   = DateTime.Now;
                    DateTime birth = Convert.ToDateTime(model.Birth);
                    int      age   = now.Year - birth.Year;
                    if (now.Month < birth.Month || (now.Month == birth.Month && now.Day < birth.Day))
                    {
                        age--;
                    }
                    entity.Age = age < 0 ? 0 : age;
                }
                else
                {
                    entity.Birth = "";
                }
                if (model.QianYiEtime != null && model.QianYiEtime != "")
                {
                    entity.QianYiEtime = DateTime.Parse(model.QianYiEtime).ToString("yyyy-MM-dd");
                }
                else
                {
                    entity.QianYiEtime = "";
                }
                if (model.QianYiStime != null && model.QianYiStime != "")
                {
                    entity.QianYiStime = DateTime.Parse(model.QianYiStime).ToString("yyyy-MM-dd");
                }
                else
                {
                    entity.QianYiStime = "";
                }
                entity.AddTime   = DateTime.Now.ToString("yyyy-MM-dd");
                entity.AddPeople = AuthContextService.CurrentUser.DisplayName;
                //entity.AddTime = DateTime.Now.ToString("yyyy-MM-dd");
                //entity.AddPeople = model.addPeople;
                entity.IsDeleted = 0;
                _dbContext.Userinfoty.Add(entity);
                int res = _dbContext.SaveChanges();
                if (res > 0)
                {
                    ToLog.AddLog("添加", "成功:添加:统一人员信息一条数据", _dbContext);
                }
                response.SetSuccess("添加成功");
                return(Ok(response));
            }
        }