From 8b0905d85891a57ca1264746213822efb6026001 Mon Sep 17 00:00:00 2001 From: shanj <18996038927@163.com> Date: Mon, 27 Nov 2023 22:04:54 +0800 Subject: [PATCH] =?UTF-8?q?pjzs=E6=94=AF=E6=8C=81=E7=BB=93=E6=9E=84?= =?UTF-8?q?=E5=8C=96=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BBWY.JDSDK/Request/AttrValueAliasJson.cs | 13 ++ ...ategoryReadFindSaleAttrTemplatesRequest.cs | 17 +++ ...tegoryReadFindSaleAttrTemplatesResponse.cs | 8 ++ .../PlatformSDK/JDBusiness.cs | 116 ++++++++++++++---- 4 files changed, 133 insertions(+), 21 deletions(-) create mode 100644 BBWY.JDSDK/Request/AttrValueAliasJson.cs create mode 100644 BBWY.JDSDK/Request/CategoryReadFindSaleAttrTemplatesRequest.cs create mode 100644 BBWY.JDSDK/Response/CategoryReadFindSaleAttrTemplatesResponse.cs diff --git a/BBWY.JDSDK/Request/AttrValueAliasJson.cs b/BBWY.JDSDK/Request/AttrValueAliasJson.cs new file mode 100644 index 00000000..7c326401 --- /dev/null +++ b/BBWY.JDSDK/Request/AttrValueAliasJson.cs @@ -0,0 +1,13 @@ +namespace BBWY.JDSDK.Request +{ + public class AttrValueAliasJson + { + public int id { get; set; } + + public string unit { get; set; } + + public string value { get; set; } + + public int? valueId { get; set; } + } +} diff --git a/BBWY.JDSDK/Request/CategoryReadFindSaleAttrTemplatesRequest.cs b/BBWY.JDSDK/Request/CategoryReadFindSaleAttrTemplatesRequest.cs new file mode 100644 index 00000000..dc16eecb --- /dev/null +++ b/BBWY.JDSDK/Request/CategoryReadFindSaleAttrTemplatesRequest.cs @@ -0,0 +1,17 @@ +using Jd.Api.Response; +using System.Collections.Generic; + +namespace Jd.Api.Request +{ + public class CategoryReadFindSaleAttrTemplatesRequest : JdRequestBase + { + public override string ApiName => "jingdong.category.read.findSaleAttrTemplates"; + + public int cid { get; set; } + + protected override void PrepareParam(IDictionary paramters) + { + paramters.Add("cid", cid); + } + } +} diff --git a/BBWY.JDSDK/Response/CategoryReadFindSaleAttrTemplatesResponse.cs b/BBWY.JDSDK/Response/CategoryReadFindSaleAttrTemplatesResponse.cs new file mode 100644 index 00000000..418efffe --- /dev/null +++ b/BBWY.JDSDK/Response/CategoryReadFindSaleAttrTemplatesResponse.cs @@ -0,0 +1,8 @@ +using Jd.Api; + +namespace Jd.Api.Response +{ + public class CategoryReadFindSaleAttrTemplatesResponse : JdResponse + { + } +} diff --git a/BBWY.Server.Business/PlatformSDK/JDBusiness.cs b/BBWY.Server.Business/PlatformSDK/JDBusiness.cs index b99eb336..f080ddf6 100644 --- a/BBWY.Server.Business/PlatformSDK/JDBusiness.cs +++ b/BBWY.Server.Business/PlatformSDK/JDBusiness.cs @@ -989,26 +989,85 @@ namespace BBWY.Server.Business #region 获取销售属性 stepText = "获取销售属性"; - IList colorSaleAttrs = null; + var attrId = string.Empty; + //IList colorSaleAttrs = null; + //{ + // var req = new CategoryReadFindAttrsByCategoryIdUnlimitCateRequest(); + // req.cid = long.Parse(request.MainProductCategoryId); + // req.attributeType = 4; + // req.field = "attrValueList,attrGroup"; + // var response = jdClient.Execute(req, request.AppToken, DateTime.Now.ToLocalTime()); + // if (response.Json == null) + // response.Json = JObject.Parse(response.Body); + // var colorProperty = response.Json["jingdong_category_read_findAttrsByCategoryIdUnlimitCate_responce"]["findattrsbycategoryidunlimitcate_result"].FirstOrDefault(j => j.Value("name") == "颜色"); + // if (colorProperty == null) + // { + // colorProperty = response.Json["jingdong_category_read_findAttrsByCategoryIdUnlimitCate_responce"]["findattrsbycategoryidunlimitcate_result"].FirstOrDefault(j => j["attrValueList"] != null && j["attrValueList"].Count() > 0); + // } + // if (colorProperty == null) + // { + // RollBackWhenStartPromotionError(request.AppKey, request.AppSecret, request.AppToken, giftSkuIdList, request.MainProductSpu, request.FullTitle, brandName, haveGiftTemplateSku); + // throw new BusinessException("缺少销售属性"); + // } + // colorSaleAttrs = colorProperty["attrValueList"].ToList(); + //} + List strutsSaleAttrValueList = new List(); + var isStruts = false; { - var req = new CategoryReadFindAttrsByCategoryIdUnlimitCateRequest(); - req.cid = long.Parse(request.MainProductCategoryId); - req.attributeType = 4; - req.field = "attrValueList,attrGroup"; - var response = jdClient.Execute(req, request.AppToken, DateTime.Now.ToLocalTime()); - if (response.Json == null) - response.Json = JObject.Parse(response.Body); - var colorProperty = response.Json["jingdong_category_read_findAttrsByCategoryIdUnlimitCate_responce"]["findattrsbycategoryidunlimitcate_result"].FirstOrDefault(j => j.Value("name") == "颜色"); - if (colorProperty == null) + var req = new CategoryReadFindSaleAttrTemplatesRequest(); + req.cid = int.Parse(request.MainProductCategoryId); + var res = jdClient.Execute(req, request.AppToken, DateTime.Now.ToLocalTime()); + if (res.IsError) { - colorProperty = response.Json["jingdong_category_read_findAttrsByCategoryIdUnlimitCate_responce"]["findattrsbycategoryidunlimitcate_result"].FirstOrDefault(j => j["attrValueList"] != null && j["attrValueList"].Count() > 0); + var errorMsg = string.IsNullOrEmpty(res.ErrorMsg) ? res.ErrMsg : res.ErrorMsg; + throw new BusinessException(errorMsg); } - if (colorProperty == null) + if (res.Json == null) + res.Json = JObject.Parse(res.Body); + + if (res.Json["jingdong_category_read_findSaleAttrTemplates_responce"]["categoryAttrTemplates"]["templateType"].ToString() == "1") { - RollBackWhenStartPromotionError(request.AppKey, request.AppSecret, request.AppToken, giftSkuIdList, request.MainProductSpu, request.FullTitle, brandName, haveGiftTemplateSku); - throw new BusinessException("缺少销售属性"); + //非结构属性 + var tempDataJarray = JArray.Parse(res.Json["jingdong_category_read_findSaleAttrTemplates_responce"]["categoryAttrTemplates"]["templateData"].ToString()); + var colorProperty = tempDataJarray.FirstOrDefault(j => j.Value("name") == "颜色"); + if (colorProperty == null) + colorProperty = tempDataJarray.FirstOrDefault(j => j["attrValueList"] != null && j["attrValueList"].Count() > 0); + if (colorProperty == null) + { + RollBackWhenStartPromotionError(request.AppKey, request.AppSecret, request.AppToken, giftSkuIdList, request.MainProductSpu, request.FullTitle, brandName, haveGiftTemplateSku); + throw new BusinessException("缺少销售属性"); + } + var colorSaleAttrs = colorProperty["attrValueList"].ToList(); + attrId = colorSaleAttrs[0].Value("attId"); + } + else if (res.Json["jingdong_category_read_findSaleAttrTemplates_responce"]["categoryAttrTemplates"]["templateType"].ToString() == "2") + { + isStruts = true; + var tempDataJarray = JArray.Parse(res.Json["jingdong_category_read_findSaleAttrTemplates_responce"]["categoryAttrTemplates"]["templateData"].ToString()); + var tempDataJToken = tempDataJarray.FirstOrDefault(j => j.Value("valueRules").Contains("颜色")); + if (tempDataJToken == null) + { + RollBackWhenStartPromotionError(request.AppKey, request.AppSecret, request.AppToken, giftSkuIdList, request.MainProductSpu, request.FullTitle, brandName, haveGiftTemplateSku); + throw new BusinessException("缺少结构化属性-颜色"); + } + attrId = tempDataJToken.Value("attrId"); + var tempDataValuesJToken = JObject.Parse(tempDataJToken["valueRules"].ToString()); + var tempDataValues_Level1_JToken = tempDataValuesJToken["items"][0]; + if (tempDataValues_Level1_JToken["properties"]["id"]["type"].ToString() == "model") + { + //[{\"id\":5,\"unit\":\"\",\"value\":\"红色\","valueId":1234567}] + var tempJarray = tempDataValues_Level1_JToken["properties"]["value"]["enum"][0]["preprocessingSaleAttrValTemplateValueList"] as JArray; + strutsSaleAttrValueList.AddRange(tempJarray.Select(j => new AttrValueAliasJson + { + id = int.Parse(tempDataValues_Level1_JToken["properties"]["id"]["enum"][0].ToString()), + value = j.Value("name"), + valueId = j.Value("id"), + unit = string.Empty + })); + } + else + throw new BusinessException($"不支持的properties.id.type {tempDataValues_Level1_JToken["properties"]["id"]["type"]}"); } - colorSaleAttrs = colorProperty["attrValueList"].ToList(); } #endregion @@ -1061,12 +1120,26 @@ namespace BBWY.Server.Business var skusParamList = new List(); var updateSkuTitleParamList = new List(); var imageWriteUpdateRequestList = new List(); - var skuIndex = noUsedIndexList.Count() - 1; + //var skuIndex = noUsedIndexList.Count() - 1; + var skuIndex = isStruts ? strutsSaleAttrValueList.Count() - 1 : noUsedIndexList.Count() - 1; + for (var i = 0; i < request.GiftTemplateSkuList.Count(); i++) { var giftSku = request.GiftTemplateSkuList[i]; //var colorPropertyValue = colorSaleAttrs[takeColorIndex]; + var attrValueAlias = string.Empty; + if (isStruts) + { + var value = strutsSaleAttrValueList[skuIndex]; + value.value = giftSku.Title; + attrValueAlias = JsonConvert.SerializeObject(new object[] { value }); + } + else + { + attrValueAlias = giftSku.Title; + } + var p = new SkuWriteUpdateSkusItem() { type = "com.jd.pop.ware.ic.api.domain.Sku", @@ -1080,8 +1153,8 @@ namespace BBWY.Server.Business new SkuWriteUpdateSkusItemSaleAttrs() { type = "com.jd.pop.ware.ic.api.domain.Prop", - attrId = colorSaleAttrs[0].Value("attId"), - attrValueAlias = new List(){ giftSku.Title }, + attrId = attrId, + attrValueAlias = new List(){ attrValueAlias }, index = noUsedIndexList[skuIndex], attrValuesSeqNo = new List(){ noUsedIndexList[skuIndex] } } @@ -1192,7 +1265,7 @@ namespace BBWY.Server.Business foreach (var ps in newMainProductSkuList) { - if (request.GiftTemplateSkuList.Any(x => x.Title == ps.Title)) + if (request.GiftTemplateSkuList.Any(x => x.Title == ps.Title || ps.Title.Contains(x.Title))) { //此sku是赠品sku //newSkuIdList.Add(ps.Id); @@ -1213,7 +1286,8 @@ namespace BBWY.Server.Business foreach (var giftsku in giftSkuList) { var skuJToken = giftsku.Source; - var skuTitle = skuJToken["saleAttrs"][0]["attrValueAlias"][0].ToString(); + //var skuTitle = skuJToken["saleAttrs"][0]["attrValueAlias"][0].ToString(); + var skuTitle = GetSkuTitle(skuJToken); var colorId = "0000000000"; try { @@ -1222,7 +1296,7 @@ namespace BBWY.Server.Business catch { } var currentImgIndex = imgIndex; imgIndex++; - var giftSku = request.GiftTemplateSkuList.FirstOrDefault(x => x.Title == skuTitle); + var giftSku = request.GiftTemplateSkuList.FirstOrDefault(x => x.Title == skuTitle || skuTitle.Contains(x.Title)); var imgUrl = giftSku.Logo.Substring(giftSku.Logo.IndexOf("jfs")); colorBuilder.Append($"{colorId},");