DeviceService.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. /// <returns></returns>
  38. public ApiResult<DeviceProductResponseDto> GetDeviceProduct()
  39. {
  40. var session = _userService.GetLoginSession();
  41. var result = (from dp in _dbContent.Set<YW_DeviceProduct>().Where(q => q.IsDelete == 0)
  42. join p in _dbContent.Set<YW_Product>().Where(q => q.IsDelete == 0) on dp.ProductId equals p.Id
  43. where dp.DeviceId == session.UserId
  44. select new DeviceProductResponseDto
  45. {
  46. Id = dp.Id,
  47. ProductId = p.Id,
  48. ProductName = p.Name,
  49. Code = p.Code,
  50. Img = ProductService.ImagePrefix + p.Img,
  51. Info = p.Info,
  52. Price = dp.Price,
  53. Stock = dp.Stock
  54. }).FirstOrDefault();
  55. return new ApiResult<DeviceProductResponseDto>(result);
  56. }
  57. /// <summary>
  58. /// 设备商品保存
  59. /// </summary>
  60. /// <param name="req"></param>
  61. /// <returns></returns>
  62. public ApiResult SaveDeviceProduct(DeviceProductRequestDto req)
  63. {
  64. if (req == null
  65. || string.IsNullOrWhiteSpace(req.ProductId)
  66. || req.Price <= 0
  67. || req.Stock < 0)
  68. {
  69. return new ApiResult(ApiStatusCode.InvalidParameter, "您设置的参数不正确,请检查");
  70. }
  71. var session = _userService.GetLoginSession();
  72. // 获取商品
  73. var product = _dbContent.Set<YW_Product>().FirstOrDefault(p => p.IsDelete == 0 && p.Id == req.ProductId);
  74. if (product == null)
  75. {
  76. return new ApiResult(ApiStatusCode.InvalidParameter, "您选择的商品不存在");
  77. }
  78. if (req.Price < product.BuyingPrice)
  79. {
  80. return new ApiResult(ApiStatusCode.InvalidParameter, $"您设置的价格低于成本价(¥{product.BuyingPrice}),请重新设置价格");
  81. }
  82. // 查询出原来就有的信息,一个设备只能卖一件商品
  83. var deviceProduct = _dbContent.Set<YW_DeviceProduct>().FirstOrDefault(p => p.IsDelete == 0 && p.DeviceId == session.UserId);
  84. if (deviceProduct == null)
  85. {
  86. // 新增
  87. deviceProduct = new YW_DeviceProduct()
  88. {
  89. Id = IdGenerator.NewId(),
  90. DeviceId = session.UserId,
  91. ProductId = product.Id,
  92. Price = req.Price,
  93. Stock = req.Stock,
  94. CreateBY = session.UserId
  95. };
  96. _dbContent.Set<YW_DeviceProduct>().Add(deviceProduct);
  97. }
  98. else
  99. {
  100. // 修改
  101. deviceProduct.ProductId = product.Id;
  102. deviceProduct.Price = req.Price;
  103. deviceProduct.Stock = req.Stock;
  104. deviceProduct.ModifyBY = session.UserId;
  105. deviceProduct.ModifyDT = DateTime.Now;
  106. }
  107. // 执行保存
  108. _dbContent.SaveChanges();
  109. return new ApiResult();
  110. }
  111. /// <summary>
  112. /// 获取设备正在使用的包装盒表
  113. /// </summary>
  114. /// <returns></returns>
  115. public ApiResult<DevicePackingResponseDto> GetDevicePacking()
  116. {
  117. var session = _userService.GetLoginSession();
  118. var result = (from dp in _dbContent.Set<YW_DevicePacking>().Where(q => q.IsDelete == 0)
  119. join p in _dbContent.Set<YW_Packing>().Where(q => q.IsDelete == 0) on dp.PackingId equals p.Id
  120. where dp.DeviceId == session.UserId
  121. select new DevicePackingResponseDto
  122. {
  123. Id = dp.Id,
  124. PackingId = p.Id,
  125. PackingName = p.Name,
  126. PackingImg = ProductService.ImagePrefix + p.Img,
  127. PackingMin = p.Min,
  128. PackingMax = p.Max,
  129. Capacity = dp.Capacity,
  130. Stock = dp.Stock
  131. }).FirstOrDefault();
  132. return new ApiResult<DevicePackingResponseDto>(result);
  133. }
  134. /// <summary>
  135. /// 设备包装盒表
  136. /// </summary>
  137. /// <param name="kw"></param>
  138. /// <param name="skip"></param>
  139. /// <param name="limit"></param>
  140. /// <returns></returns>
  141. public PagedApiResult<DeviceCanUsePackingResponseDto> GetDeviceCanUsePackings(string kw = "", int skip = 0, int limit = 1)
  142. {
  143. var query = _dbContent.Set<YW_Packing>().Where(q => q.IsDelete == 0);
  144. // kw搜索
  145. query = query.WhereIf(!string.IsNullOrWhiteSpace(kw), p => p.Name.Contains(kw));
  146. // total
  147. var total = query.Count();
  148. // 查询结果
  149. var queryResult = query.OrderByDescending(p => p.CreateDT).Skip(skip).Take(limit).ToList();
  150. List<DeviceCanUsePackingResponseDto> result = new List<DeviceCanUsePackingResponseDto>();
  151. queryResult.ForEach(item =>
  152. {
  153. // 图片转换
  154. // current.Img = ;
  155. result.Add(new DeviceCanUsePackingResponseDto()
  156. {
  157. PackingId = item.Id,
  158. PackingName = item.Name,
  159. PackingImg = $"{ProductService.ImagePrefix}{item.Img}",
  160. PackingMin = item.Min,
  161. PackingMax = item.Max,
  162. });
  163. });
  164. return new PagedApiResult<DeviceCanUsePackingResponseDto>(result, total, skip, limit);
  165. }
  166. /// <summary>
  167. /// 设备包装盒保存
  168. /// </summary>
  169. /// <param name="req"></param>
  170. /// <returns></returns>
  171. public ApiResult SaveDevicePacking(DevicePackingRequestDto req)
  172. {
  173. if (req == null
  174. || string.IsNullOrWhiteSpace(req.PackingId)
  175. || req.Stock < 0
  176. || req.Capacity <= 0)
  177. {
  178. return new ApiResult(ApiStatusCode.InvalidParameter, "请求参数不正确,请检查");
  179. }
  180. // 获取原始包装盒数据
  181. var packing = _dbContent.Set<YW_Packing>().FirstOrDefault(p => p.IsDelete == 0 && p.Id == req.PackingId);
  182. if (packing == null)
  183. {
  184. return new ApiResult(ApiStatusCode.InvalidParameter, "您选择的包装盒数据不存在");
  185. }
  186. var session = _userService.GetLoginSession();
  187. // 每个设备,只能用一一种包装盒
  188. var devicePacking = _dbContent.Set<YW_DevicePacking>().FirstOrDefault(p => p.IsDelete == 0 && p.DeviceId == session.UserId);
  189. if (devicePacking == null)
  190. {
  191. // 新增
  192. devicePacking = new YW_DevicePacking()
  193. {
  194. Id = IdGenerator.NewId(),
  195. PackingId = packing.Id,
  196. DeviceId = session.UserId,
  197. Capacity = req.Capacity,
  198. Stock = req.Stock,
  199. CreateBY = session.UserId
  200. };
  201. _dbContent.Set<YW_DevicePacking>().Add(devicePacking);
  202. }
  203. else
  204. {
  205. // 修改
  206. devicePacking.Capacity = req.Capacity;
  207. devicePacking.Stock = req.Stock;
  208. devicePacking.ModifyDT = DateTime.Now;
  209. devicePacking.ModifyBY = session.UserId;
  210. }
  211. // 具体执行
  212. _dbContent.SaveChanges();
  213. return new ApiResult();
  214. }
  215. }
  216. }