public IActionResult HomeAddressCreate(HomeAddressViewModel model) { var response = ResponseModelFactory.CreateInstance; using (_dbContext) { var entity = new HaikanSmartTownCockpit.Api.Entities.HomeAddress(); entity.HomeAddressUuid = Guid.NewGuid(); entity.Address = model.Address; entity.Addresscode = model.Addresscode; entity.Village = model.Village; entity.Town = model.Town; entity.Door = model.Door; entity.Unit = model.Unit; entity.Resregion = model.Resregion; //entity.AddTime = DateTime.Now.ToString("yyyy-MM-dd"); //entity.AddPeople = model.addPeople; entity.AddTime = DateTime.Now.ToString("yyyy-MM-dd"); entity.AddPeople = AuthContextService.CurrentUser.DisplayName; entity.IsDeleted = 0; _dbContext.HomeAddress.Add(entity); int res = _dbContext.SaveChanges(); if (res > 0) { ToLog.AddLog("添加", "成功:添加:地址库信息一条数据", _dbContext); } response.SetSuccess("添加成功"); return(Ok(response)); } }
public IActionResult backtoken() { var response = ResponseModelFactory.CreateResultInstance; var code = "0"; try { var timeStamp = GetTimeStamp(); var timepass = timeStamp.ToString() + "123"; string valPassword = GetMD5Hash(timepass); string serviceUrl = "http://10.33.188.56:8090/addrMatch/auth/getToken?" + "user=abc&time=" + timeStamp + "&secret=" + valPassword; //response.SetData(serviceUrl); //return Ok(response); //创建Web访问对象 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl); //把用户传过来的数据转成“UTF-8”的字节流 //byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data); //myRequest.Method = "GET"; //myRequest.ContentLength = buf.Length; myRequest.ContentType = "application/json"; //myRequest.MaximumAutomaticRedirections = 1; //myRequest.AllowAutoRedirect = true; //发送请求 //Stream stream = myRequest.GetRequestStream(); //stream.Write(buf, 0, buf.Length); //stream.Close(); //获取接口返回值 //通过Web访问对象获取响应内容 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); //string returnXml = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法 string returnJson = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾 code = myResponse.StatusCode.ToString(); reader.Close(); myResponse.Close(); var datatoken = JsonConvert.DeserializeObject <ViewModels.ToTkenInfo.Token>(returnJson); string token = ""; if (datatoken != null) { token = datatoken.data.token; using (_dbContext) { var sql = string.Format("TRUNCATE TABLE HomeAddress"); _dbContext.Database.ExecuteSqlRaw(sql); var sno = Guid.NewGuid(); List <HaikanSmartTownCockpit.Api.Entities.HomeAddress> addresses = new List <HaikanSmartTownCockpit.Api.Entities.HomeAddress>(); string infoUrl = "http://10.33.188.56:8090/addrMatch/addrApi/searchAddr?token=" + token + "&addr=河桥&page=100&limit=9999&fuzzy=false&sno" + sno; HttpWebRequest myRequest1 = (HttpWebRequest)WebRequest.Create(infoUrl); myRequest1.ContentType = "application/json"; HttpWebResponse myResponse1 = (HttpWebResponse)myRequest1.GetResponse(); StreamReader reader1 = new StreamReader(myResponse1.GetResponseStream(), Encoding.UTF8); string returnJson1 = reader1.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾 code = myResponse1.StatusCode.ToString(); reader1.Close(); myResponse1.Close(); var data1 = JsonConvert.DeserializeObject <ViewModels.AddresInfo.AddressInfo>(returnJson1); //response.SetData(data1); //return Ok(response); for (int j = 0; j < data1.data.addrList.Count; j++) { //var entity = new HaikanSmartTownCockpit.Api.Entities.HomeAddress(); HaikanSmartTownCockpit.Api.Entities.HomeAddress entity = new HaikanSmartTownCockpit.Api.Entities.HomeAddress(); entity.Address = data1.data.addrList[j].addr; entity.Addresscode = data1.data.addrList[j].code; entity.Town = data1.data.addrList[j].town; entity.Ccmmunity = data1.data.addrList[j].community; entity.Squad = data1.data.addrList[j].squad; entity.Village = data1.data.addrList[j].village; entity.Szone = data1.data.addrList[j].szone; entity.Street = data1.data.addrList[j].street; entity.Door = data1.data.addrList[j].door; entity.Resregion = data1.data.addrList[j].resregion; entity.Building = data1.data.addrList[j].building; entity.BuildingNum = data1.data.addrList[j].building_num; entity.Unit = data1.data.addrList[j].unit; entity.Floor = data1.data.addrList[j].floor; //entity.Room = data1.data.addrList[j].room; //entity.RoomFloor = data1.data.addrList[j].room_floor; entity.HomeAddressUuid = Guid.NewGuid(); entity.IsDeleted = 0; addresses.Add(entity); } _dbContext.AddRange(addresses); _dbContext.SaveChanges(); } } return(Ok(response)); } catch (Exception ex) { response.SetFailed("获取失败"); response.SetData(ex.Message); return(Ok(response)); } }
public IActionResult HomeAddressImport(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.HomeAddress(); entity.HomeAddressUuid = Guid.NewGuid(); if (!string.IsNullOrEmpty(dt.Rows[i]["地址"].ToString())) { entity.Address = dt.Rows[i]["地址"].ToString(); } else { responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行地址为空" + "</p></br>"; defaultcount++; continue; } if (!string.IsNullOrEmpty(dt.Rows[i]["地址编码"].ToString())) { entity.Addresscode = dt.Rows[i]["地址编码"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["自然村"].ToString())) { entity.Village = dt.Rows[i]["自然村"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["街道"].ToString())) { entity.Town = dt.Rows[i]["街道"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["门牌"].ToString())) { entity.Door = dt.Rows[i]["门牌"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["单元"].ToString())) { entity.Unit = dt.Rows[i]["单元"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["小区"].ToString())) { entity.Resregion = 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.HomeAddress.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)); } } }