PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Yintai.Hangzhou.Repository/Impl/ProductRepository.cs

https://github.com/yuchaoonline/ytoo.service
C# | 439 lines | 297 code | 79 blank | 63 comment | 59 complexity | ea14e2b17703215c9210f4503c1c7aed MD5 | raw file
  1. using com.intime.fashion.common.message;
  2. using com.intime.fashion.common.message.Messages;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. using System.Linq;
  8. using System.Linq.Expressions;
  9. using System.Transactions;
  10. using Yintai.Architecture.Common.Data.EF;
  11. using Yintai.Architecture.Common.Models;
  12. using Yintai.Architecture.Framework.ServiceLocation;
  13. using Yintai.Hangzhou.Data.Models;
  14. using Yintai.Hangzhou.Model.Enums;
  15. using Yintai.Hangzhou.Model.Filters;
  16. using Yintai.Hangzhou.Repository.Contract;
  17. namespace Yintai.Hangzhou.Repository.Impl
  18. {
  19. public class ProductRepository : RepositoryBase<ProductEntity, int>, IProductRepository
  20. {
  21. private readonly ISpecialTopicProductRelationRepository _specialTopicProductRelationRepository;
  22. private readonly IPromotionProductRelationRepository _promotionProductRelationRepository;
  23. private IUserAuthRepository _userAuthRepo;
  24. public ProductRepository(IPromotionProductRelationRepository promotionProductRelationRepository
  25. , ISpecialTopicProductRelationRepository specialTopicProductRelationRepository
  26. ,IUserAuthRepository userAuthRepo)
  27. {
  28. _specialTopicProductRelationRepository = specialTopicProductRelationRepository;
  29. _promotionProductRelationRepository = promotionProductRelationRepository;
  30. _userAuthRepo = userAuthRepo;
  31. }
  32. #region methods
  33. /// <summary>
  34. /// 获取商品Ids
  35. /// </summary>
  36. /// <param name="id"></param>
  37. /// <returns></returns>
  38. private List<int> GetTopicRelationIds(int? id)
  39. {
  40. if (id == null || id.Value < 1)
  41. {
  42. return new List<int>(0);
  43. }
  44. var s = new List<int> { id.Value };
  45. return GetTopicRelationIds(s);
  46. }
  47. /// <summary>
  48. /// 获取商品Ids
  49. /// </summary>
  50. /// <param name="promotionId"></param>
  51. /// <returns></returns>
  52. private List<int?> GetPromotionRelationIds(int? promotionId)
  53. {
  54. if (promotionId == null || promotionId.Value < 1)
  55. {
  56. return new List<int?>(0);
  57. }
  58. var entities = _promotionProductRelationRepository.GetList(promotionId.Value);
  59. return entities.Select(v => v.ProdId).Distinct().ToList();
  60. }
  61. private IEnumerable<Promotion2ProductEntity> GetListByPromotion4Linq(int id)
  62. {
  63. return _promotionProductRelationRepository.GetListByPromotionLinq(id);
  64. }
  65. private IEnumerable<SpecialTopicProductRelationEntity> GetTopicRelationIds4Linq(int id)
  66. {
  67. var s = new List<int> { id };
  68. return _specialTopicProductRelationRepository.GetList4Linq(s);
  69. }
  70. /// <summary>
  71. ///
  72. /// </summary>
  73. /// <param name="ids"></param>
  74. /// <returns></returns>
  75. private List<int> GetTopicRelationIds(List<int> ids)
  76. {
  77. if (ids == null || ids.Count == 0)
  78. {
  79. return new List<int>(0);
  80. }
  81. var entitys = _specialTopicProductRelationRepository.GetList(ids);
  82. if (entitys == null || entitys.Count == 0)
  83. {
  84. return new List<int>(0);
  85. }
  86. var result = entitys.Select(v => v.Product_Id).Distinct().ToList();
  87. return result;
  88. }
  89. private static Expression<Func<ProductEntity, bool>> Filter(DataStatus? dataStatus, Timestamp timestamp,
  90. ICollection<int> tagid,
  91. int? recommendUser, int? brandId = null, List<int> productIds = null, string productName = null)
  92. {
  93. var filter = PredicateBuilder.True<ProductEntity>();
  94. //always exclude deleted
  95. filter = filter.And(v => v.Status != (int)DataStatus.Deleted);
  96. if (dataStatus != null)
  97. {
  98. filter = filter.And(v => v.Status == (int)dataStatus);
  99. }
  100. if (timestamp != null)
  101. {
  102. switch (timestamp.TsType)
  103. {
  104. case TimestampType.New:
  105. filter = filter.And(v => v.UpdatedDate > timestamp.Ts);
  106. break;
  107. case TimestampType.Old:
  108. default:
  109. filter = filter.And(v => v.UpdatedDate <= timestamp.Ts);
  110. break;
  111. }
  112. }
  113. if (tagid != null && tagid.Count > 0)
  114. {
  115. filter = filter.And(v => tagid.Any(s => s == v.Tag_Id));
  116. }
  117. if (recommendUser != null)
  118. {
  119. filter = filter.And(v => v.RecommendUser == recommendUser.Value);
  120. }
  121. if (brandId != null)
  122. {
  123. filter = filter.And(v => v.Brand_Id == brandId);
  124. }
  125. if (productIds != null && productIds.Count > 0)
  126. {
  127. filter = filter.And(v => productIds.Any(s => s == v.Id));
  128. }
  129. if (productName != null)
  130. {
  131. filter = filter.And(v => v.Name.StartsWith(productName));
  132. }
  133. return filter;
  134. }
  135. private Expression<Func<ProductEntity, bool>> Filter(ProductFilter productFilter)
  136. {
  137. //if (productFilter == null)
  138. //{
  139. // return null;
  140. //}
  141. List<int> pids = null;
  142. //if (productFilter.TopicId != null && productFilter.TopicId > 0)
  143. //{
  144. // pids = GetTopicRelationIds(productFilter.TopicId);
  145. // //特殊处理处理
  146. // if (pids == null || pids.Count == 0)
  147. // {
  148. // pids = new List<int> { -1 };
  149. // }
  150. //}
  151. //if (productFilter.PromotionId != null && productFilter.PromotionId > 0)
  152. //{
  153. // var ids = GetPromotionRelationIds(productFilter.PromotionId);
  154. // if (ids != null && ids.Count > 0)
  155. // {
  156. // if (pids == null)
  157. // {
  158. // pids = new List<int>(ids.Count);
  159. // }
  160. // foreach (var id in ids)
  161. // {
  162. // pids.Add(id ?? 0);
  163. // }
  164. // }
  165. // else
  166. // {
  167. // //特殊处理处理
  168. // if (pids == null || pids.Count == 0)
  169. // {
  170. // pids = new List<int> { -2 };
  171. // }
  172. // }
  173. //}
  174. //if (pids != null)
  175. //{
  176. // pids = pids.Distinct().ToList();
  177. //}
  178. return Filter(productFilter.DataStatus, productFilter.Timestamp, productFilter.TagIds,
  179. productFilter.RecommendUser, productFilter.BrandId, pids, productFilter.ProductName);
  180. }
  181. /// <summary>
  182. /// 排序
  183. /// </summary>
  184. /// <param name="sort"></param>
  185. /// <returns></returns>
  186. private static Func<IQueryable<ProductEntity>, IOrderedQueryable<ProductEntity>> GetOrder(ProductSortOrder sort)
  187. {
  188. Func<IQueryable<ProductEntity>, IOrderedQueryable<ProductEntity>> order = null;
  189. switch (sort)
  190. {
  191. case ProductSortOrder.CreatedDateDesc:
  192. order = v => v.OrderByDescending(s => s.CreatedDate);
  193. break;
  194. case ProductSortOrder.Default:
  195. default:
  196. order = v => v.OrderByDescending(s => s.SortOrder).ThenByDescending(s => s.CreatedDate);
  197. break;
  198. }
  199. return order;
  200. }
  201. #endregion
  202. public IQueryable<ProductEntity> Search(PagerRequest pagerRequest, out int totalCount, ProductSortOrder sortOrder, Timestamp timestamp,
  203. string productName, string brandName, int? recommendUser, List<int> tagids, int? brandId, DataStatus? dataStatus)
  204. {
  205. var filter = Filter(dataStatus, timestamp, tagids, recommendUser, brandId);
  206. if (!String.IsNullOrWhiteSpace(productName))
  207. {
  208. filter = filter.And(v => v.Name.Contains(productName));
  209. }
  210. totalCount = 0;
  211. var p1 = base.Get(filter).Take(pagerRequest.PageSize);
  212. if (!String.IsNullOrEmpty(brandName))
  213. {
  214. var p2 = base.Get(Filter(dataStatus, timestamp, tagids, recommendUser, brandId));
  215. var b = (ServiceLocator.Current.Resolve<IBrandRepository>().Get(DataStatus.Normal) as IQueryable<BrandEntity>).Where(v => v.Name.Contains(brandName) || v.EnglishName.Contains(brandName));
  216. var r = p2.Join(b, v => v.Brand_Id, j => j.Id, (v, j) => v).Take(pagerRequest.PageSize);
  217. return r.Union(p1).Take(pagerRequest.PageSize);
  218. }
  219. return p1;
  220. }
  221. public List<ProductEntity> GetPagedListForSearch(PagerRequest pagerRequest, out int totalCount, ProductSortOrder sortOrder, Timestamp timestamp,
  222. string productName, int? recommendUser, List<int> tagids, int? brandId)
  223. {
  224. var filter = Filter(DataStatus.Normal, timestamp, tagids, recommendUser, brandId);
  225. if (!String.IsNullOrWhiteSpace(productName))
  226. {
  227. filter = filter.And(v => v.Name.StartsWith(productName));
  228. }
  229. return
  230. base.Get(filter, out totalCount, pagerRequest.PageIndex, pagerRequest.PageSize, GetOrder(sortOrder))
  231. .ToList();
  232. }
  233. public List<ProductEntity> GetPagedList(PagerRequest pagerRequest, out int totalCount, ProductSortOrder sortOrder, Timestamp timestamp,
  234. int? tagId, int? recommendUser, int? brandId)
  235. {
  236. List<int> tags;
  237. if (tagId == null)
  238. {
  239. tags = null;
  240. }
  241. else
  242. {
  243. tags = new List<int>(1) { tagId.Value };
  244. }
  245. return
  246. base.Get(Filter(DataStatus.Normal, timestamp, tags, recommendUser, brandId), out totalCount,
  247. pagerRequest.PageIndex, pagerRequest.PageSize, GetOrder(sortOrder)).ToList();
  248. }
  249. public List<ProductEntity> GetList(List<int> ids)
  250. {
  251. return base.Get(v => ids.Any(s => s == v.Id) && v.Status == (int)DataStatus.Normal).ToList();
  252. }
  253. public ProductEntity SetSortOrder(ProductEntity entity, int sortOrder, int updateUser)
  254. {
  255. var parames = new List<SqlParameter>
  256. {
  257. new SqlParameter("@SortOrder", sortOrder),
  258. new SqlParameter("@Id", entity.Id),
  259. };
  260. const string sql = "UPDATE [dbo].[Product] SET [SortOrder] = @SortOrder WHERE Id = @Id;";
  261. var i = SqlHelper.ExecuteNonQuery(SqlHelper.GetConnection(), CommandType.Text, sql, parames.ToArray());
  262. if (i > 0)
  263. {
  264. return GetItem(entity.Id);
  265. }
  266. else
  267. {
  268. return null;
  269. }
  270. }
  271. public ProductEntity SetIsHasImage(int entityId, bool hasImage, DataStatus dataStatus, int updateUser, string des)
  272. {
  273. var parames = new List<SqlParameter>
  274. {
  275. new SqlParameter("@IsHasImage", hasImage),
  276. new SqlParameter("@Id", entityId),
  277. new SqlParameter("@UpdatedDate", DateTime.Now),
  278. new SqlParameter("@UpdatedUser", updateUser),
  279. new SqlParameter("@Status", (int)dataStatus),
  280. };
  281. const string sql = "UPDATE [dbo].[Product] SET [IsHasImage] = @IsHasImage, [UpdatedUser] = @UpdatedUser ,[UpdatedDate] = @UpdatedDate , [Status]= @Status WHERE Id = @Id;";
  282. var i = SqlHelper.ExecuteNonQuery(SqlHelper.GetConnection(), CommandType.Text, sql, parames.ToArray());
  283. if (i > 0)
  284. {
  285. return GetItem(entityId);
  286. }
  287. return null;
  288. }
  289. public List<ProductEntity> GetPagedList(PagerRequest pagerRequest, out int totalCount,
  290. ProductSortOrder sortOrder, ProductFilter productFilter)
  291. {
  292. return Get(pagerRequest, out totalCount, sortOrder, productFilter).ToList();
  293. }
  294. public IQueryable<ProductEntity> Get(PagerRequest pagerRequest, out int totalCount, ProductSortOrder sortOrder, ProductFilter productFilter)
  295. {
  296. var linq = base.Get(Filter(productFilter));
  297. //var linq = base.Get(Filter(productFilter), out totalCount, pagerRequest.PageIndex, pagerRequest.PageSize,
  298. // GetOrder(sortOrder));
  299. if (productFilter.TopicId != null)
  300. {
  301. linq = linq.Join(GetTopicRelationIds4Linq(productFilter.TopicId.Value), r => r.Id, v => v.Product_Id,
  302. (r, v) => r);
  303. }
  304. if (productFilter.PromotionId != null)
  305. {
  306. linq = linq.Join(GetListByPromotion4Linq(productFilter.PromotionId.Value), r => r.Id, v => v.ProdId,
  307. (r, v) => r);
  308. }
  309. var resetSet = linq;
  310. var orderBy = GetOrder(sortOrder);
  311. resetSet = orderBy != null ? orderBy(resetSet).AsQueryable() : resetSet.AsQueryable();
  312. totalCount = resetSet.Count();
  313. var skipCount = (pagerRequest.PageIndex - 1) * pagerRequest.PageSize;
  314. resetSet = skipCount == 0 ? resetSet.Take(pagerRequest.PageSize) : resetSet.Skip(skipCount).Take(pagerRequest.PageSize);
  315. return resetSet;
  316. }
  317. public IQueryable<ProductEntity> Get(ProductSortOrder? sortOrder, ProductFilter productFilter)
  318. {
  319. var linq = base.Get(Filter(productFilter));
  320. if (sortOrder != null)
  321. {
  322. var orderBy = GetOrder(sortOrder.Value);
  323. linq = orderBy != null ? orderBy(linq).AsQueryable() : linq.AsQueryable();
  324. }
  325. return linq;
  326. }
  327. public override ProductEntity GetItem(int key)
  328. {
  329. return base.Find(key);
  330. }
  331. public ProductEntity SetCount(ProductCountType countType, int id, int count)
  332. {
  333. var productEntity = Find(id);
  334. switch (countType)
  335. {
  336. case ProductCountType.FavoriteCount:
  337. productEntity.FavoriteCount++;
  338. break;
  339. case ProductCountType.InvolvedCount:
  340. productEntity.InvolvedCount++;
  341. break;
  342. case ProductCountType.ShareCount:
  343. productEntity.ShareCount++;
  344. break;
  345. }
  346. productEntity.UpdatedDate = DateTime.Now;
  347. Update(productEntity);
  348. return productEntity;
  349. }
  350. public override Yintai.Hangzhou.Data.Models.ProductEntity Insert(Yintai.Hangzhou.Data.Models.ProductEntity entity)
  351. {
  352. var newEntity = base.Insert(entity);
  353. return newEntity;
  354. }
  355. public override void Update(ProductEntity entity)
  356. {
  357. base.Update(entity);
  358. }
  359. }
  360. }