Files
KJD/TEMUKJD/KJDForms/ProductForms/ProductChangePriceForm.cs
T
2025-12-04 20:45:18 +08:00

627 lines
26 KiB
C#

using AllBase;
using AllBase.Model.Result;
using NaiBa.Util.Extension;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using TemuKJD.BaseForms;
using TemuKJD.BLL;
using TemuKJD.Extension;
using TemuKJD.KJDForms.ShopForms;
namespace TemuKJD.KJDForms.ProductForms
{
/// <summary>
/// 商品调价(改价)
/// </summary>
public partial class ProductChangePriceForm : LRBaseForm
{
public bool IsAuto = false;
decimal wantPrice;
string skcId = "";
string productId = "";
string productName = "";
ShopLoginInfo shop = null;
TemuListProductInfo productInfo = null;
WarehouseProductSubOrderInfo wareHouseOrderInfo = null;
Dictionary<string, int> dicAdjustReason = new Dictionary<string, int>();
public string ErrMsg = "";
public bool IsAdjustPrice = false; // 是否调价成功
public ProductChangePriceForm(ShopLoginInfo _shop, WarehouseProductSubOrderInfo _wareHouseOrderInfo)
{
InitializeComponent();
shop = _shop;
wareHouseOrderInfo = _wareHouseOrderInfo;
skcId = wareHouseOrderInfo?.productSkcId.ParseToString();
productId = wareHouseOrderInfo?.productId.ParseToString();
productName = wareHouseOrderInfo?.productName.ParseToString();
}
//TemuListProductInfo
public ProductChangePriceForm(ShopLoginInfo _shop, TemuListProductInfo _prod, decimal _wantPrice = 0)
{
InitializeComponent();
shop = _shop;
productInfo = _prod;
skcId = productInfo?.productSkcId.ParseToString();
productId = productInfo?.productId.ParseToString();
productName = productInfo?.productName.ParseToString();
wantPrice = _wantPrice;
}
private void ProductChangePriceForm_Load(object sender, EventArgs e)
{
//https://agentseller.temu.com/api/kiana/mms/gmp/bg/magneto/api/price/priceAdjust/gmpProductBatchAdjustPrice
//{"result":{"priceAdjustReasonList":[
//{"id":1,"desc":"关闭jit,转备货"},
//{"id":2,"desc":"降价清仓"},
//{"id":3,"desc":"降价提升竞争力"},
//{"id":4,"desc":"促销"},
//{"id":14,"desc":"促销-商品有滞销风险,调申报价可促销变现。拒绝导致长期滞销,退供成本由商家承担。"},
//{"id":17,"desc":"爆款调价"},
//{"id":11,"desc":"jit涨运费"}]},
//"success":true,"errorCode":1000000,"errorMsg":null}
dicAdjustReason["关闭jit,转备货"] = 1;
dicAdjustReason["降价清仓"] = 2;
dicAdjustReason["降价提升竞争力"] = 3;
dicAdjustReason["促销"] = 4;
dicAdjustReason["促销-商品有滞销风险,调申报价可促销变现。拒绝导致长期滞销,退供成本由商家承担。"] = 14;
dicAdjustReason["爆款调价"] = 17;
dicAdjustReason["jit涨运费"] = 11;
this.dgvOrder.AddSelectorColumn("colSelect");
numDiscount.Visible = true;
numPriceDiff.Location = numDiscount.Location;
pbLoading.Visible = true;
Task.Run(() =>
{
GetProductAdjust();
Invoke(new Action(() =>
{
pbLoading.Visible = false;
}));
if (IsAuto)
{
if (!string.IsNullOrEmpty(ErrMsg))
return;
SaveNewPrice();
}
});
}
void GetProductAdjust()
{
if (shop == null || productId.IsNullOrEmpty() || skcId.IsNullOrEmpty() || productName.IsNullOrEmpty())
{
if (IsAuto)
ErrMsg = "店铺商品信息为空!";
else
BaseMsgForm.ShowError("店铺商品信息为空!");
return;
}
#region 检测登录
if (shop.LoginStatus2 != "国际版在线")
{
if (IsAuto)
ErrMsg = "店铺未登录!";
else
BaseMsgForm.ShowError("店铺未登录!");
return;
}
ShopListForm.Instance.CheckShopLogin(new string[] { shop.LoginName }, false);
if (shop.LoginStatus2 != "国际版在线")
{
if (IsAuto)
ErrMsg = "店铺未登录!";
else
BaseMsgForm.ShowError("店铺未登录!");
return;
}
#endregion
int tryCount = 0;
tryAgain:
var adjustRst = QueryProductAdjust(shop, productId.ParseToLong());
if (adjustRst.Contains("当前操作人数太多,请稍后重试") && tryCount < 5)
{
tryCount++;
Thread.Sleep(Randomx.GetId(500, 1000));
goto tryAgain;
}
if (!adjustRst.Contains("\"success\":true"))
{
string sErr = BaseRegex.GetStringJson(ref adjustRst, "errorMsg");
if (string.IsNullOrEmpty(sErr))
sErr = adjustRst;
if (IsAuto)
ErrMsg = "查询失败:" + sErr;
else
BaseMsgForm.ShowError("查询失败:" + sErr);
return;
}
try
{
var jObj = JObject.Parse(adjustRst);
var objStr = JsonHelper.SerializeObject(jObj["result"]);
var adjustPriceInfo = JsonHelper.DeserializeJsonToObject<AdjustPriceInfo>(objStr);
foreach (var item in adjustPriceInfo.spuAdjustResult[productId].skcItems[0].items)
{
int mainRowId = -1;
// 修改日常价格
Invoke(new Action(() =>
{
mainRowId = this.dgvOrder.Rows.Add(
true,
productId,
skcId,
item.productSkuId,
item.skuSpec, //属性集
"",
"日常价格", //申报价格类型
item.originSupplyPrice.ParseToDecimal() / 100m, //原申报价格
wantPrice == 0 ? "" : wantPrice.ToString("0.00"), //调整后的申报价格
"调价成功之后生成的交易订单",
""
);
this.dgvOrder.Rows[mainRowId].Height = 50;
}));
if (item?.marketingActivityPriceDTOList == null)
continue;
// 修改活动价格
foreach (var activityPriceDTO in item.marketingActivityPriceDTOList)
{
int rowId = -1;
Invoke(new Action(() =>
{
rowId = this.dgvOrder.Rows.Add(
IsAuto ? false : true,
productId,
skcId,
item.productSkuId,
item.skuSpec, //属性集
activityPriceDTO.activityInvitationId, //申报价格类型
activityPriceDTO.activityInvitationName, //申报价格类型
activityPriceDTO.supplyPrice.ParseToDecimal() / 100m, //原申报价格
"", //调整后的申报价格
"调价成功之后生成的交易订单",
""
);
this.dgvOrder.Rows[rowId].Height = 50;
}));
}
}
}
catch (Exception ex)
{
}
}
string QueryProductAdjust(ShopLoginInfo shop, long spuId)
{
string url = "https://agentseller.temu.com/api/kiana/mms/magneto/price-adjust/product-adjust-query";
string body;
body = @"{""items"":[{""supplierId"":" + shop.ShopId + @",""productId"":" + spuId + @"}]}";
string refere = "https://agentseller.temu.com/newon/product-select";
return HttpResponseBLL.PostResponse2(shop, url, body, refere);
}
string BatchAdjustPrice(ShopLoginInfo shop, AdjustParam adjustParam)
{
string url = "https://agentseller.temu.com/api/kiana/mms/gmp/bg/magneto/api/price/priceAdjust/gmpProductBatchAdjustPrice";
string body;
body = JsonHelper.SerializeObject(adjustParam);
string refere = "https://agentseller.temu.com/newon/product-select";
return HttpResponseBLL.PostResponse2(shop, url, body, refere);
}
private void chkDiscount_CheckedChanged(object sender, EventArgs e)
{
this.chkPriceDiff.Checked = !this.chkDiscount.Checked;
this.label1.Text = chkDiscount.Checked ? "降价折扣" : "降价差额";
this.label3.Text = chkDiscount.Checked ? "折" : "元";
this.numPriceDiff.Visible = !chkDiscount.Checked;
this.numDiscount.Visible = chkDiscount.Checked;
}
private void chkPriceDiff_CheckedChanged(object sender, EventArgs e)
{
this.chkDiscount.Checked = !this.chkPriceDiff.Checked;
}
private void btnMultiChangePrice_Click(object sender, EventArgs e)
{
txtReason.Focus();
if (chkDiscount.Checked)
{
foreach (DataGridViewRow row in dgvOrder.Rows)
{
if (row.Cells["colSelect"].Value.ParseToBool() == true)
{
row.Cells["colSkuNewPrice"].Value = (row.Cells["colSkuPrice"].Value.ParseToDecimal() * this.numDiscount.Value / 10m).ToString();
if (row.Cells["colSkuNewPrice"].Value.ParseToDecimal() >= row.Cells["colSkuPrice"].Value.ParseToDecimal())
{
row.Cells["colSkuNewPrice"].Value = (row.Cells["colSkuPrice"].Value.ParseToDecimal() - 0.01m).ToString();
}
}
}
}
else
{
foreach (DataGridViewRow row in dgvOrder.Rows)
{
if (row.Cells["colSelect"].Value.ParseToBool() == true)
{
row.Cells["colSkuNewPrice"].Value = (row.Cells["colSkuPrice"].Value.ParseToDecimal() - this.numPriceDiff.Value).ToString();
if (row.Cells["colSkuNewPrice"].Value.ParseToDecimal() >= row.Cells["colSkuPrice"].Value.ParseToDecimal())
{
row.Cells["colSkuNewPrice"].Value = (row.Cells["colSkuPrice"].Value.ParseToDecimal() - 0.01m).ToString();
}
}
}
}
}
private void btnReset_Click(object sender, EventArgs e)
{
txtReason.Focus();
foreach (DataGridViewRow row in dgvOrder.Rows)
{
if (row.Cells["colSelect"].Value.ParseToBool() == true)
{
row.Cells["colSkuNewPrice"].Value = "";
}
}
}
private void dgvOrder_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0 || e.ColumnIndex < 0)
return;
string columnName = dgvOrder.Columns[e.ColumnIndex].Name;
var row = dgvOrder.Rows[e.RowIndex];
switch (columnName)
{
case "colCheck":
row.Cells["colCheck"].Value = !row.Cells["colCheck"].Value.ParseToBool();
break;
}
}
public async void btnSave_Click(object sender, EventArgs e)
{
txtReason.Focus();
await Task.Run(() =>
{
Thread.Sleep(10);
});
Task.Run(() =>
{
SaveNewPrice();
});
}
public void SaveNewPrice()
{
if (shop == null || productId.IsNullOrEmpty() || skcId.IsNullOrEmpty() || productName.IsNullOrEmpty())
{
if (IsAuto)
ErrMsg = "店铺商品信息为空!";
else
BaseMsgForm.ShowError("店铺商品信息为空!");
return;
}
if (!dgvOrder.Rows.Cast<DataGridViewRow>().Any(r => r.Cells["colSelect"].Value.ParseToBool() == true))
{
if (IsAuto)
ErrMsg = "请至少勾选一项!" + dgvOrder.Rows.Cast<DataGridViewRow>().Count();
else
BaseMsgForm.ShowError("请至少勾选一项!"); // todo,查找自动设置出现次错误原因
return;
}
foreach (DataGridViewRow r in dgvOrder.Rows)
{
if (r.Cells["colSelect"].Value.ParseToBool() == true)
{
if (r.Cells["colSkuNewPrice"].Value.ParseToDecimal() == 0)
{
if (IsAuto)
ErrMsg = "请输入合理的新申报价格!";
else
BaseMsgForm.ShowError("请输入合理的新申报价格!");
return;
}
}
}
Invoke(new Action(() =>
{
pbLoading.Visible = true;
}));
#region 检测登录
if (shop.LoginStatus2 != "国际版在线")
{
if (IsAuto)
ErrMsg = "店铺未登录!";
else
BaseMsgForm.ShowError("店铺未登录!");
pbLoading.Visible = false;
return;
}
ShopListForm.Instance.CheckShopLogin(new string[] { shop.LoginName }, false);
if (shop.LoginStatus2 != "国际版在线")
{
if (IsAuto)
ErrMsg = "店铺未登录!";
else
BaseMsgForm.ShowError("店铺未登录!");
pbLoading.Visible = false;
return;
}
#endregion
AdjustParam param = new AdjustParam();
Invoke(new Action(() =>
{
if (dicAdjustReason.Keys.Contains(cmbChangePriceReason.Text))
param.adjustReason = dicAdjustReason[cmbChangePriceReason.Text];
if (!string.IsNullOrWhiteSpace(txtReason.Text))
param.reason = txtReason.Text.Trim();
param.adjustItems = new List<adjustItem>();
adjustItem adjustItem = new adjustItem()
{
productId = this.productId.ParseToLong(),
productName = this.productName,
productSkcId = this.skcId.ParseToLong(),
supplierId = this.shop.ShopId.ParseToLong(),
};
param.adjustItems.Add(adjustItem);
adjustItem.skuAdjustList = new List<skuAdjust>();
foreach (DataGridViewRow row in dgvOrder.Rows)
{
if (row.Cells["colSelect"].Value.ParseToBool() == true)
{
var adjItem = new skuAdjust
{
activityInvitationId = row.Cells["colAdjustPriceTypeId"].Value.ParseToLong(),
oldPriceCurrency = "CNY",
targetPriceCurrency = "CNY",
oldSupplyPrice = (row.Cells["colSkuPrice"].Value.ParseToDecimal() * 100).ParseToInt(),
syncPurchasePrice = 1,
targetSupplyPrice = (row.Cells["colSkuNewPrice"].Value.ParseToDecimal() * 100).ParseToInt(),
skuId = row.Cells["colSkuId"].Value.ParseToLong(),
};
adjustItem.skuAdjustList.Add(adjItem);
if (adjItem.activityInvitationId == 0)
adjItem.activityInvitationId = null;
}
}
}));
int tryCount = 0;
tryAgain:
//var adjustBatchRst = "\"success\":true";
var adjustBatchRst = BatchAdjustPrice(shop, param);
if (adjustBatchRst.Contains("当前操作人数太多,请稍后重试") && tryCount < 5)
{
tryCount++;
Thread.Sleep(Randomx.GetId(500, 1000));
goto tryAgain;
}
if (!adjustBatchRst.Contains("\"success\":true"))
{
string sErr = BaseRegex.GetStringJson(ref adjustBatchRst, "errorMsg");
if (string.IsNullOrEmpty(sErr))
sErr = adjustBatchRst;
sErr = sErr.Replace("\\n", "\n").Replace("\n", "\r\n").Replace("\r\r", "\r");
if (IsAuto)
ErrMsg = "调价失败:" + sErr;
else
BaseMsgForm.ShowError("调价失败:" + sErr);
Invoke(new Action(() =>
{
pbLoading.Visible = false;
}));
return;
}
// 更新统计列表的数据价格
foreach (DataGridViewRow row in dgvOrder.Rows)
{
Invoke(new Action(() =>
{
if (row.Cells["colSelect"].Value.ParseToBool() == true)
{
var adjItem = new skuAdjust
{
activityInvitationId = row.Cells["colAdjustPriceTypeId"].Value.ParseToLong(),
oldPriceCurrency = "CNY",
targetPriceCurrency = "CNY",
oldSupplyPrice = (row.Cells["colSkuPrice"].Value.ParseToDecimal() * 100).ParseToInt(),
syncPurchasePrice = 1,
targetSupplyPrice = (row.Cells["colSkuNewPrice"].Value.ParseToDecimal() * 100).ParseToInt(),
skuId = row.Cells["colSkuId"].Value.ParseToLong(),
};
if (adjItem.activityInvitationId == 0) // 为0的是商品基础价格,否则是活动价格
{
if (wareHouseOrderInfo != null)
{
var sku = wareHouseOrderInfo.skuQuantityDetailList.FirstOrDefault(s => s.productSkuId.ParseToLong() == adjItem.skuId);
if (sku != null)
sku.supplierPrice = adjItem.targetSupplyPrice;
}
if (productInfo != null)
{
var sku = productInfo.productSkuSummaries.FirstOrDefault(s => s.productSkuId.ParseToLong() == adjItem.skuId);
if (sku != null)
{
sku.supplierPrice = adjItem.targetSupplyPrice;
}
}
}
}
}));
}
Invoke(new Action(() =>
{
pbLoading.Visible = false;
}));
IsAdjustPrice = true;
if (!IsAuto)
{
BaseMsgForm.ShowInfo("调价成功!");
Invoke(new Action(() =>
{
this.Close();
}));
}
}
}
public class AdjustPriceInfo
{
public Dictionary<string, spuAdjustResultInfo> spuAdjustResult { get; set; } // key spu:543806635
public Dictionary<string, adjustResultInfo> adjustResult { get; set; } // key skc:1278480907
public object hasSelectAutoPassRights { get; set; }
}
public class spuAdjustResultInfo
{
public long? supplierId { get; set; }
public object semiHostedBindSiteIdList { get; set; }
public long? productId { get; set; }
public List<skcItemInfo> skcItems { get; set; }
public bool? apparel { get; set; } // false,
public bool? isBatchSkcAdjustPrice { get; set; } // false,
public bool? supportShutdownJustInTimeAndStockUp { get; set; } // false,
public bool? isShoe { get; set; } // null,
public bool? semiHosted { get; set; } // false,
public object semiHostedBindSiteNameList { get; set; } // null
}
public class skcItemInfo
{
public string imageUrl { get; set; } // "https://img.kwcdn.com/product/fancy/ae03173d-641a-4895-b25c-5ce13bd544d4.jpg"
public jitRaiseInfo jitRaiseInfo { get; set; }
public long? productSkcId { get; set; } // 1278480907
public List<itemInfo> items { get; set; }
public bool? supportShutdownJustInTimeAndStockUp { get; set; } // false
public string spec { get; set; } // "black"
}
public class jitRaiseInfo
{
public object notApparelSkuJitPriceInfo { get; set; } // null,
public string canNotApplyReason { get; set; } // "当前商品暂不支持涨JIT运费",
public object notApparelSkuNewestStatus { get; set; } // null,
public bool? canApplyJitRaise { get; set; } // false,
public object apparelSkcJitPriceInfo { get; set; } // null,
public object hasDownOrderAfterJitRaise { get; set; } // null,
public bool? isShowJitRaise { get; set; } // false,
public object apparelNewestStatus { get; set; } // null
}
public class itemInfo
{
public object orderStatus { get; set; } // null,
public object targetSupplyPrice { get; set; } // null,
public object forbid { get; set; } // null,
public int? priceBeforeExchange { get; set; } // 3384,
public object punishInfo { get; set; } // null,
public object forbidMsg { get; set; } // null,
public string currencyName { get; set; } // "人民币",
public int? reviewSupplyPrice { get; set; } // 7356,
public object suggestSupplyPrice { get; set; } // null,
public int? originSupplyPrice { get; set; } // 3384,
public bool syncPurchaseHitGray { get; set; } // true,
public object syncPurchasePrice { get; set; } // null,
public int? limitRemindSupplyPrice { get; set; } // 4384,
public string targetPriceCurrency { get; set; } // "CNY",
public long? productSkuId { get; set; } // 1922741923,
public string skuSpec { get; set; } // "black",
public object exchangeDesc { get; set; } // null,
public object supplyPriceCNY { get; set; } // null,
public string priceCurrency { get; set; } // "CNY",
public string specName { get; set; } // "black",
public object suggestPriceCurrency { get; set; } // null,
public List<marketingActivityPriceDTOInfo> marketingActivityPriceDTOList { get; set; }
public object forbidTag { get; set; } // null,
public object ratio { get; set; } // null
}
public class marketingActivityPriceDTOInfo
{
public int? supplyPrice { get; set; } // 2368,
public long? activityEndTime { get; set; } // 1762481729822,
public long? activityInvitationId { get; set; } // 2510080228784679,
public string activityInvitationName { get; set; } // "活动价格-清仓甩卖",
public long? activityStartTime { get; set; } // 1759630529822,
public long? targetSupplyPrice { get; set; } // null,
public long? afterSupplyPrice { get; set; } // null
}
public class adjustResultInfo
{
public long? supplierId { get; set; } // 634418214768016
public List<itemInfo> skuPriceList { get; set; }
public long? productId { get; set; } // 543806635
public bool? apparel { get; set; } // false
public long? productSkcId { get; set; } // 1278480907
public bool? supportShutdownJustInTimeAndStockUp { get; set; }
public bool? isShoe { get; set; } // null
}
//---------------------------------------------------------------------------------
public class AdjustParam
{
public int? adjustReason { get; set; }
public string reason { get; set; }
public List<adjustItem> adjustItems { get; set; }
}
public class adjustItem
{
public string productName { get; set; }
public long? productSkcId { get; set; }
public List<skuAdjust> skuAdjustList { get; set; }
public long? productId { get; set; } // 202140159,
public long? supplierId { get; set; } // 634418216147124
}
public class skuAdjust
{
public long? activityInvitationId { get; set; } // 2510080229923561
public string targetPriceCurrency { get; set; } // "CNY",
public string oldPriceCurrency { get; set; } // "CNY",
public int? oldSupplyPrice { get; set; } // 3505,
public long? skuId { get; set; } // 4960442242,
public int? targetSupplyPrice { get; set; } // 3504,
public int? syncPurchasePrice { get; set; } // 1
}
}