//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();
}
}