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

353 lines
14 KiB

  1. using BPA.SAAS.Manage.Application.DataBase.Dtos.Batching;
  2. using BPA.SAAS.Manage.Application.DataBase.Dtos.Goods;
  3. using BPA.SAAS.Manage.Application.DataBase.Dtos.GoodsAttribute;
  4. using BPA.SAAS.Manage.Application.DataBase.Interface;
  5. using BPA.SAAS.Manage.Comm.Const;
  6. using BPA.SAAS.Manage.Comm.Enum;
  7. using BPA.SAAS.Manage.Core.Base;
  8. using BPA.SAAS.Manage.Core.DataBase;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace BPA.SAAS.Manage.Application.DataBase.Services
  15. {
  16. public class GoodsService: IGoodsService, ITransient
  17. {
  18. private readonly ISqlSugarClient _db;
  19. public GoodsService(ISqlSugarClient db)
  20. {
  21. _db=db;
  22. }
  23. /// <summary>
  24. /// 分页查询
  25. /// </summary>
  26. /// <param name="dto"></param>
  27. /// <returns></returns>
  28. public async Task<PageUtil> GetGoodsPage(GoodsQueryDto dto)
  29. {
  30. List<IConditionalModel> conModels = new List<IConditionalModel>();
  31. string groupId = App.User.FindFirst(ClaimConst.GroupId)?.Value;
  32. #region 条件查询
  33. if (!string.IsNullOrEmpty(dto.Name))
  34. {
  35. conModels.Add(new ConditionalModel() { FieldName = "a.GoodsName", ConditionalType = ConditionalType.Like, FieldValue = dto.Name });
  36. }
  37. if (!string.IsNullOrEmpty(dto.GoodsTypeId))
  38. {
  39. conModels.Add(new ConditionalModel() { FieldName = "a.GoodsTypeId", ConditionalType = ConditionalType.Equal, FieldValue = dto.GoodsTypeId });
  40. }
  41. if (!string.IsNullOrEmpty(dto.Status))
  42. {
  43. conModels.Add(new ConditionalModel() { FieldName = "a.Status", ConditionalType = ConditionalType.Like, FieldValue = dto.Status });
  44. }
  45. #endregion
  46. RefAsync<int> total = 0;
  47. var res =await _db.Queryable<BPA_GoodsInfo, BPA_GoodsType>((a, b) => new JoinQueryInfos(
  48. JoinType.Left, a.GoodsTypeId == b.Id
  49. ))
  50. .Where((a, b) => a.IsDeleted == 0)
  51. .WhereIF(!string.IsNullOrWhiteSpace(dto.Code), (a, b) => a.Code.Contains(dto.Code))
  52. // .WhereIF(dto.CreateAt.HasValue, (a, b) => SqlFunc.DateIsSame(a.CreateAt, Convert.ToDateTime(dto.CreateAt), DateType.Day))
  53. .Where(conModels)
  54. .OrderBy(a => a.CreateAt, OrderByType.Desc)
  55. .Select((a, b) => new
  56. {
  57. Id = a.Id,
  58. Code = a.Code,
  59. Name = a.Name,
  60. Price = a.Price,
  61. ImgUrl = a.ImgUrl,
  62. Status = a.Status,
  63. GoodsTypeId = b.IsDeleted == 0 ? a.GoodsTypeId : "",
  64. GoodsTypeName = b.IsDeleted == 0 ? b.Name : "",
  65. Remark = a.Descritption,
  66. IsDeleted = a.IsDeleted,
  67. // CreateAt = a.CreateAt,
  68. GoodsUintId = a.GoodsUintId,
  69. ForeignKeyRe = a.ForeignKeyRe,
  70. Design = a.Design,
  71. IsWeigh = a.IsWeigh,
  72. DefaultMate = a.DefaultMate,
  73. IsAttrubute = a.IsAttrubute
  74. })
  75. .ToPageListAsync(dto.Current, dto.PageSize, total);
  76. PageUtil util = new PageUtil()
  77. {
  78. Total = total,
  79. Data = res
  80. };
  81. return util;
  82. }
  83. public async Task<List<BPA_GoodsInfo>> GetGoodsList()
  84. {
  85. var res = await _db.Queryable<BPA_GoodsInfo>().ToListAsync();
  86. return res;
  87. }
  88. /// <summary>
  89. /// 添加商品
  90. /// </summary>
  91. /// <param name="dto"></param>
  92. /// <returns></returns>
  93. public async Task<bool> AddGoods(GoodsDto dto)
  94. {
  95. if (string.IsNullOrWhiteSpace(dto.Id))
  96. {
  97. var resEntity = new BPA_GoodsInfo();
  98. resEntity.Id = Guid.NewGuid().ToString();
  99. resEntity.GoodsTypeId = dto.GoodsTypeId;
  100. resEntity.Name = dto.Name;
  101. resEntity.Descritption = dto.Descritption;
  102. resEntity.ImgUrl = dto.ImgUrl;
  103. resEntity.Price = dto.Price;
  104. resEntity.IsWeigh = dto.IsWeigh == "false" ? 0 : 1;
  105. resEntity.GoodsUintId = dto.GoodsUintId;
  106. resEntity.IsAttrubute = Convert.ToBoolean(dto.IsAttrubute);
  107. resEntity.Code = GetNumber2(8);
  108. var data = _db.Queryable<BPA_GoodsInfo>().Max(x => x.AutoKey);
  109. if (data == 0) data = 1000;
  110. else data = data + 1;
  111. resEntity.AutoKey = data;
  112. var res = await _db.Insertable(resEntity).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  113. return res > 0;
  114. }
  115. else
  116. {
  117. return await UpdateGoods(dto);
  118. }
  119. }
  120. /// <summary>
  121. /// 更新商品
  122. /// </summary>
  123. /// <param name="dto"></param>
  124. /// <returns></returns>
  125. public async Task<bool> UpdateGoods(GoodsDto dto)
  126. {
  127. // 查询数据库中是否存在未删除的商品类型
  128. var resEntity = _db.Queryable<BPA_GoodsInfo>().Where(it => it.IsDeleted == 0).First(it => it.Id == dto.Id);
  129. if (null == resEntity)
  130. {
  131. throw Oops.Oh("商品不存在");
  132. }
  133. resEntity.GoodsTypeId = dto.GoodsTypeId;
  134. resEntity.Name = dto.Name;
  135. resEntity.Descritption = dto.Descritption;
  136. resEntity.ImgUrl = dto.ImgUrl;
  137. resEntity.Price = dto.Price;
  138. resEntity.IsWeigh = dto.IsWeigh=="false"?0:1;
  139. resEntity.GoodsUintId = dto.GoodsUintId;
  140. resEntity.IsAttrubute = Convert.ToBoolean(dto.IsAttrubute);
  141. if (!string.IsNullOrWhiteSpace(dto.Status))
  142. {
  143. resEntity.Status = (CommonStatus)Enum.ToObject(typeof(CommonStatus), int.Parse(dto.Status));
  144. }
  145. var res = await _db.Updateable(resEntity).ExecuteCommandAsync();
  146. return res > 0;
  147. }
  148. /// <summary>
  149. /// 删除商品
  150. /// </summary>
  151. /// <param name="id"></param>
  152. /// <returns></returns>
  153. public async Task<bool> DeleteGoods(string id)
  154. {
  155. var goods = _db.Queryable<BPA_GoodsInfo>().Where(x => x.Id== id).First();
  156. if (goods == null) throw Oops.Oh("商品不存在");
  157. goods.IsDeleted = 1;
  158. var res = await _db.Updateable(goods).ExecuteCommandAsync();
  159. return res > 0;
  160. }
  161. /// <summary>
  162. /// 查询商品单位
  163. /// </summary>
  164. /// <returns></returns>
  165. public async Task<List<dynamic>> GetGoodsUintList()
  166. {
  167. var res = await _db.Queryable<BPA_GoodsUint>().Select<dynamic>(x => new
  168. {
  169. id = x.Id,
  170. name = x.Name
  171. }).ToListAsync();
  172. return res;
  173. }
  174. /// <summary>
  175. /// 添加商品单位
  176. /// </summary>
  177. /// <param name="dto"></param>
  178. /// <returns></returns>
  179. public async Task<bool> AddGoodsUint(GoodsUintDto dto)
  180. {
  181. var groupId = App.User?.FindFirst(ClaimConst.GroupId)?.Value;
  182. var productCode = _db.Queryable<BPA_GoodsUint>().Where(x => x.GroupId == groupId && x.Name == dto.Name).ToList();
  183. if (productCode.Count() > 0)
  184. {
  185. throw Oops.Oh("商品单位已存在");
  186. }
  187. try
  188. {
  189. BPA_GoodsUint bPA_Product = new BPA_GoodsUint();
  190. bPA_Product.Name = dto.Name;
  191. var res = await _db.Insertable(bPA_Product).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  192. return res > 0;
  193. }
  194. catch (Exception ex)
  195. {
  196. throw Oops.Oh("添加失败");
  197. }
  198. }
  199. /// <summary>
  200. /// 商品配方
  201. /// </summary>
  202. /// <param name="dto"></param>
  203. /// <returns></returns>
  204. public async Task<PageUtil> GetGoodsBomPageAsync(OrtherGoodsQueryDto dto)
  205. {
  206. RefAsync<int> total = 0;
  207. var res =await _db.Queryable<BPA_Bom, BPA_GoodsBom>((a, b) => new JoinQueryInfos(
  208. JoinType.Left, a.Id == b.BomId
  209. ))
  210. .WhereIF(!string.IsNullOrWhiteSpace(dto.GoodsId), (a, b) => b.Goods_Id == dto.GoodsId)
  211. .Select((a, b) => new
  212. {
  213. Id = b.Id,
  214. Name = a.Name,
  215. BomId= b.BomId,
  216. IsMain = a.IsMain
  217. })
  218. .ToPageListAsync(dto.Current, dto.PageSize, total);
  219. PageUtil util = new PageUtil()
  220. {
  221. Total = total,
  222. Data = res
  223. };
  224. return util;
  225. }
  226. /// <summary>
  227. /// 删除商品配方
  228. /// </summary>
  229. /// <param name="Ids"></param>
  230. /// <returns></returns>
  231. public async Task<bool> BatchDelGoodsBomAsync(string Ids)
  232. {
  233. // var GoodsBom=_db.Queryable<BPA_GoodsBom>().Where(x => x.Id == Ids).First();
  234. // 查询数据库中是否存在未删除的商品
  235. var res = _db.Deleteable<BPA_GoodsBom>().In(Ids).ExecuteCommand();
  236. return res > 0;
  237. }
  238. /// <summary>
  239. /// 添加商品配方
  240. /// </summary>
  241. /// <param name="dto"></param>
  242. /// <returns></returns>
  243. public async Task<bool> AddGoodsBomAttribute(GoodsBomAttributeDto dto)
  244. {
  245. _db.Ado.BeginTran();
  246. try
  247. {
  248. string groupId = App.User.FindFirst(ClaimConst.GroupId)?.Value;
  249. var sortMax = _db.Queryable<BPA_Bom>().Max(x => x.Sort);
  250. BPA_Bom newbPA_BOM = new BPA_Bom
  251. {
  252. Name = dto.BomName,
  253. Code = DateTime.Now.ToString("yyyyMMddhhmmsss"),
  254. IsMain = dto.BomType == "1" ? true : false,
  255. Sort = sortMax + 1,
  256. Id = Guid.NewGuid().ToString(),
  257. CreateAt = DateTime.Now,
  258. Status = CommonStatus.ENABLE,
  259. GroupId = groupId
  260. };
  261. //添加配方
  262. var res = _db.Insertable(newbPA_BOM).CallEntityMethod(m => m.Create()).ExecuteCommand();
  263. //添加配方分类
  264. var list = dto.BomtypeList.Select(item => new BPA_BomTypeInfo() { BomId = newbPA_BOM.Id, BomTypeId = item }).ToList();
  265. _db.Insertable(list).CallEntityMethod(m => m.Create()).ExecuteCommand();
  266. BPA_GoodsBom bom = new BPA_GoodsBom();
  267. bom.Goods_Id = dto.GoodsId;
  268. bom.BomId = newbPA_BOM.Id;
  269. bom.Status = CommonStatus.ENABLE;
  270. //添加商品配方关系
  271. _db.Insertable(bom).CallEntityMethod(m => m.Create()).ExecuteCommand();
  272. List<BPA_BomEntry> BOMEntryList = new();
  273. if (dto.Mate.Count > 0)
  274. {
  275. for (int i = 0; i < dto.Mate.Count; i++)
  276. {
  277. BPA_BomEntry newbPA_BOMEnty = new BPA_BomEntry
  278. {
  279. BatchingId = dto.Mate[i].batchingId,
  280. //BatchingKey = dto.Mate[i].AutoKey,
  281. BomQty = dto.Mate[i].dosage,
  282. BomId = newbPA_BOM.Id,
  283. Status = CommonStatus.ENABLE,
  284. IsReplace = false,
  285. };
  286. BOMEntryList.Add(newbPA_BOMEnty);
  287. }
  288. }
  289. //添加配方物料关系
  290. _db.Insertable(BOMEntryList).CallEntityMethod(m => m.Create()).ExecuteCommand();
  291. BPA_BomAttributeValueRe bPA_BomAttributeValueRe = new();
  292. bPA_BomAttributeValueRe.Id = Guid.NewGuid().ToString();
  293. bPA_BomAttributeValueRe.GoodsId = dto.GoodsId;
  294. bPA_BomAttributeValueRe.BoomId = newbPA_BOM.Id;
  295. bPA_BomAttributeValueRe.GoodsAttributeValueId = string.Join(",", dto.Shuxing);
  296. //添加商品配方属性关系
  297. _db.Insertable(bPA_BomAttributeValueRe).ExecuteCommand();
  298. _db.Ado.CommitTran();
  299. return true;
  300. }
  301. catch (Exception e)
  302. {
  303. _db.Ado.RollbackTran();
  304. throw Oops.Oh("添加失败,失败信息:" + e.Message);
  305. }
  306. }
  307. public async Task<bool> AddGoodsBom(GoodsBomDto dto)
  308. {
  309. var res = false;
  310. List<BPA_GoodsBom> list = new();
  311. if (dto.BomId.Count() > 0)
  312. {
  313. for (int i = 0; i < dto.BomId.Count; i++)
  314. {
  315. BPA_GoodsBom goodsBom = new();
  316. goodsBom.BomId = dto.BomId[i];
  317. goodsBom.Goods_Id= dto.GoodsId;
  318. goodsBom.Status = CommonStatus.ENABLE;
  319. list.Add(goodsBom);
  320. }
  321. }
  322. if (list.Count>0)
  323. {
  324. res=await _db.Insertable(list).CallEntityMethod(m => m.Create()).ExecuteCommandAsync()>0;
  325. }
  326. return res;
  327. }
  328. private string GetNumber2(int Length = 10)
  329. {
  330. byte[] buffer = Guid.NewGuid().ToByteArray();
  331. var ram = BitConverter.ToInt64(buffer, 0);
  332. var str = string.Format("{0}", ram.ToString().Substring(0, Length));
  333. return str;
  334. }
  335. }
  336. }