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
{
///
///
///
public class DeviceService: IDeviceService
{
private readonly IDbContext _dbContent;
private readonly IUserService _userService;
///
///
///
///
public DeviceService(
IDbContext dbContent,
IUserService userService)
{
_dbContent = dbContent;
_userService = userService;
}
///
/// 获取设备售卖的商品
///
///
public ApiResult GetDeviceProduct()
{
var session = _userService.GetLoginSession();
var result = (from dp in _dbContent.Set().Where(q => q.IsDelete == 0)
join p in _dbContent.Set().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(result);
}
///
/// 设备商品保存
///
///
///
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().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().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().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();
}
///
/// 获取设备正在使用的包装盒表
///
///
public ApiResult GetDevicePacking()
{
var session = _userService.GetLoginSession();
var result = (from dp in _dbContent.Set().Where(q => q.IsDelete == 0)
join p in _dbContent.Set().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(result);
}
///
/// 设备包装盒表
///
///
///
///
///
public PagedApiResult GetDeviceCanUsePackings(string kw = "", int skip = 0, int limit = 1)
{
var query = _dbContent.Set().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 result = new List();
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(result, total, skip, limit);
}
///
/// 设备包装盒保存
///
///
///
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().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().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().Add(devicePacking);
}
else
{
// 修改
devicePacking.Capacity = req.Capacity;
devicePacking.Stock = req.Stock;
devicePacking.ModifyDT = DateTime.Now;
devicePacking.ModifyBY = session.UserId;
}
// 具体执行
_dbContent.SaveChanges();
return new ApiResult();
}
}
}