You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

470 lines
14 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using POSV.Entity;
using POSV.Utils;
using POSV.Bean;
using DevComponents.DotNetBar.Metro;
using DevComponents.DotNetBar;
using POSV.ShoppingCart;
using POSV.Component;
using POSV.MessageEvent;
namespace POSV.Template.Component
{
[ToolboxItem(true)]
public partial class MakeControl : BaseUserControl
{
/// <summary>
/// 数据源
/// </summary>
private List<Tuple<ProductMakeType, ProductMakeDetail>> _dataSource = null;
public MakeControl()
{
InitializeComponent();
this.BackColor = Color.Transparent;
//订购界面变更通知事件
MsgEvent.RemoveListener(Constant.FLAVOR_CHANGED_NOTIFY, this.FlavorChangedEventNotify);
MsgEvent.Receive(Constant.FLAVOR_CHANGED_NOTIFY, this.FlavorChangedEventNotify);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.DesignMode) return;
var productJsonTemplate = Global.Instance.GlobalConfigStringValue(ConfigConstant.MAKE_JSON_TEMPLATE);
if (string.IsNullOrEmpty(productJsonTemplate))
{
productJsonTemplate = JsonUtils.Serialize(new ProductMakeItem());
}
this.RefreshUi(JsonUtils.Deserialize<ProductMakeItem>(productJsonTemplate));
//当前页码
this.PageNumber = 1;
}
protected void FlavorChangedEventNotify(object sender, MsgEventArgs args)
{
if (args.Data is ProductMakeItem)
{
var data = args.Data as ProductMakeItem;
Global.Instance.ReloadConfig(ConfigConstant.SPECMAKE_GROUP);
this.RefreshUi(data);
this.BindDataSource(this._dataSource);
}
}
public void RefreshUi(ProductMakeItem item)
{
this.FontSize = item.MakeFont;
this.Columns = item.Cols;
this.ItemWidth = item.ItemWidth;
this.ItemHeight = item.ItemHeight;
this.BackColor1 = item.BackColor1;
this.BackColor2 = item.BackColor2;
this.TextColor = item.TxtColor;
UpdateTemplate();
}
private void UpdateTemplate()
{
}
private MetroTileItem GetItemTemplate()
{
MetroTileItem template = new MetroTileItem();
template.TileStyle.PaddingLeft = template.TileStyle.PaddingRight = -2;
template.EnableMarkup = true;
template.OptionGroup = "Make";
template.TileStyle.Font = this.itemPanel1.Font;
//this.itemPanel1.ResizeItemsToFit = false;
this.itemPanel1.BackgroundStyle.Class = "MetroTileGroupTitle";//很关键
template.TileStyle.TextAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Center;
template.TileStyle.TextLineAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Near;
if (this.ShowPrice)
{
template.TitleTextAlignment = System.Drawing.ContentAlignment.BottomCenter;
}
template.TileStyle.TextColor = ColorTranslator.FromHtml(this.TextColor);
template.TitleTextColor = ColorTranslator.FromHtml(this.TextColor);
template.TileStyle.BackColor = ColorTranslator.FromHtml(this.BackColor1);
template.TileStyle.BackColor2 = ColorTranslator.FromHtml(this.BackColor2);
template.TileStyle.BorderWidth = 1;
//显示大小
template.TileSize = new Size(this.ItemWidth, this.ItemHeight);
//圆角
template.TileStyle.CornerType = eCornerType.Rounded;
template.MouseDown += OnItemMouseDown;
return template;
}
private void OnItemMouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MetroTileItem item = (MetroTileItem)sender;
var productMake = item.Tag as ProductMakeDetail;
switch (productMake.Memo)
{
case Constant.PREV_PAGER:
{
int pageNumber = 1;
int.TryParse(productMake.Ext3, out pageNumber);
this.PageNumber = pageNumber;
this.BindDataSource(this._dataSource);
}
break;
case Constant.NEXT_PAGER:
{
int pageNumber = 1;
int.TryParse(productMake.Ext3, out pageNumber);
this.PageNumber = pageNumber;
this.BindDataSource(this._dataSource);
}
break;
default:
{
this.OnProductMakeCheckedChanged(new ProductMakeEventArgs(productMake));
}
break;
}
}
}
public event ProductMakeEventHandler ProductMakeCheckedChanged;
protected virtual void OnProductMakeCheckedChanged(ProductMakeEventArgs e)
{
ProductMakeCheckedChanged?.Invoke(this, e);
}
public void BindDataSource(List<Tuple<ProductMakeType, ProductMakeDetail>> dataSource)
{
if (dataSource == null)
{
dataSource = new List<Tuple<ProductMakeType, ProductMakeDetail>>();
}
this._dataSource = dataSource;
//总数量
this.TotalCount = dataSource.Count;
//每页的显示数量
this.PageSize = this.Rows * this.Columns;
//清空历史数据
this.itemPanel1.Items.Clear();
this.itemPanel1.Font = this.GetSystemFont(this._fontSize);
var merge = MakeMerge.Merge(this.Rows, this.Columns, dataSource);
var pager = ListPager(merge);
ItemContainer ic = new ItemContainer();
ic.MultiLine = true;
ic.ItemSpacing = 0;
ic.LayoutOrientation = eOrientation.Horizontal;
foreach (var entity in pager)
{
var type = entity.Item1;
var make = entity.Item2;
MetroTileItem item = this.GetItemTemplate();
item.Checked = false;
//绑定品类ID到Tag属性
item.Tag = make;
switch (entity.Item2.Memo)
{
case Constant.PREV_PAGER:
{
item.Image = POSV.Properties.Resources.left;
item.ImageTextAlignment = System.Drawing.ContentAlignment.MiddleCenter;
item.TileStyle.BackColor = ColorTranslator.FromHtml(this.BackColor1);
item.TileStyle.BackColor2 = ColorTranslator.FromHtml(this.BackColor2);
item.TileStyle.BorderColor = ColorTranslator.FromHtml(this.BackColor1);
item.TileStyle.BorderColor2 = ColorTranslator.FromHtml(this.BackColor2);
}
break;
case Constant.NEXT_PAGER:
{
item.Image = POSV.Properties.Resources.right;
item.ImageTextAlignment = System.Drawing.ContentAlignment.MiddleCenter;
item.TileStyle.BackColor = ColorTranslator.FromHtml(this.BackColor1);
item.TileStyle.BackColor2 = ColorTranslator.FromHtml(this.BackColor2);
item.TileStyle.BorderColor = ColorTranslator.FromHtml(this.BackColor1);
item.TileStyle.BorderColor2 = ColorTranslator.FromHtml(this.BackColor2);
}
break;
default:
{
item.Text = make.Memo;
item.TileStyle.BackColor = ColorTranslator.FromHtml(type.Color);
item.TileStyle.BackColor2 = ColorTranslator.FromHtml(type.Color);
item.TileStyle.BorderColor = ColorTranslator.FromHtml(type.Color);
item.TileStyle.BorderColor2 = ColorTranslator.FromHtml(type.Color);
if (this.ShowPrice)
{
if (make.AddPrice != 0)
{
item.TitleText = StringUtils.FormatDataNoDigit(make.AddPrice);
}
}
}
break;
}
ic.SubItems.Add(item);
}
this.itemPanel1.Items.Add(ic);
this.itemPanel1.Invalidate();
}
public void BindDataSource(List<ProductMakeDetail> dataSource, int pageNum)
{
int pageNumber = 1;
int.TryParse(pageNum.ToString(), out pageNumber);
this.PageNumber = pageNumber;
if (dataSource == null)
{
dataSource = new List<ProductMakeDetail>();
}
//根据做法分类的顺序对做法进行排序
List<Tuple<ProductMakeType, ProductMakeDetail>> newDataSource = new List<Tuple<ProductMakeType, ProductMakeDetail>>();
foreach(var type in BusinessUtils.Instance.GetMakeTypeList)
{
var list = dataSource.FindAll(x => x.TypeId == type.Id);
foreach(var temp in list)
{
newDataSource.Add(new Tuple<ProductMakeType, ProductMakeDetail>(type, temp));
}
}
BindDataSource(newDataSource);
}
/// <summary>
/// 分页List数据
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public List<Tuple<ProductMakeType, ProductMakeDetail>> ListPager(List<Tuple<ProductMakeType, ProductMakeDetail>> data)
{
var result = new List<Tuple<ProductMakeType, ProductMakeDetail>>();
result.AddRange(data.Skip((this.PageNumber - 1) * this.PageSize).Take(this.PageSize));
return result;
}
private int _itemHeight = 70;
/// <summary>
///品类显示的高度
/// </summary>
public int ItemHeight
{
get { return _itemHeight; }
set
{
_itemHeight = (value <= 0 ? 70 : value);
}
}
private int _itemWidth = 90;
/// <summary>
/// 品类的宽度
/// </summary>
public int ItemWidth
{
get
{
return this._itemWidth;
}
set
{
if (value <= 0 || ((value * this._columns) > this.Width))
{
this._itemWidth = this.Width / this._columns;
}
else
{
this._itemWidth = value;
this._columns = this.Width / this._itemWidth;
}
}
}
private SystemFont _fontSize = SystemFont.;
/// <summary>
/// 字体大小
/// </summary>
public SystemFont FontSize
{
get { return this._fontSize; }
set { this._fontSize = value; }
}
private string _backColor1 = ColorTranslator.ToHtml(Color.DimGray);
/// <summary>
/// 分页显示颜色
/// </summary>
public string BackColor1
{
get { return this._backColor1; }
set { this._backColor1 = value; }
}
private string _backColor2 = ColorTranslator.ToHtml(Color.DimGray);
/// <summary>
/// 分页显示颜色
/// </summary>
public string BackColor2
{
get { return this._backColor2; }
set { this._backColor2 = value; }
}
private string _textColor = ColorTranslator.ToHtml(Color.White);
/// <summary>
/// 品类显示颜色
/// </summary>
public string TextColor
{
get { return this._textColor; }
set { this._textColor = value; }
}
/// <summary>
/// 每页的大小
/// </summary>
public int PageSize { get; private set; }
/// <summary>
/// 总页数
/// </summary>
public int PageCount => this.PageSize <= 0 ? 0 : ((this.TotalCount + this.PageSize - 1) / this.PageSize);
/// <summary>
/// 当前页码
/// </summary>
public int PageNumber { get; set; }
/// <summary>
/// 总数量
/// </summary>
public int TotalCount { get; private set; }
private int _columns = 1;
/// <summary>
/// 每一行显示的数量
/// </summary>
public int Columns
{
get
{
return _columns;
}
set
{
_columns = (value <= 0 ? 1 : value);
}
}
/// <summary>
///品类默认行数
/// </summary>
public int Rows => this.Height / this.ItemHeight;
//是否有上一页
public bool HasPreviousPage
{
get { return PageNumber > 1; }
}
//是否有下一页
public bool HasNextPage
{
get { return PageNumber < this.PageCount; }
}
/// <summary>
/// 显示零售价
/// </summary>
private bool _showPrice = true;
public bool ShowPrice
{
get
{
return _showPrice;
}
set
{
_showPrice = value;
}
}
}
public delegate void ProductMakeEventHandler(object sender, ProductMakeEventArgs e);
public class ProductMakeEventArgs : EventArgs
{
public ProductMakeDetail ProductMakeDetail;
public ProductMakeEventArgs(ProductMakeDetail data)
{
this.ProductMakeDetail = data;
}
}
}