public async Task <NewtonsoftJsonActionResult <ProducListResponse> > GetProductList([FromQuery] ProducListRequest request) { return(await _storeService.QueryProductList(request)); }
public async Task <ProducListResponse> QueryProductList(ProducListRequest request) { var producListResponse = new ProducListResponse(); var dicProduct = new Dictionary <Guid, Product>(); var dicRightspackage = new Dictionary <Guid, Rightspackage>(); var dicRights = new Dictionary <Guid, JObject>(); #region 查询所有商品 var xdoc = await Task <XDocument> .Run(() => { var fetchXml = $@" <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'> <entity name='mcs_tc_product'> <order attribute='createdon' descending='true' /> <filter type='and'> <condition attribute='mcs_type' operator='in'> <value>7</value> <value>2</value> <value>1</value> </condition> <condition attribute='mcs_state' operator='eq' value='2' /> </filter> </entity> </fetch>"; return(XDocument.Parse(fetchXml)); }); var fetchRequest = new CrmRetrieveMultipleFetchRequestMessage() { EntityName = "mcs_tc_product", FetchXml = xdoc }; fetchRequest.Headers.Add(dicHeadKey, dicHead[dicHeadKey]); var fetchResponse = await _crmService.Execute(fetchRequest); var procudtResponse = fetchResponse as CrmRetrieveMultipleFetchResponseMessage; #endregion #region 查询商品的图片 xdoc = await Task <XDocument> .Run(() => { var fetchXml = $@" <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'> <entity name='mcs_tc_productimage'> <attribute name='mcs_tc_productimageid' /> <attribute name='mcs_imagetype' /> <attribute name='mcs_imagename' /> <attribute name='mcs_product' /> <order attribute='createdon' descending='true' /> <filter type='and'> <condition attribute='statecode' operator='eq' value='0' /> <condition attribute='mcs_product' operator='not-null' /> </filter> </entity> </fetch>"; return(XDocument.Parse(fetchXml)); }); fetchRequest = new CrmRetrieveMultipleFetchRequestMessage() { EntityName = "mcs_tc_productimage", FetchXml = xdoc }; fetchRequest.Headers.Add(dicHeadKey, dicHead[dicHeadKey]); fetchResponse = await _crmService.Execute(fetchRequest); var productImageResponse = fetchResponse as CrmRetrieveMultipleFetchResponseMessage; #endregion #region 查询商品的规格型号 xdoc = await Task <XDocument> .Run(() => { var fetchXml = $@" <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'> <entity name='mcs_tc_productspecification'> <attribute name='mcs_tc_productspecificationid' /> <attribute name='mcs_attributegroupname' /> <attribute name='mcs_attributename' /> <attribute name='mcs_attributevalue' /> <attribute name='mcs_product' /> <attribute name='mcs_attributegroupindex' /> <order attribute='createdon' descending='true' /> <filter type='and'> <condition attribute='statecode' operator='eq' value='0' /> <condition attribute='mcs_product' operator='not-null' /> </filter> </entity> </fetch>"; return(XDocument.Parse(fetchXml)); }); fetchRequest = new CrmRetrieveMultipleFetchRequestMessage() { EntityName = "mcs_tc_productspecification", FetchXml = xdoc }; fetchRequest.Headers.Add(dicHeadKey, dicHead[dicHeadKey]); fetchResponse = await _crmService.Execute(fetchRequest); var productSpecificationResponse = fetchResponse as CrmRetrieveMultipleFetchResponseMessage; #endregion #region 查询商品的订购属性 xdoc = await Task <XDocument> .Run(() => { var fetchXml = $@" <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'> <entity name='mcs_tc_productorderingattribute'> <filter type='and'> <condition attribute='statecode' operator='eq' value='0' /> <condition attribute='mcs_product' operator='not-null' /> </filter> </entity> </fetch>"; return(XDocument.Parse(fetchXml)); }); fetchRequest = new CrmRetrieveMultipleFetchRequestMessage() { EntityName = "mcs_tc_productorderingattribute", FetchXml = xdoc }; fetchRequest.Headers.Add(dicHeadKey, dicHead[dicHeadKey]); fetchResponse = await _crmService.Execute(fetchRequest); var productOrderingattributeResponse = fetchResponse as CrmRetrieveMultipleFetchResponseMessage; #endregion #region 查询商品的SKU xdoc = await Task <XDocument> .Run(() => { var fetchXml = $@" <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'> <entity name='mcs_tc_productprice'> <order attribute='createdon' descending='true' /> <filter type='and'> <condition attribute='statecode' operator='eq' value='0' /> <condition attribute='mcs_product' operator='not-null' /> </filter> </entity> </fetch>"; return(XDocument.Parse(fetchXml)); }); fetchRequest = new CrmRetrieveMultipleFetchRequestMessage() { EntityName = "mcs_tc_productprice", FetchXml = xdoc }; fetchRequest.Headers.Add(dicHeadKey, dicHead[dicHeadKey]); fetchResponse = await _crmService.Execute(fetchRequest); var productPriceResponse = fetchResponse as CrmRetrieveMultipleFetchResponseMessage; #endregion #region 查询商品SKU的订购关系(弃用) //xdoc = await Task<XDocument>.Run(() => //{ // var fetchXml = $@" // <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'> // <entity name='mcs_tc_skuattr'> // <order attribute='createdon' descending='true' /> // </entity> // </fetch>"; // return XDocument.Parse(fetchXml); //}); //fetchRequest = new CrmRetrieveMultipleFetchRequestMessage() //{ // EntityName = "mcs_tc_skuattr", // FetchXml = xdoc //}; //fetchRequest.Headers.Add(dicHeadKey, dicHead[dicHeadKey]); //fetchResponse = await _crmService.Execute(fetchRequest); //var skuattrResponse = fetchResponse as CrmRetrieveMultipleFetchResponseMessage; #endregion #region 查询商品的关联关系 xdoc = await Task <XDocument> .Run(() => { var fetchXml = $@" <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'> <entity name='mcs_tc_productrelated'> <order attribute='createdon' descending='true' /> <filter type='and'> <condition attribute='statecode' operator='eq' value='0' /> <condition attribute='mcs_product' operator='not-null' /> </filter> <link-entity name='mcs_tc_productprice' from='mcs_tc_productpriceid' to='mcs_relatedproductsku' link-type='outer' alias='a' visible='false'> <all-attributes/> </link-entity> </entity> </fetch>"; return(XDocument.Parse(fetchXml)); }); fetchRequest = new CrmRetrieveMultipleFetchRequestMessage() { EntityName = "mcs_tc_productrelated", FetchXml = xdoc }; fetchRequest.Headers.Add(dicHeadKey, dicHead[dicHeadKey]); fetchResponse = await _crmService.Execute(fetchRequest); var productrElatedArrayResponse = fetchResponse as CrmRetrieveMultipleFetchResponseMessage; #endregion #region 查询权益包 xdoc = await Task <XDocument> .Run(() => { var fetchXml = $@" <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'> <entity name='mcs_rc_rightspackage'> <order attribute='createdon' descending='true' /> </entity> </fetch>"; return(XDocument.Parse(fetchXml)); }); fetchRequest = new CrmRetrieveMultipleFetchRequestMessage() { EntityName = "mcs_rc_rightspackage", FetchXml = xdoc }; fetchRequest.Headers.Add(dicHeadKey, dicHead[dicHeadKey]); fetchResponse = await _crmService.Execute(fetchRequest); var rightspackageArrayResponse = fetchResponse as CrmRetrieveMultipleFetchResponseMessage; #endregion #region 查询权益项 xdoc = await Task <XDocument> .Run(() => { var fetchXml = $@" <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'> <entity name='mcs_rc_rights'> <order attribute='createdon' descending='true' /> </entity> </fetch>"; return(XDocument.Parse(fetchXml)); }); fetchRequest = new CrmRetrieveMultipleFetchRequestMessage() { EntityName = "mcs_rc_rights", FetchXml = xdoc }; fetchRequest.Headers.Add(dicHeadKey, dicHead[dicHeadKey]); fetchResponse = await _crmService.Execute(fetchRequest); var rightsArrayResponse = fetchResponse as CrmRetrieveMultipleFetchResponseMessage; #endregion #region 查询权益包与权益项的关系 xdoc = await Task <XDocument> .Run(() => { var fetchXml = $@" <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'> <entity name='mcs_rc_rightspackagedetail'> <order attribute='createdon' descending='true' /> </entity> </fetch>"; return(XDocument.Parse(fetchXml)); }); fetchRequest = new CrmRetrieveMultipleFetchRequestMessage() { EntityName = "mcs_rc_rightspackagedetail", FetchXml = xdoc }; fetchRequest.Headers.Add(dicHeadKey, dicHead[dicHeadKey]); fetchResponse = await _crmService.Execute(fetchRequest); var rightspackagedetailArrayResponse = fetchResponse as CrmRetrieveMultipleFetchResponseMessage; #endregion #region 查询权益包与商品的关系 xdoc = await Task <XDocument> .Run(() => { var fetchXml = $@" <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'> <entity name='mcs_rc_rightspackageproduct'> <order attribute='createdon' descending='true' /> </entity> </fetch>"; return(XDocument.Parse(fetchXml)); }); fetchRequest = new CrmRetrieveMultipleFetchRequestMessage() { EntityName = "mcs_rc_rightspackageproduct", FetchXml = xdoc }; fetchRequest.Headers.Add(dicHeadKey, dicHead[dicHeadKey]); fetchResponse = await _crmService.Execute(fetchRequest); var rightspackageproductArrayResponse = fetchResponse as CrmRetrieveMultipleFetchResponseMessage; #endregion #region 组装商品 foreach (var entity in procudtResponse.Value.Results) { var product = new Product(); product.ProductInfo = entity.Attributes; dicProduct.Add(entity.Id, product); } #endregion #region 组装商品图片 var host = "https://ceo-oss.oss-cn-hangzhou.aliyuncs.com/"; foreach (var entity in productImageResponse.Value.Results) { var productGuid = Guid.Parse(entity.Attributes.Value <string>("_mcs_product_value")); entity.Attributes.Add("ext_fullurl", host + entity.Attributes.Value <string>("mcs_imagename")); if (dicProduct.ContainsKey(productGuid)) { dicProduct[productGuid].ProductImageArray.Add(entity.Attributes); } } #endregion #region 组装商品规格型号 foreach (var entity in productSpecificationResponse.Value.Results) { var productGuid = Guid.Parse(entity.Attributes.Value <string>("_mcs_product_value")); if (dicProduct.ContainsKey(productGuid)) { dicProduct[productGuid].ProductSpecificationArray.Add(entity.Attributes); } } #endregion #region 组装商品的订购属性 foreach (var entity in productOrderingattributeResponse.Value.Results) { var productGuid = Guid.Parse(entity.Attributes.Value <string>("_mcs_product_value")); if (dicProduct.ContainsKey(productGuid)) { dicProduct[productGuid].ProductOrderingattributeArray.Add(entity.Attributes); } } #endregion #region 组装商品的关联关系 foreach (var entity in productrElatedArrayResponse.Value.Results) { var productGuid = Guid.Parse(entity.Attributes.Value <string>("_mcs_product_value")); if (dicProduct.ContainsKey(productGuid)) { dicProduct[productGuid].ProductRelatedArray.Add(entity.Attributes); } } #endregion #region 组装商品SKU订购关系Map (弃用) //var skuattrMap = new Dictionary<string, JObject>(); //foreach (var entity in skuattrResponse.Value.Results) //{ // var key = entity.Attributes.Value<string>("_mcs_sku_value"); // if (!skuattrMap.ContainsKey(key)) // { // skuattrMap.Add(key, new JObject()); // } // skuattrMap[key].Add(entity.Attributes.Value<string>("_mcs_attr_value"), new JObject()); //} #endregion #region 组装商品的SKU foreach (var entity in productPriceResponse.Value.Results) { var productGuid = Guid.Parse(entity.Attributes.Value <string>("_mcs_product_value")); //var key = entity.Id.ToString(); //if (skuattrMap.ContainsKey(key)) //{ // entity.Attributes.Add("skuattr", skuattrMap[key]); //} //else //{ // entity.Attributes.Add("skuattr", new JObject()); //} if (dicProduct.ContainsKey(productGuid)) { dicProduct[productGuid].ProductPriceArray.Add(entity.Attributes); } } #endregion #region 组装权益包 foreach (var entity in rightspackageArrayResponse.Value.Results) { var rightspackage = new Rightspackage(); rightspackage.RightspackageInfo = entity.Attributes; dicRightspackage.Add(entity.Id, rightspackage); } #endregion #region 组装权益项 foreach (var entity in rightsArrayResponse.Value.Results) { var rights = new JObject(); rights = entity.Attributes; dicRights.Add(entity.Id, rights); } #endregion #region 组装权益包与权益项的关系 foreach (var entity in rightspackagedetailArrayResponse.Value.Results) { var rightsGuid = Guid.Parse(entity.Attributes.Value <string>("_mcs_rights_value")); var rightspackageGuid = Guid.Parse(entity.Attributes.Value <string>("_mcs_rightspackage_value")); if (dicRights.ContainsKey(rightsGuid) && dicRightspackage.ContainsKey(rightspackageGuid)) { dicRightspackage[rightspackageGuid].RightsArray.Add(dicRights[rightsGuid]); } } #endregion #region 组装权益包与商品的关系 foreach (var entity in rightspackageproductArrayResponse.Value.Results) { var productGuid = Guid.Parse(entity.Attributes.Value <string>("_mcs_product_value")); if (entity.Attributes.Value <string>("_mcs_rightspackage_value") != null) { var rightspackageGuid = Guid.Parse(entity.Attributes.Value <string>("_mcs_rightspackage_value")); if (dicProduct.ContainsKey(productGuid) && dicRightspackage.ContainsKey(rightspackageGuid)) { dicProduct[productGuid].ProductRightspackageArray.Add(dicRightspackage[rightspackageGuid]); } } } #endregion #region 返回对象组装 foreach (var kv in dicProduct) { producListResponse.ProductList.Add(kv.Value); } #endregion return(producListResponse); }