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

BomService.cs 13 KiB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. using BPA.SAAS.Manage.Application.DataBase.Dtos.Bom;
  2. using BPA.SAAS.Manage.Application.DataBase.Dtos.GoodsAttribute;
  3. using BPA.SAAS.Manage.Application.DataBase.Interface;
  4. using BPA.SAAS.Manage.Comm.Enum;
  5. using BPA.SAAS.Manage.Core.Base;
  6. using BPA.SAAS.Manage.Core.DataBase;
  7. using NPOI.SS.Formula.Functions;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace BPA.SAAS.Manage.Application.DataBase.Services
  14. {
  15. public class BomService: IBomService,ITransient
  16. {
  17. private readonly ISqlSugarClient _db;
  18. public BomService(ISqlSugarClient db)
  19. {
  20. _db=db;
  21. }
  22. /// <summary>
  23. /// 配方分类列表
  24. /// </summary>
  25. /// <returns></returns>
  26. public async Task<List<BomSelectList>> GetBomList()
  27. {
  28. var res = await _db.Queryable<BPA_Bom>().Select(x => new BomSelectList
  29. {
  30. Id = x.Id,
  31. Name = x.Name
  32. }).ToListAsync();
  33. return res;
  34. }
  35. /// <summary>
  36. /// 根据配方id查询配方信息
  37. /// </summary>
  38. /// <param name="bomId"></param>
  39. /// <returns></returns>
  40. public async Task<BomList> GetBomList(string bomId)
  41. {
  42. var res = await _db.Queryable<BPA_Bom>().Where(x=>x.Id== bomId).Select(x => new BomList
  43. {
  44. Id = x.Id,
  45. Name = x.Name,
  46. Code=x.Code,
  47. IsMain=x.IsMain,
  48. Sort=x.Sort,
  49. }).Mapper(x =>
  50. {
  51. x.BomTypeIds = _db.Queryable<BPA_BomTypeInfo>().Where(t => t.BomId == x.Id).Select(t => t.BomTypeId).ToList();
  52. x.BomEntry = _db.Queryable<BPA_BomEntry>().Where(t => t.BomId == x.Id).ToList();
  53. }).FirstAsync();
  54. return res;
  55. }
  56. /// <summary>
  57. /// 添加
  58. /// </summary>
  59. /// <param name="dto"></param>
  60. /// <returns></returns>
  61. public async Task<bool> AddBom(BomInputDto dto)
  62. {
  63. BPA_Bom newbPA_BOM = new BPA_Bom
  64. {
  65. Name = dto.Name,
  66. Code = GetNumber2(),
  67. IsMain = dto.IsMain,
  68. Sort = dto.Sort,
  69. };
  70. var bom =await _db.Insertable(newbPA_BOM).CallEntityMethod(m => m.Create()).ExecuteReturnEntityAsync();
  71. //添加配方分类
  72. var list = dto.BomTypeIds.Select(item => new BPA_BomTypeInfo() { BomId = bom.Id, BomTypeId = item }).ToList();
  73. var res =await _db.Insertable(list).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  74. return res > 0;
  75. }
  76. /// <summary>
  77. /// 更新
  78. /// </summary>
  79. /// <param name="dto"></param>
  80. /// <returns></returns>
  81. public async Task<bool> UpdateBom(BomInputDto dto)
  82. {
  83. try
  84. {
  85. _db.Ado.BeginTran();
  86. // 查询数据库中是否存在未删除的商品类型
  87. var resEntity = _db.Queryable<BPA_Bom>().Where(it => it.IsDeleted == 0).First(it => it.Id == dto.Id);
  88. if (null == resEntity)
  89. {
  90. return false;
  91. }
  92. resEntity.Name = dto.Name;
  93. resEntity.IsMain = dto.IsMain;
  94. resEntity.Sort = dto.Sort;
  95. var res = await _db.Updateable(resEntity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
  96. var bomType = _db.Queryable<BPA_BomTypeInfo>().Where(it => it.BomId == dto.Id).ToList();
  97. await _db.Deleteable(bomType).ExecuteCommandAsync();
  98. List<BPA_BomTypeInfo> list = new();
  99. for (int i = 0; i < dto.BomTypeIds.Count; i++)
  100. {
  101. BPA_BomTypeInfo bomTypeInfo = new();
  102. bomTypeInfo.BomId = dto.Id;
  103. bomTypeInfo.BomTypeId = dto.BomTypeIds[i];
  104. bomTypeInfo.Status = CommonStatus.ENABLE;
  105. list.Add(bomTypeInfo);
  106. }
  107. await _db.Insertable(list).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  108. _db.Ado.CommitTran();
  109. return res > 0;
  110. }
  111. catch (Exception e)
  112. {
  113. _db.Ado.RollbackTran();
  114. throw Oops.Oh("更新失败,失败信息:"+e.Message);
  115. }
  116. }
  117. /// <summary>
  118. /// 更新配方详情
  119. /// </summary>
  120. /// <param name="dto"></param>
  121. /// <returns></returns>
  122. public async Task<bool> UpdateBomBatcing(BomBatcingInputDto dto)
  123. {
  124. try
  125. {
  126. _db.Ado.BeginTran();
  127. // 查询数据库中是否存在未删除的商品类型
  128. var resEntity = _db.Queryable<BPA_Bom>().Where(it => it.IsDeleted == 0).First(it => it.Id == dto.BomId);
  129. if (null == resEntity)
  130. {
  131. throw Oops.Oh("配方不存在");
  132. }
  133. var bomEntry = _db.Queryable<BPA_BomEntry>().Where(it => it.BomId == dto.BomId).ToList();
  134. await _db.Deleteable(bomEntry).ExecuteCommandAsync();
  135. List<BPA_BomEntry> list = new();
  136. for (int i = 0; i < dto.BomEntry.Count; i++)
  137. {
  138. BPA_BomEntry bomEntryInfo = new();
  139. bomEntryInfo.BomId = dto.BomEntry[i].BomId;
  140. bomEntryInfo.BatchingId = dto.BomEntry[i].BatchingId;
  141. bomEntryInfo.BomQty = dto.BomEntry[i].Dosage;
  142. bomEntryInfo.IsReplace = false;
  143. bomEntryInfo.Status = CommonStatus.ENABLE;
  144. list.Add(bomEntryInfo);
  145. }
  146. var res=await _db.Insertable(list).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  147. _db.Ado.CommitTran();
  148. return res > 0;
  149. }
  150. catch (Exception e)
  151. {
  152. _db.Ado.RollbackTran();
  153. throw Oops.Oh("更新失败,失败信息:" + e.Message);
  154. }
  155. }
  156. /// <summary>
  157. /// 删除
  158. /// </summary>
  159. /// <param name="id"></param>
  160. /// <returns></returns>
  161. public async Task<bool> DeleteBom(string id)
  162. {
  163. try
  164. {
  165. _db.Ado.BeginTran();
  166. var resEntity = _db.Queryable<BPA_Bom>().Where(it => it.IsDeleted == 0).First(it => it.Id == id);
  167. if (null == resEntity)
  168. {
  169. throw Oops.Oh("配方不存在");
  170. }
  171. resEntity.IsDeleted = 1;
  172. var res = await _db.Updateable(resEntity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
  173. var BomTypeInfo = _db.Queryable<BPA_BomTypeInfo>().Where(it => it.BomId == id).Select(x => x.Id).ToList();
  174. await _db.Deleteable<BPA_BomTypeInfo>(BomTypeInfo).ExecuteCommandAsync();
  175. _db.Ado.CommitTran();
  176. return res > 0;
  177. }
  178. catch (Exception)
  179. {
  180. _db.Ado.RollbackTran();
  181. throw Oops.Oh("删除失败"); ;
  182. }
  183. }
  184. /// <summary>
  185. /// 添加
  186. /// </summary>
  187. /// <param name="dto"></param>
  188. /// <returns></returns>
  189. public async Task<bool> AddBomType(AddBomTypeInputDto dto)
  190. {
  191. BPA_BomType newbPA_BOM = new BPA_BomType
  192. {
  193. Name = dto.Name
  194. };
  195. var res = await _db.Insertable(newbPA_BOM).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  196. return res > 0;
  197. }
  198. /// <summary>
  199. /// 配方分类列表
  200. /// </summary>
  201. /// <returns></returns>
  202. public async Task<List<dynamic>> GetBomTypeList()
  203. {
  204. var res = await _db.Queryable<BPA_BomType>().Select<dynamic>(x => new
  205. {
  206. id = x.Id,
  207. name = x.Name
  208. }).ToListAsync();
  209. return res;
  210. }
  211. private string GetNumber2(int Length = 10)
  212. {
  213. byte[] buffer = Guid.NewGuid().ToByteArray();
  214. var ram = BitConverter.ToInt64(buffer, 0);
  215. var str = string.Format("{0}", ram.ToString().Substring(0, Length));
  216. return str;
  217. }
  218. /// <summary>
  219. ///获取 配方列表
  220. /// </summary>
  221. /// <param name="inputDto"></param>
  222. /// <returns></returns>
  223. public async Task<PageUtil> BomPage(BomPageInputDto inputDto)
  224. {
  225. var pertainList = _db.Queryable<BPA_BomTypeInfo, BPA_BomType>((a, b) =>
  226. new JoinQueryInfos(JoinType.Left, a.BomTypeId == b.Id))
  227. .Where((a, b) => a.IsDeleted == 0 && b.IsDeleted == 0)
  228. //.WhereIF(inputDto.Pertains != null && inputDto.Pertains.Count > 0, (a, b) => inputDto.Pertains.Contains(b.Pertain))
  229. //.WhereIF(!string.IsNullOrWhiteSpace(inputDto.bomTypeList), (a, b) => b.Id == inputDto.bomTypeList)
  230. .Select((a, b) => a.BomId)
  231. .ToList();
  232. int total = new RefAsync<int>();
  233. var res = _db.Queryable<BPA_Bom>()
  234. .Where(it => it.IsDeleted == 0)
  235. .WhereIF(!string.IsNullOrEmpty(inputDto.Name),x=>x.Name.Contains(inputDto.Name))
  236. //.WhereIF(!string.IsNullOrWhiteSpace(inputDto.bomTypeList), x => pertainList.Contains(x.Id))
  237. .OrderBy(i => i.CreateAt, OrderByType.Desc)
  238. .ToPageList(inputDto.Current, inputDto.PageSize, ref total);
  239. var listtype = _db.Queryable<BPA_BomTypeInfo>()
  240. .Where(a => res.Select(x => x.Id).Contains(a.BomId) && a.IsDeleted == 0).ToList();
  241. var data = new List<object>();
  242. foreach (var item in res)
  243. {
  244. var obj = listtype.Where(x => x.BomId == item.Id).Select(x => x.BomTypeId).ToList();
  245. data.Add(new
  246. {
  247. BomTypeList = obj,
  248. Name = item.Name,
  249. Code = item.Code,
  250. item.IsMain,
  251. item.BOMInfo,
  252. item.Status,
  253. item.Id,
  254. item.Sort,
  255. });
  256. }
  257. PageUtil util = new PageUtil()
  258. {
  259. Total = total,
  260. Data = data
  261. };
  262. return util;
  263. }
  264. /// <summary>
  265. /// 添加配方
  266. /// </summary>
  267. /// <param name="dto"></param>
  268. /// <returns></returns>
  269. public async Task<bool> AddBom(BomCreateInputDto dto)
  270. {
  271. _db.Ado.BeginTran();
  272. try
  273. {
  274. var sortMax = _db.Queryable<BPA_Bom>().Max(x => x.Sort);
  275. BPA_Bom newbPA_BOM = new BPA_Bom
  276. {
  277. Name = dto.BomName,
  278. Code = GetNumber2(),
  279. IsMain = true,
  280. Sort = sortMax + 1,
  281. Id = Guid.NewGuid().ToString(),
  282. CreateAt = DateTime.Now,
  283. Status = CommonStatus.ENABLE,
  284. };
  285. //添加配方
  286. var res = _db.Insertable(newbPA_BOM).CallEntityMethod(m => m.Create()).ExecuteCommand();
  287. //添加配方分类
  288. var list = dto.BomtypeList.Select(item => new BPA_BomTypeInfo() { BomId = newbPA_BOM.Id, BomTypeId = item }).ToList();
  289. _db.Insertable(list).CallEntityMethod(m => m.Create()).ExecuteCommand();
  290. List<BPA_BomEntry> BOMEntryList = new();
  291. if (dto.Mate.Count > 0)
  292. {
  293. for (int i = 0; i < dto.Mate.Count; i++)
  294. {
  295. BPA_BomEntry newbPA_BOMEnty = new BPA_BomEntry
  296. {
  297. BatchingId = dto.Mate[i].batchingId,
  298. //BatchingKey = dto.Mate[i].AutoKey,
  299. BomQty = dto.Mate[i].dosage,
  300. BomId = newbPA_BOM.Id,
  301. Status = CommonStatus.ENABLE,
  302. IsReplace = false,
  303. };
  304. BOMEntryList.Add(newbPA_BOMEnty);
  305. }
  306. }
  307. //添加配方物料关系
  308. _db.Insertable(BOMEntryList).CallEntityMethod(m => m.Create()).ExecuteCommand();
  309. _db.Ado.CommitTran();
  310. return true;
  311. }
  312. catch (Exception e)
  313. {
  314. _db.Ado.RollbackTran();
  315. throw Oops.Oh("添加失败,失败信息:" + e.Message);
  316. }
  317. }
  318. /// <summary>
  319. /// 删除配方
  320. /// </summary>
  321. /// <param name="dto"></param>
  322. /// <returns></returns>
  323. public async Task<bool> DelBom(string id)
  324. {
  325. try
  326. {
  327. _db.Ado.BeginTran();
  328. await _db.Deleteable<BPA_Bom>().Where(x => x.Id == id).ExecuteCommandAsync();
  329. await _db.Deleteable<BPA_BomEntry>().Where(x => x.BomId == id).ExecuteCommandAsync();
  330. _db.Ado.CommitTran();
  331. return true;
  332. }
  333. catch (Exception e)
  334. {
  335. _db.Ado.RollbackTran();
  336. throw Oops.Oh("删除失败" + e.Message);
  337. }
  338. }
  339. }
  340. }