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

326 lines
14 KiB

  1. using BPA.SAAS.Manage.Application.Device.Dtos.ProductFunction;
  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 Newtonsoft.Json;
  10. using NPOI.POIFS.Crypt.Dsig;
  11. using NPOI.Util;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Text.RegularExpressions;
  17. using System.Threading.Tasks;
  18. namespace BPA.SAAS.Manage.Application.Device.Services
  19. {
  20. public class ProductFunctionService: IProductFunctionService, ITransient
  21. {
  22. ISqlSugarClient _db;
  23. public ProductFunctionService(ISqlSugarClient db)
  24. {
  25. _db = db;
  26. }
  27. #region 产品功能
  28. /// <summary>
  29. /// 获取功能基础信息列表
  30. /// </summary>
  31. /// <param name="inputDto"></param>
  32. /// <returns></returns>
  33. public async Task<PageUtil> GetProductFunctionPage(ProductFunctionPageBase inputDto)
  34. {
  35. RefAsync<int> total = 0;
  36. var res = await _db.Queryable<BPA_ProductFunction, BPA_ProductVesion>((x, b) => new JoinQueryInfos(JoinType.Left, b.Id == x.DeviceVersionKey))
  37. .Where((x,b)=>x.ProductId == inputDto.ProductId && x.DeviceVersionKey== inputDto.ProductVesionId)
  38. .WhereIF(!string.IsNullOrWhiteSpace(inputDto.Name), (x, b) => x.Name.Contains(inputDto.Name))
  39. .WhereIF(!string.IsNullOrWhiteSpace(inputDto.Type), (x, b) => x.Type==(FunctionTypeEnum)(Enum.Parse(typeof(FunctionTypeEnum), inputDto.Type)))
  40. .WhereIF(!string.IsNullOrWhiteSpace(inputDto.Vesion), (x, b) => x.DeviceVersionKey == inputDto.Vesion)
  41. //.WhereIF(inputDto.IsDefault!=null, (x, b) => x.IsDefault == inputDto.IsDefault)
  42. .WhereIF(inputDto.Status != null, (x, b) => x.Status == inputDto.Status)
  43. .OrderBy((x, b) => x.CreateAt, OrderByType.Desc).OrderBy((x, b) => x.Name, OrderByType.Desc)
  44. .Select((x, b) => new
  45. {
  46. Name = x.Name,
  47. Status = x.Status,
  48. x.Id,
  49. x.ForeignKeyRe,
  50. x.DeviceVersionKey,
  51. x.DataJson,
  52. DeviceTypeKey = b.ProductId,
  53. Vesion = b.Vesion,
  54. CreateAt=x.CreateAt,
  55. Type= x.Type,
  56. DataType= x.DataType,
  57. DataRange= x.DataRange,
  58. Unit= x.Unit,
  59. StepSize= x.StepSize,
  60. EnumValue= x.EnumValue,
  61. DataLength = x.DataLength,
  62. BoolLabel = x.BoolLabel,
  63. ReadWrite = x.ReadWrite,
  64. Description= x.Description,
  65. IsDefault= x.IsDefault,
  66. EventType= x.EventType,
  67. })
  68. .ToPageListAsync(inputDto.Current, inputDto.PageSize, total);
  69. return new PageUtil()
  70. {
  71. Data = res,
  72. Total = total
  73. };
  74. }
  75. /// <summary>
  76. /// 查询所有功能信息
  77. /// </summary>
  78. /// <returns></returns>
  79. public async Task<List<ProductFunctionDto>> GetProductFunction()
  80. {
  81. var res =await _db.Queryable<BPA_ProductFunction>().Where(x => x.Status == 0)
  82. .OrderBy(i => i.CreateAt, OrderByType.Desc)
  83. .Select(x => new ProductFunctionDto()
  84. {
  85. Id = x.Id,
  86. Name = x.Name,
  87. IsBatch = SqlFunc.Subqueryable<BPA_ProductFunctionAction>().Where(p => p.ProductFunctionId == x.Id).Any()
  88. })
  89. .ToListAsync();
  90. return res;
  91. }
  92. public async Task<List<ProductFunctionView>> GetProductFunction(string deviceId)
  93. {
  94. var device=_db.Queryable<BPA_DeviceInfo>().Where(x => x.Id == deviceId).First();
  95. var res = await _db.Queryable<BPA_ProductFunction>().Where(x => x.DeviceVersionKey == device.ProductVersionId)
  96. .OrderBy(i => i.CreateAt, OrderByType.Desc)
  97. .Select(x => new ProductFunctionView()
  98. {
  99. Id = x.Id.SelectAll(),
  100. Name = x.Name,
  101. //IsBatch = SqlFunc.Subqueryable<BPA_ProductFunctionAction>().Where(p => p.ProductFunctionId == x.Id).Any()
  102. })
  103. .Mapper(x =>
  104. {
  105. x.ProductFunctionValue = _db.Queryable<BPA_ProductFunctionAction>().Where(p => p.ProductFunctionId == x.Id).ToList();
  106. })
  107. .ToListAsync();
  108. return res;
  109. }
  110. public async Task<List<ProductFunctionView>> GetProductFunction_alm(string deviceId)
  111. {
  112. var device = _db.Queryable<BPA_DeviceInfo>().Where(x => x.Id == deviceId).First();
  113. var res = await _db.Queryable<BPA_ProductFunction>().Where(x => x.DeviceVersionKey == device.ProductVersionId)
  114. .OrderBy(i => i.CreateAt, OrderByType.Desc)
  115. .Select(x => new ProductFunctionView()
  116. {
  117. Id = x.Id.SelectAll(),
  118. Name = x.Name,
  119. //IsBatch = SqlFunc.Subqueryable<BPA_ProductFunctionAction>().Where(p => p.ProductFunctionId == x.Id).Any()
  120. })
  121. .Mapper(x =>
  122. {
  123. x.ProductFunctionValue = _db.Queryable<BPA_ProductFunctionAction>().Where(p => p.ProductFunctionId == x.Id).ToList();
  124. })
  125. .ToListAsync();
  126. return res;
  127. }
  128. /// <summary>
  129. /// 添加功能
  130. /// </summary>
  131. /// <param name="inputDto"></param>
  132. /// <returns></returns>
  133. public async Task<bool> AddProductFunction(ProductFunctionBaseDto inputDto)
  134. {
  135. var data = _db.Queryable<BPA_ProductFunction>()
  136. .Where(a => a.Name == inputDto.Name && a.ProductId== inputDto.ProductId).ToList();
  137. if (data.Count > 0)
  138. {
  139. throw Oops.Oh("功能名称已存在");
  140. }
  141. else
  142. {
  143. if (!string.IsNullOrWhiteSpace(inputDto.DataJson))
  144. {
  145. if (!IsValidJson(inputDto.DataJson.Replace("\n", ""))) throw Oops.Oh("json字符格式不正确");
  146. }
  147. var res =await _db.Insertable(new BPA_ProductFunction()
  148. {
  149. Id = Guid.NewGuid().ToString(),
  150. Name = inputDto.Name,
  151. Status = CommonStatus.ENABLE,
  152. CreateAt = DateTime.Now,
  153. IsDeleted = 0,
  154. Type= inputDto.Type,
  155. ForeignKeyRe = inputDto.ForeignKeyRe,
  156. DeviceVersionKey = inputDto.DeviceVersionKey,
  157. DataType = inputDto.DataType,
  158. DataRange = inputDto.DataRange,
  159. Unit =inputDto.Unit,
  160. StepSize = inputDto.StepSize,
  161. EnumValue = inputDto.EnumValue,
  162. DataLength = inputDto.DataLength,
  163. BoolLabel = inputDto.BoolLabel,
  164. ReadWrite = inputDto.ReadWrite,
  165. Description=inputDto.Description,
  166. ProductId= inputDto.ProductId,
  167. EventType= inputDto.EventType,
  168. DataJson= inputDto.DataJson?.Replace("\n",""),
  169. }).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  170. return res > 0;
  171. }
  172. }
  173. /// <summary>
  174. /// 修改功能
  175. /// </summary>
  176. /// <param name="inputDto"></param>
  177. /// <returns></returns>
  178. public async Task<bool> UpdateProductFunction(ProductFunctionBaseDto inputDto)
  179. {
  180. var check = _db.Queryable<BPA_ProductFunction>().Any(a => a.Id != inputDto.Id && a.Name == inputDto.Name);
  181. if (check) throw Oops.Oh("功能名称已存在");
  182. if (!string.IsNullOrWhiteSpace(inputDto.DataJson))
  183. {
  184. if(!IsValidJson(inputDto.DataJson.Replace("\n", ""))) throw Oops.Oh("json字符格式不正确");
  185. }
  186. var data = _db.Queryable<BPA_ProductFunction>().Where(a => a.Id == inputDto.Id).First();
  187. data.Name = inputDto.Name;
  188. data.DeviceVersionKey = inputDto.DeviceVersionKey;
  189. data.ForeignKeyRe = inputDto.ForeignKeyRe;
  190. data.DataType = inputDto.DataType;
  191. data.DataRange = inputDto.DataRange;
  192. data.Unit = inputDto.Unit;
  193. data.StepSize = inputDto.StepSize;
  194. data.EnumValue = inputDto.EnumValue;
  195. data.DataLength = inputDto.DataLength;
  196. data.BoolLabel = inputDto.BoolLabel;
  197. data.ReadWrite = inputDto.ReadWrite;
  198. data.Description= inputDto.Description;
  199. data.EventType = inputDto.EventType;
  200. data.DataJson= inputDto.DataJson?.Replace("\n", "");
  201. data.Status = (CommonStatus)Enum.ToObject(typeof(CommonStatus), inputDto.Status);
  202. var res =await _db.Updateable(data).ExecuteCommandAsync();
  203. return res > 0;
  204. }
  205. /// <summary>
  206. /// 删除功能
  207. /// </summary>
  208. /// <param name="ids"></param>
  209. /// <returns></returns>
  210. public async Task<bool> DeleteProductFunction(List<string> ids)
  211. {
  212. var resEntity = _db.Queryable<BPA_ProductFunction>().Where(x => ids.Contains(x.Id)).ToList();
  213. var res =await _db.Deleteable(resEntity).ExecuteCommandAsync();
  214. return res > 0;
  215. }
  216. #endregion
  217. #region 功能参数
  218. /// <summary>
  219. /// 根据功能id查询功能参数
  220. /// </summary>
  221. /// <returns></returns>
  222. public async Task<List<BPA_ProductFunctionAction>> GetProductFunctionActionList(string productFunctionId)
  223. {
  224. var res = await _db.Queryable<BPA_ProductFunctionAction>().Where(x => x.IsDeleted == 0 && x.ProductFunctionId == productFunctionId)
  225. .OrderBy(i => i.Sort, OrderByType.Asc)
  226. .ToListAsync();
  227. return res;
  228. }
  229. /// <summary>
  230. /// 查询所功能参数
  231. /// </summary>
  232. /// <returns></returns>
  233. public async Task<List<BPA_ProductFunctionAction>> GetProductFunctionActionList()
  234. {
  235. var res = await _db.Queryable<BPA_ProductFunctionAction>().Where(x => x.IsDeleted == 0)
  236. .OrderBy(i => i.Sort, OrderByType.Asc)
  237. .ToListAsync();
  238. return res;
  239. }
  240. /// <summary>
  241. /// 添加功能参数
  242. /// </summary>
  243. /// <param name="inputDto"></param>
  244. /// <returns></returns>
  245. public async Task<bool> AddProductFunctionAction(ProductFunctionActionBaseDto inputDto)
  246. {
  247. var check = _db.Queryable<BPA_ProductFunctionAction>()
  248. .Any(a => a.ProductFunctionId == inputDto.ProductFunctionId && a.ActionName == inputDto.ActionName );
  249. if (check)
  250. {
  251. throw Oops.Oh("功能参数已存在");
  252. }
  253. else
  254. {
  255. var res = await _db.Insertable(new BPA_ProductFunctionAction()
  256. {
  257. ProductFunctionId = inputDto.ProductFunctionId,
  258. ActionName = inputDto.ActionName,
  259. ActionType = inputDto.ActionType,
  260. ActionValue = inputDto.ActionValue,
  261. Unit = inputDto.Unit,
  262. // IsBatch = true,//inputDto.IsBatch,
  263. IsDeleted = 0,
  264. Sort = inputDto.Sort,
  265. }).CallEntityMethod(m => m.Create()).ExecuteCommandAsync(); ;
  266. return res > 0;
  267. }
  268. }
  269. /// <summary>
  270. /// 修改功能参数
  271. /// </summary>
  272. /// <param name="inputDto"></param>
  273. /// <returns></returns>
  274. public async Task<bool> UpdateProductFunctionAction(ProductFunctionActionBaseDto inputDto)
  275. {
  276. var check = _db.Queryable<BPA_ProductFunctionAction>().Any(a => a.ProductFunctionId == inputDto.ProductFunctionId && a.ActionName == inputDto.ActionName && a.ActionType == inputDto.ActionType && a.Id != inputDto.Id);
  277. if (check) throw Oops.Oh("功能参数已存在");
  278. var data = _db.Queryable<BPA_ProductFunctionAction>().Where(a => a.Id == inputDto.Id).First();
  279. if (data == null) throw Oops.Oh("功能参数不存在");
  280. data.ActionName = inputDto.ActionName;
  281. data.ActionType = inputDto.ActionType;
  282. data.ActionValue = inputDto.ActionValue;
  283. data.Unit = inputDto.Unit;
  284. // data.IsBatch = inputDto.IsBatch;
  285. data.Sort = inputDto.Sort;
  286. var res = await _db.Updateable(data).ExecuteCommandAsync();
  287. return res > 0;
  288. }
  289. /// <summary>
  290. /// 删除功能参数
  291. /// </summary>
  292. /// <param name="ids"></param>
  293. /// <returns></returns>
  294. public async Task<bool> DeleteProductFunctionAction(List<string> ids)
  295. {
  296. var resEntity = _db.Queryable<BPA_ProductFunctionAction>().Where(x => ids.Contains(x.Id)).ToList();
  297. foreach (var item in resEntity)
  298. {
  299. item.IsDeleted = 1;
  300. }
  301. var res = await _db.Updateable(resEntity).ExecuteCommandAsync();
  302. return res > 0;
  303. }
  304. #endregion
  305. private bool IsValidJson(string jsonString)
  306. {
  307. try
  308. {
  309. JsonConvert.DeserializeObject<object>(jsonString);
  310. return true;
  311. }
  312. catch (Exception ex)
  313. {
  314. Console.WriteLine("Invalid JSON string: " + ex.Message);
  315. return false;
  316. }
  317. }
  318. }
  319. }