//MVC 具體方法//API地址 通過 WebConfig配置 private static string apiAdds = ConfigurationManager.AppSettings["ApiAddress"]; //具體方法 public int AddSelectFlowerBll(... ...
//MVC 具體方法
//API地址 通過 WebConfig配置 private static string apiAdds = ConfigurationManager.AppSettings["ApiAddress"]; //具體方法 public int AddSelectFlowerBll(string selectProduct, string productName, string productSum,int UserID) { try { //非空判斷 if (!string.IsNullOrEmpty(selectProduct) && !string.IsNullOrEmpty(productName) && !string.IsNullOrEmpty(productSum)) { AssembleFlower aFlower = new AssembleFlower(); aFlower.Name = productName; aFlower.PriceSum = Convert.ToInt32(productSum); //添加人為1 aFlower.AssembleMan = UserID; aFlower.AssembleTime = DateTime.Now; aFlower.IsDel = 0; List<AssembleMaterial> materialList = new List<AssembleMaterial>(); string[] productArry = selectProduct.Split(';'); for (int i = 0; i < productArry.Length - 1; i++) { string[] flowerArry = productArry[i].Split(','); AssembleMaterial m = new AssembleMaterial(); m.MaterialID = Convert.ToInt32(flowerArry[0]); m.MaterialCount = Convert.ToInt32(flowerArry[1]); materialList.Add(m); } string[] strArray = { JsonConvert.SerializeObject(aFlower), JsonConvert.SerializeObject(materialList) }; #region 向後臺提交數據 //創建HttpClient對象 Uri uri = new Uri(apiAdds); HttpClient client = new HttpClient(); client.BaseAddress = uri; client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var jsonStr = JsonConvert.SerializeObject(strArray); HttpContent cont = new StringContent(jsonStr); cont.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var returnStr = ""; HttpResponseMessage resp = client.PostAsync("api/SelectFlower/AssembleAdd", cont).Result;//post提交數據, if (resp.IsSuccessStatusCode) { returnStr = resp.Content.ReadAsStringAsync().Result; } #endregion return Convert.ToInt32(JsonConvert.DeserializeObject(returnStr)); } else { //空值 return -1; } } catch { //發生錯誤 return -1; throw; } }
//API [RoutePrefix("api/SelectFlower")] public class SelectFlowerController : ApiController { /// <summary> /// 獲取自選花的類型 /// </summary> /// <param name="FlowerType"></param> /// <returns></returns> [HttpGet] [Route("GetSelectFlower")] public DataTable GetSelectFlower(string FlowerType) { string sql = string.Format("select * from FlowerMaterial where IsDel=0 and FlowerType=@FlowerType"); SqlParameter parameter = new SqlParameter("@FlowerType", SqlDbType.VarChar, 200); parameter.Value = FlowerType; return DBHelperSQL.QueryDataTable(sql, parameter); } [HttpPost] [Route("AssembleAdd")] public string AssembleAdd([FromBody] dynamic jsonStr) { //將前臺傳過來的值轉化為數組 var data = JsonConvert.DeserializeObject<string[]>(jsonStr.ToString()); AssembleFlower aFlower = JsonConvert.DeserializeObject<AssembleFlower>(data[0]); List<AssembleMaterial> MaterialList = JsonConvert.DeserializeObject<List<AssembleMaterial>>(data[1]); List<DBHelperSQL.KeyValue> list=new List<DBHelperSQL.KeyValue>(); list.Add(new DBHelperSQL.KeyValue() { Key = "insert into AssembleFlower values(@Name,@PriceSum,@AssembleMan,@AssembleTime,0)", Value = new SqlParameter[] { new SqlParameter("@Name",aFlower.Name), new SqlParameter("@PriceSum",aFlower.PriceSum), new SqlParameter("@AssembleMan",aFlower.AssembleMan), new SqlParameter("@AssembleTime",aFlower.AssembleTime), } }); foreach (var material in MaterialList) { list.Add(new DBHelperSQL.KeyValue() { Key = "insert into AssembleMaterial values(@MaterialID,@MaterialCount,(select top 1 ID from AssembleFlower order by ID desc))", Value = new SqlParameter[] { new SqlParameter("@MaterialID",material.MaterialID), new SqlParameter("@MaterialCount",material.MaterialCount) } }); } list.Add(new DBHelperSQL.KeyValue() { Key = " insert into MyCar values(3,(select top 1 ID from AssembleFlower order by ID desc),1,@UserID,@CreateTime,0)", Value = new SqlParameter[] { new SqlParameter("@UserID",aFlower.AssembleMan), new SqlParameter("@CreateTime",DateTime.Now) } }); return DBHelperSQL.ExecuteSqlTranAndReturn(list).ToString(); } }