dengjunjie
2025-04-19 9e579eda4601ed7b492b9d19a24e8146f6ebdf8d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using BarcodeStandard;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Information;
using SkiaSharp;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.Basic;
using WIDESEA_IBasicRepository;
using WIDESEA_IBasicService;
using WIDESEA_Model.Models;
 
namespace WIDESEA_BasicService
{
    public class PalletCodeInfoService : ServiceBase<Dt_PalletCodeInfo, IPalletCodeInfoRepository>, IPalletCodeInfoService
    {
        private readonly IWarehouseRepository _warehouseRepository;
 
        public PalletCodeInfoService(IPalletCodeInfoRepository BaseDal, IWarehouseRepository warehouseRepository) : base(BaseDal)
        {
            _warehouseRepository = warehouseRepository;
        }
        public override WebResponseContent AddData(SaveModel saveModel)
        {
            try
            {
                string PalletCode = saveModel.MainData["palletCode"].ToString();
                int Width = saveModel.MainData["width"].ObjToInt();
                int Height = saveModel.MainData["height"].ObjToInt();
                int Size = saveModel.MainData["size"].ObjToInt();
                if (string.IsNullOrEmpty(PalletCode)) throw new Exception("托盘号不能为空");
                Dt_PalletCodeInfo palletCodeInfo = BaseDal.QueryFirst(x => x.PalletCode == PalletCode);
                if (palletCodeInfo != null) throw new Exception("托盘号已存在");
                palletCodeInfo = new Dt_PalletCodeInfo()
                {
                    PalletCode = PalletCode,
                    Width = Width,
                    Height = Height,
                    Size = Size,
                    Qty = 1,
                    PrintFrequency = 2,
                };
                BaseDal.AddData(palletCodeInfo);
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        /// <summary>
        /// 打印条码
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        public WebResponseContent PrintBarcodeLib(List<int> keys)
        {
            try
            {
                Dt_PalletCodeInfo palletCodeInfo = BaseDal.QueryFirst(x => keys.Contains(x.Id));
                if (palletCodeInfo == null) throw new Exception("信息有误");
                #region 生成条码
                Barcode barcode = new Barcode();//创建条形码对象
                barcode.IncludeLabel = true;//确保包括标签
                barcode.Alignment = AlignmentPositions.Center;//条形码对齐方式
                barcode.LabelFont = new SKFont
                {
                    Typeface = SKTypeface.FromFamilyName("Arial", SKFontStyle.Normal),//字体样式
                    Size = palletCodeInfo.Size,// 标签字体
                };
                barcode.EncodedType = BarcodeStandard.Type.Code128;
                barcode.Width = palletCodeInfo.Width;
                barcode.Height = palletCodeInfo.Height;
                #endregion
                for (int i = 1; i <= palletCodeInfo.Qty; i++)
                {
                    #region 生成条形码图像
                    var n = DateTime.Now.ToString("yy");
                    var Barcode = n + palletCodeInfo.Code.ToString("000000");
                    //var Barcode = palletCodeInfo.PalletCode + palletCodeInfo.Code.ToString("00000000");
                    palletCodeInfo.Code++;
                    SKImage bitmap = barcode.Encode(Barcode);
                    #endregion
                    for (int j = 1; j <= palletCodeInfo.PrintFrequency; j++)
                    {
 
                    }
                }
                return WebResponseContent.Instance.OK("打印成功");
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public string newBarcodeLib(int key)
        {
            Dt_PalletCodeInfo palletCodeInfo = BaseDal.QueryFirst(x => key == x.Id);
            Barcode barcode = new Barcode();//创建条形码对象
            barcode.IncludeLabel = true;//确保包括标签
            barcode.Alignment = AlignmentPositions.Center;//条形码对齐方式
            barcode.LabelFont = new SKFont
            {
                Typeface = SKTypeface.FromFamilyName("Arial", SKFontStyle.Bold),//字体样式
                Size = palletCodeInfo.Size,// 标签字体
            };
            barcode.EncodedType = BarcodeStandard.Type.Code128;
            barcode.Width = palletCodeInfo.Width;
            barcode.Height = palletCodeInfo.Height;
            var Barcode = DateTime.Now.ToString("yy") + palletCodeInfo.Code.ToString("000000");
            palletCodeInfo.Code++;
            BaseDal.UpdateData(palletCodeInfo);
            SKImage bitmap = barcode.Encode(Barcode);
            var a = barcode.EncodedImageBytes;
 
            return Convert.ToBase64String(a, 0, (int)a.Length);
        }
        public string newBarcodeLib(palletCodeInfoDTO palletCodeInfo)
        {
            #region 生成条形码
            if (palletCodeInfo == null) throw new Exception("信息有误");
            Barcode barcode = new Barcode();//创建条形码对象
            barcode.IncludeLabel = true;//确保包括标签
            barcode.Alignment = AlignmentPositions.Center;//条形码对齐方式
            barcode.LabelFont = new SKFont
            {
                Typeface = SKTypeface.FromFamilyName("Arial", SKFontStyle.Normal),//字体样式
                Size = palletCodeInfo.Size,// 标签字体
            };
 
            //设置条形码类型
            //barcode.EncodedType = BarcodeStandard.Type.UpcA;//11-12纯数字
            barcode.EncodedType = BarcodeStandard.Type.Code128;// 数据应该包括数字和字母
 
            //设置条形码数据
            //barcode.RawData = "AA12345";
 
            //设置条形码尺寸
            barcode.Width = palletCodeInfo.Width;
            barcode.Height = palletCodeInfo.Height;
            var Barcode = DateTime.Now.ToString("yy") + palletCodeInfo.Code.ToString("000000");
            //生成条形码图像
            SKImage bitmap = barcode.Encode(Barcode);
            var a = barcode.EncodedImageBytes;
            var b = Convert.ToBase64String(a, 0, (int)a.Length);
            //保存条形码图像到文件(可选)
            //barcode.SaveImage("C://Users//dell//Desktop//1.控制打印机/barcode.png", SaveTypes.Png);
 
            // 打印条形码图像(需要有打印机,并且配置好打印机)
            //barcode.Print();
 
            // 清理资源
            //barcode.Dispose(); 
            #endregion
            return b;
        }
    }
}