using HH.MData;
using HH.WMS.Common;
using HH.WMS.Entitys.Algorithm;
using HH.WMS.Entitys.Basic;
using HH.WMS.Entitys.Common;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace HH.WMS.DAL.Basic
{
public class TN_WMS_ITEMDAL : DapperBaseDAL
{
#region 根据物料编码返回物料实体
///
/// 根据物料编码返回物料实体
///
///
///
public AutoBomItemEntity GetItemEntity(string itemCode)
{
AutoBomItemEntity entity = new AutoBomItemEntity();
var query = Query.EQ("CN_S_ITEM_CODE", itemCode);
entity = MongoDBSingleton.Instance.FindOne(query, "TN_AB_ITEM");
return entity;
}
public AutoBomItemEntityYS GetItemEntityYS(string itemCode)
{
AutoBomItemEntityYS entity = new AutoBomItemEntityYS();
var query = Query.EQ("CN_S_ITEM_CODE", itemCode);
entity = MongoDBSingleton.Instance.FindOne(query, "TN_AB_ITEM");
return entity;
}
public List GetItemByNameSpec(string itemName, string specification)
{
try
{
Log.Info("===>进度1", "");
#region 首先通过物料名称全部匹配查询
List queryList = new List();
if (!string.IsNullOrEmpty(itemName))
{
queryList.Add(Query.EQ("CN_S_ITEM_NAME", itemName));
}
if (!string.IsNullOrEmpty(specification))
{
queryList.Add(Query.EQ("CN_S_MODEL", specification));
}
IMongoQuery query = null;
if (queryList.Any()) { query = Query.And(queryList); }
var listCount = MongoDBSingleton.Instance.Find(query, "TN_AB_ITEM");
#endregion
if (listCount.Count == 1)//全匹配情况下单条,直接成功返回
{
Log.Info("===>进度2", "");
return listCount;
}
if (listCount.Count > 1)//全匹配情况下多条,直接报错返回
{
Log.Info("===>进度3", "");
return new List();
}
if (listCount.Count == 0)//全匹配情况下没有,开始模糊查询
{
Log.Info("===>进度4", "");
#region 然后通过物料名称模糊匹配查询
queryList = new List();
if (!string.IsNullOrEmpty(itemName))
{
queryList.Add(Query.Matches("CN_S_ITEM_NAME", BsonRegularExpression.Create(new Regex("^" + itemName))));
}
if (!string.IsNullOrEmpty(specification))
{
queryList.Add(Query.EQ("CN_S_MODEL", specification));
}
query = null;
if (queryList.Any()) { query = Query.And(queryList); }
var listCount1 = MongoDBSingleton.Instance.Find(query, "TN_AB_ITEM");
#endregion
if (listCount1.Count == 0)//模糊匹配情况下没有,直接报错返回
{
Log.Info("===>进度5", "");
return null;
}
if (listCount1.Count >= 1)//模糊匹配情况下超过或者等于一条时,开始加括号模糊匹配查询
{
Log.Info("===>进度6", "");
#region 然后通过物料名称加括号模糊匹配查询
queryList = new List();
if (!string.IsNullOrEmpty(itemName))
{
queryList.Add(Query.Or(Query.Matches("CN_S_ITEM_NAME", BsonRegularExpression.Create(new Regex("^" + itemName + "\\("))), Query.Or(Query.Matches("CN_S_ITEM_NAME", BsonRegularExpression.Create(new Regex("^" + itemName + "\\("))))));
}
if (!string.IsNullOrEmpty(specification))
{
queryList.Add(Query.EQ("CN_S_MODEL", specification));
}
query = null;
if (queryList.Any()) { query = Query.And(queryList); }
var listCount2 = MongoDBSingleton.Instance.Find(query, "TN_AB_ITEM");
#endregion
if (listCount2.Count >= 1)//多条或者单条的情况下,返回至前台
{
Log.Info("===>进度7", "");
return listCount2;
}
else//全匹配情况下多条,直接报错返回
{
Log.Info("===>进度8", "");
return new List();
}
}
}
Log.Info("===>进度9", "");
return new List();
}
catch (Exception ex)
{
Log.Info("===>进度10", ex.Message.ToString());
return null;
}
}
///
/// 根据物料编码返回物料实体
///
///
///
public List GetItemList(List itemCodes)
{
try
{
var itemCode = itemCodes.ConvertAll(x => x);
var query = Query.In("CN_S_ITEM_CODE", itemCode);
List list = MongoDBSingleton.Instance.Find(query, "TN_AB_ITEM");
return list;
}
catch (Exception ex)
{
return null;
}
}
///
/// 根据物料编码返回物料实体
///
///
///
public List GetAllItem(string itemCode)
{
List list = new List();
if (string.IsNullOrEmpty(itemCode))
{
list = MongoDBSingleton.Instance.Find(null, "TN_AB_ITEM");
}
else
{
var query = Query.EQ("CN_S_ITEM_CODE", itemCode);
list = MongoDBSingleton.Instance.Find(query, "TN_AB_ITEM");
}
return list;
}
///
/// 根据物料编码返回物料实体
///
///
///
/// [HanHe(LT)] CREATED 2018/11/26
/// [HanHe(DBS)] CREATED 2019/1/7
public List GetListItemByCode(string itemCode, string itemName, string model, int pageIndex, int pageSize, out int total)
{
List entity = new List();
SortByDocument sortBy = new SortByDocument();
sortBy.Add("CN_S_ITEM_CODE", -1);
List queryList = new List();
if (!string.IsNullOrEmpty(itemCode))
{
queryList.Add(Query.Matches("CN_S_ITEM_CODE", itemCode));
}
if (!string.IsNullOrEmpty(itemName))
{
queryList.Add(Query.Matches("CN_S_ITEM_NAME", itemName));
}
if (!string.IsNullOrEmpty(model))
{
queryList.Add(Query.Matches("CN_S_MODEL", model));
}
IMongoQuery query = null;
if (queryList.Any())
query = Query.And(queryList);
total = Convert.ToInt32(MongoDBSingleton.Instance.FindCount(query, "TN_AB_ITEM"));
entity = MongoDBSingleton.Instance.Find(query, pageIndex, pageSize, sortBy, "TN_AB_ITEM");
return entity;
}
#endregion
#region 物料列表
///
/// 物料列表
///
///
public List GetItemList(SearchModel searchModel, out int total)
{
List queryList = new List();
if (searchModel.SearchCondition != null)
{
string itemCode = Util.ToString(searchModel.SearchCondition.ItemCode);
if (!string.IsNullOrEmpty(itemCode))
{
queryList.Add(Query.Matches("CN_S_ITEM_CODE", Util.ToMongoLike(itemCode)));
}
string itemName = Util.ToString(searchModel.SearchCondition.ItemName);
if (!string.IsNullOrEmpty(itemName))
{
queryList.Add(Query.Matches("CN_S_ITEM_NAME", Util.ToMongoLike(itemName)));
}
string itemModel = Util.ToString(searchModel.SearchCondition.ItemModel);
if (!string.IsNullOrEmpty(itemModel))
{
queryList.Add(Query.Matches("CN_S_MODEL", Util.ToMongoLike(itemModel)));
}
string itemFigureNo = Util.ToString(searchModel.SearchCondition.ItemFigureNo);
if (!string.IsNullOrEmpty(itemFigureNo))
{
queryList.Add(Query.Matches("CN_S_FIGURE_NO", Util.ToMongoLike(itemFigureNo)));
}
string itemOwner = Util.ToStringInput(searchModel.SearchCondition.ItemOwner);
if (!string.IsNullOrEmpty(itemOwner))
{
string sql = "SELECT * FROM TN_WM_B_STOCK_QTY WHERE CN_S_OWNER='" + itemOwner + "' AND CN_F_QUANTITY>0 ";
var stockQtyList = ExecuteQuery(sql);
var itemCodes = stockQtyList.ConvertAll(x => x.CN_S_ITEM_CODE);
queryList.Add(Query.In("CN_S_ITEM_CODE", itemCodes));
}
}
IMongoQuery query = null;
if (queryList.Any())
query = Query.And(queryList);
total = Convert.ToInt32(MongoDBSingleton.Instance.FindCount(query, "TN_AB_ITEM"));
return MongoDBSingleton.Instance.Find(query, searchModel.PageIndex, searchModel.PageSize, null, "TN_AB_ITEM");
}
#endregion
#region 模糊匹配top条
///
/// 模糊匹配top条
///
///
///
///
public List GetMatchItem(string key, int top = 10)
{
IMongoQuery query = Query.Matches("CN_S_ITEM_CODE", "/" + key + "/");
return MongoDBSingleton.Instance.Find(query, 1, top, null, "TN_AB_ITEM");
}
#endregion
public TN_AB_B_ITEM_PRICEEntity GetItemPriceModel(string itemCode)
{
try
{
TN_AB_B_ITEM_PRICEEntity entity = new TN_AB_B_ITEM_PRICEEntity();
var query = Query.And(Query.EQ("CN_S_ITEM_CODE", itemCode), Query.EQ("CN_S_PRICE_TYPE", "标准成本"));
entity = MongoDBSingleton.Instance.FindOne(query, "TN_AB_B_ITEM_PRICE");
return entity;
}
catch (Exception ex)
{
}
return null;
}
//public List GetItemList()
//{
// var list= MongoDBSingleton.Instance.Find(null, "TN_AB_ITEM");
// return list;
//}
}
}