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.

267 lines
9.2 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.Windows.Forms;
using POSV.Entity;
using DevComponents.DotNetBar.Metro;
using POSV.MessageEvent;
using DevComponents.DotNetBar;
using POSV.ShoppingCart;
namespace POSV.Business
{
public partial class ProductCouponPanelEx : AbstractFlyoutPanelEx
{
/// <summary>
/// 选中行数据
/// </summary>
private readonly OrderItem _orderItem = null;
/// <summary>
/// 规格数据源
/// </summary>
private List<StoreProductCoupon> _storeProductCouponsList = null;
/// <summary>
/// 键盘、方向键
/// </summary>
private KeyboardAction _action = KeyboardAction.None;
/// <summary>
/// 当前焦点索引
/// </summary>
private int _index = 0;
/// <summary>
/// 当前焦点单品
/// </summary>
private MetroTileItem _curr = null;
public ProductCouponPanelEx(OrderItem item)
{
InitializeComponent();
this._orderItem = item;
//规格键盘通知事件
MsgEvent.RemoveListener(Constant.SPEC_KEYBOARD_NOTIFY, this.SpecKeyboardEventNotify);
MsgEvent.Receive(Constant.SPEC_KEYBOARD_NOTIFY, this.SpecKeyboardEventNotify);
}
protected void SpecKeyboardEventNotify(object sender, MsgEventArgs args)
{
if (args != null && args.Data != null)
{
Enum.TryParse<KeyboardAction>(args.Data.ToString(), out this._action);
switch (this._action)
{
case KeyboardAction.Left:
{
if (this.itemPanel.Items.Count > 0)
{
var container = this.itemPanel.Items[0] as ItemContainer;
int subItems = container.SubItems.Count;
//只有一个单品
if (subItems == 1)
{
//当前操作的
this._index = 0;
this._curr = container.SubItems[this._index] as MetroTileItem;
this.UpdateSelectedStyle(this._curr);
}
if (this._index - 1 >= 0)
{
//当前操作的,重置样式
var focus = container.SubItems[this._index] as MetroTileItem;
this.ResetMetroTileItemStyle(focus);
//当前操作的
this._index = this._index - 1;
this._curr = container.SubItems[this._index] as MetroTileItem;
this.UpdateSelectedStyle(this._curr);
}
}
}
break;
case KeyboardAction.Right:
{
if (this.itemPanel.Items.Count > 0)
{
var container = this.itemPanel.Items[0] as ItemContainer;
int subItems = container.SubItems.Count;
//只有一个单品
if (subItems == 1)
{
//当前操作的
this._index = 0;
this._curr = container.SubItems[this._index] as MetroTileItem;
this.UpdateSelectedStyle(this._curr);
}
if (this._index + 1 < subItems)
{
//重置当前样式
var focus = container.SubItems[this._index] as MetroTileItem;
this.ResetMetroTileItemStyle(focus);
//当前操作的
this._index = this._index + 1;
this._curr = container.SubItems[this._index] as MetroTileItem;
this.UpdateSelectedStyle(this._curr);
}
}
}
break;
case KeyboardAction.Enter:
{
if (this._curr != null)
{
LOGGER.Info(">>>>>>" + this._curr.Text);
LOGGER.Info(">>>>>>" + this._curr.DisplayRectangle);
LOGGER.Info(">>>>>>" + this._curr.Bounds);
LOGGER.Info(">>>>>>" + this.Parent);
LOGGER.Info(">>>>>>" + this);
var r = this.RectangleToScreen(this.DisplayRectangle);
var e = new MouseEventArgs(MouseButtons.Left, 1, r.Left + 5, r.Top + 5, 0);
this.TileItemMouseDown(this._curr, e);
}
}
break;
}
}
}
public void ResetMetroTileItemStyle(MetroTileItem item)
{
//背景颜色
item.TileColor = eMetroTileColor.Coffee;
//去除边框颜色
item.TileStyle.BorderColor = Color.Transparent;
item.TileStyle.BorderColor2 = Color.Transparent;
}
public void UpdateSelectedStyle(MetroTileItem item)
{
//背景颜色
item.TileColor = eMetroTileColor.DarkGreen;
item.TileStyle.BorderColor = Color.Transparent;
item.TileStyle.BorderColor2 = Color.Transparent;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.SetStyle(ControlStyles.Selectable, true);
if (this.DesignMode) return;
this._curr = null;
lock (Global.Instance.SyncLock)
{
//规格数据源
using (var db = Global.Instance.OpenDataBase)
{
this._storeProductCouponsList = db.Query<StoreProductCoupon>("where productId = @0 and specId = @1", this._orderItem.ProductId, this._orderItem.SpecId).ToList();
}
}
if (this._storeProductCouponsList == null)
{
this._storeProductCouponsList = new List<StoreProductCoupon>();
}
BuilderAll();
}
private void BuilderAll()
{
//清空历史数据
this.itemPanel.Items.Clear();
ItemContainer ic = new ItemContainer();
ic.MultiLine = true;
ic.ItemSpacing = 0;
ic.LayoutOrientation = eOrientation.Horizontal;
foreach (var entity in this._storeProductCouponsList)
{
MetroTileItem tileItem = new MetroTileItem();
tileItem.TileStyle.PaddingLeft = tileItem.TileStyle.PaddingRight = -1;
tileItem.OptionGroup = "Coupons";
tileItem.SymbolColor = Color.Empty;
tileItem.TileSize = new Size(150, 60);
tileItem.TileStyle.CornerType = eCornerType.Square;
tileItem.TileStyle.TextAlignment = eStyleTextAlignment.Center;
tileItem.TileStyle.TextLineAlignment = eStyleTextAlignment.Near;
tileItem.TitleText = Constant.CNY + entity.CouponPrice.ToString();
tileItem.TitleTextAlignment = ContentAlignment.BottomCenter;
tileItem.TitleTextFont = Constant.NORMAL_FONT;
tileItem.Text = string.Format("{0}元优惠券", entity.CouponPrice);
//背景颜色
tileItem.TileColor = eMetroTileColor.Coffee;
//去除边框颜色
tileItem.TileStyle.BorderColor = Color.Transparent;
tileItem.TileStyle.BorderColor2 = Color.Transparent;
if (entity.Id.Equals(this._orderItem.SpecId))
{
tileItem.Checked = true;
}
tileItem.Tag = entity;
tileItem.MouseDown += TileItemMouseDown;
ic.SubItems.Add(tileItem);
}
this.itemPanel.Items.Add(ic);
this.itemPanel.Invalidate();
}
private void TileItemMouseDown(object sender, MouseEventArgs e)
{
MetroTileItem item = (MetroTileItem)sender;
item.Checked = true;
var storeProductCoupon = item.Tag as StoreProductCoupon;
//通知主界面改变
this.OnNotifyChanged(new FlyoutEventArgs(FlyoutAction.Notify, storeProductCoupon));
}
}
}