using AllBase; using AllBase.Helper; 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.IO; using System.Linq; using System.Net.Http; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using TemuKJD.Extension; using TemuKJD.BaseForms; using TemuKJD.ToolForms; using Work; using TemuKJD.BLL; using System.Text.RegularExpressions; using TemuKJD.PDDForms.ProductSelectForms; using TemuKJD.WXForms.ShopForms; using AllBase.Model.Result.PDDResult; using AllBase.Model.Param.WXParam; using AllBase.Model.Result.WXResult; using Newtonsoft.Json; using NaiBa.Util; using AllBase.Model.Config; using TemuKJD.WXForms.ProductForms; namespace TemuKJD.WXForms.ShopForms { public partial class WXShopOptimizeForm : BaseForm { public CancellationTokenSource cancellationTokenSource { get; set; } = null; public bool IsLoadComplate { get; set; } = false; bool isInit = false; CheckBox chkSelectAll = null; public Dictionary dicCheckedShopId = new Dictionary(); public string pdd_product_cates = ""; ConcurrentList lstWxOptimizeOrderAfterSaleInfo = new ConcurrentList(); ConcurrentList lstWxOptimizeProductEvaluationInfo = new ConcurrentList(); public WXShopOptimizeForm() { InitializeComponent(); } private void UserSelectDbProductForm_Load(object sender, EventArgs e) { dicCheckedShopId = NaiBa.Util.Helper.ConfigHelper.LoadConfig>("wx_checked_shop_dic"); if (dicCheckedShopId == null) dicCheckedShopId = new Dictionary(); chkSelectAll = dgvShop.Columns["colSelect"].AddHeaderCheckBox(HorizontalAlignment.Center, 0, 0, null, null, false); dgvShop.Columns["colSelect"].HeaderText = ""; chkSelectAll.CheckedChanged += chkSelectAll_CheckedChanged; dgvData1.AddSelectorColumn("colDgv1Check"); dgvData1.AddImgColumn("colDgv1Img", "colDgv1ImgUrl"); IsLoadComplate = true; } private void chkSelectAll_CheckedChanged(object sender, EventArgs e) { if (!chkSelectAll.Checked) { foreach (DataGridViewRow row in dgvShop.Rows) { row.Cells["colSelect"].Value = false; } } else { foreach (DataGridViewRow row in dgvShop.Rows) { if(row.Visible == true) row.Cells["colSelect"].Value = true; } } foreach (var r in dgvShop.Rows.Cast()) { dicCheckedShopId[r.Cells["colShopId"].Value.ParseToString()] = r.Cells["colSelect"].Value.ParseToBool(); } NaiBa.Util.Helper.ConfigHelper.SaveConfig("wx_checked_shop_dic", dicCheckedShopId); } private void btnRefreshShop_Click(object sender, EventArgs e) { pbLoadingShop.Visible = true; RefreshShopLogin(); } private void UserSelectDbProductForm_VisibleChanged(object sender, EventArgs e) { if (Visible == true) { RefreshShopLogin(); if (!isInit) { Task.Run(() => { foreach (var shop in GlobalConfig.ShopLoginList) { } }); } isInit = true; } } private void RefreshShopLogin() { // 店铺Id var selectIds = dgvShop.Rows.Cast().Where(row => row.Cells["colSelect"]?.Value.ParseToBool() == true).Select(r => r.Cells["colShopId"]?.Value.ToString()).ToList(); // 获取被删除的店铺 List dgvRowList = dgvShop.Rows.Cast().Where(row => !GlobalConfig.ShopLoginList.Any(s => s.ShopId == row.Cells["colShopId"]?.Value?.ToString())).ToList(); // 更新被删除的店铺列表 foreach (var row in dgvRowList) { dgvShop.Rows.Remove(row); } // 绑定其他店铺(含新导入的店铺) foreach (ShopLoginInfo shopEntity in GlobalConfig.ShopLoginList) { if (!string.IsNullOrEmpty(shopEntity.ShopId)) BindShop(shopEntity, selectIds); } cmbFilterShopNotes.Items.Clear(); //cmbFilterShopNotes.Items.Add(""); foreach (var note in GlobalConfig.ShopLoginList.Select(s => s.Notes).Where(s => !s.IsNullOrEmpty()).Distinct()) { cmbFilterShopNotes.Items.Add(note); } } private void BindShop(ShopLoginInfo shopEntity, List selectShopId = null, string msg = "") { if (shopEntity == null) return; if (shopEntity.is_shop_expire) return; if (selectShopId == null) selectShopId = new List(); bool canUse = shopEntity.LoginStatus == "登录成功"; //canUse = true; // 内部库不需要校验登录状态 var row = dgvShop.Rows.Cast().FirstOrDefault(p => p.Cells.GetCellValueFromColumnName("colShopId").ParseToString() == shopEntity.ShopId); var day = DateTime.Now.ToString("yyyyMMdd").ParseToInt(); if (row == null) { this.dgvShop.Rows.Add( //!isInit || (selectShopId.Contains(shopEntity.ShopId) && canUse), dicCheckedShopId.Keys.Contains(shopEntity.ShopId) && dicCheckedShopId[shopEntity.ShopId] == true, shopEntity.LoginName, shopEntity.ShopId, shopEntity.ShopAccount, shopEntity.Notes, "选择商品", canUse ? "在线" : "掉线", "", //lstShopAuthInfo.FirstOrDefault(sa => sa.ShopId == shopEntity.ShopId)?.ExpiresTime > DateTime.Now.AddMinutes(5) ? "可用" : "过期", canUse, shopEntity.AddProductRemaining, "", msg ); } else { row.Cells["colSelect"].Value = !isInit || (selectShopId.Contains(shopEntity.ShopId) && canUse); row.Cells["colShopLoginName"].Value = shopEntity.LoginName; row.Cells["colShopName"].Value = shopEntity.ShopAccount; row.Cells["colShopNotes"].Value = shopEntity.Notes; row.Cells["colLoginStatus"].Value = canUse ? "在线" : "掉线"; row.Cells["colLoginStatus"].Style.ForeColor = canUse ? Color.Green : Color.Red; row.Cells["colCanUse"].Value = canUse; row.Cells["colApi"].Value = ""; //lstShopAuthInfo.FirstOrDefault(sa => sa.ShopId == shopEntity.ShopId)?.ExpiresTime > DateTime.Now.AddMinutes(5) ? "可用" : "过期"; row.Cells["colRemaining"].Value = shopEntity.AddProductRemaining; row.Cells["colMessage"].Value = msg; } displayAuthStatus(shopEntity); } void displayAuthStatus(ShopLoginInfo shop) { var rows = this.dgvShop.Rows.Cast().Where(r => r.Cells["colShopId"].Value.ParseToString() == shop?.ShopId); foreach (var row in rows) { bool canUse = row.Cells["colLoginStatus"].Value.ParseToString() == "在线"; (row.Cells["colLoginStatus"] as DataGridViewLinkCell).Style.ForeColor = canUse ? Color.Blue : Color.Red; (row.Cells["colLoginStatus"] as DataGridViewLinkCell).LinkColor = canUse ? Color.Blue : Color.Red; (row.Cells["colLoginStatus"] as DataGridViewLinkCell).ActiveLinkColor = canUse ? Color.LightBlue : Color.OrangeRed; (row.Cells["colLoginStatus"] as DataGridViewLinkCell).VisitedLinkColor = canUse ? Color.DarkBlue : Color.DarkRed; canUse = row.Cells["colApi"].Value.ParseToString() == "可用"; (row.Cells["colApi"] as DataGridViewLinkCell).Style.ForeColor = canUse ? Color.Blue : Color.Red; (row.Cells["colApi"] as DataGridViewLinkCell).LinkColor = canUse ? Color.Blue : Color.Red; (row.Cells["colApi"] as DataGridViewLinkCell).ActiveLinkColor = canUse ? Color.LightBlue : Color.OrangeRed; (row.Cells["colApi"] as DataGridViewLinkCell).VisitedLinkColor = canUse ? Color.DarkBlue : Color.DarkRed; } } private void dgvShop_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex < 0 || e.RowIndex < 0) return; string headerText = dgvShop.Columns[e.ColumnIndex].HeaderText; string columnName = dgvShop.Columns[e.ColumnIndex].Name; string shopId = dgvShop.Rows[e.RowIndex].Cells["colShopId"].Value.ParseToString(); string shopAccount = dgvShop.Rows[e.RowIndex].Cells["colShopName"].Value.ToString(); var shop = GlobalConfig.ShopLoginList.FirstOrDefault(s => s.ShopId == shopId.ToString()); var row = dgvShop.Rows[e.RowIndex]; switch (columnName) { case "colSelect": //if (row.Cells["colCanUse"].Value.ParseToBool()) { row.Cells["colSelect"].Value = !row.Cells["colSelect"].Value.ParseToBool(); chkSelectAll.CheckedChanged -= chkSelectAll_CheckedChanged; if (dgvShop.Rows.Cast().All(r => r.Cells["colSelect"].Value.ParseToBool())) chkSelectAll.Checked = true; else chkSelectAll.Checked = false; chkSelectAll.CheckedChanged += chkSelectAll_CheckedChanged; } //else { //BaseMsgForm.ShowInfo("店铺未登录!"); } dicCheckedShopId[row.Cells["colShopId"].Value.ParseToString()] = row.Cells["colSelect"].Value.ParseToBool(); NaiBa.Util.Helper.ConfigHelper.SaveConfig("wx_checked_shop_dic", dicCheckedShopId); break; case "colLoginStatus": { if (WXShopListForm.Instance == null) WXShopListForm.Instance = new WXShopListForm(); WXShopListForm.Instance.CheckShopLogin(new string[] { shop.LoginName }, false); WXShopLoginForm shopLoginForm = new WXShopLoginForm(shop); //shopLoginForm.prodUserSelectForm = this; if (shop.LoginStatus != "登录成功") { shopLoginForm.openType = EnumOpenType.login; shopLoginForm.ShowDialog(); RefreshShopLogin(); } else { shopLoginForm.openType = EnumOpenType.manage; shopLoginForm.WindowState = FormWindowState.Maximized; shopLoginForm.Show(); } } break; case "colApi": { WXShopLoginForm shopLoginForm = new WXShopLoginForm(shop); shopLoginForm.openType = EnumOpenType.manage; shopLoginForm.WindowState = FormWindowState.Maximized; shopLoginForm.InitUrl = "https://store.weixin.qq.com/shop/servicemarket/detail?serviceId=3984039898079707139"; shopLoginForm.Show(); } break; } } private void dgvSelectProduct_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex < 0 || e.RowIndex < 0) return; string headerText = dgvData3.Columns[e.ColumnIndex].HeaderText; var dbId = dgvData3.Rows[e.RowIndex].Cells["colSelectId"].Value.ParseToString(); var shopId = dgvData3.Rows[e.RowIndex].Cells["colSupplierId"].Value.ParseToString(); var draftId = dgvData3.Rows[e.RowIndex].Cells["colDraftId"].Value.ParseToString(); var shop = GlobalConfig.ShopLoginList.FirstOrDefault(s => s.ShopId == shopId); switch (headerText) { case "删除": break; case "登录": { WXShopLoginForm shopLoginForm = new WXShopLoginForm(shop); //shopLoginForm.prodUserSelectForm = this; shopLoginForm.openType = EnumOpenType.login; shopLoginForm.ShowDialog(); RefreshShopLogin(); } break; case "进店": { WXShopLoginForm shopLoginForm = new WXShopLoginForm(shop); WXShopListForm.Instance.CheckShopLogin(new ShopLoginInfo[] { shop }, false); if (shop.LoginStatus == "登录成功") { shopLoginForm.openType = EnumOpenType.manage; shopLoginForm.WindowState = FormWindowState.Maximized; shopLoginForm.InitUrl = "https://store.weixin.qq.com/shop/home"; shopLoginForm.Show(); } else { shopLoginForm.openType = EnumOpenType.login; shopLoginForm.ShowDialog(); RefreshShopLogin(); } } break; case "图片": { ShowImgForm imgFrom = new ShowImgForm(dgvData3.Rows[e.RowIndex].Cells["colImgUrl"].Value.ParseToString()); imgFrom.ShowDialog(); break; } case "SKU替换图": { ShowImgForm imgFrom = new ShowImgForm(dgvData3.Rows[e.RowIndex].Cells["colSkuFakeImgUrl"].Value.ParseToString()); imgFrom.ShowDialog(); break; } case "": dgvData3.Rows[e.RowIndex].Cells["colCheck"].Value = !(bool)dgvData3.Rows[e.RowIndex].Cells["colCheck"].Value; break; default: break; } } private void btnStop_Click(object sender, EventArgs e) { if (this.cancellationTokenSource != null) this.cancellationTokenSource.Cancel(); } private void btnFilterShop_Click(object sender, EventArgs e) { foreach(DataGridViewRow row in dgvShop.Rows) { string notes = cmbFilterShopNotes.Text.Trim().Trim(';'); if (notes == "全部") notes = ""; List notesLst = notes.Split(new string[] { ";", ";", ",", "," }, StringSplitOptions.RemoveEmptyEntries).ToList(); if (!row.Cells["colShopName"].Value.ParseToString().Contains(txtFilterShopName.Text.Trim())) { row.Visible = false; row.Cells["colSelect"].Value = false; continue; } if (!string.IsNullOrWhiteSpace(notes) && !notesLst.Contains(row.Cells["colShopNotes"].Value.ParseToString())) { row.Visible = false; row.Cells["colSelect"].Value = false; continue; } row.Visible = true; } } private void btnEmptyShop_Click(object sender, EventArgs e) { txtFilterShopName.Text = ""; cmbFilterShopNotes.Text = "全部"; btnFilterShop_Click(null, null); } private void btnReverse_Click(object sender, EventArgs e) { foreach(DataGridViewRow row in dgvShop.Rows) { if (row.Visible == true) row.Cells["colSelect"].Value = !row.Cells["colSelect"].Value.ParseToBool(); } } private void btnAddSelect_Click(object sender, EventArgs e) { string notes = cmbFilterShopNotes.Text.Trim().Trim(';'); if (notes == "全部") notes = ""; List notesLst = notes.Split(new string[] { ";", ";", ",", "," }, StringSplitOptions.RemoveEmptyEntries).ToList(); foreach (DataGridViewRow row in dgvShop.Rows) { if (row.Visible == true) { if (row.Cells["colShopName"].Value.ParseToString().Contains(txtFilterShopName.Text.Trim()) && (string.IsNullOrWhiteSpace(notes) || notesLst.Contains(row.Cells["colShopNotes"].Value.ParseToString()))) { row.Cells["colSelect"].Value = true; } } } } private void btnAddUnSelect_Click(object sender, EventArgs e) { string notes = cmbFilterShopNotes.Text.Trim().Trim(';'); if (notes == "全部") notes = ""; List notesLst = notes.Split(new string[] { ";", ";", ",", "," }, StringSplitOptions.RemoveEmptyEntries).ToList(); foreach (DataGridViewRow row in dgvShop.Rows) { if (row.Visible == true) { if (row.Cells["colShopName"].Value.ParseToString().Contains(txtFilterShopName.Text.Trim()) && (string.IsNullOrWhiteSpace(notes) || notesLst.Contains(row.Cells["colShopNotes"].Value.ParseToString()))) { row.Cells["colSelect"].Value = false; } } } } private void btnSelectOnline_Click(object sender, EventArgs e) { foreach (DataGridViewRow row in dgvShop.Rows) { if (row.Visible == true) { if (row.Cells["colLoginStatus"].Value.ParseToString() == "在线") { row.Cells["colSelect"].Value = true; } else { row.Cells["colSelect"].Value = false; } } } } private void btnSelectOffline_Click(object sender, EventArgs e) { foreach (DataGridViewRow row in dgvShop.Rows) { if (row.Visible == true) { if (row.Cells["colLoginStatus"].Value.ParseToString() != "在线") { row.Cells["colSelect"].Value = true; } else { row.Cells["colSelect"].Value = false; } } } } private void btnSearch_Click(object sender, EventArgs e) { var lstShopId = dgvShop.Rows.Cast().Where(row => row.Cells["colSelect"]?.Value.ParseToBool() == true).Select(r => r.Cells["colShopId"]?.Value.ToString()).ToList(); if (lstShopId.Count == 0) { BaseMsgForm.ShowInfo("请选择店铺!"); return; } btnSearch.Enabled = false; pictureBox1.Visible = true; cancellationTokenSource = new CancellationTokenSource(); dgvData1.Rows.Clear(); lstWxOptimizeOrderAfterSaleInfo.Clear(); Task.Run(() => { List shopLst = GlobalConfig.ShopLoginList.Where(s => lstShopId.Contains(s.ShopId)).ToList(); foreach (var shop in shopLst) { SearchData1(shop); } Invoke(new Action(() => { if (cancellationTokenSource.IsCancellationRequested) { this.statusBar.lblTaskStatus.Text = "品质退款查询完毕 " + this.statusBar.lblTaskStatus.Text; BaseMsgForm.ShowInfo("品质退款查询完毕"); } else { this.statusBar.lblTaskStatus.Text = "品质退款查询完毕"; BaseMsgForm.ShowInfo("品质退款查询完毕"); } pictureBox1.Visible = false; btnSearch.Enabled = true; })); }, cancellationTokenSource.Token).ContinueWith(delegate { }, cancellationTokenSource.Token); } public void SearchData1(ShopLoginInfo shop) { if (shop.LoginStatus != "登录成功") { return; } int page = 0; int totalPage = 1; int pageSize = 50; int startTime = BaseClass.DateTimeToTimestampSeconds(DateTime.Now.Date.AddDays(-30)); int endTime = BaseClass.DateTimeToTimestampSeconds(DateTime.Now.Date) - 1; while (page < totalPage) { page++; if (cancellationTokenSource.IsCancellationRequested) break; var getData1 = ShopOptimize.GetProductRefundOrder(shop, page, startTime, endTime); Thread.Sleep(Randomx.GetId(300, 600)); if (!getData1.Contains("\"code\":0")) { var error = BaseRegex.GetStringJson(ref getData1, "msg"); if (string.IsNullOrEmpty(error)) error = getData1; WXShopListForm.Instance.CheckShopLogin(new ShopLoginInfo[] { shop }, false); if (shop.LoginStatus != "登录成功") { var shopIdLst = dgvShop.Rows.Cast().Where(row => row.Cells["colSelect"]?.Value.ParseToBool() == true).Select(r => r.Cells["colShopId"]?.Value.ToString()).ToList(); Invoke(new Action(() => { BindShop(shop, shopIdLst, "店铺掉线"); })); } else { Invoke(new Action(() => { var shopRow = dgvShop.Rows.Cast().FirstOrDefault(r => r.Cells["colShopId"].Value.ParseToString() == shop.ShopId); if (shopRow != null) shopRow.Cells["colMessage"].Value = error; })); } break; } try { var prodObjs = JsonHelper.JObjectParse(getData1); if (page == 1) { var totalNum = prodObjs["total"].ParseToInt(); totalPage = Math.Ceiling(1m * totalNum / pageSize).ParseToInt(); } Invoke(new Action(() => { this.pbLoading.Visible = true; this.statusBar.lblTaskStatus.Text = "正在查询品质退款,店铺:" + shop.ShopAccount + " " + page + "/" + totalPage; })); foreach (JObject refundOrderObj in prodObjs["data"]) { var oStr = JsonHelper.SerializeObject(refundOrderObj); var refundOrder = JsonHelper.DeserializeJsonToObject(oStr); if (lstWxOptimizeOrderAfterSaleInfo.Any(o => o.orderInfo.orderId == refundOrder.orderInfo.orderId)) continue; refundOrder.shopId = shop.ShopId; refundOrder.shopName = shop.ShopAccount; lstWxOptimizeOrderAfterSaleInfo.Add(refundOrder); Invoke(new Action(() => { var rowIndex = this.dgvData1.Rows.Add( false, shop.ShopId, shop.ShopAccount, "登录", "进店", refundOrder.orderInfo.imgUrl, DataGridViewExtension.defaultBitmap, refundOrder.orderInfo.spuId, refundOrder.orderInfo.name, refundOrder.orderInfo.skuName, refundOrder.orderInfo.skuCode, refundOrder.orderInfo.orderId, BaseClass.StampToDateTime(refundOrder.orderInfo.createTime.ParseToInt()).ToString("yyyy-MM-dd HH:mm:ss"), refundOrder.reason); dgvData1.Rows[rowIndex].Height = 60; })); } } catch (Exception ex) { Invoke(new Action(() => { var shopRow = dgvShop.Rows.Cast().FirstOrDefault(r => r.Cells["colShopId"].Value.ParseToString() == shop.ShopId); if (shopRow != null) shopRow.Cells["colMessage"].Value = ex.Message; })); } } } private void dgvData1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } } public static class ShopOptimize { public static string GetProductRefundOrder(ShopLoginInfo shop, int pageId, int beginTick, int endTick) { string url = "https://store.weixin.qq.com/shop-faas/statistic/dsr/product/refund/order?token=&lang=zh_CN"; string referer = "https://store.weixin.qq.com/shop/setting/ratedetail?type=product&key=productQualityRatio_30d&detail=product"; string body = @"{""page"":" + pageId + @",""pageSize"":20,""beginTs"":" + beginTick + @",""endTs"":" + endTick + @"}"; var result = WXHttpResponseBLL.WeiXinPostResponse(shop, url, body, referer); return result; } } }