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.

354 lines
15 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Drawing;
namespace POSV.Printer
{
[Serializable]
[JsonObject(MemberSerialization.OptIn)]
public class DesignerSurface
{
[JsonProperty(PropertyName = "pager")]
public PagerType PagerType { get; set; }
[JsonProperty(PropertyName = "width")]
public int Width { get; set; }
[JsonProperty(PropertyName = "version")]
public int Version { get; set; } = 1;
[JsonProperty(PropertyName = "dataSource")]
public Dictionary<string , Dictionary<string , string>> DataSource { get; set; }
/// <summary>
/// 当前控件列表
/// </summary>
[JsonProperty(PropertyName = "controls")]
public List<DesignerControls> DesignerControls { get; set; }
private bool ParseCondition(VariableItem var,string dataSourceKey, List<VariableValue> args)
{
bool result = true;
//数据源参数,无论单行还是列表数据源采用List方式兼容单行选择集合数据源重复行打印
var data = new List<Dictionary<string , string>>();
//包含数据源参数,重置
if (args.Exists(x => x.Key == dataSourceKey))
{
//当前数据源
var vars = args.FindLast(x => x.Key == dataSourceKey);
//List集合数据
if (vars.DataType == DataType.List)
{
data = JSON.Deserialize<List<Dictionary<string , string>>>(vars.Value.ToString());
}
else
{
var row = JSON.Deserialize<Dictionary<string , string>>(vars.Value.ToString());
data.Add(row);
}
}
//参数中是否包含条件参数,如果包含,取值,如果不包含,忽略,继续打印
if(var != null && data.Exists(x => x.ContainsKey(var.Value)))
{
//取第一个值
var _value = data[0][var.Value];
if ("false".Equals(_value.ToLower()))
{
result = false;
}
}
return result;
}
public PrinterTemplate Parse(List<VariableValue> args)
{
PrinterTemplate printer = new PrinterTemplate();
printer.PagerType = this.PagerType;
printer.MaxLength = (int)this.PagerType;
foreach (DesignerControls ctrls in this.DesignerControls)
{
switch (ctrls.ControlType)
{
case ControlType.:
{
var parameter = JSON.Deserialize<R1Parameter>(ctrls.Template);
R1Template template = parameter.Template.Item1;
//判断是否符合打印条件
var condition = ParseCondition(template.Condition , template.DataSourceKey, args);
if (!condition)
{
continue;
}
RowTemplate row = new RowTemplate();
row.RowFormat = RowFormat.Line;
LineTemplate line = new LineTemplate();
line.DataSourceKey = template.DataSourceKey;
line.FontStyle = parameter.FontStyle;
line.AlignStyle = parameter.AlignStyle;
line.LineStyle = parameter.LineStyle;
line.Length = (int)this.PagerType;
var content = string.Format("{0}{1}{2}{3}{4}" , template.Prefix , template.Var1.Value , template.Middle , template.Var2.Value , template.Suffix);
line.Content = content;
row.Template = JSON.Serialize(line);
printer.AddRow(row);
}
break;
case ControlType.:
{
var parameter = JSON.Deserialize<R2Parameter>(ctrls.Template);
R2Template left = parameter.Template.Item1;
//判断是否符合打印条件
var leftCondition = ParseCondition(left.Condition ,left.DataSourceKey, args);
R2Template right = parameter.Template.Item2;
//判断是否符合打印条件
var rightCondition = ParseCondition(right.Condition , left.DataSourceKey , args);
if(!leftCondition && !rightCondition)
{
continue;
}
RowTemplate row = new RowTemplate();
row.RowFormat = RowFormat.Column;
List<LineTemplate> lines = new List<LineTemplate>();
LineTemplate line = new LineTemplate();
line.DataSourceKey = left.DataSourceKey;
line.FontStyle = parameter.FontStyle;
line.AlignStyle = left.AlignStyle;
line.LineStyle = parameter.LineStyle;
//列打印字符数,根据每行字符数*列的宽度百分比
int leftStringLength = Convert.ToInt32(((int)this.PagerType) * left.Percent / 100 );
//将宽度都重置为偶数,防止比例算出来为奇数,导致两列模板换行失控
switch (line.FontStyle)
{
case FontStyle.:
case FontStyle.:
{
line.Length = ((int)leftStringLength / 4) * 4;
}
break;
default:
{
line.Length = ((int)leftStringLength / 2) * 2;
}
break;
}
leftStringLength = line.Length;
var content = string.Format("{0}{1}{2}{3}{4}" , left.Prefix , left.Var1.Value , left.Middle , left.Var2.Value , left.Suffix);
line.Content = leftCondition ? content : "";
lines.Add(line);
line = new LineTemplate();
line.DataSourceKey = right.DataSourceKey;
line.FontStyle = parameter.FontStyle;
line.AlignStyle = right.AlignStyle;
line.LineStyle = parameter.LineStyle;
//左标签长度确定后,右标签采用减的方式
line.Length = (int)this.PagerType - leftStringLength;
content = string.Format("{0}{1}{2}{3}{4}" , right.Prefix , right.Var1.Value , right.Middle , right.Var2.Value , right.Suffix);
line.Content = rightCondition ? content : "";
lines.Add(line);
row.Template = JSON.Serialize(lines);
printer.AddRow(row);
}
break;
case ControlType.:
{
var template = JSON.Deserialize<GridXParameter>(ctrls.Template);
RowTemplate row = new RowTemplate();
row.RowFormat = RowFormat.Grid;
GridTemplate grid = new GridTemplate();
grid.DataSourceKey = template.Template.DataSourceKey;
//包含合计行
grid.ContainsTotalRow = template.Template.ContainsTotalRow;
//自动换新行
grid.AllowNewLine = template.Template.AllowNewLine;
//打印合计行
grid.OutputTotalRow = template.Template.OutputTotalRow;
//字体
grid.FontStyle = template.FontStyle;
//表头下划线格式
grid.LineStyle = template.LineStyle;
//空记录集是否打印表头
grid.NotAllowEmpty = template.Template.NotAllowEmpty;
//禁止打印表头
grid.NotAllowHeader = template.Template.NotAllowHeader;
//表头采用正常字体打印
grid.HeaderFontStyle = template.Template.HeaderFontStyle;
foreach (var cols in template.Template.Columns)
{
var r = template.Template.RowTemplates[cols.Index];
grid.AddColumn(new ColumnTempate(cols.Name ,template.AlignStyle , r.Content , cols.Length));
}
row.Template = JSON.Serialize(grid);
printer.AddRow(row);
}
break;
case ControlType.:
{
var parameter = JSON.Deserialize<BarCodeXParameter>(ctrls.Template);
BarCodeXTemplate template = parameter.Template.Item1;
//判断是否符合打印条件
var condition = ParseCondition(template.Condition , template.DataSourceKey , args);
if (!condition)
{
continue;
}
RowTemplate row = new RowTemplate();
row.RowFormat = RowFormat.Barcode;
BarcodeTemplate barcode = new BarcodeTemplate();
barcode.DataSourceKey = template.DataSourceKey;
barcode.FontStyle = parameter.FontStyle;
barcode.AlignStyle = parameter.AlignStyle;
barcode.Length = (int)this.PagerType;
barcode.ShowLable = template.ShowLabel;
barcode.Content = template.Var1.Value;
barcode.LableContent = template.Var2.Value;
row.Template = JSON.Serialize(barcode);
printer.AddRow(row);
}
break;
case ControlType.:
{
var parameter = JSON.Deserialize<QRCodeXParameter>(ctrls.Template);
QRCodeXTemplate template = parameter.Template.Item1;
//判断是否符合打印条件
var condition = ParseCondition(template.Condition, template.DataSourceKey, args);
if (!condition)
{
continue;
}
RowTemplate row = new RowTemplate();
row.RowFormat = RowFormat.QRCode;
QRCodeTemplate qrcode = new QRCodeTemplate();
qrcode.DataSourceKey = template.DataSourceKey;
qrcode.FontStyle = parameter.FontStyle;
qrcode.AlignStyle = parameter.AlignStyle;
qrcode.SizeMode = parameter.SizeMode;
qrcode.Length = (int)this.PagerType;
qrcode.Content = template.Var1.Value;
row.Template = JSON.Serialize(qrcode);
printer.AddRow(row);
}
break;
case ControlType.:
{
var parameter = JSON.Deserialize<BitmapXParameter>(ctrls.Template);
BitmapXTemplate template = parameter.Template.Item1;
//判断是否符合打印条件
var condition = ParseCondition(template.Condition , template.DataSourceKey , args);
if (!condition)
{
continue;
}
RowTemplate row = new RowTemplate();
row.RowFormat = RowFormat.Bitmap;
BitmapTemplate bitmap = new BitmapTemplate();
bitmap.DataSourceKey = template.DataSourceKey;
bitmap.FontStyle = parameter.FontStyle;
bitmap.AlignStyle = parameter.AlignStyle;
bitmap.Length = (int)this.PagerType;
bitmap.Content = template.Var1.Value;
row.Template = JSON.Serialize(bitmap);
printer.AddRow(row);
}
break;
}
}
return printer;
}
}
[Serializable]
[JsonObject(MemberSerialization.OptIn)]
public class DesignerControls
{
[JsonProperty(PropertyName = "type")]
public ControlType ControlType { get; set; }
[JsonProperty(PropertyName = "text")]
public string Text { get; set; }
[JsonProperty(PropertyName = "x")]
public int X { get; set; }
[JsonProperty(PropertyName = "y")]
public int Y { get; set; }
[JsonProperty(PropertyName = "width")]
public int Width { get; set; }
[JsonProperty(PropertyName = "height")]
public int Height { get; set; }
[JsonProperty(PropertyName = "color")]
public Color BackColor { get; set; }
[JsonProperty(PropertyName = "template")]
public string Template { get; set; }
}
}