public IActionResult RuzhuCreate(HoteltongjiViewModel model) { var response = ResponseModelFactory.CreateInstance; using (_dbContext) { var entity = new HaikanSmartTownCockpit.Api.Entities.RuzhuInfo(); entity.RuzhuInfoUuid = Guid.NewGuid(); entity.RuzhuName = model.RuzhuName; entity.RuzhuPeople = model.RuzhuPeople; entity.RuzhuRoom = model.RuzhuRoom; entity.RuzhuMoney = model.RuzhuMoney; entity.RuzhuTime = DateTime.Parse(model.RuzhuTime[0]).ToString("yyyy-MM-dd"); entity.LikaiTime = DateTime.Parse(model.RuzhuTime[1]).ToString("yyyy-MM-dd"); if (entity.RuzhuTime != null && entity.LikaiTime != null) { DateTime d1 = Convert.ToDateTime(entity.RuzhuTime); DateTime d2 = Convert.ToDateTime(entity.LikaiTime); DateTime d3 = Convert.ToDateTime(string.Format("{0}-{1}-{2}", d1.Year, d1.Month, d1.Day)); DateTime d4 = Convert.ToDateTime(string.Format("{0}-{1}-{2}", d2.Year, d2.Month, d2.Day)); int num = (d4 - d3).Days; entity.RuzhuDay = num.ToString(); } entity.HotelName = model.HotelName; 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.RuzhuInfo.Add(entity); int res = _dbContext.SaveChanges(); if (res > 0) { ToLog.AddLog("添加", "成功:添加:酒店入住信息一条数据", _dbContext); } response.SetSuccess("添加成功"); return(Ok(response)); } }
public IActionResult RuzhuImport(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.RuzhuInfo(); entity.RuzhuInfoUuid = Guid.NewGuid(); if (!string.IsNullOrEmpty(dt.Rows[i]["入住登记姓名"].ToString())) { entity.RuzhuName = dt.Rows[i]["入住登记姓名"].ToString(); } else { responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行入住登记姓名为空" + "</p></br>"; defaultcount++; continue; } if (!string.IsNullOrEmpty(dt.Rows[i]["入住人数"].ToString())) { entity.RuzhuPeople = dt.Rows[i]["入住人数"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["房间号"].ToString())) { entity.RuzhuRoom = dt.Rows[i]["房间号"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["价格"].ToString())) { entity.RuzhuMoney = dt.Rows[i]["价格"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["入住时间"].ToString())) { entity.RuzhuTime = dt.Rows[i]["入住时间"].ToString(); } else { responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行入住时间为空" + "</p></br>"; defaultcount++; continue; } if (!string.IsNullOrEmpty(dt.Rows[i]["离开时间"].ToString())) { entity.LikaiTime = dt.Rows[i]["离开时间"].ToString(); if (entity.RuzhuTime != null && entity.LikaiTime != null) { DateTime d1 = Convert.ToDateTime(entity.RuzhuTime); DateTime d2 = Convert.ToDateTime(entity.LikaiTime); DateTime d3 = Convert.ToDateTime(string.Format("{0}-{1}-{2}", d1.Year, d1.Month, d1.Day)); DateTime d4 = Convert.ToDateTime(string.Format("{0}-{1}-{2}", d2.Year, d2.Month, d2.Day)); int num = (d4 - d3).Days; entity.RuzhuDay = num.ToString(); } } else { responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行离开时间为空" + "</p></br>"; defaultcount++; continue; } if (!string.IsNullOrEmpty(dt.Rows[i]["民宿名称"].ToString())) { entity.HotelName = dt.Rows[i]["民宿名称"].ToString(); } 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.RuzhuInfo.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)); } } }