|
- using BPA.SAAS.Manage.Application.DataBase.Dtos.Batching;
- using BPA.SAAS.Manage.Application.DataBase.Dtos.Goods;
- using BPA.SAAS.Manage.Application.DataBase.Dtos.GoodsAttribute;
- using BPA.SAAS.Manage.Application.DataBase.Interface;
- using BPA.SAAS.Manage.Comm.Const;
- using BPA.SAAS.Manage.Comm.Enum;
- using BPA.SAAS.Manage.Core.Base;
- using BPA.SAAS.Manage.Core.DataBase;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace BPA.SAAS.Manage.Application.DataBase.Services
- {
- public class GoodsService: IGoodsService, ITransient
- {
- private readonly ISqlSugarClient _db;
- public GoodsService(ISqlSugarClient db)
- {
- _db=db;
- }
- /// <summary>
- /// 分页查询
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- public async Task<PageUtil> GetGoodsPage(GoodsQueryDto dto)
- {
- List<IConditionalModel> conModels = new List<IConditionalModel>();
- string groupId = App.User.FindFirst(ClaimConst.GroupId)?.Value;
- #region 条件查询
- if (!string.IsNullOrEmpty(dto.Name))
- {
- conModels.Add(new ConditionalModel() { FieldName = "a.GoodsName", ConditionalType = ConditionalType.Like, FieldValue = dto.Name });
- }
-
- if (!string.IsNullOrEmpty(dto.GoodsTypeId))
- {
- conModels.Add(new ConditionalModel() { FieldName = "a.GoodsTypeId", ConditionalType = ConditionalType.Equal, FieldValue = dto.GoodsTypeId });
- }
-
- if (!string.IsNullOrEmpty(dto.Status))
- {
- conModels.Add(new ConditionalModel() { FieldName = "a.Status", ConditionalType = ConditionalType.Like, FieldValue = dto.Status });
- }
-
- #endregion
-
- RefAsync<int> total = 0;
-
-
- var res =await _db.Queryable<BPA_GoodsInfo, BPA_GoodsType>((a, b) => new JoinQueryInfos(
- JoinType.Left, a.GoodsTypeId == b.Id
- ))
- .Where((a, b) => a.IsDeleted == 0)
- .WhereIF(!string.IsNullOrWhiteSpace(dto.Code), (a, b) => a.Code.Contains(dto.Code))
- // .WhereIF(dto.CreateAt.HasValue, (a, b) => SqlFunc.DateIsSame(a.CreateAt, Convert.ToDateTime(dto.CreateAt), DateType.Day))
-
- .Where(conModels)
- .OrderBy(a => a.CreateAt, OrderByType.Desc)
- .Select((a, b) => new
- {
- Id = a.Id,
- Code = a.Code,
- Name = a.Name,
- Price = a.Price,
- ImgUrl = a.ImgUrl,
- Status = a.Status,
- GoodsTypeId = b.IsDeleted == 0 ? a.GoodsTypeId : "",
- GoodsTypeName = b.IsDeleted == 0 ? b.Name : "",
- Remark = a.Descritption,
- IsDeleted = a.IsDeleted,
-
- // CreateAt = a.CreateAt,
- GoodsUintId = a.GoodsUintId,
- ForeignKeyRe = a.ForeignKeyRe,
- Design = a.Design,
- IsWeigh = a.IsWeigh,
- DefaultMate = a.DefaultMate,
- IsAttrubute = a.IsAttrubute
- })
-
- .ToPageListAsync(dto.Current, dto.PageSize, total);
- PageUtil util = new PageUtil()
- {
- Total = total,
- Data = res
- };
- return util;
- }
- public async Task<List<BPA_GoodsInfo>> GetGoodsList()
- {
- var res = await _db.Queryable<BPA_GoodsInfo>().ToListAsync();
- return res;
- }
- /// <summary>
- /// 添加商品
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- public async Task<bool> AddGoods(GoodsDto dto)
- {
- if (string.IsNullOrWhiteSpace(dto.Id))
- {
- var resEntity = new BPA_GoodsInfo();
- resEntity.Id = Guid.NewGuid().ToString();
- resEntity.GoodsTypeId = dto.GoodsTypeId;
- resEntity.Name = dto.Name;
- resEntity.Descritption = dto.Descritption;
- resEntity.ImgUrl = dto.ImgUrl;
- resEntity.Price = dto.Price;
- resEntity.IsWeigh = dto.IsWeigh == "false" ? 0 : 1;
- resEntity.GoodsUintId = dto.GoodsUintId;
- resEntity.IsAttrubute = Convert.ToBoolean(dto.IsAttrubute);
- resEntity.Code = GetNumber2(8);
- var data = _db.Queryable<BPA_GoodsInfo>().Max(x => x.AutoKey);
- if (data == 0) data = 1000;
- else data = data + 1;
- resEntity.AutoKey = data;
- var res = await _db.Insertable(resEntity).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
- return res > 0;
- }
- else
- {
- return await UpdateGoods(dto);
- }
-
- }
-
- /// <summary>
- /// 更新商品
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- public async Task<bool> UpdateGoods(GoodsDto dto)
- {
- // 查询数据库中是否存在未删除的商品类型
- var resEntity = _db.Queryable<BPA_GoodsInfo>().Where(it => it.IsDeleted == 0).First(it => it.Id == dto.Id);
- if (null == resEntity)
- {
- throw Oops.Oh("商品不存在");
- }
- resEntity.GoodsTypeId = dto.GoodsTypeId;
- resEntity.Name = dto.Name;
- resEntity.Descritption = dto.Descritption;
- resEntity.ImgUrl = dto.ImgUrl;
- resEntity.Price = dto.Price;
- resEntity.IsWeigh = dto.IsWeigh=="false"?0:1;
- resEntity.GoodsUintId = dto.GoodsUintId;
- resEntity.IsAttrubute = Convert.ToBoolean(dto.IsAttrubute);
- if (!string.IsNullOrWhiteSpace(dto.Status))
- {
- resEntity.Status = (CommonStatus)Enum.ToObject(typeof(CommonStatus), int.Parse(dto.Status));
- }
-
- var res = await _db.Updateable(resEntity).ExecuteCommandAsync();
- return res > 0;
- }
- /// <summary>
- /// 删除商品
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public async Task<bool> DeleteGoods(string id)
- {
- var goods = _db.Queryable<BPA_GoodsInfo>().Where(x => x.Id== id).First();
- if (goods == null) throw Oops.Oh("商品不存在");
- goods.IsDeleted = 1;
- var res = await _db.Updateable(goods).ExecuteCommandAsync();
- return res > 0;
- }
- /// <summary>
- /// 查询商品单位
- /// </summary>
- /// <returns></returns>
- public async Task<List<dynamic>> GetGoodsUintList()
- {
- var res = await _db.Queryable<BPA_GoodsUint>().Select<dynamic>(x => new
-
- {
- id = x.Id,
- name = x.Name
- }).ToListAsync();
- return res;
- }
- /// <summary>
- /// 添加商品单位
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- public async Task<bool> AddGoodsUint(GoodsUintDto dto)
- {
- var groupId = App.User?.FindFirst(ClaimConst.GroupId)?.Value;
- var productCode = _db.Queryable<BPA_GoodsUint>().Where(x => x.GroupId == groupId && x.Name == dto.Name).ToList();
- if (productCode.Count() > 0)
- {
- throw Oops.Oh("商品单位已存在");
- }
- try
- {
- BPA_GoodsUint bPA_Product = new BPA_GoodsUint();
- bPA_Product.Name = dto.Name;
- var res = await _db.Insertable(bPA_Product).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
- return res > 0;
- }
- catch (Exception ex)
- {
- throw Oops.Oh("添加失败");
- }
- }
- /// <summary>
- /// 商品配方
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- public async Task<PageUtil> GetGoodsBomPageAsync(OrtherGoodsQueryDto dto)
- {
- RefAsync<int> total = 0;
- var res =await _db.Queryable<BPA_Bom, BPA_GoodsBom>((a, b) => new JoinQueryInfos(
- JoinType.Left, a.Id == b.BomId
- ))
- .WhereIF(!string.IsNullOrWhiteSpace(dto.GoodsId), (a, b) => b.Goods_Id == dto.GoodsId)
- .Select((a, b) => new
- {
- Id = b.Id,
- Name = a.Name,
- BomId= b.BomId,
- IsMain = a.IsMain
- })
- .ToPageListAsync(dto.Current, dto.PageSize, total);
- PageUtil util = new PageUtil()
- {
- Total = total,
- Data = res
- };
- return util;
- }
- /// <summary>
- /// 删除商品配方
- /// </summary>
- /// <param name="Ids"></param>
- /// <returns></returns>
- public async Task<bool> BatchDelGoodsBomAsync(string Ids)
- {
- // var GoodsBom=_db.Queryable<BPA_GoodsBom>().Where(x => x.Id == Ids).First();
-
- // 查询数据库中是否存在未删除的商品
- var res = _db.Deleteable<BPA_GoodsBom>().In(Ids).ExecuteCommand();
- return res > 0;
- }
- /// <summary>
- /// 添加商品配方
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- public async Task<bool> AddGoodsBomAttribute(GoodsBomAttributeDto dto)
- {
- _db.Ado.BeginTran();
- try
- {
- string groupId = App.User.FindFirst(ClaimConst.GroupId)?.Value;
- var sortMax = _db.Queryable<BPA_Bom>().Max(x => x.Sort);
- BPA_Bom newbPA_BOM = new BPA_Bom
- {
- Name = dto.BomName,
- Code = DateTime.Now.ToString("yyyyMMddhhmmsss"),
- IsMain = dto.BomType == "1" ? true : false,
- Sort = sortMax + 1,
- Id = Guid.NewGuid().ToString(),
- CreateAt = DateTime.Now,
- Status = CommonStatus.ENABLE,
- GroupId = groupId
- };
- //添加配方
- var res = _db.Insertable(newbPA_BOM).CallEntityMethod(m => m.Create()).ExecuteCommand();
- //添加配方分类
- var list = dto.BomtypeList.Select(item => new BPA_BomTypeInfo() { BomId = newbPA_BOM.Id, BomTypeId = item }).ToList();
- _db.Insertable(list).CallEntityMethod(m => m.Create()).ExecuteCommand();
-
- BPA_GoodsBom bom = new BPA_GoodsBom();
- bom.Goods_Id = dto.GoodsId;
- bom.BomId = newbPA_BOM.Id;
- bom.Status = CommonStatus.ENABLE;
- //添加商品配方关系
- _db.Insertable(bom).CallEntityMethod(m => m.Create()).ExecuteCommand();
- List<BPA_BomEntry> BOMEntryList = new();
- if (dto.Mate.Count > 0)
- {
- for (int i = 0; i < dto.Mate.Count; i++)
- {
- BPA_BomEntry newbPA_BOMEnty = new BPA_BomEntry
- {
- BatchingId = dto.Mate[i].batchingId,
- //BatchingKey = dto.Mate[i].AutoKey,
- BomQty = dto.Mate[i].dosage,
- BomId = newbPA_BOM.Id,
- Status = CommonStatus.ENABLE,
- IsReplace = false,
- };
- BOMEntryList.Add(newbPA_BOMEnty);
- }
- }
- //添加配方物料关系
- _db.Insertable(BOMEntryList).CallEntityMethod(m => m.Create()).ExecuteCommand();
- BPA_BomAttributeValueRe bPA_BomAttributeValueRe = new();
- bPA_BomAttributeValueRe.Id = Guid.NewGuid().ToString();
- bPA_BomAttributeValueRe.GoodsId = dto.GoodsId;
- bPA_BomAttributeValueRe.BoomId = newbPA_BOM.Id;
- bPA_BomAttributeValueRe.GoodsAttributeValueId = string.Join(",", dto.Shuxing);
- //添加商品配方属性关系
- _db.Insertable(bPA_BomAttributeValueRe).ExecuteCommand();
- _db.Ado.CommitTran();
- return true;
- }
- catch (Exception e)
- {
- _db.Ado.RollbackTran();
- throw Oops.Oh("添加失败,失败信息:" + e.Message);
- }
- }
- public async Task<bool> AddGoodsBom(GoodsBomDto dto)
- {
- var res = false;
- List<BPA_GoodsBom> list = new();
- if (dto.BomId.Count() > 0)
- {
- for (int i = 0; i < dto.BomId.Count; i++)
- {
- BPA_GoodsBom goodsBom = new();
- goodsBom.BomId = dto.BomId[i];
- goodsBom.Goods_Id= dto.GoodsId;
- goodsBom.Status = CommonStatus.ENABLE;
- list.Add(goodsBom);
- }
- }
- if (list.Count>0)
- {
- res=await _db.Insertable(list).CallEntityMethod(m => m.Create()).ExecuteCommandAsync()>0;
- }
- return res;
- }
- private string GetNumber2(int Length = 10)
- {
- byte[] buffer = Guid.NewGuid().ToByteArray();
- var ram = BitConverter.ToInt64(buffer, 0);
- var str = string.Format("{0}", ram.ToString().Substring(0, Length));
- return str;
- }
- }
- }
|