From 8aa5f0e094e6ff51aa7c47d5b4e8331a16eb09ff Mon Sep 17 00:00:00 2001
From: leiqunqing <zhengqifeng@hnkhzn.com>
Date: 星期五, 06 二月 2026 15:19:05 +0800
Subject: [PATCH] 提交ddl

---
 代码管理/WIDESEAWCS_Server/WIDESEAWCS_Server/WIDESEAWCS_BasicInfoService/ScanStationService.cs |  369 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 367 insertions(+), 2 deletions(-)

diff --git "a/\344\273\243\347\240\201\347\256\241\347\220\206/WIDESEAWCS_Server/WIDESEAWCS_Server/WIDESEAWCS_BasicInfoService/ScanStationService.cs" "b/\344\273\243\347\240\201\347\256\241\347\220\206/WIDESEAWCS_Server/WIDESEAWCS_Server/WIDESEAWCS_BasicInfoService/ScanStationService.cs"
index 172962a..5345f81 100644
--- "a/\344\273\243\347\240\201\347\256\241\347\220\206/WIDESEAWCS_Server/WIDESEAWCS_Server/WIDESEAWCS_BasicInfoService/ScanStationService.cs"
+++ "b/\344\273\243\347\240\201\347\256\241\347\220\206/WIDESEAWCS_Server/WIDESEAWCS_Server/WIDESEAWCS_BasicInfoService/ScanStationService.cs"
@@ -1,20 +1,385 @@
-锘縰sing System;
+锘縰sing 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
     {
-        public ScanStationService(IRepository<Dt_ScanStation> BaseDal) : base(BaseDal)
+        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("鏈壘鍒颁富鎺LC璁惧淇℃伅");
+                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=鏆傚仠锛宖alse=鎭㈠</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("鏈壘鍒颁富鎺LC璁惧淇℃伅");
+                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("鏈壘鍒颁富鎺LC璁惧淇℃伅");
+            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. 澶嶅埗鏍峰紡锛堝吋瀹规墍鏈塃PPlus鐗堟湰锛�
+                    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 = "姝e父";
+                }
+                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);
+
+            // 杞崲绗竴涓伐浣滆〃鍒癙DF
+            workbook.SaveToFile(pdfPath, FileFormat.PDF);
+            return WebResponseContent.Instance.OK(data: new { path = pdfPath, fileName = pdfFileName });
+
+        }
+
+
+        // <summary>
+        /// 鑷畾涔塃xcel鍗曞厓鏍煎悎骞惰鍒欏鍒舵柟娉曪紙鍏煎浣庣増鏈珽PPlus锛�
+        /// </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;
+            }
+        }
     }
 }

--
Gitblit v1.9.3