ProductService.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. /// <returns></returns>
  44. public ApiResult<string> Test()
  45. {
  46. var ww = _dbContent.Set<SYS_Device>().FirstOrDefault(p => p.Id == "cb8b354c-b713-4dd7-9752-a6f3217004a0");
  47. if (ww != null)
  48. {
  49. ww.Name = ww.Name + "1";
  50. }
  51. _dbContent.SaveChanges();
  52. _cacheHelper.Set<string>("test01", "95990934");
  53. var ss = _cacheHelper.Get<string>("test01");
  54. _cacheHelper.Set("test02", new Test001()
  55. {
  56. Id = Guid.NewGuid().ToString(),
  57. Token = "998097"
  58. });
  59. var ss1 = _cacheHelper.Get<Test001>("test02");
  60. return new ApiResult<string>("查库得到数据:" + ww?.Name ?? "nodata");
  61. }
  62. /// <summary>
  63. /// 商品列表
  64. /// </summary>
  65. /// <param name="kw"></param>
  66. /// <param name="skip"></param>
  67. /// <param name="limit"></param>
  68. /// <returns></returns>
  69. public PagedApiResult<ProductResponseDto> Query(string kw = "", int skip = 0, int limit = 1)
  70. {
  71. var query = _dbContent.Set<YW_Product>().Where(p => p.IsDelete == 0 && p.Sale == SaleEnum.On);
  72. // kw搜索
  73. query = query.WhereIf(!string.IsNullOrWhiteSpace(kw), p => p.Name.Contains(kw));
  74. // total
  75. var total = query.Count();
  76. // 查询结果
  77. var queryResult = query.OrderByDescending(p => p.CreateDT).Skip(skip).Take(limit).ToList();
  78. List<ProductResponseDto> result = new List<ProductResponseDto>();
  79. queryResult.ForEach(item =>
  80. {
  81. var current = SafeClone<YW_Product, ProductResponseDto>.Trans(item);
  82. // 图片转换TODO
  83. current.Img = $"{ImagePrefix}{current.Img}";
  84. result.Add(current);
  85. });
  86. return new PagedApiResult<ProductResponseDto>(result, total, skip, limit);
  87. }
  88. }
  89. public class Test001
  90. {
  91. public string Id { set; get; }
  92. public string Token { set; get; }
  93. }
  94. }