DeviceService.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using Ant.Core.SqlServer;
  2. using Ant.Core.Utils;
  3. using Ant.Core.WebApi.Enum;
  4. using Ant.Core.WebApi.Model;
  5. using Central.Control.WebApi.DbEntity;
  6. using Central.Control.WebApi.EFDbContext;
  7. using Central.Control.WebApi.Models.Request;
  8. using Central.Control.WebApi.Models.Response;
  9. using Central.Control.WebApi.Service.Interface;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Web;
  14. namespace Central.Control.WebApi.Service
  15. {
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. public class DeviceService: IDeviceService
  20. {
  21. private readonly IDbContext _dbContent;
  22. private readonly IUserService _userService;
  23. /// <summary>
  24. ///
  25. /// </summary>
  26. /// <param name="dbContent"></param>
  27. public DeviceService(
  28. IDbContext dbContent,
  29. IUserService userService)
  30. {
  31. _dbContent = dbContent;
  32. _userService = userService;
  33. }
  34. /// <summary>
  35. /// 设备包装盒表
  36. /// </summary>
  37. /// <param name="kw"></param>
  38. /// <param name="skip"></param>
  39. /// <param name="limit"></param>
  40. /// <returns></returns>
  41. public PagedApiResult<DevicePackingResponseDto> GetDevicePackings(string kw = "", int skip = 0, int limit = 1)
  42. {
  43. var session = _userService.GetLoginSession();
  44. var query = from p in _dbContent.Set<YW_Packing>().Where(q => q.IsDelete == 0)
  45. join dp in _dbContent.Set<YW_DevicePacking>().Where(q => q.IsDelete == 0 && q.DeviceId == session.UserId) on p.Id equals dp.PackingId into temp
  46. from dpp in temp.DefaultIfEmpty()
  47. select new
  48. {
  49. Packing = p,
  50. DevicePacking = dpp
  51. };
  52. // kw搜索
  53. query = query.WhereIf(!string.IsNullOrWhiteSpace(kw), p => p.Packing.Name.Contains(kw));
  54. // total
  55. var total = query.Count();
  56. // 查询结果
  57. var queryResult = query.OrderByDescending(p => p.Packing.CreateDT).Skip(skip).Take(limit).ToList();
  58. List<DevicePackingResponseDto> result = new List<DevicePackingResponseDto>();
  59. queryResult.ForEach(item =>
  60. {
  61. // 图片转换
  62. // current.Img = ;
  63. result.Add(new DevicePackingResponseDto()
  64. {
  65. Id = item.DevicePacking?.Id ?? string.Empty,
  66. PackingId = item.Packing.Id,
  67. PackingName = item.Packing.Name,
  68. PackingImg = $"{ProductService.ImagePrefix}{item.Packing.Img}",
  69. PackingMin = item.Packing.Min,
  70. PackingMax = item.Packing.Max,
  71. Capacity = item.DevicePacking?.Capacity ?? 1,
  72. Stock = item.DevicePacking?.Stock ?? 0,
  73. });
  74. });
  75. return new PagedApiResult<DevicePackingResponseDto>(result, total, skip, limit);
  76. }
  77. /// <summary>
  78. /// 设备包装盒保存
  79. /// </summary>
  80. /// <param name="req"></param>
  81. /// <returns></returns>
  82. public ApiResult SaveDevicePacking(DevicePackingRequestDto req)
  83. {
  84. if (req == null
  85. || string.IsNullOrWhiteSpace(req.PackingId)
  86. || req.Stock < 0
  87. || req.Capacity <= 0)
  88. {
  89. return new ApiResult(ApiStatusCode.InvalidParameter, "请求参数不正确,请检查");
  90. }
  91. var session = _userService.GetLoginSession();
  92. // 每个设备,对于每一种包装只能新增一次,其余的都只能是在上面修改库存
  93. var devicePacking = _dbContent.Set<YW_DevicePacking>().FirstOrDefault(p => p.IsDelete == 0 && p.DeviceId == session.UserId && p.PackingId == req.PackingId);
  94. if (devicePacking == null && string.IsNullOrWhiteSpace(req.Id))
  95. {
  96. // 新增
  97. var packing = _dbContent.Set<YW_Packing>().FirstOrDefault(p => p.IsDelete == 0 && p.Id == req.PackingId);
  98. if (packing == null)
  99. {
  100. return new ApiResult(ApiStatusCode.InvalidParameter, "您选择的包装盒数据不存在");
  101. }
  102. devicePacking = new YW_DevicePacking()
  103. {
  104. Id = IdGenerator.NewId(),
  105. PackingId = packing.Id,
  106. DeviceId = session.UserId,
  107. Capacity = req.Capacity,
  108. Stock = req.Stock,
  109. CreateBY = session.UserId
  110. };
  111. _dbContent.Set<YW_DevicePacking>().Add(devicePacking);
  112. }
  113. else
  114. {
  115. // 修改
  116. //var devicePacking = _dbContent.Set<YW_DevicePacking>().FirstOrDefault(p => p.IsDelete == 0 && p.Id == req.Id);
  117. //if (devicePacking == null)
  118. //{
  119. // return new ApiResult(ApiStatusCode.InvalidParameter, "您修改的记录不存在");
  120. //}
  121. devicePacking.Capacity = req.Capacity;
  122. devicePacking.Stock = req.Stock;
  123. devicePacking.ModifyDT = DateTime.Now;
  124. devicePacking.ModifyBY = session.UserId;
  125. }
  126. // 具体执行
  127. _dbContent.SaveChanges();
  128. return new ApiResult();
  129. }
  130. }
  131. }