using AllBase; using AllBase.Helper; using AllBase.Model.Config; using AllBase.Model.Result; using NaiBa.Util.Extension; using NaiBa.Util.Winform.Extension; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using TemuKJD.BLL; using TemuKJD.KJDForms.ShopForms; using TemuKJD.UserComponent; namespace TemuKJD.WXForms.ShopForms { public partial class WxShopStatisticsForm : BaseForms.BaseForm { bool isInit = false; public List lstShopStatisticsInfo = new List(); public WxShopStatisticsForm() { InitializeComponent(); } private void WxShopStatisticsForm_Load(object sender, EventArgs e) { this.dgvShop.AddSelectorColumn("colSelect"); foreach(DataGridViewColumn column in dgvShop.Columns) { if (!string.IsNullOrEmpty(column.HeaderText) && column.HeaderText != "店铺登录号码") { var chk = new UcCheckBox() { Name = "chk" + column.Name, Tag = column.Name, Text = column.HeaderText, Checked = column.Visible, }; chk.CheckedChanged += Chk_CheckedChanged; this.flowLayoutPanel1.Controls.Add(chk); } } } private void Chk_CheckedChanged(object sender, EventArgs e) { var chk = sender as UcCheckBox; if (chk != null) { dgvShop.Columns[chk.Tag.ParseToString()].Visible = chk.Checked; } } private void WxShopStatisticsForm_VisibleChanged(object sender, EventArgs e) { if (Visible == true) { if (!isInit) { isInit = true; RefreshShop(); } } } public void RefreshShop() { this.pbLoading.Visible = true; // 店铺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); } // 获取被删除的统计信息并删除 List statisticsDeleteLst = lstShopStatisticsInfo.Where(row => !GlobalConfig.ShopLoginList.Any(s => s.ShopId == row.ShopId.ToString())).ToList(); foreach (var s in statisticsDeleteLst) { lstShopStatisticsInfo.Remove(s); } // 绑定店铺(含新导入的店铺) foreach (ShopLoginInfo shopEntity in GlobalConfig.ShopLoginList) { if (!string.IsNullOrEmpty(shopEntity.ShopId)) BindShop(shopEntity, selectIds); } GetShopStatistics(GlobalConfig.ShopLoginList.ToList(), selectIds); } private void GetShopStatistics(List shops, List selectIds) { Task.Run(() => { foreach (ShopLoginInfo shopEntity in shops) { if (string.IsNullOrEmpty(shopEntity.ShopId)) continue; bool canUse = shopEntity.LoginStatus == "登录成功"; if (!canUse) { BindShop(shopEntity, selectIds); continue; } WxShopSaleStatisticsGroup statistics = lstShopStatisticsInfo.FirstOrDefault(s => s.ShopId.ToString() == shopEntity.ShopId); if (statistics == null) { statistics = new WxShopSaleStatisticsGroup() { Notes = shopEntity.Notes, GroupName = shopEntity.GroupName, ShopId = shopEntity.ShopId.ParseToString(), ShopAccount = shopEntity.ShopAccount, }; lstShopStatisticsInfo.Add(statistics); } GetShopStatistics(shopEntity, statistics); Invoke(new Action(() => { this.lblPayCnt.Text = this.lstShopStatisticsInfo.Sum(s => s.WxShopSaleInfoToday.pay_order_cnt.ParseToLong()).ToString(); this.lblPayCnt7.Text = this.lstShopStatisticsInfo.Sum(s => s.WxShopSaleInfo7Day.pay_order_cnt.ParseToLong()).ToString(); this.lblPayCnt30.Text = this.lstShopStatisticsInfo.Sum(s => s.WxShopSaleInfo30Day.pay_order_cnt.ParseToLong()).ToString(); this.lblPay.Text = this.lstShopStatisticsInfo.Sum(s => s.WxShopSaleInfoToday.pay_gmv.ParseToDecimal()).ToString("f2"); this.lblPay7.Text = this.lstShopStatisticsInfo.Sum(s => s.WxShopSaleInfo7Day.pay_gmv.ParseToDecimal()).ToString("f2"); this.lblPay30.Text = this.lstShopStatisticsInfo.Sum(s => s.WxShopSaleInfo30Day.pay_gmv.ParseToDecimal()).ToString("f2"); this.lblPayRefund.Text = this.lstShopStatisticsInfo.Sum(s => s.WxShopSaleInfoToday.pay_refund_gmv.ParseToLong()).ToString(); this.lblPayRefund7.Text = this.lstShopStatisticsInfo.Sum(s => s.WxShopSaleInfo7Day.pay_refund_gmv.ParseToLong()).ToString(); this.lblPayRefund30.Text = this.lstShopStatisticsInfo.Sum(s => s.WxShopSaleInfo30Day.pay_refund_gmv.ParseToLong()).ToString(); this.lblOrderNum.Text = this.lstShopStatisticsInfo.Sum(s => s.WxShopMainInfo.orderNumRet?.num.ParseToInt()).ToString(); this.lblWaybillError.Text = this.lstShopStatisticsInfo.Sum(s => s.WxShopMainInfo.waybillErrorRet?.num.ParseToInt()).ToString(); this.lblAfterSaleAll.Text = this.lstShopStatisticsInfo.Sum(s => s.WxShopMainInfo.aftersaleAllRet?.num.ParseToInt()).ToString(); this.lblPunish.Text = this.lstShopStatisticsInfo.Sum(s => s.WxShopMainInfo.punishRet?.num.ParseToInt()).ToString(); //this.lblAvailableBalance.Text = this.lstShopStatisticsInfo.Sum(s => s.availableBalance).ToString(); //this.lblMallDepositLimitAmount.Text = this.lstShopStatisticsInfo.Sum(s => s.mallDepositLimitAmount).ToString(); //this.lblWithdrawCashAmount.Text = this.lstShopStatisticsInfo.Sum(s => s.withdrawCashAmount).ToString(); BindShop(shopEntity, selectIds); })); } Invoke(new Action(() => { this.pbLoading.Visible = false; })); }); } private void BindShop(ShopLoginInfo shopEntity, List selectShopId = null) { if (shopEntity == null) return; if (selectShopId == null) selectShopId = new List(); WxShopSaleStatisticsGroup statistics = lstShopStatisticsInfo.FirstOrDefault(s => s.ShopId.ToString() == shopEntity.ShopId); if (statistics == null) { statistics = new WxShopSaleStatisticsGroup() { Notes = shopEntity.Notes, GroupName = shopEntity.GroupName, ShopId = shopEntity.ShopId.ParseToString(), ShopAccount = shopEntity.ShopAccount, }; lstShopStatisticsInfo.Add(statistics); } bool canUse = shopEntity.LoginStatus == "登录成功"; var row = dgvShop.Rows.Cast().FirstOrDefault( p => p.Cells.GetCellValueFromColumnName("colShopId").ParseToString() == shopEntity.ShopId); if (row == null) { int rowId = this.dgvShop.Rows.Add( false, canUse ? "在线" : "掉线", "详情", shopEntity.LoginName, shopEntity.ShopId, shopEntity.ShopAccount, shopEntity.Notes, shopEntity.GroupName, "", // 在售商品 "", // 店铺体验分 statistics.WxShopSaleInfoToday.pay_order_cnt, // 今日订单 statistics.WxShopSaleInfo7Day.pay_order_cnt, // 7日订单 statistics.WxShopSaleInfo30Day.pay_order_cnt, // 30日订单 statistics.WxShopSaleInfoToday.pay_gmv, // 今日成交金额 statistics.WxShopSaleInfo7Day.pay_gmv, // 7日成交金额 statistics.WxShopSaleInfo30Day.pay_gmv, // 30日成交金额 statistics.WxShopSaleInfoToday.pay_refund_gmv, // 今日退款金额 statistics.WxShopSaleInfo7Day.pay_refund_gmv, // 7日退款金额 statistics.WxShopSaleInfo30Day.pay_refund_gmv, // 30日退款金额 statistics.WxShopMainInfo.orderNumRet?.num, // 待发货 statistics.WxShopMainInfo.waybillErrorRet?.num, // 物流异常 statistics.WxShopMainInfo.aftersaleAllRet?.num, // 售后 statistics.WxShopMainInfo.punishRet?.num, // 店铺违规 "", "", "", "" ); row = dgvShop.Rows[rowId]; (row.Cells["colLoginStatus"] as DataGridViewLinkCell).Style.ForeColor = canUse ? Color.Green : Color.Red; (row.Cells["colLoginStatus"] as DataGridViewLinkCell).LinkColor = canUse ? Color.Green : Color.Red; (row.Cells["colLoginStatus"] as DataGridViewLinkCell).ActiveLinkColor = canUse ? Color.LightGreen : Color.OrangeRed; (row.Cells["colLoginStatus"] as DataGridViewLinkCell).VisitedLinkColor = canUse ? Color.DarkGreen : Color.DarkRed; row.Height = 32; } else { row.Cells["colSelect"].Value = selectShopId.Contains(shopEntity.ShopId) && canUse; row.Cells["colLoginStatus"].Value = canUse ? "在线" : "掉线"; (row.Cells["colLoginStatus"] as DataGridViewLinkCell).Style.ForeColor = canUse ? Color.Green : Color.Red; (row.Cells["colLoginStatus"] as DataGridViewLinkCell).LinkColor = canUse ? Color.Green : Color.Red; (row.Cells["colLoginStatus"] as DataGridViewLinkCell).ActiveLinkColor = canUse ? Color.LightGreen : Color.OrangeRed; (row.Cells["colLoginStatus"] as DataGridViewLinkCell).VisitedLinkColor = canUse ? Color.DarkGreen : Color.DarkRed; row.Cells["colShopLoginName"].Value = shopEntity.LoginName; row.Cells["colShopAccount"].Value = shopEntity.ShopAccount; row.Cells["colNotes"].Value = shopEntity.Notes; row.Cells["colGroup"].Value = shopEntity.GroupName; // 在售商品 // 店铺体验分 row.Cells["colPayCnt"].Value = statistics.WxShopSaleInfoToday.pay_order_cnt; // 今日订单 row.Cells["colPayCnt7"].Value = statistics.WxShopSaleInfo7Day.pay_order_cnt; // 7日订单 row.Cells["colPayCnt30"].Value = statistics.WxShopSaleInfo30Day.pay_order_cnt; // 30日订单 row.Cells["colPay"].Value = statistics.WxShopSaleInfoToday.pay_gmv; // 今日成交金额 row.Cells["colPay7"].Value = statistics.WxShopSaleInfo7Day.pay_gmv; // 7日成交金额 row.Cells["colPay30"].Value = statistics.WxShopSaleInfo30Day.pay_gmv; // 30日成交金额 row.Cells["colRefund"].Value = statistics.WxShopSaleInfoToday.pay_refund_gmv; // 今日退款金额 row.Cells["colRefund7"].Value = statistics.WxShopSaleInfo7Day.pay_refund_gmv; // 7日退款金额 row.Cells["colRefund30"].Value = statistics.WxShopSaleInfo30Day.pay_refund_gmv; // 30日退款金额 row.Cells["colOrderNum"].Value = statistics.WxShopMainInfo.orderNumRet?.num; // 待发货 row.Cells["colShipErr"].Value = statistics.WxShopMainInfo.waybillErrorRet?.num; // 物流异常 row.Cells["colAfterSale"].Value = statistics.WxShopMainInfo.aftersaleAllRet?.num; // 售后 row.Cells["colPunish"].Value = statistics.WxShopMainInfo.punishRet?.num; // 店铺违规 //row.Cells["colOnSaleProductNumber"].Value = statistics.onSaleProductNumber == null ? "-" : statistics.onSaleProductNumber.ToString(); //在售商品数 //row.Cells["colSaleVolume"].Value = statistics.saleVolume == null ? "-" : statistics.saleVolume.ToString(); //今日销量 //row.Cells["colTodayProfit"].Value = statistics.todayProfit == null ? "-" : statistics.todayProfit.Value.ToString("0.00"); //今日利润 //row.Cells["colSevenDaysSaleVolume"].Value = statistics.sevenDaysSaleVolume == null ? "-" : statistics.sevenDaysSaleVolume.ToString(); //7日销量 //row.Cells["colSevenDaysProfit"].Value = statistics.sevenDaysProfit == null ? "-" : statistics.sevenDaysProfit.Value.ToString("0.00"); //7日利润 //row.Cells["colThirtyDaysSaleVolume"].Value = statistics.thirtyDaysSaleVolume == null ? "-" : statistics.thirtyDaysSaleVolume.ToString(); //30日销量 //row.Cells["colThirtyDaysProfit"].Value = statistics.thirtyDaysProfit == null ? "-" : statistics.thirtyDaysProfit.Value.ToString("0.00"); //30日利润 //row.Cells["colGoodsValue"].Value = statistics.goodsValue == null ? "-" : (statistics.goodsCost.Value * 0.01m).ToString("0.00"); //销售货值 //row.Cells["colGoodsCost"].Value = statistics.goodsCost == null ? "-" : (statistics.goodsCost.Value * 0.01m).ToString("0.00"); //成本 } } private void GetShopStatistics(ShopLoginInfo shop, WxShopSaleStatisticsGroup statistics) { if (statistics == null || statistics.WxShopMainInfo == null || statistics.WxShopSaleInfoToday == null || statistics.WxShopSaleInfo7Day == null || statistics.WxShopSaleInfo30Day == null) return; if (statistics.WxShopMainInfo.LastRefreshDate != DateTime.Now.Date) { try { string html = GetShopMainInfoList(shop); if (html.Contains("\"code\":0")) { statistics.WxShopMainInfo = JsonHelper.DeserializeJsonToObject(html); statistics.WxShopMainInfo.LastRefreshDate = DateTime.Now; } else { string errStr = BaseRegex.GetStringJson(ref html, "errorMsg"); if (string.IsNullOrEmpty(errStr)) errStr = BaseRegex.GetStringJson(ref html, "error_msg"); if (string.IsNullOrEmpty(errStr)) errStr = html; ShopListForm.Instance.CheckShopLogin(new string[] { shop.LoginName }, false); System.Threading.Thread.Sleep(Randomx.GetId(100, 200)); statistics.err_msg = errStr; return; } System.Threading.Thread.Sleep(Randomx.GetId(50, 100)); } catch (Exception ex) { System.Threading.Thread.Sleep(Randomx.GetId(50, 100)); statistics.err_msg = ex.Message; return; } } } private void btnSetColumn_Click(object sender, EventArgs e) { this.panel1.Visible = true; } private void btnClose_Click(object sender, EventArgs e) { this.panel1.Visible = false; } /// /// 获取店铺后台主页售后数量等数据 /// /// /// public static string GetShopMainInfoList(ShopLoginInfo shop) { string url = "https://store.weixin.qq.com/shop-faas/mmchannelstradehome/home/main?token=&lang=zh_CN"; string referer = "https://store.weixin.qq.com/shop/home"; var result = WXHttpResponseBLL.WeiXinGetResponse(shop, url, referer); return result; //{"code":0,"orderNumRet":{"code":0,"pendingShipNum":"1"},"waybillErrorRet":{"code":0,"totalNum":0},"aftersaleAllRet":{"code":0,"totalNum":3},"guaranteeRet":{"code":0,"totalNum":0},"prewarnRet":{"code":0,"waitStatusCount":0},"punishRet":{"code":0,"pendingNum":1},"mchUndealRet":{"code":0,"totalNum":0}} //pendingShipNum待发货 waybillErrorRet物流异常 aftersaleAllRet售后 guaranteeRet服务保障 prewarnRet店铺预警 punishRet店铺违规 mchUndealRet工单 } 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["colShopAccount"].Value.ToString(); var shop = GlobalConfig.ShopLoginList.FirstOrDefault(s => s.ShopId == shopId.ToString()); var row = dgvShop.Rows[e.RowIndex]; switch (columnName) { case "colSelect": row.Cells["colSelect"].Value = !row.Cells["colSelect"].Value.ParseToBool(); break; case "colLoginStatus": { WXShopListForm.Instance.CheckShopLogin(new string[] { shop.LoginName }, false); WXShopLoginForm shopLoginForm = new WXShopLoginForm(shop); if (shop.LoginStatus != "登录成功") { shopLoginForm.openType = EnumOpenType.login; shopLoginForm.ShowDialog(); } 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 btnGetSelect_Click(object sender, EventArgs e) { var selectIds = dgvShop.Rows.Cast().Where(row => row.Cells["colSelect"]?.Value.ParseToBool() == true).Select(r => r.Cells["colShopId"]?.Value.ToString()).ToList(); ReGetStatistics(selectIds); } void ReGetStatistics(List reGetShopIds) { var shopList = GlobalConfig.ShopLoginList.Where(s => reGetShopIds.Contains(s.ShopId)).ToList(); foreach (var shop in shopList) { WxShopSaleStatisticsGroup statistics = lstShopStatisticsInfo.FirstOrDefault(s => s.ShopId.ToString() == shop.ShopId); if (statistics != null) { statistics.WxShopMainInfo.LastRefreshDate = DateTime.MinValue; statistics.WxShopSaleInfoToday.LastRefreshDate = DateTime.MinValue; statistics.WxShopSaleInfo7Day.LastRefreshDate = DateTime.MinValue; statistics.WxShopSaleInfo30Day.LastRefreshDate = DateTime.MinValue; } //this.lstSubOrderInfo.RemoveAll(o => o.supplierId.ParseToString() == shop.ShopId); } this.pbLoading.Visible = true; GetShopStatistics(shopList, reGetShopIds); } } }