123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- 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>
- /// <param name="kw"></param>
- /// <param name="skip"></param>
- /// <param name="limit"></param>
- /// <returns></returns>
- public PagedApiResult<DevicePackingResponseDto> GetDevicePackings(string kw = "", int skip = 0, int limit = 1)
- {
- var session = _userService.GetLoginSession();
- var query = from p in _dbContent.Set<YW_Packing>().Where(q => q.IsDelete == 0)
- join dp in _dbContent.Set<YW_DevicePacking>().Where(q => q.IsDelete == 0 && q.DeviceId == session.UserId) on p.Id equals dp.PackingId into temp
- from dpp in temp.DefaultIfEmpty()
- select new
- {
- Packing = p,
- DevicePacking = dpp
- };
- // kw搜索
- query = query.WhereIf(!string.IsNullOrWhiteSpace(kw), p => p.Packing.Name.Contains(kw));
- // total
- var total = query.Count();
- // 查询结果
- var queryResult = query.OrderByDescending(p => p.Packing.CreateDT).Skip(skip).Take(limit).ToList();
- List<DevicePackingResponseDto> result = new List<DevicePackingResponseDto>();
- queryResult.ForEach(item =>
- {
- // 图片转换
- // current.Img = ;
- result.Add(new DevicePackingResponseDto()
- {
- Id = item.DevicePacking?.Id ?? string.Empty,
- PackingId = item.Packing.Id,
- PackingName = item.Packing.Name,
- PackingImg = $"{ProductService.ImagePrefix}{item.Packing.Img}",
- PackingMin = item.Packing.Min,
- PackingMax = item.Packing.Max,
- Capacity = item.DevicePacking?.Capacity ?? 1,
- Stock = item.DevicePacking?.Stock ?? 0,
- });
- });
- return new PagedApiResult<DevicePackingResponseDto>(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 session = _userService.GetLoginSession();
- // 每个设备,对于每一种包装只能新增一次,其余的都只能是在上面修改库存
- var devicePacking = _dbContent.Set<YW_DevicePacking>().FirstOrDefault(p => p.IsDelete == 0 && p.DeviceId == session.UserId && p.PackingId == req.PackingId);
- if (devicePacking == null && string.IsNullOrWhiteSpace(req.Id))
- {
- // 新增
- var packing = _dbContent.Set<YW_Packing>().FirstOrDefault(p => p.IsDelete == 0 && p.Id == req.PackingId);
- if (packing == null)
- {
- return new ApiResult(ApiStatusCode.InvalidParameter, "您选择的包装盒数据不存在");
- }
- 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
- {
- // 修改
- //var devicePacking = _dbContent.Set<YW_DevicePacking>().FirstOrDefault(p => p.IsDelete == 0 && p.Id == req.Id);
- //if (devicePacking == null)
- //{
- // return new ApiResult(ApiStatusCode.InvalidParameter, "您修改的记录不存在");
- //}
- devicePacking.Capacity = req.Capacity;
- devicePacking.Stock = req.Stock;
- devicePacking.ModifyDT = DateTime.Now;
- devicePacking.ModifyBY = session.UserId;
- }
- // 具体执行
- _dbContent.SaveChanges();
- return new ApiResult();
- }
- }
- }
|