Files
KJD/TEMUKJD/WXForms/ProductForms/CloudGoodsForm.cs
T
2026-11-05 18:50:13 +08:00

485 lines
13 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using AllBase;
using AllBase.Helper;
using AllBase.Model.Result;
using AllBase.Model.Result.PDDResult;
using AllBase.Model.Result.WXResult;
using NaiBa.Util.Extension;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TemuKJD.BaseForms;
using TemuKJD.Extension;
using TemuKJD.ToolForms;
using TemuKJD.UserControls;
using Work;
using Work.GoodDetail;
using Work.Services;
namespace TemuKJD.WXForms.ProductForms
{
public partial class CloudGoodsForm : BaseForm
{
private GoodsSearchRequest search = new GoodsSearchRequest();
private List<ComboBox> catBoxList = new List<ComboBox>();
private BindingList<CloudGoods> dataList = new BindingList<CloudGoods>();
CheckBox chkSelectAll = null;
public CloudGoodsForm()
{
InitializeComponent();
}
private void AdminCardCodeForm_Load(object sender, EventArgs e)
{
pbLoading.Visible = false;
ucSplitPage1.PageChange += UcSplitPage1_PageChange;
InitCategory();
InitBindingDataGridView();
num_minsold.Value = 0;
num_maxsold.Value = 9999999;
num_minprice.Value = 0;
num_maxprice.Value = 999;
cb_Platform.SelectedIndex = 0;
ucSplitPage1.TotalCount = 0;
if (EnvironmentHelper.IsDevelopment)
{
txt_title.Text = "连衣裙";
}
}
private void InitBindingDataGridView()
{
this.dgvData.AutoGenerateColumns = false;
this.Column1.Width = 70;
// Column1 是图片列,DataGridViewImageColumn 无法把 ThumbUrl 的 URL 字符串直接渲染成图片,
// 因此不绑定 DataPropertyName,改为数据加载后由 ShowDgvImg() 异步下载图片填入该列。
this.colSelect.DataPropertyName = nameof(CloudGoods.IsSelected);
this.colSelect.ReadOnly = false;
this.Column2.DataPropertyName = nameof(CloudGoods.Title);
this.Column3.DataPropertyName = nameof(CloudGoods.Price);
this.Column4.DataPropertyName = nameof(CloudGoods.SoldQuantity);
this.c_goodsid.DataPropertyName = nameof(CloudGoods.GoodsId);
this.c_Platform.DataPropertyName = nameof(CloudGoods.Platform);
// 2. 设置列样式
// 时间列格式化
//this.Column6.DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
//this.Column8.DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
this.dgvData.DataSource = dataList;
// 滚动到可视区时按需加载缩略图
this.dgvData.Scroll += (s, e) => ShowDgvImg();
this.dgvData.CellContentClick += DgvData_CellContentClick;
dgvData.CellFormatting += DgvData_CellFormatting;
chkSelectAll = dgvData.Columns["colSelect"].AddHeaderCheckBox(HorizontalAlignment.Center, 0, 0, null, null, false);
dgvData.Columns["colSelect"].HeaderText = "";
//chkSelectAll.Width = chkSelectAll.Height = 13;
chkSelectAll.CheckedChanged += chkSelectAll_CheckedChanged;
//chkSelectAll.Checked = true;
}
private void DgvData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == this.c_Platform.Index && e.Value != null && e.Value != DBNull.Value)
{
if (int.TryParse(e.Value.ToString(), out int v))
{
e.Value = v == 1 ? ("多多") : v == 2 ? ("微信") : ("未知");
e.FormattingApplied = true;
}
}
}
private void chkSelectAll_CheckedChanged(object sender, EventArgs e)
{
foreach (var item in dataList)
{
item.IsSelected = (sender as CheckBox).Checked;
}
//this.dgvData.Refresh();
}
private void DgvData_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dgvData.Columns[e.ColumnIndex] == colSelect)
{
var data = dgvData.Rows[e.RowIndex].DataBoundItem as CloudGoods;
data.IsSelected = !data.IsSelected;
}
if (dgvData.Columns[e.ColumnIndex] == Column1)
{
var data = dgvData.Rows[e.RowIndex].DataBoundItem as CloudGoods;
if (dgvData.Rows[e.RowIndex].Cells[e.ColumnIndex].Value is Image img)
{
ShowImgForm imgFrom = new ShowImgForm(img, data.ThumbUrl);
imgFrom.ShowDialog();
}
}
}
/// <summary>
/// 异步下载 CloudGoods.ThumbUrl 图片并填充到 Column1 图片列(只加载可视区行,避免一次下载整页)
/// </summary>
private void ShowDgvImg()
{
// 先在 UI 线程上快照“行 + 图片地址”,避免在后台线程遍历绑定的 DataGridView
List<KeyValuePair<DataGridViewRow, string>> waitList = new List<KeyValuePair<DataGridViewRow, string>>();
foreach (DataGridViewRow row in this.dgvData.Rows)
{
try
{
if (row.Index < 0)
continue;
// 不在可视区:先放占位图,滚动到可视区时再真正加载
if (row.Index > 0 && row.Cells["Column1"].Displayed == false)
{
if (row.Cells["Column1"].Value != DataGridViewExtension.defaultBitmap)
row.Cells["Column1"].Value = DataGridViewExtension.defaultBitmap;
continue;
}
// 已加载过就不再重复下载
if (row.Cells["Column1"].Value != null && row.Cells["Column1"].Value != DataGridViewExtension.defaultBitmap)
continue;
string imgUrl = (row.DataBoundItem as CloudGoods)?.ThumbUrl;
if (!string.IsNullOrWhiteSpace(imgUrl))
waitList.Add(new KeyValuePair<DataGridViewRow, string>(row, imgUrl));
else
row.Cells["Column1"].Value = TemuKJD.Properties.Resources.errpic;
}
catch
{
}
}
if (waitList.Count == 0)
return;
Task.Run(() =>
{
foreach (var item in waitList)
{
try
{
DataGridViewRow row = item.Key;
string getImgError = "";
Image image = GetHtml.GetUrlImg(item.Value, out getImgError);
if (image == null)
image = TemuKJD.Properties.Resources.errpic;
Invoke(new Action(() =>
{
try
{
if (row.Index >= 0)
row.Cells["Column1"].Value = image;
}
catch { }
}));
}
catch
{
}
}
});
}
private void InitCategory()
{
catBoxList.Add(cmbCatSelect1);
catBoxList.Add(cmbCatSelect2);
catBoxList.Add(cmbCatSelect3);
catBoxList.Add(cmbCatSelect4);
for (int i = 0; i < catBoxList.Count; i++)
{
catBoxList[i].DropDownStyle = ComboBoxStyle.DropDownList;
catBoxList[i].Tag = i;
catBoxList[i].SelectedIndexChanged += CmbCatSelect_SelectedIndexChanged;
catBoxList[i].DisplayMember = "cat_name";
catBoxList[i].ValueMember = "cat_id";
}
cmbCatSelect1.Items.Add(new PddCatInfo
{
cat_name = "全部",
cat_id = 0
});
foreach (PddCatInfo cat in GlobalConfig.PddCatList.Where(c => c.level == 1).ToList())
{
cmbCatSelect1.Items.Add(cat);
}
catBoxList[0].SelectedIndex = 0;
}
private void CmbCatSelect_SelectedIndexChanged(object sender, EventArgs e)
{
if (sender is ComboBox ccb)
{
ccb.SelectedIndexChanged -= CmbCatSelect_SelectedIndexChanged;
try
{
int currentLevel = (int)ccb.Tag;
// 先清空并禁用所有下级 ComboBox
for (int i = currentLevel + 1; i < catBoxList.Count; i++)
{
catBoxList[i].Items.Clear();
catBoxList[i].SelectedIndex = -1;
}
var parentCategory = ccb.SelectedItem as PddCatInfo;
// 启用并填充当前级别及上级(如果需要)
for (int i = currentLevel + 1; i < catBoxList.Count; i++)
{
// 清空所有下级
catBoxList[i].Items.Clear();
catBoxList[i].SelectedIndex = -1;
catBoxList[i].Text = "";
catBoxList[i].SelectedItem = null;
if (i == currentLevel + 1)
{
catBoxList[i].Enabled = true;
// 根据当前选中的值过滤下一级数据
if (parentCategory != null)
{
var childItems = GlobalConfig.PddCatList
.Where(c => c.level == i + 1 && c.parent_cat_id == parentCategory.cat_id)
.ToList();
foreach (var cat in childItems)
{
catBoxList[i].Items.Add(cat);
}
}
}
else
{
// 更下级的全部禁用并清空
catBoxList[i].Enabled = false;
}
}
if (currentLevel == 0)
{
catBoxList[1].Enabled = parentCategory.cat_id != 0;
}
}
finally
{
// 重新绑定事件
ccb.SelectedIndexChanged += CmbCatSelect_SelectedIndexChanged;
}
}
}
private void UcSplitPage1_PageChange(object obj)
{
btnSearch_Click(obj, null);
}
private async void btnSearch_Click(object sender, EventArgs e)
{
try
{
this.pbLoading.Visible = true;
btnSearch.Enabled = false;
GoodsSearchRequest search = new GoodsSearchRequest();
search.Platform = cb_Platform.SelectedIndex == 1 ? PlatformType.PDD : cb_Platform.SelectedIndex == 2 ? PlatformType.WX : PlatformType.None;
search.Title = txt_title.Text;
search.MinSoldQuantity = (int)num_minsold.Value;
search.MaxSoldQuantity = (int)num_maxsold.Value;
search.MinPrice = (long)num_minprice.Value * 100;
search.MaxPrice = (long)num_maxprice.Value * 100;
if (cmbCatSelect1.SelectedItem is PddCatInfo cat1)
{
search.CatId1 = (int)(cat1.cat_id);
search.CatId1 = search.CatId1 == 0 ? null : search.CatId1;
}
if (cmbCatSelect2.SelectedItem is PddCatInfo cat2)
search.CatId2 = (int)(cat2.cat_id);
if (cmbCatSelect3.SelectedItem is PddCatInfo cat3)
search.CatId3 = (int)(cat3.cat_id);
if (cmbCatSelect4.SelectedItem is PddCatInfo cat4)
search.CatId4 = (int)(cat4.cat_id);
search.PageIndex = ucSplitPage1.CurrentPage;
search.PageSize = ucSplitPage1.PageSize;
await GetGoods(search);
}
catch (Exception ex)
{
BaseMsgForm.ShowError($"查询失败:{ex.Message}");
}
finally
{
this.pbLoading.Visible = false;
btnSearch.Enabled = true;
}
}
private void pnlEmpty_Click(object sender, EventArgs e)
{
txt_title.Text = "";
num_minsold.Value = 0;
num_maxsold.Value = 999;
num_minprice.Value = 0;
num_maxprice.Value = 999;
cmbCatSelect1.SelectedIndex = 0;
cb_Platform.SelectedIndex = 0;
ucSplitPage1.PageChange -= UcSplitPage1_PageChange;
ucSplitPage1.CurrentPage = 1;
ucSplitPage1.PageChange += UcSplitPage1_PageChange;
}
public async Task GetGoods(GoodsSearchRequest request)
{
dataList.Clear();
PageData<CloudGoods> ret = await CloudGoodsService.GetGoods(request);
Invoke(new Action(() =>
{
ret.List.ForEach(a => dataList.Add(a));
ucSplitPage1.TotalPage = Math.Max(1, ret.TotalPages);
ucSplitPage1.TotalCount = ret.Total;
this.pbLoading.Visible = false;
// 异步加载商品缩略图(Column1 绑定 ThumbUrl)
ShowDgvImg();
}));
}
/// <summary>
/// 导入数据到待上传
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void bt_importUploadList_Click(object sender, EventArgs e)
{
int succ = 0;
int err = 0;
foreach (var item in dataList)
{
try
{
if (item.IsSelected == false) continue;
string catName = "";
if (item.CatId4.HasValue)
{
catName = GlobalConfig.PddCatList.FirstOrDefault(a => a.cat_id == item.CatId4)?.cat_name ?? "";
}
else if (item.CatId3.HasValue)
{
catName = GlobalConfig.PddCatList.FirstOrDefault(a => a.cat_id == item.CatId3)?.cat_name ?? "";
}
else if (item.CatId2.HasValue)
{
catName = GlobalConfig.PddCatList.FirstOrDefault(a => a.cat_id == item.CatId2)?.cat_name ?? "";
}
WXDBProdUploadForm.lstTemuDBProductSelect.Add(new TemuDBProductSelect
{
//out_product_Id = item.GoodsId.ToString(),
spu_id = item.GoodsId.ToString(),
main_img = item.ThumbUrl,
product_name = item.Title,
cat_name = catName,
product_get_type_text = item.Platform == 1 ? ("多多") : item.Platform == 2 ? ("微信") : ("未知"),
db_type = "云端数据",
});
succ++;
}
catch
{
err++;
}
}
NaiBa.Util.Helper.ConfigHelper.SaveConfig("wx_db_prod_upload_list", WXDBProdUploadForm.lstTemuDBProductSelect);
BaseMsgForm.ShowInfo($"导入完成!{succ} 成功 {err} 失败");
}
/// <summary>
/// 选中收藏
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void btn_favorite_Click(object sender, EventArgs e)
{
int succ = 0;
int err = 0;
try
{
this.pbLoading.Visible = true;
btn_favorite.Enabled = false;
var goodsIds = dataList.Where(a => a.IsSelected).Select(a => a.GoodsId).ToList();
if (goodsIds.Count == 0)
{
BaseMsgForm.ShowInfo($"请勾选商品");
return;
}
var ret = await CloudGoodsService.Favorite(goodsIds);
succ = ret;
if (ret != goodsIds.Count)
{
err = goodsIds.Count - ret;
}
BaseMsgForm.ShowInfo($"收藏完成!{succ} 成功 {err} 失败");
}
catch (Exception ex)
{
BaseMsgForm.ShowError($"收藏失败:{ex.Message}");
}
finally
{
this.pbLoading.Visible = false;
btn_favorite.Enabled = true;
}
}
}
}