基础服务api
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

140 lines
4.7 KiB

  1. using BPA.SAAS.Manage.Application.DataBase.Dtos.Bom;
  2. using BPA.SAAS.Manage.Application.DataBase.Interface;
  3. using BPA.SAAS.Manage.Comm.Enum;
  4. using BPA.SAAS.Manage.Core.DataBase;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace BPA.SAAS.Manage.Application.DataBase.Services
  11. {
  12. public class BomService: IBomService,ITransient
  13. {
  14. private readonly ISqlSugarClient _db;
  15. public BomService(ISqlSugarClient db)
  16. {
  17. _db=db;
  18. }
  19. /// <summary>
  20. /// 配方分类列表
  21. /// </summary>
  22. /// <returns></returns>
  23. public async Task<List<BomSelectList>> GetBomList()
  24. {
  25. var res = await _db.Queryable<BPA_Bom>().Select(x => new BomSelectList
  26. {
  27. Id = x.Id,
  28. Name = x.Name
  29. }).ToListAsync();
  30. return res;
  31. }
  32. /// <summary>
  33. /// 添加
  34. /// </summary>
  35. /// <param name="dto"></param>
  36. /// <returns></returns>
  37. public async Task<bool> AddBom(BomInputDto dto)
  38. {
  39. BPA_Bom newbPA_BOM = new BPA_Bom
  40. {
  41. Name = dto.Name,
  42. Code = GetNumber2(),
  43. IsMain = dto.IsMain,
  44. Sort = dto.Sort,
  45. };
  46. var bom =await _db.Insertable(newbPA_BOM).CallEntityMethod(m => m.Create()).ExecuteReturnEntityAsync();
  47. //添加配方分类
  48. var list = dto.BomTypeIds.Select(item => new BPA_BomTypeInfo() { BomId = bom.Id, BomTypeId = item }).ToList();
  49. var res =await _db.Insertable(list).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  50. return res > 0;
  51. }
  52. /// <summary>
  53. /// 更新
  54. /// </summary>
  55. /// <param name="dto"></param>
  56. /// <returns></returns>
  57. public async Task<bool> UpdateBom(BomInputDto dto)
  58. {
  59. // 查询数据库中是否存在未删除的商品类型
  60. var resEntity = _db.Queryable<BPA_Bom>().Where(it => it.IsDeleted == 0).First(it => it.Id == dto.Id);
  61. if (null == resEntity)
  62. {
  63. return false;
  64. }
  65. resEntity.Name = dto.Name;
  66. resEntity.IsMain = dto.IsMain;
  67. resEntity.Sort = dto.Sort;
  68. var res =await _db.Updateable(resEntity).IgnoreColumns(ignoreAllNullColumns: true) .ExecuteCommandAsync();
  69. return res > 0;
  70. }
  71. /// <summary>
  72. /// 删除
  73. /// </summary>
  74. /// <param name="id"></param>
  75. /// <returns></returns>
  76. public async Task<bool> DeleteBom(string id)
  77. {
  78. try
  79. {
  80. _db.Ado.BeginTran();
  81. var resEntity = _db.Queryable<BPA_Bom>().Where(it => it.IsDeleted == 0).First(it => it.Id == id);
  82. if (null == resEntity)
  83. {
  84. throw Oops.Oh("配方不存在");
  85. }
  86. resEntity.IsDeleted = 1;
  87. var res = await _db.Updateable(resEntity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
  88. var BomTypeInfo = _db.Queryable<BPA_BomTypeInfo>().Where(it => it.BomId == id).Select(x => x.Id).ToList();
  89. await _db.Deleteable<BPA_BomTypeInfo>(BomTypeInfo).ExecuteCommandAsync();
  90. _db.Ado.CommitTran();
  91. return res > 0;
  92. }
  93. catch (Exception)
  94. {
  95. _db.Ado.RollbackTran();
  96. throw Oops.Oh("删除失败"); ;
  97. }
  98. }
  99. /// <summary>
  100. /// 添加
  101. /// </summary>
  102. /// <param name="dto"></param>
  103. /// <returns></returns>
  104. public async Task<bool> AddBomType(AddBomTypeInputDto dto)
  105. {
  106. BPA_BomType newbPA_BOM = new BPA_BomType
  107. {
  108. Name = dto.Name
  109. };
  110. var res = await _db.Insertable(newbPA_BOM).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  111. return res > 0;
  112. }
  113. /// <summary>
  114. /// 配方分类列表
  115. /// </summary>
  116. /// <returns></returns>
  117. public async Task<List<dynamic>> GetBomTypeList()
  118. {
  119. var res = await _db.Queryable<BPA_BomType>().Select<dynamic>(x => new
  120. {
  121. id = x.Id,
  122. name = x.Name
  123. }).ToListAsync();
  124. return res;
  125. }
  126. private string GetNumber2(int Length = 10)
  127. {
  128. byte[] buffer = Guid.NewGuid().ToByteArray();
  129. var ram = BitConverter.ToInt64(buffer, 0);
  130. var str = string.Format("{0}", ram.ToString().Substring(0, Length));
  131. return str;
  132. }
  133. }
  134. }