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

392 lines
17 KiB

  1. using BPA.SAAS.Manage.Application.Device.Dtos.DeviceVesion;
  2. using BPA.SAAS.Manage.Application.Device.Interface;
  3. using BPA.SAAS.Manage.Comm.Const;
  4. using BPA.SAAS.Manage.Comm.Enum;
  5. using BPA.SAAS.Manage.Core.Base;
  6. using BPA.SAAS.Manage.Core.Device;
  7. using BPA.SAAS.Manage.Core.Product;
  8. using Microsoft.AspNetCore.Server.IISIntegration;
  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.Device.Services
  15. {
  16. public class DeviceVesionService: IDeviceVesionService, ITransient
  17. {
  18. ISqlSugarClient _db;
  19. public DeviceVesionService(ISqlSugarClient db)
  20. {
  21. _db=db;
  22. }
  23. /// <summary>
  24. /// 分页查询
  25. /// </summary>
  26. /// <param name="inputDto"></param>
  27. /// <returns></returns>
  28. public async Task<PageUtil> GetDeviceVesionPageAsync(DeviceVesionQueryInputDto inputDto)
  29. {
  30. var total = new RefAsync<int>();
  31. var data = await _db.Queryable<BPA_ProductVesion,BPA_Product>((a, b) => new JoinQueryInfos(JoinType.Left, b.Id == a.ProductId))
  32. .WhereIF(!string.IsNullOrWhiteSpace(inputDto.ProductName), (a, b) => b.Name.Contains(inputDto.ProductName))
  33. .WhereIF(!string.IsNullOrWhiteSpace(inputDto.Vesion), (a, b) => a.Vesion.Contains(inputDto.Vesion))
  34. .OrderBy(a => a.CreateAt, OrderByType.Desc)
  35. .Select((a,b)=>new DeviceVesionModel()
  36. {
  37. Id=a.Id.SelectAll(),
  38. ProductName=b.Name,
  39. ProductCode=b.Code,
  40. ProductNumber=a.ProductNumber,
  41. ProductUrl=a.ProductUrl,
  42. })
  43. .ToPageListAsync(inputDto.Current, inputDto.PageSize, total);
  44. return new PageUtil()
  45. {
  46. Data = data,
  47. Total = total
  48. };
  49. }
  50. /// <summary>
  51. /// 新增
  52. /// </summary>
  53. /// <returns></returns>
  54. public async Task<bool> AddDeviceVesionAsync(DeviceVesionBaseDto inputDto)
  55. {
  56. try
  57. {
  58. _db.Ado.BeginTran();
  59. var res = await _db.Insertable(new BPA_ProductVesion
  60. {
  61. Vesion = inputDto.Vesion,
  62. ProductId = inputDto.ProductId,
  63. TemplatePath = inputDto.TemplatePath,
  64. ProductNumber = inputDto.ProductNumber,
  65. ProductUrl = inputDto.ProductUrl,
  66. Status = CommonStatus.ENABLE
  67. }).CallEntityMethod(m => m.Create()).ExecuteReturnEntityAsync();
  68. var Product = _db.Queryable<BPA_Product>().Where(x => x.Id == inputDto.ProductId).First();
  69. #region 添加默认功能和topics
  70. List<BPA_ProductTopics> list = new();
  71. var goodspushtopics = new BPA_ProductTopics()
  72. {
  73. Topics = " /" + Product.Key + "/" + inputDto.Vesion + "/${deviceKey}/defaul/goodspush",
  74. TopicsType = TopicsTypeEnum.SUBSCRIBE,
  75. Description = "商品数据下发",
  76. ProductId = inputDto.ProductId,
  77. ProductVesionId = res.Id,
  78. Name = "商品数据下发",
  79. IsDefault = true
  80. };
  81. var goodspushtopics_reply = new BPA_ProductTopics()
  82. {
  83. Topics = " /" + Product.Key + "/" + inputDto.Vesion + "/${deviceKey}/defaul/goodspush_reply",
  84. TopicsType = TopicsTypeEnum.PUBLISH,
  85. Description = "响应商品数据下发",
  86. ProductId = inputDto.ProductId,
  87. ProductVesionId = res.Id,
  88. Name = "商品数据下发",
  89. IsDefault = true
  90. };
  91. var batchingpushtopics = new BPA_ProductTopics()
  92. {
  93. Topics = " /" + Product.Key + "/" + inputDto.Vesion + "/${deviceKey}/defaul/batvhingpush",
  94. TopicsType = TopicsTypeEnum.PUBLISH,
  95. Description = "物料数据下发",
  96. ProductId = inputDto.ProductId,
  97. ProductVesionId = res.Id,
  98. Name = "物料数据下发",
  99. IsDefault = true
  100. };
  101. var batchingpushtopics_reply = new BPA_ProductTopics()
  102. {
  103. Topics = " /" + Product.Key + "/" + inputDto.Vesion + "/${deviceKey}/defaul/batvhingpush_reply",
  104. TopicsType = TopicsTypeEnum.SUBSCRIBE,
  105. Description = "响应物料数据下发",
  106. ProductId = inputDto.ProductId,
  107. ProductVesionId = res.Id,
  108. Name = "物料数据下发",
  109. IsDefault = true
  110. };
  111. var chnologypushtopics = new BPA_ProductTopics()
  112. {
  113. Topics = " /" + Product.Key + "/" + inputDto.Vesion + "/${deviceKey}/defaul/chnologypush",
  114. TopicsType = TopicsTypeEnum.PUBLISH,
  115. Description = "工艺数据下发",
  116. ProductId = inputDto.ProductId,
  117. ProductVesionId = res.Id,
  118. Name = "工艺数据下发",
  119. IsDefault = true
  120. };
  121. var chnologypushtopics_reply = new BPA_ProductTopics()
  122. {
  123. Topics = " /" + Product.Key + "/" + inputDto.Vesion + "/${deviceKey}/defaul/chnologypush_reply",
  124. TopicsType = TopicsTypeEnum.SUBSCRIBE,
  125. Description = "响应工艺数据下发",
  126. ProductId = inputDto.ProductId,
  127. Name = "工艺数据下发",
  128. ProductVesionId = res.Id,
  129. IsDefault = true
  130. };
  131. list.Add(goodspushtopics);
  132. list.Add(goodspushtopics_reply);
  133. list.Add(batchingpushtopics);
  134. list.Add(batchingpushtopics_reply);
  135. list.Add(chnologypushtopics);
  136. list.Add(chnologypushtopics_reply);
  137. await _db.Insertable(list).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  138. List<BPA_ProductFunction> ProductFunctionlist = new();
  139. var goodsProductFunction = new BPA_ProductFunction()
  140. {
  141. Id=Guid.NewGuid().ToString(),
  142. CreateAt= DateTime.Now,
  143. DataType = "",
  144. Type = FunctionTypeEnum.SERVER,
  145. Name = "商品数据下发",
  146. Description = "商品数据下发",
  147. ProductId = inputDto.ProductId,
  148. DeviceVersionKey = res.Id,
  149. IsDefault = true
  150. };
  151. var goodsProductFunction_reply = new BPA_ProductFunction()
  152. {
  153. Id = Guid.NewGuid().ToString(),
  154. CreateAt = DateTime.Now,
  155. DataType = "",
  156. Type = FunctionTypeEnum.SERVER,
  157. Name = "响应商品数据下发",
  158. Description = "响应商品数据下发",
  159. ProductId = inputDto.ProductId,
  160. DeviceVersionKey = res.Id,
  161. IsDefault = true
  162. };
  163. var batchingProductFunction = new BPA_ProductFunction()
  164. {
  165. Id = Guid.NewGuid().ToString(),
  166. CreateAt = DateTime.Now,
  167. DataType = "",
  168. Type = FunctionTypeEnum.SERVER,
  169. Name = "物料数据下发",
  170. Description = "物料数据下发",
  171. ProductId = inputDto.ProductId,
  172. DeviceVersionKey = res.Id,
  173. IsDefault = true
  174. };
  175. var batchingProductFunction_reply = new BPA_ProductFunction()
  176. {
  177. Id = Guid.NewGuid().ToString(),
  178. CreateAt = DateTime.Now,
  179. DataType = "",
  180. Type = FunctionTypeEnum.SERVER,
  181. Name = "响应物料数据下发",
  182. Description = "响应物料数据下发",
  183. ProductId = inputDto.ProductId,
  184. DeviceVersionKey = res.Id,
  185. IsDefault = true
  186. };
  187. var chnologyProductFunction = new BPA_ProductFunction()
  188. {
  189. Id = Guid.NewGuid().ToString(),
  190. CreateAt = DateTime.Now,
  191. DataType = "",
  192. Type = FunctionTypeEnum.SERVER,
  193. Name = "工艺数据下发",
  194. Description = "工艺数据下发",
  195. ProductId = inputDto.ProductId,
  196. DeviceVersionKey = res.Id,
  197. IsDefault = true
  198. };
  199. var chnologyProductFunction_reply = new BPA_ProductFunction()
  200. {
  201. Id = Guid.NewGuid().ToString(),
  202. CreateAt = DateTime.Now,
  203. DataType = "",
  204. Type = FunctionTypeEnum.SERVER,
  205. Name = "响应工艺数据下发",
  206. Description = "响应工艺数据下发",
  207. ProductId = inputDto.ProductId,
  208. DeviceVersionKey = res.Id,
  209. IsDefault = true
  210. };
  211. ProductFunctionlist.Add(goodsProductFunction);
  212. ProductFunctionlist.Add(goodsProductFunction_reply);
  213. ProductFunctionlist.Add(batchingProductFunction);
  214. ProductFunctionlist.Add(batchingProductFunction_reply);
  215. ProductFunctionlist.Add(chnologyProductFunction);
  216. ProductFunctionlist.Add(chnologyProductFunction_reply);
  217. await _db.Insertable(ProductFunctionlist).ExecuteCommandAsync();
  218. List<BPA_ProductFunctionAction> ProductFunctionActionlist = new();
  219. var goodsProductFunctionActionlist = new BPA_ProductFunctionAction()
  220. {
  221. ActionType = "textareaary",
  222. ProductFunctionId = goodsProductFunction.Id,
  223. ActionName = "DataSoure",
  224. ActionValue = "",
  225. IsDefault = true
  226. };
  227. var batchingProductFunctionActionlist = new BPA_ProductFunctionAction()
  228. {
  229. ActionType = "textareaary",
  230. ProductFunctionId = batchingProductFunction.Id,
  231. ActionName = "DataSoure",
  232. ActionValue = "",
  233. IsDefault = true
  234. };
  235. var chnologyProductFunctionActionlist = new BPA_ProductFunctionAction()
  236. {
  237. ActionType = "textareaary",
  238. ProductFunctionId = chnologyProductFunction.Id,
  239. ActionName = "DataSoure",
  240. ActionValue = "",
  241. IsDefault = true
  242. };
  243. var goodsProductFunctionActionlist_reply = new BPA_ProductFunctionAction()
  244. {
  245. ActionType = "textareaary",
  246. ProductFunctionId = goodsProductFunction_reply.Id,
  247. ActionName = "DataSoure",
  248. ActionValue = "",
  249. IsDefault = true
  250. };
  251. var batchingProductFunctionActionlist_reply = new BPA_ProductFunctionAction()
  252. {
  253. ActionType = "textareaary",
  254. ProductFunctionId = batchingProductFunction_reply.Id,
  255. ActionName = "DataSoure",
  256. ActionValue = "",
  257. IsDefault = true
  258. };
  259. var chnologyProductFunctionActionlist_reply = new BPA_ProductFunctionAction()
  260. {
  261. ActionType = "textareaary",
  262. ProductFunctionId = chnologyProductFunction_reply.Id,
  263. ActionName = "DataSoure",
  264. ActionValue = "",
  265. IsDefault = true
  266. };
  267. ProductFunctionActionlist.Add(goodsProductFunctionActionlist);
  268. ProductFunctionActionlist.Add(batchingProductFunctionActionlist);
  269. ProductFunctionActionlist.Add(chnologyProductFunctionActionlist);
  270. ProductFunctionActionlist.Add(goodsProductFunctionActionlist_reply);
  271. ProductFunctionActionlist.Add(batchingProductFunctionActionlist_reply);
  272. ProductFunctionActionlist.Add(chnologyProductFunctionActionlist_reply);
  273. await _db.Insertable(ProductFunctionActionlist).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  274. #endregion
  275. _db.Ado.CommitTran();
  276. return res != null;
  277. }
  278. catch (Exception e)
  279. {
  280. _db.Ado.RollbackTran();
  281. throw Oops.Oh("添加失败,失败信息:"+e.Message);
  282. }
  283. }
  284. /// <summary>
  285. /// 删除
  286. /// </summary>
  287. /// <param name="inputList"></param>
  288. /// <returns></returns>
  289. public async Task<bool> DelDeviceVesionAsync(List<string> inputList)
  290. {
  291. try
  292. {
  293. _db.Ado.BeginTran();
  294. var data = await _db.Queryable<BPA_DeviceInfo>().Where(x => inputList.Contains(x.ProductVersionId)).ToListAsync();
  295. if (data.Count > 0)
  296. {
  297. throw Oops.Oh("当前版本已有设备使用,无法删除");
  298. }
  299. var datas = await _db.Queryable<BPA_ProductVesion>()
  300. .Where(x => inputList.Contains(x.Id))
  301. .ToListAsync();
  302. var ProductFunctions = await _db.Queryable<BPA_ProductFunction>()
  303. .Where(x => inputList.Contains(x.DeviceVersionKey))
  304. .ToListAsync();
  305. var ProductTopics = await _db.Queryable<BPA_ProductTopics>()
  306. .Where(x => inputList.Contains(x.ProductVesionId))
  307. .ToListAsync();
  308. _db.Deleteable(ProductTopics).ExecuteCommand();//删除相关topic
  309. _db.Deleteable(ProductFunctions).ExecuteCommand();//删除相关服务和功能
  310. var res=await _db.Deleteable(datas).ExecuteCommandAsync();
  311. _db.Ado.CommitTran();
  312. return res>0;
  313. }
  314. catch (Exception e)
  315. {
  316. _db.Ado.RollbackTran();
  317. throw Oops.Oh("删除失败,失败信息:"+e.Message);
  318. }
  319. }
  320. /// <summary>
  321. /// 修改
  322. /// </summary>
  323. /// <returns></returns>
  324. public async Task<bool> UpdateDeviceVesionAsync(DeviceVesionBaseDto inputDto)
  325. {
  326. var data = await _db.Queryable<BPA_ProductVesion>().Where(x => x.Id == inputDto.Id).FirstAsync();
  327. if (data != null)
  328. {
  329. var res = _db.Updateable<BPA_ProductVesion>().SetColumns(t => t.Vesion == inputDto.Vesion)
  330. .SetColumns(t => t.TemplatePath == inputDto.TemplatePath)
  331. .SetColumns(t => t.ProductNumber == inputDto.ProductNumber)
  332. .SetColumns(t => t.ProductUrl == inputDto.ProductUrl)
  333. .SetColumns(t => t.ProductId == inputDto.ProductId)
  334. .SetColumns(t => t.Status == (CommonStatus)inputDto.Status).Where(t => t.Id == inputDto.Id)
  335. .ExecuteCommandHasChange();
  336. return res;
  337. }
  338. return false;
  339. }
  340. /// <summary>
  341. /// 更新状态
  342. /// </summary>
  343. /// <param name="inputDto"></param>
  344. /// <returns></returns>
  345. public async Task<bool> UpdateDeviceVesionSatatus(DeviceVesionSatatusDto inputDto)
  346. {
  347. var data = await _db.Queryable<BPA_ProductVesion>().Where(x => x.Id == inputDto.Id).FirstAsync();
  348. if (data != null)
  349. {
  350. var res = _db.Updateable<BPA_ProductVesion>()
  351. .SetColumns(t => t.Status == (CommonStatus)inputDto.Status).Where(t => t.Id == inputDto.Id)
  352. .ExecuteCommandHasChange();
  353. return res;
  354. }
  355. return false;
  356. }
  357. /// <summary>
  358. /// 根据产品查询版本
  359. /// </summary>
  360. /// <param name="Code"></param>
  361. /// <returns></returns>
  362. public async Task<List<BPA_ProductVesion>> GetDeviceVesion(string ProductId)
  363. {
  364. var resEntity =await _db.Queryable<BPA_ProductVesion>().Where(a => a.ProductId == ProductId && a.Status==0).Select(a => new BPA_ProductVesion() { Id = a.Id.SelectAll() }).ToListAsync();
  365. return resEntity;
  366. }
  367. /// <summary>
  368. /// 查询版本列表
  369. /// </summary>
  370. /// <returns></returns>
  371. public async Task<List<BPA_ProductVesion>> GetDeviceVesionList()
  372. {
  373. var resEntity =await _db.Queryable<BPA_ProductVesion>()
  374. .Select(a => new BPA_ProductVesion() { Id = a.Id.SelectAll() })
  375. .ToListAsync();
  376. return resEntity;
  377. }
  378. }
  379. }