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

194 lines
9.2 KiB

  1. using BPA.SAAS.Manage.Application.DataBase.Dtos.GoodsTechnology;
  2. using BPA.SAAS.Manage.Application.DataBase.Interface;
  3. using BPA.SAAS.Manage.Comm.Const;
  4. using BPA.SAAS.Manage.Core.Base;
  5. using BPA.SAAS.Manage.Core.DataBase;
  6. using BPA.SAAS.Manage.Core.Device;
  7. using BPA.SAAS.Manage.Core.Product;
  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 GoodsTechnologyService: IGoodsTechnologyService, ITransient
  16. {
  17. ISqlSugarClient _db;
  18. public GoodsTechnologyService(ISqlSugarClient db)
  19. {
  20. _db = db;
  21. }
  22. #region 商品工艺(新版本工艺制作)
  23. /// <summary>
  24. /// 添加商品工艺
  25. /// </summary>
  26. /// <param name="inputDto"></param>
  27. /// <returns></returns>
  28. public async Task<bool> UpdateGoodsTechnologyAction(List<GoodsTechnologyActionBaseDto> inputDto)
  29. {
  30. try
  31. {
  32. var addlist = new List<BPA_GoodsTechnologyAction>();
  33. var list = _db.Queryable<BPA_GoodsTechnologyAction>().Where(x => x.DeviceId == inputDto[0].DeviceId && x.GoodsId == inputDto[0].GoodsId).ToList();
  34. _db.Ado.BeginTran();
  35. _db.Deleteable(list).ExecuteCommand();
  36. //var list = _db.Queryable<BPA_GoodsTechnologyAction>().Where(x => x.GroupId == groupId && x.IsDeleted == 0).ToList();
  37. for (int i = 0; i < inputDto.Count; i++)
  38. {
  39. var goodsinfo = _db.Queryable<BPA_GoodsInfo>().Where(x => x.Id == inputDto[i].GoodsId).First();
  40. var maxsort = _db.Queryable<BPA_GoodsTechnologyAction>().Where(x => x.GoodsId == inputDto[i].GoodsId && x.DeviceId == inputDto[i].DeviceId).Max(x => x.Sort);
  41. var data = new BPA_GoodsTechnologyAction()
  42. {
  43. Id = string.IsNullOrEmpty(inputDto[i].Id) ? Guid.NewGuid().ToString() : inputDto[i].Id,
  44. GoodsId = inputDto[i].GoodsId,
  45. ActionJson = inputDto[i].ActionJson,
  46. StepName = inputDto[i].StepName,
  47. ChnologyId = inputDto[i].ChnologyId,
  48. CreateAt = DateTime.Now,
  49. IsDeleted = 0,
  50. GoodsAttributeId = inputDto[i].GoodsAttributeId,
  51. Sort = string.IsNullOrWhiteSpace(inputDto[i].Sort.ToString()) ? maxsort + (i + 1) : Convert.ToInt32(inputDto[i].Sort.ToString()),
  52. IsBatch = inputDto[i].IsBatch,
  53. DeviceId = inputDto[i].DeviceId,
  54. };
  55. if (!goodsinfo.IsAttrubute)
  56. {
  57. var sx = _db.Queryable<BPA_GoodsAttributeValue, BPA_GoodsAttribute>((a, b) => new JoinQueryInfos(
  58. JoinType.Left, a.GoodsAttributeId == b.Id)).Where((a, b) => b.AttributeName == "默认属性").Select((a, b) => new BPA_GoodsAttributeValue() { Id = a.Id.SelectAll() }).First();
  59. data.GoodsAttributeId = sx?.Id;
  60. }
  61. addlist.Add(data);
  62. }
  63. var res = await _db.Insertable(addlist).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  64. _db.Ado.CommitTran();
  65. return res > 0;
  66. }
  67. catch (Exception e)
  68. {
  69. _db.Ado.RollbackTran();
  70. throw Oops.Oh("保存失败");
  71. }
  72. }
  73. public async Task<bool> AddGoodsTechnologyAction(List<GoodsTechnologyActionBaseDto> inputDto)
  74. {
  75. try
  76. {
  77. var addlist = new List<BPA_GoodsTechnologyAction>();
  78. for (int i = 0; i < inputDto.Count; i++)
  79. {
  80. var goodsinfo = _db.Queryable<BPA_GoodsInfo>().Where(x => x.Id == inputDto[i].GoodsId).First();
  81. var maxsort = _db.Queryable<BPA_GoodsTechnologyAction>().Where(x => x.GoodsId == inputDto[i].GoodsId && x.DeviceId == inputDto[i].DeviceId).Max(x => x.Sort);
  82. var data = new BPA_GoodsTechnologyAction()
  83. {
  84. Id = string.IsNullOrEmpty(inputDto[i].Id) ? Guid.NewGuid().ToString() : inputDto[i].Id,
  85. GoodsId = inputDto[i].GoodsId,
  86. ActionJson = inputDto[i].ActionJson,
  87. StepName = inputDto[i].StepName,
  88. ChnologyId = inputDto[i].ChnologyId,
  89. CreateAt = DateTime.Now,
  90. IsDeleted = 0,
  91. GoodsAttributeId = inputDto[i].GoodsAttributeId,
  92. Sort = string.IsNullOrWhiteSpace(inputDto[i].Sort.ToString()) ? maxsort + (i + 1) : Convert.ToInt32(inputDto[i].Sort.ToString()),
  93. IsBatch = inputDto[i].IsBatch,
  94. DeviceId = inputDto[i].DeviceId,
  95. };
  96. if (!goodsinfo.IsAttrubute)
  97. {
  98. var sx = _db.Queryable<BPA_GoodsAttributeValue, BPA_GoodsAttribute>((a, b) => new JoinQueryInfos(
  99. JoinType.Left, a.GoodsAttributeId == b.Id)).Where((a, b) => b.AttributeName == "默认属性").Select((a, b) => new BPA_GoodsAttributeValue() { Id = a.Id.SelectAll() }).First();
  100. data.GoodsAttributeId = sx?.Id;
  101. }
  102. addlist.Add(data);
  103. }
  104. var res = await _db.Insertable(addlist).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  105. return res > 0;
  106. }
  107. catch (Exception e)
  108. {
  109. throw Oops.Oh("保存失败");
  110. }
  111. }
  112. public async Task<List<GoodsTechnologyActionListView>> GetGoodsTechnologyAction(string goodsId)
  113. {
  114. List<GoodsTechnologyActionListView> goodsTechnologyActionListViews = new List<GoodsTechnologyActionListView>();
  115. var list = _db.Queryable<BPA_GoodsTechnologyAction>().Where(x => x.GoodsId == goodsId)
  116. .Select(x => new GoodsTechnologyActionView()
  117. {
  118. Id = x.Id.SelectAll(),
  119. }).OrderBy(x => x.Sort, OrderByType.Asc).ToList();
  120. var Devicelist = await _db.Queryable<BPA_DeviceInfo>().ToListAsync();
  121. var s = list.GroupBy(x => x.DeviceId).ToList();
  122. for (int i = 0; i < s.Count; i++)
  123. {
  124. GoodsTechnologyActionListView item = new()
  125. {
  126. DeviceId = s[i].Key,
  127. DeviceName = Devicelist?.FirstOrDefault(x => x.Id == s[i].Key).DeviceName,
  128. Data = s[i].AsQueryable().ToList(),
  129. };
  130. goodsTechnologyActionListViews.Add(item);
  131. }
  132. return goodsTechnologyActionListViews;
  133. }
  134. public async Task<bool> DeleteGoodsTechnologyAction(string id)
  135. {
  136. var item = await _db.Queryable<BPA_GoodsTechnologyAction>().Where(x => x.Id == id).FirstAsync();
  137. if (item != null)
  138. {
  139. var res = await _db.Deleteable(item).ExecuteCommandAsync() > 0;
  140. var list = _db.Queryable<BPA_GoodsTechnologyAction>().Where(x =>x.GoodsId == item.GoodsId && x.DeviceId == item.DeviceId).OrderBy(x => x.Sort, OrderByType.Asc).ToList();
  141. var uplist = new List<BPA_GoodsTechnologyAction>();
  142. for (int i = 0; i < list.Count; i++)
  143. {
  144. list[i].Sort = i + 1;
  145. uplist.Add(list[i]);
  146. }
  147. if (uplist.Count > 0)
  148. {
  149. await _db.Updateable(uplist).ExecuteCommandAsync();
  150. }
  151. return true;
  152. }
  153. else
  154. {
  155. throw Oops.Oh("找不到相关数据,删除失败");
  156. }
  157. }
  158. public async Task<bool> DeleteGoodsTechnologyAction(GoodsTechnologDelete dto)
  159. {
  160. var item = await _db.Queryable<BPA_GoodsTechnologyAction>().Where(x => x.DeviceId == dto.devideId && x.GoodsId == dto.goodsId).ToListAsync();
  161. if (item.Count > 0)
  162. {
  163. return await _db.Deleteable(item).ExecuteCommandAsync() > 0;
  164. }
  165. else
  166. {
  167. throw Oops.Oh("找不到相关数据,删除失败");
  168. }
  169. }
  170. public async Task<List<DeviceGoodsTechnologyView>> GetDeviceTechnology()
  171. {
  172. var list = await _db.Queryable<BPA_DeviceInfo>().Select(x => new DeviceGoodsTechnologyView()
  173. {
  174. Id = x.Id.SelectAll(),
  175. }).Mapper(a =>
  176. {
  177. a.GoodsTechnologyInfo = _db.Queryable<BPA_ProductFunction>().Where(d => d.DeviceVersionKey == a.ProductVersionId && d.Type==1).Select(d => new GoodsTechnologyInfo()
  178. {
  179. DeviceTechnologyId = d.Id,
  180. DeviceVersionKey = d.DeviceVersionKey,
  181. Name = d.Name,
  182. }).ToList();
  183. }).ToListAsync();
  184. return list;
  185. }
  186. #endregion
  187. }
  188. }