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.

491 lines
17 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar.Controls;
using POSV.Entity;
using POSV.Utils;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Metro;
namespace POSV.Component
{
public partial class AddResourceForm
: BaseForm
{
private List<Resources> _dataSource;
public AddResourceForm()
{
InitializeComponent();
this.controlBox.Text = "注册优惠功能";
this.superTabItem1.Text = "新增资源";
this.superTabControl1.SelectedTabIndex = 0;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
lock (Global.Instance.SyncLock)
{
//获取数据库的资源数据
using (var db = Global.Instance.OpenDataBase)
{
this._dataSource = db.Query<Resources>().ToList();
}
}
//如果数据库数据为空
if (this._dataSource == null)
{
this._dataSource = new List<Resources>();
}
this.checkBoxX1.Checked = true;
InstallResources();
}
/// <summary>
/// 加载自定义资源
/// </summary>
private void InstallResources()
{
this.itemPanel1.Items.Clear();
var customList = this._dataSource.FindAll(x => x.Group == "自定义");
ItemContainer ic = new ItemContainer();
ic.MultiLine = true;
foreach (Resources entity in customList)
{
MetroTileItem item = new MetroTileItem();
item.TileStyle.Padding = -2;
item.CheckBehavior = eMetroTileCheckBehavior.LeftMouseButtonClick;
item.Text = entity.Name;
item.TileStyle.Font = Constant.NORMAL_FONT;
item.TileStyle.TextAlignment = eStyleTextAlignment.Center;
item.SymbolColor = Color.Empty;
item.TileColor = eMetroTileColor.Gray;
item.TileSize = new Size(81, 40);
item.OptionGroup = "resources";
item.Tag = entity;
item.MouseDown += Item_MouseDown; ;
ic.SubItems.Add(item);
}
this.itemPanel1.Items.Add(ic);
this.itemPanel1.Invalidate();
}
private Resources _checkedResource = null;
private void Item_MouseDown(object sender, MouseEventArgs e)
{
MetroTileItem item = sender as MetroTileItem;
var resources = item.Tag as Resources;
item.Checked = true;
if (item.Checked)
{
_checkedResource = resources;
}
}
private void ButtonSaveClick(object sender , EventArgs e)
{
bool isException = true;
try
{
if (string.IsNullOrEmpty(txtName.Text.Trim()))
{
this.ShowMessage(this.lblInfo , "资源名称不能为空" , true);
txtName.Focus();
return;
}
if (string.IsNullOrEmpty(txtKeyData.Text.Trim()))
{
this.ShowMessage(this.lblInfo , "操作数值不能为空" , true);
return;
}
string authCode = null;
var tempData = this.txtKeyData.Text;
var data = StringUtils.GetDecimal(tempData);
var type = keyCodePanel.Tag.ToString();
if (!string.IsNullOrEmpty(type) && !string.IsNullOrEmpty(tempData))
{
ModuleKeyCode keyCode = ModuleKeyCode.;
Enum.TryParse(type, out keyCode);
switch (keyCode)
{
case ModuleKeyCode.:
{
if (data == 0)
{
this.ShowMessage(this.lblInfo, "数值非法", true);
return;
}
else
{
authCode = "10045";
}
}
break;
case ModuleKeyCode.:
{
if (data == 0)
{
this.ShowMessage(this.lblInfo, "数值非法", true);
return;
}
else
{
authCode = "10046";
}
}
break;
case ModuleKeyCode.:
{
if (data <= 0 && data >= 100)
{
this.ShowMessage(this.lblInfo, "数值非法", true);
return;
}
else
{
authCode = "10047";
}
}
break;
case ModuleKeyCode.:
{
if (data <= 0 && data >= 100)
{
this.ShowMessage(this.lblInfo, "数值非法", true);
return;
}
else
{
authCode = "10048";
}
}
break;
}
}
Resources entity = null;
string oldKeyCode = null;
if (this.superTabItem1.Text == "新增资源")
{
//新增
if (this._dataSource.Exists(x => x.Name.Equals(txtName.Text.Trim())))
{
this.ShowMessage(this.lblInfo , "资源名称已经存在" , true);
txtName.Focus();
return;
}
entity = new Resources();
entity.Id = IdWorkerUtils.Instance.NextId();
}
else
{
//修改
if(_checkedResource == null)
{
this.ShowToastNotify(this, "请先选择要修改的资源");
return;
}
entity = _checkedResource;
oldKeyCode = entity.KeyCode;
}
entity.TenantId = Global.Instance.Authc.TenantId;
entity.Group = ResourcesGroup..ToString();
entity.Name = txtName.Text.Trim();
entity.KeyCode = keyCodePanel.Tag.ToString();
entity.KeyData = txtKeyData.Text.Trim();
entity.PermissionCode = authCode;
entity.Enable = 1;
lock (Global.Instance.SyncLock)
{
using (var db = Global.Instance.OpenDataBase)
{
using (var transaction = db.GetTransaction())
{
db.Save<Resources>(entity);
if (this.superTabItem1.Text == "修改资源")
{
var sql = @"update [pos_module] set [keycode] = '{0}', [keydata] = '{1}', [name] = '{2}', [alias] = '{3}' where [keycode] = '{4}';";
db.Execute(string.Format(sql, entity.KeyCode, entity.KeyData, entity.Name, entity.Name, oldKeyCode));
}
transaction.Complete();
}
}
}
isException = false;
}
catch (Exception ex)
{
isException = true;
this.ShowMessage(this.lblInfo , "保存自定义优惠功能异常" , true);
LOGGER.Error(ex , "保存自定义优惠功能异常");
}
finally
{
if (!isException)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
}
}
private void OnCloseClick(object sender , EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
private void OnKeyCodeCheckedChanged(object sender , EventArgs e)
{
var keycode = (sender as CheckBoxX);
keycode.Parent.Tag = keycode.Tag.ToString();
OnDataValueChanged(sender, null);
}
/// <summary>
/// 数值变更提示
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnDataValueChanged(object sender, EnterEventArg e)
{
noticeLabel.Text = string.Empty;
var tempData = this.txtKeyData.Text;
var data = StringUtils.GetDecimal(tempData);
var type = keyCodePanel.Tag.ToString();
if (!string.IsNullOrEmpty(type) && !string.IsNullOrEmpty(tempData))
{
string info = null;
ModuleKeyCode keyCode = ModuleKeyCode.;
Enum.TryParse(type, out keyCode);
switch (keyCode)
{
case ModuleKeyCode.:
{
if(data == 0)
{
info = "数值非法";
}
else if(data > 0)
{
info = string.Format("单品加{0}元", data);
}
else if(data < 0)
{
info = string.Format("单品减{0}元", data * -1);
}
}
break;
case ModuleKeyCode.:
{
if (data == 0)
{
info = "数值非法";
}
else if (data > 0)
{
info = string.Format("整单加{0}元", data);
}
else if (data < 0)
{
info = string.Format("整单减{0}元", data * -1);
}
}
break;
case ModuleKeyCode.:
{
if(data > 0 && data < 100)
{
info = string.Format("单品打{0}折", data / 10);
}
else
{
this.txtKeyData.Text = string.Empty;
info = "数值非法";
}
}
break;
case ModuleKeyCode.:
{
if (data > 0 && data < 100)
{
info = string.Format("整单打{0}折", data / 10);
}
else
{
this.txtKeyData.Text = string.Empty;
info = "数值非法";
}
}
break;
}
if (!string.IsNullOrEmpty(info))
{
noticeLabel.Text = info;
}
}
}
/// <summary>
/// 修改
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnModifyClick(object sender, EventArgs e)
{
if(_checkedResource != null)
{
InitSourcePage(_checkedResource);
this.superTabControl1.SelectedTabIndex = 0;
}
else
{
this.ShowToastNotify(this, "请选择修改的资源");
}
}
private void InitSourcePage(Resources res)
{
this.noticeLabel.Text = string.Empty;
if(res != null)
{
this.superTabItem1.Text = "修改资源";
this.txtName.Text = res.Name;
ModuleKeyCode keyCode = ModuleKeyCode.;
Enum.TryParse<ModuleKeyCode>(res.KeyCode, out keyCode);
switch (keyCode)
{
case ModuleKeyCode.:
{
this.checkBoxX1.Checked = true;
}
break;
case ModuleKeyCode.:
{
this.checkBoxX2.Checked = true;
}
break;
case ModuleKeyCode.:
{
this.checkBoxX3.Checked = true;
}
break;
case ModuleKeyCode.:
{
this.checkBoxX4.Checked = true;
}
break;
}
this.txtKeyData.Text = res.KeyData;
}
else
{
_checkedResource = null;
this.txtName.Text = string.Empty;
this.txtKeyData.Text = string.Empty;
this.superTabItem1.Text = "新增资源";
this.checkBoxX1.Checked = true;
}
}
/// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnDeleteItemClick(object sender, EventArgs e)
{
try
{
if (_checkedResource != null)
{
//校验当前资源是否存在引用
List<ModuleMenu> menuList = new List<ModuleMenu>();
using(var db = Global.Instance.OpenDataBase)
{
menuList = db.Query<ModuleMenu>("where resourceId = @0", _checkedResource.Id).ToList();
}
if (menuList.Count() > 0)
{
this.ShowToastNotify(this, "该资源已经被使用,如要删除,请先移除使用!");
return;
}
else
{
lock (Global.Instance.SyncLock)
{
using (var db = Global.Instance.OpenDataBase)
{
using (var transaction = db.GetTransaction())
{
db.Execute("delete from pos_resources where id = @0", _checkedResource.Id);
transaction.Complete();
}
this._dataSource = db.Query<Resources>().ToList();
}
}
InstallResources();
_checkedResource = null;
this.ShowToastNotify(this, "删除成功!");
}
}
else
{
this.ShowToastNotify(this, "请选择修改的资源");
}
}
catch(Exception ex)
{
LOGGER.Error(ex, "删除资源异常");
this.ShowToastNotify(this, "删除资源异常");
}
}
}
}