ProductService.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using Ant.Core.SqlServer;
  2. using Ant.Core.Utils;
  3. using Ant.Core.WebApi.Model;
  4. using Central.Control.WebApi.Cache;
  5. using Central.Control.WebApi.DbEntity;
  6. using Central.Control.WebApi.EFDbContext;
  7. using Central.Control.WebApi.Enum;
  8. using Central.Control.WebApi.Models.Response;
  9. using Central.Control.WebApi.Service.Interface;
  10. using Microsoft.Extensions.Caching.Memory;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Configuration;
  14. using System.Linq;
  15. using System.Web;
  16. namespace Central.Control.WebApi.Service
  17. {
  18. /// <summary>
  19. ///
  20. /// </summary>
  21. public class ProductService: IProductService
  22. {
  23. /// <summary>
  24. ///
  25. /// </summary>
  26. public readonly static string ImagePrefix = ConfigurationManager.AppSettings["ImagePrefix"].ToString();
  27. private readonly ICacheHelper _cacheHelper;
  28. private readonly IDbContext _dbContent;
  29. /// <summary>
  30. ///
  31. /// </summary>
  32. /// <param name="dbContent"></param>
  33. public ProductService(
  34. ICacheHelper cacheHelper,
  35. IDbContext dbContent)
  36. {
  37. _cacheHelper = cacheHelper;
  38. _dbContent = dbContent;
  39. }
  40. /// <summary>
  41. /// 商品列表
  42. /// </summary>
  43. /// <param name="kw"></param>
  44. /// <param name="skip"></param>
  45. /// <param name="limit"></param>
  46. /// <returns></returns>
  47. public PagedApiResult<ProductResponseDto> Query(string kw = "", int skip = 0, int limit = 1)
  48. {
  49. var query = _dbContent.Set<YW_Product>().Where(p => p.IsDelete == 0 && p.Sale == SaleEnum.On);
  50. // kw搜索
  51. query = query.WhereIf(!string.IsNullOrWhiteSpace(kw), p => p.Name.Contains(kw));
  52. // total
  53. var total = query.Count();
  54. // 查询结果
  55. var queryResult = query.OrderByDescending(p => p.CreateDT).Skip(skip).Take(limit).ToList();
  56. List<ProductResponseDto> result = new List<ProductResponseDto>();
  57. queryResult.ForEach(item =>
  58. {
  59. var current = SafeClone<YW_Product, ProductResponseDto>.Trans(item);
  60. // 图片转换
  61. current.Img = $"{ImagePrefix}{current.Img}";
  62. result.Add(current);
  63. });
  64. return new PagedApiResult<ProductResponseDto>(result, total, skip, limit);
  65. }
  66. }
  67. }