Files
KJD/TEMUKJD/KJDForms/JITForms/JitShopActiveForm.cs
T

243 lines
10 KiB
C#

using AllBase;
using AllBase.Helper;
using AllBase.Model.Result;
using NaiBa.Util.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.BaseForms;
using TemuKJD.Extension;
using Work;
namespace TemuKJD.KJDForms.JITForms
{
public partial class JitShopActiveForm : Form
{
private CheckBox chkSelectAll = new CheckBox();
public JitShopActiveForm()
{
InitializeComponent();
}
private void JitShopActiveForm_Load(object sender, EventArgs e)
{
//chkSelectAll = dgvShop.Columns["colSelect"].AddHeaderCheckBox(HorizontalAlignment.Center, 0, 0, null, null, false);
//dgvShop.Columns["colSelect"].HeaderText = "";
//chkSelectAll.Width = chkSelectAll.Height = 13;
//chkSelectAll.CheckedChanged += chkSelectAll_CheckedChanged;
}
private void chkSelectAll_CheckedChanged(object sender, EventArgs e)
{
if (!chkSelectAll.Checked)
{
foreach (DataGridViewRow row in dgvShop.Rows)
{
row.Cells["colSelect"].Value = false;
}
}
else
{
var list = GlobalConfig.ShopLoginList.Where(s => s.LoginStatus == "登录成功").Select(ss => ss.ShopId);
foreach (DataGridViewRow row in dgvShop.Rows)
{
if (list.Any(l => l == row.Cells["colShopId"]?.Value?.ToString()))
row.Cells["colSelect"].Value = true;
}
}
}
private void JitShopActiveForm_VisibleChanged(object sender, EventArgs e)
{
if (Visible == true)
RefreshShop();
}
private void btnRefreshShop_Click(object sender, EventArgs e)
{
RefreshShop();
}
private void RefreshShop()
{
// 店铺Id
var selectIds = dgvShop.Rows.Cast<DataGridViewRow>().Where(row
=> row.Cells["colSelect"]?.Value.ParseToBool() == true).Select(r => r.Cells["colShopId"]?.Value.ToString()).ToList();
var shopLst = GlobalConfig.ShopLoginList.ToList();
if (!string.IsNullOrWhiteSpace(txtShopId.Text.Trim()))
{
shopLst = shopLst.Where(s => ("," + txtShopId.Text.Trim() + ",").Contains("," + s.ShopId + ",") || s.ShopId.Contains(txtShopId.Text.Trim())).ToList();
}
if (!string.IsNullOrWhiteSpace(txtShopName.Text.Trim()))
{
shopLst = shopLst.Where(s => ("," + txtShopName.Text.Trim() + ",").Contains("," + s.ShopAccount + ",") || s.ShopAccount.ToLower().Contains(txtShopName.Text.Trim().ToLower())).ToList();
}
if (!string.IsNullOrWhiteSpace(txtNotes.Text.Trim()))
{
shopLst = shopLst.Where(s => ("," + txtNotes.Text.Trim() + ",").Contains("," + s.Notes + ",") || s.Notes.ToLower().Contains(txtNotes.Text.Trim().ToLower())).ToList();
}
// 获取被删除的店铺
List<DataGridViewRow> dgvRowList = dgvShop.Rows.Cast<DataGridViewRow>().Where(row
=> !shopLst.Any(s => s.ShopId == row.Cells["colShopId"]?.Value?.ToString())).ToList();
// 更新被删除的店铺列表
foreach (var row in dgvRowList)
{
dgvShop.Rows.Remove(row);
}
// 绑定其他店铺(含新导入的店铺)
foreach (ShopLoginInfo shopEntity in shopLst)
{
if (!string.IsNullOrEmpty(shopEntity.ShopId))
BindShop(shopEntity, selectIds);
}
}
private void BindShop(ShopLoginInfo shopEntity, List<string> selectShopId = null)
{
if (shopEntity == null)
return;
if (selectShopId == null)
selectShopId = new List<string>();
bool canUse = shopEntity.LoginStatus == "登录成功" && shopEntity.LoginStatus2 == "国际版在线";
var row = dgvShop.Rows.Cast<DataGridViewRow>().FirstOrDefault(p => p.Cells.GetCellValueFromColumnName("colShopId").ParseToString() == shopEntity.ShopId);
if (row == null)
{
int id = this.dgvShop.Rows.Add(
selectShopId.Contains(shopEntity.ShopId) && canUse,
shopEntity.LoginName,
shopEntity.ShopId,
shopEntity.ShopAccount,
shopEntity.Notes,
canUse ? "在线" : "掉线",
GetJitStatus(shopEntity),
shopEntity.jit_expire_time?.ToString("yyyy/MM/dd"),
"激活",
canUse,
string.Empty
);
row = dgvShop.Rows[id];
(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["colJitStatus"].Style.ForeColor = GetJitStatusStyleColor(shopEntity);
}
else
{
row.Cells["colSelect"].Value = 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"] 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["colCanUse"].Value = canUse;
row.Cells["colJitStatus"].Value = GetJitStatus(shopEntity);
row.Cells["colJitExpireTime"].Value = shopEntity.jit_expire_time?.ToString("yyyy/MM/dd");
row.Cells["colJitStatus"].Style.ForeColor = GetJitStatusStyleColor(shopEntity);
}
}
public static string GetJitStatus(ShopLoginInfo shop)
{
if (shop.jit_expire_time == null)
return "未开通";
else if (shop.jit_expire_time?.Date >= DateTime.Now.Date)
return "已开通";
else
return "已过期";
}
public static Color GetJitStatusStyleColor(ShopLoginInfo shop)
{
if (shop.jit_expire_time == null)
return Color.Orange;
else if (shop.jit_expire_time?.Date >= DateTime.Now.Date)
return Color.Green;
else
return Color.Red;
}
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;
long shopId = dgvShop.Rows[e.RowIndex].Cells["colShopId"].Value.ParseToLong();
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 "colActiveJitCardCode":
if (shop != null)
{
this.txtAcitveShopId.Text = shop.ShopId;
this.txtActiveShopName.Text = shop.ShopAccount;
this.txtActiveJitCardCode.Text = "";
}
else
{
BaseMsgForm.ShowInfo("店铺不存在!");
}
break;
}
}
private void btnEmpty_Click(object sender, EventArgs e)
{
txtNotes.Text = txtShopId.Text = txtShopName.Text = "";
}
private void btnActive_Click(object sender, EventArgs e)
{
var url = BaseClass.BuildObjString("shop/active_jit_card_code");
var data = UserLogin.GetSignaturex()
//+ "&login_name=" + shop.LoginName
//直通车相同账户的不同店铺之间是隔离的,所以只能填店铺id更新一个店铺
+ "&shop_id=" + txtAcitveShopId.Text.Trim()
+ "&shop_account=" + txtActiveShopName.Text.Trim()
+ "&card_code_id=" + txtActiveJitCardCode.Text.Trim();
var result = GetHtml.PostUtf8(url, data);
if (!result.Contains("\"code\":200"))
{
var msg = BaseRegex.GetStringJson(ref result, "msg");
if (string.IsNullOrEmpty(msg))
msg = "JIT激活失败!";
BaseMsgForm.ShowError(msg);
}
else
{
var expire_time = BaseRegex.GetStringJson(ref result, "expire_time");
var shop = GlobalConfig.ShopLoginList.FirstOrDefault(s => s.ShopId == txtAcitveShopId.Text.Trim());
if (shop != null)
{
try
{
shop.jit_expire_time = Convert.ToDateTime(expire_time);
RefreshShop();
}
catch
{
}
}
BaseMsgForm.ShowInfo("激活成功!");
}
}
}
}