leiqunqing
2026-02-06 15b3879cd259108e7ebb755fe02c190f28f1e20c
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
using Autofac.Core;
using OfficeOpenXml;
using Spire.Xls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Common.PLCEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.BaseRepository;
using WIDESEAWCS_Core.BaseServices;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO.BasicInfo;
using WIDESEAWCS_IBasicInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
 
namespace WIDESEAWCS_BasicInfoService
{
    public class ScanStationService : ServiceBase<Dt_ScanStation, IRepository<Dt_ScanStation>>, IScanStationService
    {
        private readonly IFormulaService _formulaService;
        private readonly IFormulaDetailService _formulaDetailService;
        private readonly IProcessInfoService _processInfoService;
        private readonly IProcessInfoDetailService _processInfoDetailService;
 
 
        public ScanStationService(IRepository<Dt_ScanStation> BaseDal,
            IFormulaService formulaService,
            IFormulaDetailService formulaDetailService,
            IProcessInfoService processInfoService,
            IProcessInfoDetailService processInfoDetailService
            ) : base(BaseDal)
        {
            _formulaService = formulaService;
            _formulaDetailService = formulaDetailService;
            _processInfoService = processInfoService;
            _processInfoDetailService = processInfoDetailService;
        }
 
        public IRepository<Dt_ScanStation> Repository => BaseDal;
 
 
        /// <summary>
        /// 启动PLC
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        /// <summary>
        public WebResponseContent StartPLC(bool isStop)
        {
            try
            {
                OtherDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceName == "主控PLC") as OtherDevice;
                if (device == null) throw new Exception("未找到主控PLC设备信息");
                if (!device.IsConnected) throw new Exception($"主控PLC设备通讯异常");
                if (isStop)
                {
                    device.SetValue(W_PLCDBName.wboolAutoStart, false);
                    //暂停信号
                    device.SetValue(W_PLCDBName.wboolAutoPause, false);
                    return WebResponseContent.Instance.OK();
                }
                var Heart = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolHeart);
                var EMG = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolEMG);
                var OnlineExecuting = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolOnlineExecuting);
                var Error = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolError);
 
                if (Heart && !EMG && OnlineExecuting && !Error)
                {
                    device.SetValue(W_PLCDBName.wboolAutoStart, true);
                    //暂停信号
                    device.SetValue(W_PLCDBName.wboolAutoPause, false);
                    return WebResponseContent.Instance.OK();
                }
 
                return WebResponseContent.Instance.Error("设备不是在线状态");
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
 
        /// <summary>
        /// 暂停/恢复PLC(双向控制)
        /// </summary>
        /// <param name="isPause">是否为暂停操作:true=暂停,false=恢复</param>
        /// <returns>统一格式的响应结果</returns>
        public WebResponseContent PausePLC(bool isPause)
        {
            try
            {
                OtherDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceName == "主控PLC") as OtherDevice;
                if (device == null) throw new Exception("未找到主控PLC设备信息");
                if (!device.IsConnected) throw new Exception($"主控PLC设备通讯异常");
 
                var Heart = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolHeart);
                var EMG = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolEMG);
                var Error = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolError);
                if (!device.GetValue<W_PLCDBName, bool>(W_PLCDBName.wboolAutoStart))
                {
                    return WebResponseContent.Instance.Error("设备未启动");
                }
 
                if (!Heart || EMG || Error)
                {
                    return WebResponseContent.Instance.Error("设备状态异常,无法执行暂停/恢复操作");
                }
                device.SetValue(W_PLCDBName.wboolAutoPause, isPause);
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
 
        /// <summary>
        /// 返回信号
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public WebResponseContent GetSignalStates()
        {
            OtherDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceName == "主控PLC") as OtherDevice;
            if (device == null) throw new Exception("未找到主控PLC设备信息");
            if (!device.IsConnected) throw new Exception($"主控PLC设备通讯异常");
 
            //获取信号灯状态
            var Heart = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolHeart);
            var EMG = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolEMG);
            var AutoExecuting = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolAutoExecuting);
            var OnlineExecuting = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolOnlineExecuting);
            var Error = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolError);
 
            //获取启动暂停状态
            var IsStarted = device.GetValue<W_PLCDBName, bool>(W_PLCDBName.wboolAutoStart);
            var IsPaused = device.GetValue<W_PLCDBName, bool>(W_PLCDBName.wboolAutoPause);
 
            //返回对象
            var responseData = new
            {
                // 信号灯状态数组(保持原有顺序)
                signalStates = new bool[] { Heart, EMG, AutoExecuting, OnlineExecuting, Error },
                // PLC启动/暂停状态对象
                plcStatus = new
                {
                    isStarted = IsStarted,
                    isPaused = IsPaused
                }
            };
            return WebResponseContent.Instance.OK(data: responseData);
        }
 
        /// <summary>
        /// 获取成品信息
        /// </summary>
        /// <returns></returns>
        public WebResponseContent GetLeftInitialData()
        {
            try
            {
                Dt_ScanStation dt_ScanStation = BaseDal.QueryFirst(x => x.StationCode == "001");
                if (dt_ScanStation == null)
                {
                    return WebResponseContent.Instance.Error("未找到工位编码为001的工位信息");
                }
                Dt_Formula dt_Formula = _formulaService.Repository.QueryFirst(x => x.ProductCode == dt_ScanStation.StationEndProduct);
                if (dt_Formula == null)
                {
                    return WebResponseContent.Instance.Error($"未找到成品编号【{dt_ScanStation.StationEndProduct}】对应的配方信息");
                }
                List<Dt_FormulaDetail> dt_FormulaDetails = _formulaDetailService.Repository.QueryData(x => x.FormulaId == dt_Formula.Id);
 
                var responseData = new
                {
                    finishedProductId = dt_Formula.Id,
                    finishedProduct = dt_Formula.ProductCode,
                    leftPartCodes = new List<string>(),
                    leftPartIds = new List<int>(),
                    leftPartChecked = new List<int>()
                };
 
                foreach (var detail in dt_FormulaDetails.Take(10))
                {
                    responseData.leftPartCodes.Add(detail.ComponentCode ?? "");
                    responseData.leftPartIds.Add(detail.Id);
                    responseData.leftPartChecked.Add(detail.IsScanned);
                }
 
                int needFillCount = 10 - responseData.leftPartCodes.Count;
                for (int i = 0; i < needFillCount; i++)
                {
                    responseData.leftPartCodes.Add("");
                    responseData.leftPartIds.Add(0);
                    responseData.leftPartChecked.Add(0);
                }
                return WebResponseContent.Instance.OK("获取左侧初始数据成功", responseData);
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error($"获取左侧初始数据失败,请稍后重试【{ex.Message}】");
            }
        }
 
        /// <summary>
        /// 更新是否扫码
        /// </summary>
        /// <param name="updatePartScannedStatusRequest"></param>
        /// <returns></returns>
        public WebResponseContent UpdatePartScannedStatus(UpdatePartScannedStatusRequest updatePartScannedStatusRequest)
        {
            try
            {
                Dt_FormulaDetail dt_FormulaDetail = _formulaDetailService.Repository.QueryFirst(x => x.Id == updatePartScannedStatusRequest.Id);
                dt_FormulaDetail.IsScanned = updatePartScannedStatusRequest.IsScanned;
                bool flag = _formulaDetailService.Repository.UpdateData(dt_FormulaDetail);
                if (flag)
                {
                    return WebResponseContent.Instance.OK();
                }
                return WebResponseContent.Instance.Error("更新错误");
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error("获取数据失败");
            }
        }
 
 
        /// <summary>
        /// 下载流程卡
        /// </summary>
        /// <param name="dt_ProcessInfo"></param>
        /// <returns></returns>
        public WebResponseContent ExportData(Dt_ScanStation dt_ScanStation)
        {
 
            string fileName = $"{DateTime.Now.ToString("yyyyMMddHHssmm")}.xlsx";
            string templatePath = $"{AppDomain.CurrentDomain.BaseDirectory}ExprotTemplate\\发电机弹性支撑信息化流程卡.xlsx";//模板路径
            string savePath = $"{AppDomain.CurrentDomain.BaseDirectory}Download\\{fileName}";//保存文件路径
 
            using Stream templateStream = new FileStream(templatePath, FileMode.Open);
            using Stream saveStream = new FileStream(savePath, FileMode.Create);
            using ExcelPackage package = new ExcelPackage(saveStream, templateStream);
            ExcelWorksheet worksheet = package.Workbook.Worksheets["流程卡"];
            Dt_Formula formula = _formulaService.Repository.QueryFirst(x => x.ProductCode == dt_ScanStation.StationEndProduct);
            List<Dt_FormulaDetail> dt_FormulaDetails = _formulaDetailService.Repository.QueryData(x => x.FormulaId == formula.Id && x.IsScanned == 1).ToList();
            for (int i = dt_FormulaDetails.Count; i < 10; i++)
            {
                dt_FormulaDetails.Add(new Dt_FormulaDetail());
            }
 
            worksheet.Cells[3, 5].Value = dt_ScanStation.StationEndProduct;//成品编号
            worksheet.Cells[3, 17].Value = dt_ScanStation.LastProductSn.ObjToInt() - dt_ScanStation.FirstProductSn.ObjToInt() + 1;//数量;
            worksheet.Cells[3, 29].Value = dt_ScanStation.FirstProductSn + "-" + dt_ScanStation.LastProductSn;//流水号
            worksheet.Cells[4, 5].Value = DateTime.Now.ToString("yyyy/MM/dd");//组装日期
            worksheet.Cells[4, 17].Value = dt_ScanStation.AssembleUser;//组装人员
 
            int row = 7;
            for (int j = 0; j < 5; j++)
            {
                worksheet.Cells[row, 1].Value = dt_FormulaDetails[j].ComponentName;
                worksheet.Cells[row, 5].Value = dt_FormulaDetails[j].ComponentCode;
                worksheet.Cells[row, 12].Value = dt_FormulaDetails[j].SupplierCode;
 
                worksheet.Cells[row, 19].Value = dt_FormulaDetails[j + 5].ComponentName;
                worksheet.Cells[row, 23].Value = dt_FormulaDetails[j + 5].ComponentCode;
                worksheet.Cells[row, 30].Value = dt_FormulaDetails[j + 5].SupplierCode;
                row++;
            }
 
            row = 13;
            for (int j = 0; j < 4; j++)
            {
                worksheet.Cells[row + j, 12].Value = "☑ 是     □ 否";
                worksheet.Cells[row + j, 30].Value = "☑ 是     □ 否";
            }
            worksheet.Cells[17, 12].Value = "☑ 是     □ 否";
            worksheet.Cells[23, 5].Value = "☑ 是     □ 否";
 
            List<Dt_ProcessInfoDetail> dt_ProcessInfoDetails = _processInfoDetailService.Repository
                .QueryData(x => x.ProductCode == dt_ScanStation.StationEndProduct
                && x.ProductSn.CompareTo(dt_ScanStation.LastProductSn) >= 0
                && x.ProductSn.CompareTo(dt_ScanStation.LastProductSn) <= 0)
                .OrderBy(x => x.CreateDate)
                .ToList();
            row = 27;
            ExcelRange baseStyleRow = worksheet.Cells[$"{row}:{row}"];
            foreach (var item in dt_ProcessInfoDetails)
            {
                if (row > 61)
                {
                    var sourceRange = worksheet.Cells[row - 1, 1, row - 1, 36];
                    var targetRange = worksheet.Cells[row, 1, row, 36];
 
                    // 1. 复制合并规则(原有逻辑保留)
                    this.CopyCellMerge(worksheet, sourceRange, targetRange);
                    // 2. 复制样式(兼容所有EPPlus版本)
                    targetRange.StyleID = sourceRange.StyleID;
                    // 3. 同步行高
                    worksheet.Row(row).Height = worksheet.Row(row - 1).Height;
                }
 
                worksheet.Cells[row, 1].Value = item.ProductSn;
                worksheet.Cells[row, 7].Value = item.Height2;
                worksheet.Cells[row, 13].Value = item.Height1;
                worksheet.Cells[row, 19].Value = item.PressPressure;
                worksheet.Cells[row, 25].Value = item.ScrewTorque;
                if (item.PressTightenUnfinished == 1)
                {
                    worksheet.Cells[row, 31].Value = "压装拧紧异常";
                }
                else if (item.CheckUnfinished == 1)
                {
                    worksheet.Cells[row, 31].Value = "检测异常";
                }
                else
                {
                    worksheet.Cells[row, 31].Value = "正常";
                }
                row++;
 
            }
            //worksheet.DeleteRow(row, worksheet.Dimension.End.Row - row + 1);
 
            package.Save();
 
            string pdfFileName = $"{DateTime.Now.ToString("yyyyMMddHHssmm")}.pdf";
            string pdfPath = $"{AppDomain.CurrentDomain.BaseDirectory}Download\\{pdfFileName}";
 
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(savePath);
 
            // 转换第一个工作表到PDF
            workbook.SaveToFile(pdfPath, FileFormat.PDF);
            return WebResponseContent.Instance.OK(data: new { path = pdfPath, fileName = pdfFileName });
 
        }
 
 
        // <summary>
        /// 自定义Excel单元格合并规则复制方法(兼容低版本EPPlus)
        /// </summary>
        /// <param name="worksheet">Excel工作表</param>
        /// <param name="sourceRange">源单元格范围</param>
        /// <param name="targetRange">目标单元格范围</param>
        public void CopyCellMerge(ExcelWorksheet worksheet, ExcelRange sourceRange, ExcelRange targetRange)
        {
            if (sourceRange == null || targetRange == null) return;
            if (sourceRange.Worksheet != worksheet || targetRange.Worksheet != worksheet) return;
 
            // 1. 先将源范围内的合并区域提取到临时列表中
            var sourceMergeList = new List<string>();
            foreach (var merge in worksheet.MergedCells)
            {
                var mergeRange = worksheet.Cells[merge];
                if (mergeRange.Start.Row >= sourceRange.Start.Row && mergeRange.End.Row <= sourceRange.End.Row
                    && mergeRange.Start.Column >= sourceRange.Start.Column && mergeRange.End.Column <= sourceRange.End.Column)
                {
                    sourceMergeList.Add(merge);
                }
            }
 
            // 2. 遍历临时列表,安全地创建目标合并区域
            foreach (var merge in sourceMergeList)
            {
                var mergeRange = worksheet.Cells[merge];
                int rowOffset = targetRange.Start.Row - sourceRange.Start.Row;
                int colOffset = targetRange.Start.Column - sourceRange.Start.Column;
                var targetMergeRange = worksheet.Cells[
                    mergeRange.Start.Row + rowOffset,
                    mergeRange.Start.Column + colOffset,
                    mergeRange.End.Row + rowOffset,
                    mergeRange.End.Column + colOffset
                ];
                targetMergeRange.Merge = true;
            }
        }
    }
}