DeviceService.cs 8.9 KB

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