using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
using CoreEntity.Entity;
using CoreEntity.ESEntity;
using JCSoft.WX.Framework.Api;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using PublicLibrary.Model;
using SupplierWeb.Commonss;
namespace SupplierWeb.Controllers
{
[Route("web/mem_orderproducts")]
public class mem_orderproductsController : BaseController
{
public mem_orderproductsController(IMemoryCache cache, IApiClient client) : base(cache, client)
{
}
public IActionResult Index()
{
return View();
}
///
/// all订单产品列表
///
///
///
///
[HttpPost]
[Route("OrderproductsList")]
public ActionResult OrderproductsList([FromBody]dynamic data, QueryFilter[] filters)
{
int start = 0;
string filter = "";
Object query = null;
if (data != null)
{
filters = data.filters.ToObject();
filter = QueryFilter.getFilterString(filters);
if (data.pageIndex == 1)
{
start = 0;
}
else
{
start = ((data.pageIndex - 1) * data.pageSize) + 1;
}
//根据会员电话号码,先找到他的所有订单
//找到属于处方的订单
query = new { query = "select * from mem_orderproducts where 1=1"+
filter +
" order by CreationDate " +
"limit " + data.pageSize
};
}
//将参数转换成JSON数据
var json = JsonConvert.SerializeObject(query);
//通过传来的SQL语句,转换成DSL语句
string sql = Util.ElasticQuery("_sql/translate?format=json", "POST", json);
string from = "\"from\":" + start + ",";
sql = sql.Insert(1, from);
//通过转换的DSL语句,获取数据
var str = Util.ElasticQuery("mem_orderproducts/_search?format=json", "POST", sql);
//JSON转Root对象
Root stobj = EsCommon.FromJSON>(str);
//总数
List _source = new List();
long totalcount = 0;
if (stobj.hits.hits.Count > 0) {
//给filed额外的属性赋值
stobj.hits.hits.ForEach(x =>
{
x._source._id = x._id;
//x._source.ProductCode = x.fields.ProductCode[0].ToString();
x.fields.ActualPrice[0] = x.fields.ActualPrice[0] > 0 ?
x.fields.ActualPrice[0] / 100 : 0;
});
//将Root对象中的子对象取出来
_source = stobj.hits.hits.Select(x => x.entity()).ToList();
var countdata = new { query = "select count(1) from mem_orderproducts where 1=1"+
filter
};
var jsons = JsonConvert.SerializeObject(countdata);
//ES获取SQL执行结果
string obj = Util.ElasticQuery("_sql?format=json", "POST", jsons);
Num num = FromJSON(obj);
totalcount = num.rows[0][0];
}
var jsonData = JsonConvert.SerializeObject(_source);
return Json(
new
{
items = JsonConvert.DeserializeObject(jsonData),
totalCount = totalcount
});
}
///
/// 订单产品列表ByOderCode
///
///
///
///
[HttpPost]
[Route("ProductsListByOderCode")]
public ActionResult ProductsListByOderCode([FromBody]dynamic data, QueryFilter[] filters)
{
int start = 0;
//string OrdersCode = "JD84666969985";
string OrdersCode = "";
if (data != null)
{
OrdersCode = data.query[0];
data = new { query = "select * from mem_orderproducts where OrdersCode='" + OrdersCode + "' " };
}
//将参数转换成JSON数据
var json = JsonConvert.SerializeObject(data);
//通过传来的SQL语句,转换成DSL语句
string sql = Util.ElasticQuery("_sql/translate?format=json", "POST", json);
string from = "\"from\":" + start + ",";
sql = sql.Insert(1, from);
//通过转换的DSL语句,获取数据
var str = Util.ElasticQuery("mem_orderproducts/_search?format=json", "POST", sql);
//JSON转Root对象
Root stobj = FromJSON>(str);
//List Fields = new List();
//Fields = stobj.hits.hits.Select(x => x.fields).ToList();
//给filed额外的属性赋值
stobj.hits.hits.ForEach(x =>
{
x._source._id = x._id;
//x._source.ProductCode = x.fields.ProductCode[0].ToString();
x.fields.ActualPrice[0] = x.fields.ActualPrice[0] > 0 ?
x.fields.ActualPrice[0] / 100 : 0;
});
//将Root对象中的子对象取出来
List _source = new List();
_source = stobj.hits.hits.Select(x => x.entity()).ToList();
//总数
long totalcount = 0;
data = new { query = "select count(1) from mem_orderproducts where OrdersCode='"+ OrdersCode + "'" };
var jsons = JsonConvert.SerializeObject(data);
//ES获取SQL执行结果
string obj = Util.ElasticQuery("_sql?format=json", "POST", jsons);
Num num = FromJSON(obj);
totalcount = num.rows[0][0];
var jsonData = JsonConvert.SerializeObject(_source);
return Json(
new
{
items = JsonConvert.DeserializeObject(jsonData),
totalCount = totalcount,
success = true
});
}
///
/// JSON转对象
///
///
///
///
public static T FromJSON(string input)
{
try
{
return JsonConvert.DeserializeObject(input);
}
catch (Exception ex)
{
string a = ex.Message;
return default(T);
}
}
//#region 列表实体类
//public class _shards
//{
// ///
// ///
// ///
// public int total { get; set; }
// ///
// ///
// ///
// public int successful { get; set; }
// ///
// ///
// ///
// public int skipped { get; set; }
// ///
// ///
// ///
// public int failed { get; set; }
//}
//public class Total
//{
// ///
// ///
// ///
// public int value { get; set; }
// ///
// ///
// ///
// public string relation { get; set; }
//}
//public class _source
//{
// public string ProductCode { get; set; }
// ///
// ///
// ///
// public string CreationDate { get; set; }
// ///
// /// 仁和 痔速宁片 0.31g*12片*3板
// ///
// public string ProductName { get; set; }
// ///
// ///
// ///
// public string ProductPromotionSet { get; set; }
// ///
// ///
// ///
// public string AssociateProudctCode { get; set; }
// ///
// /// 盒
// ///
// public string Unit { get; set; }
// ///
// ///
// ///
// public string ProfitLevel { get; set; }
// ///
// ///
// ///
// public string LastModified { get; set; }
// ///
// /// 0.31g*12片*3板(仁和)
// ///
// public string Packing { get; set; }
// ///
// ///
// ///
// public string Batch { get; set; }
// ///
// ///
// ///
// public string Picture { get; set; }
// ///
// ///
// ///
// public string Barcode { get; set; }
// ///
// /// 江西药都仁和制药有限公司
// ///
// public string Manufacturer { get; set; }
// ///
// ///
// ///
// public string OrdersCode { get; set; }
// ///
// ///
// ///
// public string DamagedRemark { get; set; }
// ///
// ///
// ///
// public string MemberPhone { get; set; }
// ///
// ///
// ///
// public string Id { get; set; }
//}
//public class Fields
//{
// ///
// ///
// ///
// public List ActualPrice { get; set; }
// ///
// ///
// ///
// public List AvgDiscount { get; set; }
// ///
// ///
// ///
// public List MarketPrice { get; set; }
// ///
// ///
// ///
// public List ProductCode { get; set; }
// ///
// ///
// ///
// public List AvgFreight { get; set; }
// ///
// ///
// ///
// public List OurPrice { get; set; }
// ///
// ///
// ///
// public List Amount { get; set; }
// ///
// ///
// ///
// public List IntegralAmount { get; set; }
// ///
// ///
// ///
// public List PurchasePrice { get; set; }
//}
//public class HitsItem
//{
// ///
// ///
// ///
// public string _index { get; set; }
// ///
// ///
// ///
// public string _type { get; set; }
// ///
// ///
// ///
// public string _id { get; set; }
// ///
// ///
// ///
// public string _score { get; set; }
// ///
// ///
// ///
// public string _routing { get; set; }
// ///
// ///
// ///
// public _source _source { get; set; }
// ///
// ///
// ///
// public Fields fields { get; set; }
// ///
// ///
// ///
// public List sort { get; set; }
//}
//public class Hits
//{
// ///
// ///
// ///
// public Total total { get; set; }
// ///
// ///
// ///
// public string max_score { get; set; }
// ///
// ///
// ///
// public List hits { get; set; }
//}
//public class Root
//{
// ///
// ///
// ///
// public int took { get; set; }
// ///
// ///
// ///
// public string timed_out { get; set; }
// ///
// ///
// ///
// public _shards _shards { get; set; }
// ///
// ///
// ///
// public Hits hits { get; set; }
//}
//#endregion
//#region 总数实体类
//public class ColumnsItem
//{
// ///
// ///
// ///
// public string name { get; set; }
// ///
// ///
// ///
// public string type { get; set; }
//}
//public class Num
//{
// ///
// ///
// ///
// public List columns { get; set; }
// ///
// ///
// ///
// public List> rows { get; set; }
//}
//#endregion
}
}