基础服务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.
 
 

224 lines
8.3 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 NPOI.SS.Formula.Functions;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace BPA.SAAS.Manage.Application.DataBase.Services
  12. {
  13. public class BomService: IBomService,ITransient
  14. {
  15. private readonly ISqlSugarClient _db;
  16. public BomService(ISqlSugarClient db)
  17. {
  18. _db=db;
  19. }
  20. /// <summary>
  21. /// 配方分类列表
  22. /// </summary>
  23. /// <returns></returns>
  24. public async Task<List<BomSelectList>> GetBomList()
  25. {
  26. var res = await _db.Queryable<BPA_Bom>().Select(x => new BomSelectList
  27. {
  28. Id = x.Id,
  29. Name = x.Name
  30. }).ToListAsync();
  31. return res;
  32. }
  33. /// <summary>
  34. /// 根据配方id查询配方信息
  35. /// </summary>
  36. /// <param name="bomId"></param>
  37. /// <returns></returns>
  38. public async Task<BomList> GetBomList(string bomId)
  39. {
  40. var res = await _db.Queryable<BPA_Bom>().Where(x=>x.Id== bomId).Select(x => new BomList
  41. {
  42. Id = x.Id,
  43. Name = x.Name,
  44. Code=x.Code,
  45. IsMain=x.IsMain,
  46. Sort=x.Sort,
  47. }).Mapper(x =>
  48. {
  49. x.BomTypeIds = _db.Queryable<BPA_BomTypeInfo>().Where(t => t.BomId == x.Id).Select(t => t.BomTypeId).ToList();
  50. x.BomEntry = _db.Queryable<BPA_BomEntry>().Where(t => t.BomId == x.Id).ToList();
  51. }).FirstAsync();
  52. return res;
  53. }
  54. /// <summary>
  55. /// 添加
  56. /// </summary>
  57. /// <param name="dto"></param>
  58. /// <returns></returns>
  59. public async Task<bool> AddBom(BomInputDto dto)
  60. {
  61. BPA_Bom newbPA_BOM = new BPA_Bom
  62. {
  63. Name = dto.Name,
  64. Code = GetNumber2(),
  65. IsMain = dto.IsMain,
  66. Sort = dto.Sort,
  67. };
  68. var bom =await _db.Insertable(newbPA_BOM).CallEntityMethod(m => m.Create()).ExecuteReturnEntityAsync();
  69. //添加配方分类
  70. var list = dto.BomTypeIds.Select(item => new BPA_BomTypeInfo() { BomId = bom.Id, BomTypeId = item }).ToList();
  71. var res =await _db.Insertable(list).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  72. return res > 0;
  73. }
  74. /// <summary>
  75. /// 更新
  76. /// </summary>
  77. /// <param name="dto"></param>
  78. /// <returns></returns>
  79. public async Task<bool> UpdateBom(BomInputDto dto)
  80. {
  81. try
  82. {
  83. _db.Ado.BeginTran();
  84. // 查询数据库中是否存在未删除的商品类型
  85. var resEntity = _db.Queryable<BPA_Bom>().Where(it => it.IsDeleted == 0).First(it => it.Id == dto.Id);
  86. if (null == resEntity)
  87. {
  88. return false;
  89. }
  90. resEntity.Name = dto.Name;
  91. resEntity.IsMain = dto.IsMain;
  92. resEntity.Sort = dto.Sort;
  93. var res = await _db.Updateable(resEntity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
  94. var bomType = _db.Queryable<BPA_BomTypeInfo>().Where(it => it.BomId == dto.Id).ToList();
  95. await _db.Deleteable(bomType).ExecuteCommandAsync();
  96. List<BPA_BomTypeInfo> list = new();
  97. for (int i = 0; i < dto.BomTypeIds.Count; i++)
  98. {
  99. BPA_BomTypeInfo bomTypeInfo = new();
  100. bomTypeInfo.BomId = dto.Id;
  101. bomTypeInfo.BomTypeId = dto.BomTypeIds[i];
  102. bomTypeInfo.Status = CommonStatus.ENABLE;
  103. list.Add(bomTypeInfo);
  104. }
  105. await _db.Insertable(list).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  106. _db.Ado.CommitTran();
  107. return res > 0;
  108. }
  109. catch (Exception e)
  110. {
  111. _db.Ado.RollbackTran();
  112. throw Oops.Oh("更新失败,失败信息:"+e.Message);
  113. }
  114. }
  115. /// <summary>
  116. /// 更新配方详情
  117. /// </summary>
  118. /// <param name="dto"></param>
  119. /// <returns></returns>
  120. public async Task<bool> UpdateBomBatcing(BomBatcingInputDto dto)
  121. {
  122. try
  123. {
  124. _db.Ado.BeginTran();
  125. // 查询数据库中是否存在未删除的商品类型
  126. var resEntity = _db.Queryable<BPA_Bom>().Where(it => it.IsDeleted == 0).First(it => it.Id == dto.BomId);
  127. if (null == resEntity)
  128. {
  129. throw Oops.Oh("配方不存在");
  130. }
  131. var bomEntry = _db.Queryable<BPA_BomEntry>().Where(it => it.BomId == dto.BomId).ToList();
  132. await _db.Deleteable(bomEntry).ExecuteCommandAsync();
  133. List<BPA_BomEntry> list = new();
  134. for (int i = 0; i < dto.BomEntry.Count; i++)
  135. {
  136. BPA_BomEntry bomEntryInfo = new();
  137. bomEntryInfo.BomId = dto.BomEntry[i].BomId;
  138. bomEntryInfo.BatchingId = dto.BomEntry[i].BatchingId;
  139. bomEntryInfo.BomQty = dto.BomEntry[i].Dosage;
  140. bomEntryInfo.IsReplace = false;
  141. bomEntryInfo.Status = CommonStatus.ENABLE;
  142. list.Add(bomEntryInfo);
  143. }
  144. var res=await _db.Insertable(list).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  145. _db.Ado.CommitTran();
  146. return res > 0;
  147. }
  148. catch (Exception e)
  149. {
  150. _db.Ado.RollbackTran();
  151. throw Oops.Oh("更新失败,失败信息:" + e.Message);
  152. }
  153. }
  154. /// <summary>
  155. /// 删除
  156. /// </summary>
  157. /// <param name="id"></param>
  158. /// <returns></returns>
  159. public async Task<bool> DeleteBom(string id)
  160. {
  161. try
  162. {
  163. _db.Ado.BeginTran();
  164. var resEntity = _db.Queryable<BPA_Bom>().Where(it => it.IsDeleted == 0).First(it => it.Id == id);
  165. if (null == resEntity)
  166. {
  167. throw Oops.Oh("配方不存在");
  168. }
  169. resEntity.IsDeleted = 1;
  170. var res = await _db.Updateable(resEntity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
  171. var BomTypeInfo = _db.Queryable<BPA_BomTypeInfo>().Where(it => it.BomId == id).Select(x => x.Id).ToList();
  172. await _db.Deleteable<BPA_BomTypeInfo>(BomTypeInfo).ExecuteCommandAsync();
  173. _db.Ado.CommitTran();
  174. return res > 0;
  175. }
  176. catch (Exception)
  177. {
  178. _db.Ado.RollbackTran();
  179. throw Oops.Oh("删除失败"); ;
  180. }
  181. }
  182. /// <summary>
  183. /// 添加
  184. /// </summary>
  185. /// <param name="dto"></param>
  186. /// <returns></returns>
  187. public async Task<bool> AddBomType(AddBomTypeInputDto dto)
  188. {
  189. BPA_BomType newbPA_BOM = new BPA_BomType
  190. {
  191. Name = dto.Name
  192. };
  193. var res = await _db.Insertable(newbPA_BOM).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  194. return res > 0;
  195. }
  196. /// <summary>
  197. /// 配方分类列表
  198. /// </summary>
  199. /// <returns></returns>
  200. public async Task<List<dynamic>> GetBomTypeList()
  201. {
  202. var res = await _db.Queryable<BPA_BomType>().Select<dynamic>(x => new
  203. {
  204. id = x.Id,
  205. name = x.Name
  206. }).ToListAsync();
  207. return res;
  208. }
  209. private string GetNumber2(int Length = 10)
  210. {
  211. byte[] buffer = Guid.NewGuid().ToByteArray();
  212. var ram = BitConverter.ToInt64(buffer, 0);
  213. var str = string.Format("{0}", ram.ToString().Substring(0, Length));
  214. return str;
  215. }
  216. }
  217. }