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

208 lines
10 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 && x.GoodsAttributeId == inputDto[0].GoodsAttributeId).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. WarehousrTemplateId = inputDto[i].WarehousrTemplateId,
  50. IsDeleted = 0,
  51. GoodsAttributeId = inputDto[i].GoodsAttributeId,
  52. Sort = string.IsNullOrWhiteSpace(inputDto[i].Sort.ToString()) ? maxsort + (i + 1) : Convert.ToInt32(inputDto[i].Sort.ToString()),
  53. IsBatch = inputDto[i].IsBatch,
  54. DeviceId = inputDto[i].DeviceId,
  55. };
  56. if (!goodsinfo.IsAttrubute)
  57. {
  58. var sx = _db.Queryable<BPA_GoodsAttributeValue, BPA_GoodsAttribute>((a, b) => new JoinQueryInfos(
  59. JoinType.Left, a.GoodsAttributeId == b.Id)).Where((a, b) => b.AttributeName == "默认属性").Select((a, b) => new BPA_GoodsAttributeValue() { Id = a.Id.SelectAll() }).First();
  60. data.GoodsAttributeId = sx?.Id;
  61. }
  62. addlist.Add(data);
  63. }
  64. var res = await _db.Insertable(addlist).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  65. _db.Ado.CommitTran();
  66. return res > 0;
  67. }
  68. catch (Exception e)
  69. {
  70. _db.Ado.RollbackTran();
  71. throw Oops.Oh("保存失败");
  72. }
  73. }
  74. public async Task<bool> AddGoodsTechnologyAction(List<GoodsTechnologyActionBaseDto> inputDto)
  75. {
  76. try
  77. {
  78. var addlist = new List<BPA_GoodsTechnologyAction>();
  79. for (int i = 0; i < inputDto.Count; i++)
  80. {
  81. var goodsinfo = _db.Queryable<BPA_GoodsInfo>().Where(x => x.Id == inputDto[i].GoodsId).First();
  82. var maxsort = _db.Queryable<BPA_GoodsTechnologyAction>().Where(x => x.GoodsId == inputDto[i].GoodsId && x.DeviceId == inputDto[i].DeviceId).Max(x => x.Sort);
  83. var data = new BPA_GoodsTechnologyAction()
  84. {
  85. Id = string.IsNullOrEmpty(inputDto[i].Id) ? Guid.NewGuid().ToString() : inputDto[i].Id,
  86. GoodsId = inputDto[i].GoodsId,
  87. ActionJson = inputDto[i].ActionJson,
  88. StepName = inputDto[i].StepName,
  89. ChnologyId = inputDto[i].ChnologyId,
  90. CreateAt = DateTime.Now,
  91. IsDeleted = 0,
  92. WarehousrTemplateId = inputDto[i].WarehousrTemplateId,
  93. GoodsAttributeId = inputDto[i].GoodsAttributeId,
  94. Sort = string.IsNullOrWhiteSpace(inputDto[i].Sort.ToString()) ? maxsort + (i + 1) : Convert.ToInt32(inputDto[i].Sort.ToString()),
  95. IsBatch = inputDto[i].IsBatch,
  96. DeviceId = inputDto[i].DeviceId,
  97. };
  98. if (!goodsinfo.IsAttrubute)
  99. {
  100. var sx = _db.Queryable<BPA_GoodsAttributeValue, BPA_GoodsAttribute>((a, b) => new JoinQueryInfos(
  101. JoinType.Left, a.GoodsAttributeId == b.Id)).Where((a, b) => b.AttributeName == "默认属性").Select((a, b) => new BPA_GoodsAttributeValue() { Id = a.Id.SelectAll() }).First();
  102. data.GoodsAttributeId = sx?.Id;
  103. }
  104. addlist.Add(data);
  105. }
  106. var res = await _db.Insertable(addlist).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  107. return res > 0;
  108. }
  109. catch (Exception e)
  110. {
  111. throw Oops.Oh("保存失败");
  112. }
  113. }
  114. public async Task<List<GoodsTechnologyActionListView>> GetGoodsTechnologyAction(string goodsId)
  115. {
  116. List<GoodsTechnologyActionListView> goodsTechnologyActionListViews = new List<GoodsTechnologyActionListView>();
  117. var list = _db.Queryable<BPA_GoodsTechnologyAction>().Where(x => x.GoodsId == goodsId)
  118. .Select(x => new GoodsTechnologyActionView()
  119. {
  120. Id = x.Id.SelectAll(),
  121. }).OrderBy(x => x.Sort, OrderByType.Asc).ToList();
  122. var Devicelist = await _db.Queryable<BPA_DeviceInfo>().ToListAsync();
  123. var GoodsAttributeValue = await _db.Queryable<BPA_GoodsAttributeValue>().ToListAsync();
  124. var sf = list.GroupBy(x => x.GoodsAttributeId).ToList();
  125. var sf1 = list.GroupBy(x => x.WarehousrTemplateId).ToList();
  126. for (int i = 0; i < sf.Count; i++)
  127. {
  128. var sd = GoodsAttributeValue?.Where(x => sf[i].Key.Contains(x.Id)).Select(x => x.AttributeValue).ToArray();
  129. var sdw = GoodsAttributeValue?.Where(x => sf[i].Key.Contains(x.Id)).Select(x => x.Id).ToArray();
  130. var nane =string.Join('-', sd);
  131. var s = sf[i].AsQueryable().ToList().GroupBy(x => x.DeviceId).ToList();
  132. for (int t = 0; t < s.Count; t++)
  133. {
  134. GoodsTechnologyActionListView item = new()
  135. {
  136. DeviceId = s[t].Key,
  137. WarehousrTemplateId = sf1[t].Key,
  138. GoodsAttributeId = string.Join(',', sdw),
  139. DeviceName = Devicelist?.FirstOrDefault(x => x.Id == s[t].Key).DeviceName+ "【"+ nane+"】",
  140. Data = s[t].AsQueryable().ToList(),
  141. };
  142. goodsTechnologyActionListViews.Add(item);
  143. }
  144. }
  145. return goodsTechnologyActionListViews;
  146. }
  147. public async Task<bool> DeleteGoodsTechnologyAction(string id)
  148. {
  149. var item = await _db.Queryable<BPA_GoodsTechnologyAction>().Where(x => x.Id == id).FirstAsync();
  150. if (item != null)
  151. {
  152. var res = await _db.Deleteable(item).ExecuteCommandAsync() > 0;
  153. var list = _db.Queryable<BPA_GoodsTechnologyAction>().Where(x =>x.GoodsId == item.GoodsId && x.DeviceId == item.DeviceId).OrderBy(x => x.Sort, OrderByType.Asc).ToList();
  154. var uplist = new List<BPA_GoodsTechnologyAction>();
  155. for (int i = 0; i < list.Count; i++)
  156. {
  157. list[i].Sort = i + 1;
  158. uplist.Add(list[i]);
  159. }
  160. if (uplist.Count > 0)
  161. {
  162. await _db.Updateable(uplist).ExecuteCommandAsync();
  163. }
  164. return true;
  165. }
  166. else
  167. {
  168. throw Oops.Oh("找不到相关数据,删除失败");
  169. }
  170. }
  171. public async Task<bool> DeleteGoodsTechnologyAction(GoodsTechnologDelete dto)
  172. {
  173. var item = await _db.Queryable<BPA_GoodsTechnologyAction>().Where(x => x.DeviceId == dto.devideId && x.GoodsId == dto.goodsId && x.GoodsAttributeId== dto.goodsAttributeId).ToListAsync();
  174. if (item.Count > 0)
  175. {
  176. return await _db.Deleteable(item).ExecuteCommandAsync() > 0;
  177. }
  178. else
  179. {
  180. throw Oops.Oh("找不到相关数据,删除失败");
  181. }
  182. }
  183. public async Task<List<DeviceGoodsTechnologyView>> GetDeviceTechnology()
  184. {
  185. var list = await _db.Queryable<BPA_DeviceInfo>().Select(x => new DeviceGoodsTechnologyView()
  186. {
  187. Id = x.Id.SelectAll(),
  188. }).Mapper(a =>
  189. {
  190. //a.GoodsTechnologyInfo = _db.Queryable<BPA_ProductFunction>().Where(d => d.DeviceVersionKey == a.ProductVersionId && d.Type==1).Select(d => new GoodsTechnologyInfo()
  191. //{
  192. // DeviceTechnologyId = d.Id,
  193. // DeviceVersionKey = d.DeviceVersionKey,
  194. // Name = d.Name,
  195. //}).ToList();
  196. }).ToListAsync();
  197. return list;
  198. }
  199. #endregion
  200. }
  201. }