using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using POSV.Utils;
using System.Threading.Tasks;
using POSV.Bean;
using DevComponents.DotNetBar.Controls;
using POSV.Entity;
using POSV.MessageEvent;
namespace POSV.Component
{
[ToolboxItem(true)]
public partial class CategoryParameter : AbstractParameter
{
///
/// 大类默认参数
///
private string[] mainRows = { "1" , "2" , "3" , "4" , "5" };
private string[] mainItemHeight = { "40" , "45" , "50" , "55" , "60" , "70" , "80" , "100" , "120" };
private string[] mainCols = { "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" };
private string[] mainItemWidth = { "50" , "60" , "70" , "80" , "90" , "100" , "120" , "150" };
///
/// 小类默认参数
///
private string[] subRows = { "1" , "2" , "3" , "4" , "5" };
private string[] subItemHeight = { "40" , "45" , "50" , "55" , "60" , "70" , "80" , "100" , "120" };
private string[] subCols = { "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" };
private string[] subItemWidth = { "50" , "60" , "70" , "80" , "90" , "100" , "120" , "150" };
private CategoryItem categoryItem = null;
private bool _inited = false;
public CategoryParameter()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.BackColor = Color.Transparent;
//字体支持列表
this.mainComboBoxFontSize.DataSource = System.Enum.GetNames(typeof(SystemFont));
//每行显示的品类数量
this.mainComboBoxColumns.DataSource = mainCols;
//品类显示的行数
this.mainComboBoxRows.DataSource = mainRows;
//设置系统默认的行高度
this.mainComboBoxItemHeight.DataSource = mainItemHeight;
//设置系统默认的自定义宽度
this.mainComboBoxItemWidth.DataSource = mainItemWidth;
///////////////////////////小类////////////////////////////////
//字体支持列表
this.subComboBoxFontSize.DataSource = System.Enum.GetNames(typeof(SystemFont));
//每行显示的品类数量
this.subComboBoxColumns.DataSource = subCols;
//品类显示的行数
this.subComboBoxRows.DataSource = subRows;
//设置系统默认的行高度
this.subComboBoxItemHeight.DataSource = subItemHeight;
//设置系统默认的自定义宽度
this.subComboBoxItemWidth.DataSource = subItemWidth;
//显示样式
this.comboBoxCornerType.DataSource = System.Enum.GetNames(typeof(CornerType));
this.InitUi();
}
///
/// 初始化界面
///
private void InitUi()
{
//界面加载开始
this._inited = false;
Task.Factory.StartNew(() =>
{
Dictionary data = null;
lock (Global.Instance.SyncLock)
{
//获取品类分组下的全部参数
using (var db = Global.Instance.OpenDataBase)
{
data = db.Dictionary(string.Format(SqlConstant.ConfigQueryByGroupToDictionary , ConfigConstant.CATEGORY_GROUP));
}
}
//将新增加或者变更的参数重新更新到数据库
var defaultValue = ConfigConstant.CategoryGroupDefaultValue();
//数据库有配置参数
if (data != null)
{
var diff = defaultValue.Where(kvp => !data.ContainsKey(kvp.Key)).ToDictionary(kvp => kvp.Key , kvp => kvp.Value);
if (diff != null && diff.Keys.Count > 0)
{
var list = new List();
foreach (var v in diff)
{
var config = new Config();
config.Id = IdWorkerUtils.Instance.NextId();
config.Group = ConfigConstant.CATEGORY_GROUP;
config.TenantId = Global.Instance.Authc.TenantId;
config.Keys = v.Key;
config.Values = v.Value;
list.Add(config);
}
//如果数据表中没有配置group相关的信息,更新差异
this.SaveChanged(list);
//将差异合并到data,避免再次访问数据库
data = data.Concat(diff).ToDictionary(x => x.Key , x => x.Value);
}
}
else
{
//加载系统默认参数
data = ConfigConstant.CategoryGroupDefaultValue();
var list = new List();
foreach (var v in data)
{
var config = new Config();
config.Id = IdWorkerUtils.Instance.NextId();
config.Group = ConfigConstant.CATEGORY_GROUP;
config.TenantId = Global.Instance.Authc.TenantId;
config.Keys = v.Key;
config.Values = v.Value;
list.Add(config);
}
//如果数据表中没有配置group相关的信息,保存
this.SaveChanged(list);
}
return data;
}).ContinueWith(task =>
{
if (task.IsFaulted)
{
LOGGER.Error(task.Exception.GetBaseException() , "加载品类设置参数异常");
}
else
{
this.Invoke(new Action(() =>
{
BindUi(task.Result);
}));
}
});
}
private void BindUi(Dictionary data)
{
//点单列表显示定义
string json = data[ConfigConstant.CATEGORY_JSON_TEMPLATE].ToString();
//将JSON转换为行对象
this.categoryItem = JsonUtils.Deserialize(json);
////////////////////////////品类模版数据/////////////////////////////////////
switch (categoryItem.ShowType)
{
case CategoryShowType.系统默认:
this.checkBoxX43.Checked = true;
this.groupBoxMainCategory.Enabled = true;
this.groupBoxSubCategory.Enabled = true;
break;
case CategoryShowType.仅显示全部大类:
this.checkBoxX44.Checked = true;
this.groupBoxMainCategory.Enabled = true;
this.groupBoxSubCategory.Enabled = false;
break;
case CategoryShowType.仅显示全部小类:
this.checkBoxX1.Checked = true;
this.groupBoxMainCategory.Enabled = false;
this.groupBoxSubCategory.Enabled = true;
break;
}
//品项显示宽度,自动计算宽度
this.mainCheckBoxAutoItemWidth.Checked = !categoryItem.MainAutoItemWidth;
//每行显示的数量
this.mainComboBoxColumns.SelectedIndex = this.mainComboBoxColumns.FindString(categoryItem.MainCategoryColumns.ToString());
//品类块的宽度
this.mainComboBoxItemWidth.Text = categoryItem.MainCustomItemWidth.ToString();
//字体大小
SystemFont mainFontSize = categoryItem.MainCategoryFont;
this.mainComboBoxFontSize.SelectedIndex = this.mainComboBoxFontSize.FindString(mainFontSize.ToString());
//行高
this.mainComboBoxRows.SelectedIndex = this.mainComboBoxRows.FindString(categoryItem.MainCategoryRows.ToString());
//品类块的高度
this.mainComboBoxItemHeight.Text = categoryItem.MainCategoryItemHeight.ToString();
//字体颜色
this.mainComboBoxFontColor.Color = ColorTranslator.FromHtml(categoryItem.MainCategoryTextColor);
//背景色1
this.mainComboBoxColor1.Color = ColorTranslator.FromHtml(categoryItem.MainCategoryBackColor1);
//背景色2
this.mainComboBoxColor2.Color = ColorTranslator.FromHtml(categoryItem.MainCategoryBackColor2);
//选中的颜色
this.mainComboBoxSelectedColor.Color = ColorTranslator.FromHtml(categoryItem.MainCategorySelectedColor);
//是否隐藏分页
this.mainCheckBoxAutoPager.Checked = categoryItem.MainAutoPager;
//是否显示编码
this.mainCheckBoxDisplayCode.Checked = categoryItem.MainCategoryCode;
/////////////////////////////小类///////////////////////////////////////////
//品项显示宽度,自动计算宽度
this.subCheckBoxAutoItemWidth.Checked = !categoryItem.SubAutoItemWidth;
//每行显示的数量
this.subComboBoxColumns.SelectedIndex = this.subComboBoxColumns.FindString(categoryItem.SubCategoryColumns.ToString());
//品类块的宽度
this.subComboBoxItemWidth.Text = categoryItem.SubCustomItemWidth.ToString();
//字体大小
SystemFont subFontSize = categoryItem.SubCategoryFont;
this.subComboBoxFontSize.SelectedIndex = this.mainComboBoxFontSize.FindString(subFontSize.ToString());
//行高
this.subComboBoxRows.SelectedIndex = this.subComboBoxRows.FindString(categoryItem.SubCategoryRows.ToString());
//品类块的高度
this.subComboBoxItemHeight.Text = categoryItem.SubCategoryItemHeight.ToString();
//字体颜色
this.subComboBoxFontColor.Color = ColorTranslator.FromHtml(categoryItem.SubCategoryTextColor);
//背景色1
this.subComboBoxColor1.Color = ColorTranslator.FromHtml(categoryItem.SubCategoryBackColor1);
//背景色2
this.subComboBoxColor2.Color = ColorTranslator.FromHtml(categoryItem.SubCategoryBackColor2);
//选中的颜色
this.subComboBoxSelectedColor.Color = ColorTranslator.FromHtml(categoryItem.SubCategorySelectedColor);
//是否隐藏分页
this.subCheckBoxAutoPager.Checked = categoryItem.SubAutoPager;
//是否显示编码
this.subCheckBoxDisplayCode.Checked = categoryItem.SubCategoryCode;
//品类显示中添加全部标签,该标签操作将显示全部可售单品
this.chkShowAllProduct.Checked = categoryItem.ShowAllProduct;
//显示的样式
this.comboBoxCornerType.SelectedIndex = this.comboBoxCornerType.FindString(categoryItem.CategoryCornerType.ToString());
//界面加载完成
this._inited = true;
}
public List NewChanged()
{
var result = new List();
if (!this._inited) return result;
var newCategoryItem = new CategoryItem();
//主界面品类显示方式
if (this.checkBoxX44.Checked)
{
newCategoryItem.ShowType = CategoryShowType.仅显示全部大类;
}
else if (this.checkBoxX1.Checked)
{
newCategoryItem.ShowType = CategoryShowType.仅显示全部小类;
}
else
{
newCategoryItem.ShowType = CategoryShowType.系统默认;
}
//大类///////////////////////////////////
int mainItemWidth = 80;
newCategoryItem.MainAutoItemWidth = !this.mainCheckBoxAutoItemWidth.Checked;
if (this.mainCheckBoxAutoItemWidth.Checked)
{
int.TryParse(this.mainComboBoxItemWidth.Text , out mainItemWidth);
}
//品类块的宽度
newCategoryItem.MainCustomItemWidth = mainItemWidth;
//每行显示的数量
newCategoryItem.MainCategoryColumns = Convert.ToInt32(this.mainComboBoxColumns.SelectedItem.ToString());
//字体大小
SystemFont mainFontSize = SystemFont.默认;
Enum.TryParse(this.mainComboBoxFontSize.SelectedItem.ToString() , out mainFontSize);
newCategoryItem.MainCategoryFont = mainFontSize;
//行数
newCategoryItem.MainCategoryRows = Convert.ToInt32(this.mainComboBoxRows.SelectedItem.ToString());
//品类块的高度
int mainItemHeight = 45;
int.TryParse(this.mainComboBoxItemHeight.Text , out mainItemHeight);
newCategoryItem.MainCategoryItemHeight = mainItemHeight;
//字体颜色
newCategoryItem.MainCategoryTextColor = ColorTranslator.ToHtml(this.mainComboBoxFontColor.Color);
//背景色1
newCategoryItem.MainCategoryBackColor1 = ColorTranslator.ToHtml(this.mainComboBoxColor1.Color);
//背景色2
newCategoryItem.MainCategoryBackColor2 = ColorTranslator.ToHtml(this.mainComboBoxColor2.Color);
//选中的颜色
newCategoryItem.MainCategorySelectedColor = ColorTranslator.ToHtml(this.mainComboBoxSelectedColor.Color);
//是否隐藏分页
newCategoryItem.MainAutoPager = this.mainCheckBoxAutoPager.Checked;
//是否显示编码
newCategoryItem.MainCategoryCode = this.mainCheckBoxDisplayCode.Checked;
/////////////////////////////小类///////////////////////////////////////////
int subItemWidth = 80;
newCategoryItem.SubAutoItemWidth = !this.subCheckBoxAutoItemWidth.Checked;
if (this.subCheckBoxAutoItemWidth.Checked)
{
int.TryParse(this.subComboBoxItemWidth.Text , out subItemWidth);
}
//品类块的宽度
newCategoryItem.SubCustomItemWidth = subItemWidth;
//每行显示的数量
newCategoryItem.SubCategoryColumns = Convert.ToInt32(this.subComboBoxColumns.SelectedItem.ToString());
//字体大小
SystemFont subFontSize = SystemFont.默认;
Enum.TryParse(this.subComboBoxFontSize.SelectedItem.ToString() , out subFontSize);
newCategoryItem.SubCategoryFont = subFontSize;
//行数
newCategoryItem.SubCategoryRows = Convert.ToInt32(this.subComboBoxRows.SelectedItem.ToString());
//品类块的高度
int subItemHeight = 45;
int.TryParse(this.subComboBoxItemHeight.Text , out subItemHeight);
newCategoryItem.SubCategoryItemHeight = subItemHeight;
//字体颜色
newCategoryItem.SubCategoryTextColor = ColorTranslator.ToHtml(this.subComboBoxFontColor.Color);
//背景色1
newCategoryItem.SubCategoryBackColor1 = ColorTranslator.ToHtml(this.subComboBoxColor1.Color);
//背景色2
newCategoryItem.SubCategoryBackColor2 = ColorTranslator.ToHtml(this.subComboBoxColor2.Color);
//选中的颜色
newCategoryItem.SubCategorySelectedColor = ColorTranslator.ToHtml(this.subComboBoxSelectedColor.Color);
//是否隐藏分页
newCategoryItem.SubAutoPager = this.subCheckBoxAutoPager.Checked;
//是否显示编码
newCategoryItem.SubCategoryCode = this.subCheckBoxDisplayCode.Checked;
//品类显示中添加全部标签,该标签操作将显示全部可售单品
newCategoryItem.ShowAllProduct = this.chkShowAllProduct.Checked;
//显示样式
CornerType cornerType = CornerType.默认;
Enum.TryParse(this.comboBoxCornerType.SelectedItem.ToString() , out cornerType);
newCategoryItem.CategoryCornerType = cornerType;
//判断是否更改,如果过更改压入到 NewValue
bool isChanged = !(newCategoryItem.Equals(this.categoryItem));
if (isChanged)
{
//this._newValue[ConfigConstant.CATEGORY_JSON_TEMPLATE] = JsonUtils.Serialize(newCategoryItem);
var config = new Config();
config.Id = IdWorkerUtils.Instance.NextId();
config.Group = ConfigConstant.CATEGORY_GROUP;
config.TenantId = Global.Instance.Authc.TenantId;
config.Keys = ConfigConstant.CATEGORY_JSON_TEMPLATE;
config.Values = JsonUtils.Serialize(newCategoryItem);
result.Add(config);
}
//转换为Config对象,便于存储
//foreach (var v in this._newValue)
//{
// var config = new Config();
// config.Id = IdWorkerUtils.Instance.NextId();
// config.Group = ConfigConstant.CATEGORY_GROUP;
// config.TenantId = Global.Instance.Authc.TenantId;
// config.Keys = v.Key;
// config.Values = v.Value;
// result.Add(config);
//}
return result;
}
private void OnCategoryShowTypeChecked(object sender , EventArgs e)
{
var item = sender as CheckBoxX;
if (item.Checked)
{
var showType = CategoryShowType.系统默认;
Enum.TryParse(item.Tag.ToString() , out showType);
switch (showType)
{
case CategoryShowType.系统默认:
{
this.groupBoxMainCategory.Enabled = true;
this.groupBoxSubCategory.Enabled = true;
//this.chkShowAllProduct.Checked = false;
//this.chkShowAllProduct.Enabled = false;
}
break;
case CategoryShowType.仅显示全部大类:
{
this.groupBoxMainCategory.Enabled = true;
this.groupBoxSubCategory.Enabled = false;
this.chkShowAllProduct.Enabled = true;
}
break;
case CategoryShowType.仅显示全部小类:
{
this.groupBoxMainCategory.Enabled = false;
this.groupBoxSubCategory.Enabled = true;
this.chkShowAllProduct.Enabled = true;
}
break;
}
}
}
private void OnMainItemWidthChecked(object sender , EventArgs e)
{
var item = sender as CheckBoxX;
this.mainComboBoxItemWidth.Enabled = item.Checked;
}
private void OnSubItemWidthChecked(object sender , EventArgs e)
{
var item = sender as CheckBoxX;
this.subComboBoxItemWidth.Enabled = item.Checked;
}
public Tuple SaveChanged(List data)
{
bool isSuccess = true;
string message = "参数更新成功";
try
{
lock (Global.Instance.SyncLock)
{
using (var db = Global.Instance.OpenDataBase)
{
using (var trans = db.GetTransaction())
{
foreach (var config in data)
{
db.Save(config);
}
trans.Complete();
}
}
}
}
catch (Exception ex)
{
isSuccess = false;
message = "参数更新异常";
LOGGER.Error(ex , message);
}
finally
{
if (isSuccess)
{
//更新控件UI
UpdateCategoryUi();
}
}
return new Tuple(isSuccess , message);
}
private void UpdateCategoryUi()
{
Config config = null;
using (var db = Global.Instance.OpenDataBase)
{
string sql = string.Format(SqlConstant.ConfigQueryByGroupAndKey , ConfigConstant.CATEGORY_GROUP , ConfigConstant.CATEGORY_JSON_TEMPLATE);
config = db.SingleOrDefault(sql);
}
if (config != null)
{
CategoryItem item = JsonUtils.Deserialize(config.Values);
//新修改后的参数应用后,避免用户在没有切换Tab情况下,直接修改当前Tab内容
this.categoryItem = item;
//发送分组通知
MsgEvent.Send(Constant.CATEGORY_CHANGED_NOTIFY , item);
}
}
}
}