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.
 
 

265 lines
16 KiB

  1. using BPA.KitChen.GroupMeal.Application.BaseDto;
  2. using BPA.KitChen.GroupMeal.Application.Service.Device.PushDto.Batching;
  3. using BPA.KitChen.GroupMeal.Application.Service.Device.PushDto.Goods;
  4. using BPA.KitChen.GroupMeal.Application.Service.Device.PushDto.GoodsTechnology;
  5. using BPA.KitChen.GroupMeal.Application.Service.Device.PushDto.ProductFunction;
  6. using BPA.KitChen.GroupMeal.Core.Common.Const;
  7. using Furion;
  8. using Furion.FriendlyException;
  9. using Furion.RemoteRequest.Extensions;
  10. using Newtonsoft.Json;
  11. using System.Reflection;
  12. namespace BPA.KitChen.GroupMeal.Application.Service
  13. {
  14. public class RequestParmsHeadlen
  15. {
  16. private string BaseServerUrl = App.GetConfig<string>("SAAS_Manage");
  17. public async Task<dynamic> GetParm(int type, string data, string deviceId)
  18. {
  19. var getProductFunctionurl = BaseServerUrl + "api/productfunction/getproductfunction_alm?deviceId=" + deviceId;//获取商品属性
  20. var responseProductFunction = await getProductFunctionurl.SetHttpMethod(HttpMethod.Get).GetAsStringAsync();
  21. var resProductFunction = JsonConvert.DeserializeObject<ResponDataBase>(responseProductFunction);
  22. if (resProductFunction.statusCode != "200") throw Oops.Oh("获取该设备的物模型数据失败");
  23. if (resProductFunction.data == null) throw Oops.Oh("找不到设备的物模型配置,请配置相关参数后再试");
  24. var dataProductFunction = JsonConvert.DeserializeObject<List<ProductFunctionDto>>(resProductFunction.data.ToString());
  25. dynamic obj = null;
  26. switch (type)
  27. {
  28. case 1: //商品下发
  29. var GoodsFunction = dataProductFunction.Where(x => x.Name == "商品数据下发" && x.Type == "1").FirstOrDefault();
  30. if (GoodsFunction == null) throw Oops.Oh("该设备的物模型还没配置对应功能,请配置相关参数后再试");
  31. var GoodsFunctionValue = GoodsFunction.ProductFunctionValue.Where(x => x.IsDefault == true && x.ActionName == "DataSoure").First();
  32. if (string.IsNullOrEmpty(GoodsFunctionValue.ActionValue)) throw Oops.Oh("该设备的物模型配置对应功能参数没有配置,请配置相关参数后再试");
  33. obj = await GetGoods(data, GoodsFunctionValue.ActionValue);
  34. break;
  35. case 2://物料下发
  36. var BatchingFunction = dataProductFunction.Where(x => x.Name == "物料数据下发" && x.Type == "1").FirstOrDefault();
  37. if (BatchingFunction == null) throw Oops.Oh("该设备的物模型还没配置对应功能,请配置相关参数后再试");
  38. var BatchingFunctionValue = BatchingFunction.ProductFunctionValue.Where(x => x.IsDefault == true && x.ActionName == "DataSoure").First();
  39. if (string.IsNullOrEmpty(BatchingFunctionValue.ActionValue)) throw Oops.Oh("该设备的物模型配置对应功能参数没有配置,请配置相关参数后再试");
  40. obj = await GetBatching(data, BatchingFunctionValue.ActionValue);
  41. break;
  42. case 4:
  43. var TechnologyFunction = dataProductFunction.Where(x => x.Name == "工艺数据下发" && x.Type == "1").FirstOrDefault();
  44. if (TechnologyFunction == null) throw Oops.Oh("该设备的物模型还没配置对应功能,请配置相关参数后再试");
  45. var TechnologyFunctionValue = TechnologyFunction.ProductFunctionValue.Where(x => x.IsDefault == true && x.ActionName == "DataSoure").First();
  46. if (string.IsNullOrEmpty(TechnologyFunctionValue.ActionValue)) throw Oops.Oh("该设备的物模型配置对应功能参数没有配置,请配置相关参数后再试");
  47. obj = await GetGoodsTechnology(data, deviceId, TechnologyFunctionValue.ActionValue);
  48. break;
  49. }
  50. return obj;
  51. }
  52. private async Task<dynamic> GetGoods(string data, string productFunctionData)
  53. {
  54. try
  55. {
  56. //商品信息
  57. var goodsData = JsonConvert.DeserializeObject<List<GoodsInfoDto>>(data);
  58. var cpjson = productFunctionData.Replace("\r", "").Replace("\n", "").Replace("\t", "");
  59. var DynamicObject = JsonConvert.DeserializeObject<dynamic>(cpjson);
  60. //商品信息
  61. if (IsPropertyExist(DynamicObject, ProductParametersConst_Goods.goodsinfo))
  62. {
  63. var goodsinfo = GetDynamic(goodsData, DynamicObject.goodsinfo[0]);
  64. DynamicObject.goodsinfo = JsonConvert.DeserializeObject<dynamic>(JsonConvert.SerializeObject(goodsinfo));
  65. }
  66. //商品属性
  67. if (IsPropertyExist(DynamicObject, ProductParametersConst_Goods.goodsAttribute))
  68. {
  69. //获取商品属性
  70. var getGoodsAttributeurl = BaseServerUrl + "api/goodsattribute/getgoodsattributelist_alm";//获取商品属性
  71. var responseGoodsAttribute = await getGoodsAttributeurl.SetHeaders(new
  72. {
  73. groupId = App.User?.FindFirst(ClaimConst.GroupId)?.Value
  74. }).SetHttpMethod(HttpMethod.Get).GetAsStringAsync();
  75. var resGoodsAttribute = JsonConvert.DeserializeObject<ResponDataBase>(responseGoodsAttribute);
  76. if (resGoodsAttribute.statusCode != "200") throw Oops.Oh("获取商品属性数据失败");
  77. var dataGoodsAttribute = JsonConvert.DeserializeObject<List<GoodsAttributeDto>>(resGoodsAttribute.data.ToString());
  78. var goodsAttribute = GetDynamic(dataGoodsAttribute, DynamicObject.goodsAttribute[0]);
  79. DynamicObject.goodsAttribute = JsonConvert.DeserializeObject<dynamic>(JsonConvert.SerializeObject(goodsAttribute));
  80. }
  81. //商品单位
  82. if (IsPropertyExist(DynamicObject, ProductParametersConst_Goods.goodsUnitInfo))
  83. {
  84. //获商品单位
  85. var getGoodsUinturl = BaseServerUrl + "api/goods/getgoodsuintlist_alm";//获商品单位
  86. var responseGoodsUint = await getGoodsUinturl.SetHeaders(new
  87. {
  88. groupId = App.User?.FindFirst(ClaimConst.GroupId)?.Value
  89. }).SetHttpMethod(HttpMethod.Get).GetAsStringAsync();
  90. var resGoodsUint = JsonConvert.DeserializeObject<ResponDataBase>(responseGoodsUint);
  91. if (resGoodsUint.statusCode != "200") throw Oops.Oh("获取商品单位数据失败");
  92. var dataGoodsUint = JsonConvert.DeserializeObject<List<GoodsUintDto>>(resGoodsUint.data.ToString());
  93. var goodsUnitInfo = GetDynamic(dataGoodsUint, DynamicObject.goodsUnitInfo[0]);
  94. DynamicObject.goodsUnitInfo = JsonConvert.DeserializeObject<dynamic>(JsonConvert.SerializeObject(goodsUnitInfo));
  95. }
  96. //商品分类
  97. if (IsPropertyExist(DynamicObject, ProductParametersConst_Goods.goodsTypeInfo))
  98. {
  99. //获取商品分类
  100. var getGoodsTypeturl = BaseServerUrl + "api/goodstype/getgoodstypelist_alm";//获取商品分类
  101. var responseGoodsType = await getGoodsTypeturl.SetHeaders(new
  102. {
  103. groupId = App.User?.FindFirst(ClaimConst.GroupId)?.Value
  104. }).SetHttpMethod(HttpMethod.Get).GetAsStringAsync();
  105. var resGoodsType = JsonConvert.DeserializeObject<ResponDataBase>(responseGoodsType);
  106. if (resGoodsType.statusCode != "200") throw Oops.Oh("获取商品分类数据失败");
  107. var dataGoodsType = JsonConvert.DeserializeObject<List<GoodsTypeDto>>(resGoodsType.data.ToString());
  108. var goodsTypeInfo = GetDynamic(dataGoodsType, DynamicObject.goodsTypeInfo[0]);
  109. DynamicObject.goodsTypeInfo = JsonConvert.DeserializeObject<dynamic>(JsonConvert.SerializeObject(goodsTypeInfo));
  110. }
  111. return DynamicObject;
  112. }
  113. catch (Exception e)
  114. {
  115. throw Oops.Oh("解析数据失败");
  116. }
  117. }
  118. private async Task<dynamic> GetBatching(string data, string productFunctionData)
  119. {
  120. try
  121. {
  122. var cpjson = productFunctionData.Replace("\r", "").Replace("\n", "").Replace("\t", "");
  123. var DynamicObject = JsonConvert.DeserializeObject<dynamic>(cpjson);
  124. if (IsPropertyExist(DynamicObject, ProductParametersConst_Batching.batchinginfo))
  125. {
  126. //物料信息
  127. var batchingData = JsonConvert.DeserializeObject<List<BatchingDto>>(data);
  128. var batchinginfo = GetDynamic(batchingData, DynamicObject.batchinginfo[0]);
  129. DynamicObject.batchinginfo = JsonConvert.DeserializeObject<dynamic>(JsonConvert.SerializeObject(batchinginfo));
  130. }
  131. if (IsPropertyExist(DynamicObject, ProductParametersConst_Batching.batchingTypeinfo))
  132. {
  133. //获取物料分类
  134. var getTypeurl = BaseServerUrl + "api/batching/getbatchingtypelist_alm";//获取物料分类
  135. var responseType = await getTypeurl.SetHeaders(new
  136. {
  137. groupId = App.User?.FindFirst(ClaimConst.GroupId)?.Value
  138. }).SetHttpMethod(HttpMethod.Get).GetAsStringAsync();
  139. var resType = JsonConvert.DeserializeObject<ResponDataBase>(responseType);
  140. if (resType.statusCode != "200") throw Oops.Oh("获取物料分类数据失败");
  141. var dataType = JsonConvert.DeserializeObject<List<BatchingTypeDto>>(resType.data.ToString());
  142. var batchingTypeinfo = GetDynamic(dataType, DynamicObject.batchingTypeinfo[0]);
  143. DynamicObject.batchingTypeinfo = JsonConvert.DeserializeObject<dynamic>(JsonConvert.SerializeObject(batchingTypeinfo));
  144. }
  145. if (IsPropertyExist(DynamicObject, ProductParametersConst_Batching.batchingUnitinfo))
  146. {
  147. //获取物料单位
  148. var getuinurl = BaseServerUrl + "api/batching/getbatchinguintList_alm";//获取物料单位
  149. var responseUnit = await getuinurl.SetHeaders(new
  150. {
  151. groupId = App.User?.FindFirst(ClaimConst.GroupId)?.Value
  152. }).SetHttpMethod(HttpMethod.Get).GetAsStringAsync();
  153. var resUnit = JsonConvert.DeserializeObject<ResponDataBase>(responseUnit);
  154. if (resUnit.statusCode != "200") throw Oops.Oh("获取物料单位数据失败");
  155. var dataUnit = JsonConvert.DeserializeObject<List<BatchingUintDto>>(resUnit.data.ToString());
  156. var batchingUnitinfo = GetDynamic(dataUnit, DynamicObject.batchingUnitinfo[0]);
  157. DynamicObject.batchingUnitinfo = JsonConvert.DeserializeObject<dynamic>(JsonConvert.SerializeObject(batchingUnitinfo));
  158. }
  159. return DynamicObject;
  160. }
  161. catch (Exception)
  162. {
  163. throw Oops.Oh("解析数据失败");
  164. }
  165. }
  166. private async Task<dynamic> GetGoodsTechnology(string data, string deviceId, string productFunctionData)
  167. {
  168. try
  169. {
  170. //商品工艺信息
  171. var goodsTechnologData = JsonConvert.DeserializeObject<List<GoodsTechnologysDto>>(data);
  172. var cpjson = productFunctionData.Replace("\r", "").Replace("\n", "").Replace("\t", "");
  173. var DynamicObject = JsonConvert.DeserializeObject<dynamic>(cpjson);
  174. if (IsPropertyExist(DynamicObject, ProductParametersConst_Technology.technologysInfo))
  175. {
  176. //获取工艺基础信息
  177. var gettechnologurl = BaseServerUrl + "api/technology/gettechnologylist_alm?deviceId=" + deviceId;//获取工艺基础信息
  178. var responsegetTechnologurl = await gettechnologurl.SetHeaders(new
  179. {
  180. groupId = App.User?.FindFirst(ClaimConst.GroupId)?.Value
  181. }).SetHttpMethod(HttpMethod.Get).GetAsStringAsync();
  182. var resTechnolog = JsonConvert.DeserializeObject<ResponDataBase>(responsegetTechnologurl);
  183. if (resTechnolog.statusCode != "200") throw Oops.Oh("获取工艺基础信息数据失败");
  184. var dataTechnolog = JsonConvert.DeserializeObject<List<TechnologyDto>>(resTechnolog.data.ToString());
  185. var technologysInfo = GetDynamic(dataTechnolog, DynamicObject.technologysInfo[0]);
  186. DynamicObject.technologysInfo = JsonConvert.DeserializeObject<dynamic>(JsonConvert.SerializeObject(technologysInfo));
  187. }
  188. if (IsPropertyExist(DynamicObject, ProductParametersConst_Technology.goodsTechnologysInfo))
  189. {
  190. var goodsTechnologysInfo = GetDynamic(goodsTechnologData, DynamicObject.goodsTechnologysInfo[0]);
  191. DynamicObject.goodsTechnologysInfo = JsonConvert.DeserializeObject<dynamic>(JsonConvert.SerializeObject(goodsTechnologysInfo));
  192. }
  193. return DynamicObject;
  194. }
  195. catch (Exception)
  196. {
  197. throw Oops.Oh("解析数据失败");
  198. }
  199. }
  200. /// <summary>
  201. /// 映射数据
  202. /// </summary>
  203. /// <param name="data1"></param>
  204. /// <param name="data2"></param>
  205. /// <returns></returns>
  206. private List<Dictionary<string, object>> GetDynamic(dynamic data1, dynamic data2)
  207. {
  208. List<Dictionary<string, object>> objws = new();
  209. foreach (var item in data1)
  210. {
  211. PropertyInfo[] properties = item.GetType().GetProperties(); // 获取所有公共属性
  212. Dictionary<string, object> objw = new();
  213. foreach (var property in properties)
  214. {
  215. object value = property.GetValue(item);
  216. foreach (var property1 in data2)
  217. {
  218. if (property.Name.ToLower() == property1?.Name?.ToLower())
  219. {
  220. Type dataType = property.PropertyType;
  221. if (dataType.IsGenericType)
  222. {
  223. var sd = data2[property1.Name][0];
  224. objw.Add(property1.Name, GetDynamic(value, sd));
  225. }
  226. else
  227. {
  228. objw.Add(property1.Name, value);
  229. }
  230. }
  231. }
  232. }
  233. objws.Add(objw);
  234. }
  235. return objws;
  236. }
  237. /// <summary>
  238. /// 验证属性是否存在
  239. /// </summary>
  240. /// <param name="data"></param>
  241. /// <param name="propertyname"></param>
  242. /// <returns></returns>
  243. private bool IsPropertyExist(dynamic data, string propertyname)
  244. {
  245. var check = false;
  246. var sd = data.GetType().GetProperty(propertyname);
  247. foreach (var property1 in data)
  248. {
  249. if (property1.Name == propertyname) check = true;
  250. }
  251. return check;
  252. }
  253. }
  254. }