123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- using Ant.Core.SqlServer;
- using Ant.Core.Utils;
- using Ant.Core.WebApi.Enum;
- using Ant.Core.WebApi.Model;
- using Central.Control.WebApi.DbEntity;
- using Central.Control.WebApi.EFDbContext;
- using Central.Control.WebApi.Models.Request;
- using Central.Control.WebApi.Models.Response;
- using Central.Control.WebApi.Service.Interface;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace Central.Control.WebApi.Service
- {
- /// <summary>
- ///
- /// </summary>
- public class DeviceService: IDeviceService
- {
- private readonly IDbContext _dbContent;
- private readonly IUserService _userService;
- /// <summary>
- ///
- /// </summary>
- /// <param name="dbContent"></param>
- public DeviceService(
- IDbContext dbContent,
- IUserService userService)
- {
- _dbContent = dbContent;
- _userService = userService;
- }
- /// <summary>
- /// 获取设备售卖的商品
- /// </summary>
- /// <returns></returns>
- public ApiResult<DeviceProductResponseDto> GetDeviceProduct()
- {
- var session = _userService.GetLoginSession();
- var result = (from dp in _dbContent.Set<YW_DeviceProduct>().Where(q => q.IsDelete == 0)
- join p in _dbContent.Set<YW_Product>().Where(q => q.IsDelete == 0) on dp.ProductId equals p.Id
- where dp.DeviceId == session.UserId
- select new DeviceProductResponseDto
- {
- Id = dp.Id,
- ProductId = p.Id,
- ProductName = p.Name,
- Code = p.Code,
- Img = ProductService.ImagePrefix + p.Img,
- Info = p.Info,
- Price = dp.Price,
- Stock = dp.Stock
- }).FirstOrDefault();
- return new ApiResult<DeviceProductResponseDto>(result);
- }
- /// <summary>
- /// 设备商品保存
- /// </summary>
- /// <param name="req"></param>
- /// <returns></returns>
- public ApiResult SaveDeviceProduct(DeviceProductRequestDto req)
- {
- if (req == null
- || string.IsNullOrWhiteSpace(req.ProductId)
- || req.Price <= 0
- || req.Stock < 0)
- {
- return new ApiResult(ApiStatusCode.InvalidParameter, "您设置的参数不正确,请检查");
- }
-
- var session = _userService.GetLoginSession();
- // 获取商品
- var product = _dbContent.Set<YW_Product>().FirstOrDefault(p => p.IsDelete == 0 && p.Id == req.ProductId);
- if (product == null)
- {
- return new ApiResult(ApiStatusCode.InvalidParameter, "您选择的商品不存在");
- }
- if (req.Price < product.BuyingPrice)
- {
- return new ApiResult(ApiStatusCode.InvalidParameter, $"您设置的价格低于成本价(¥{product.BuyingPrice}),请重新设置价格");
- }
- // 查询出原来就有的信息,一个设备只能卖一件商品
- var deviceProduct = _dbContent.Set<YW_DeviceProduct>().FirstOrDefault(p => p.IsDelete == 0 && p.DeviceId == session.UserId);
- if (deviceProduct == null)
- {
- // 新增
- deviceProduct = new YW_DeviceProduct()
- {
- Id = IdGenerator.NewId(),
- DeviceId = session.UserId,
- ProductId = product.Id,
- Price = req.Price,
- Stock = req.Stock,
- CreateBY = session.UserId
- };
- _dbContent.Set<YW_DeviceProduct>().Add(deviceProduct);
- }
- else
- {
- // 修改
- deviceProduct.ProductId = product.Id;
- deviceProduct.Price = req.Price;
- deviceProduct.Stock = req.Stock;
- deviceProduct.ModifyBY = session.UserId;
- deviceProduct.ModifyDT = DateTime.Now;
- }
- // 执行保存
- _dbContent.SaveChanges();
- return new ApiResult();
- }
- /// <summary>
- /// 获取设备正在使用的包装盒表
- /// </summary>
- /// <returns></returns>
- public ApiResult<DevicePackingResponseDto> GetDevicePacking()
- {
- var session = _userService.GetLoginSession();
- var result = (from dp in _dbContent.Set<YW_DevicePacking>().Where(q => q.IsDelete == 0)
- join p in _dbContent.Set<YW_Packing>().Where(q => q.IsDelete == 0) on dp.PackingId equals p.Id
- where dp.DeviceId == session.UserId
- select new DevicePackingResponseDto
- {
- Id = dp.Id,
- PackingId = p.Id,
- PackingName = p.Name,
- PackingImg = ProductService.ImagePrefix + p.Img,
- PackingMin = p.Min,
- PackingMax = p.Max,
- Capacity = dp.Capacity,
- Stock = dp.Stock
- }).FirstOrDefault();
- return new ApiResult<DevicePackingResponseDto>(result);
- }
- /// <summary>
- /// 设备包装盒表
- /// </summary>
- /// <param name="kw"></param>
- /// <param name="skip"></param>
- /// <param name="limit"></param>
- /// <returns></returns>
- public PagedApiResult<DeviceCanUsePackingResponseDto> GetDeviceCanUsePackings(string kw = "", int skip = 0, int limit = 1)
- {
- var query = _dbContent.Set<YW_Packing>().Where(q => q.IsDelete == 0);
- // kw搜索
- query = query.WhereIf(!string.IsNullOrWhiteSpace(kw), p => p.Name.Contains(kw));
- // total
- var total = query.Count();
- // 查询结果
- var queryResult = query.OrderByDescending(p => p.CreateDT).Skip(skip).Take(limit).ToList();
- List<DeviceCanUsePackingResponseDto> result = new List<DeviceCanUsePackingResponseDto>();
- queryResult.ForEach(item =>
- {
- // 图片转换
- // current.Img = ;
- result.Add(new DeviceCanUsePackingResponseDto()
- {
- PackingId = item.Id,
- PackingName = item.Name,
- PackingImg = $"{ProductService.ImagePrefix}{item.Img}",
- PackingMin = item.Min,
- PackingMax = item.Max,
- });
- });
- return new PagedApiResult<DeviceCanUsePackingResponseDto>(result, total, skip, limit);
- }
- /// <summary>
- /// 设备包装盒保存
- /// </summary>
- /// <param name="req"></param>
- /// <returns></returns>
- public ApiResult SaveDevicePacking(DevicePackingRequestDto req)
- {
- if (req == null
- || string.IsNullOrWhiteSpace(req.PackingId)
- || req.Stock < 0
- || req.Capacity <= 0)
- {
- return new ApiResult(ApiStatusCode.InvalidParameter, "请求参数不正确,请检查");
- }
- // 获取原始包装盒数据
- var packing = _dbContent.Set<YW_Packing>().FirstOrDefault(p => p.IsDelete == 0 && p.Id == req.PackingId);
- if (packing == null)
- {
- return new ApiResult(ApiStatusCode.InvalidParameter, "您选择的包装盒数据不存在");
- }
- var session = _userService.GetLoginSession();
- // 每个设备,只能用一一种包装盒
- var devicePacking = _dbContent.Set<YW_DevicePacking>().FirstOrDefault(p => p.IsDelete == 0 && p.DeviceId == session.UserId);
- if (devicePacking == null)
- {
- // 新增
- devicePacking = new YW_DevicePacking()
- {
- Id = IdGenerator.NewId(),
- PackingId = packing.Id,
- DeviceId = session.UserId,
- Capacity = req.Capacity,
- Stock = req.Stock,
- CreateBY = session.UserId
- };
- _dbContent.Set<YW_DevicePacking>().Add(devicePacking);
- }
- else
- {
- // 修改
- devicePacking.Capacity = req.Capacity;
- devicePacking.Stock = req.Stock;
- devicePacking.ModifyDT = DateTime.Now;
- devicePacking.ModifyBY = session.UserId;
- }
- // 具体执行
- _dbContent.SaveChanges();
- return new ApiResult();
- }
- }
- }
|