From 65dac074f71375381eba0a5ff8dc108cacafc3f9 Mon Sep 17 00:00:00 2001
From: Administrator <Administrator@DESKTOP-5BIMHQ3>
Date: 星期二, 25 六月 2024 10:18:16 +0800
Subject: [PATCH] Merge branch 'wxw'

---
 Wms/WMS.BLL/BllTransServer/RcsServer.cs |  424 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 422 insertions(+), 2 deletions(-)

diff --git a/Wms/WMS.BLL/BllTransServer/RcsServer.cs b/Wms/WMS.BLL/BllTransServer/RcsServer.cs
index f77469a..5cf982d 100644
--- a/Wms/WMS.BLL/BllTransServer/RcsServer.cs
+++ b/Wms/WMS.BLL/BllTransServer/RcsServer.cs
@@ -1,10 +1,430 @@
-锘縰sing System;
+锘縰sing Microsoft.IdentityModel.Protocols;
+using Newtonsoft.Json;
+using SqlSugar;
+using System;
 using System.Collections.Generic;
+using System.Linq;
 using System.Text;
+using Utility.Tools;
+using WMS.DAL;
+using WMS.Entity.BllAsnEntity;
+using WMS.Entity.BllSoEntity;
+using WMS.Entity.Context;
+using WMS.Entity.DataEntity;
+using WMS.Entity.LogEntity;
+using WMS.Entity.SysEntity;
+using WMS.IBLL.IBllTransServer;
+using static Model.InterFaceModel.RCSModel;
 
 namespace WMS.BLL.BllTransServer
 {
-    class RcsServer
+    public class RcsServer:IRcsServer
     {
+        private static readonly SqlSugarScope Db = DataContext.Db;
+        /// <summary>
+        /// RCS鍙《(鍑�妗跺拰鑴忔《)
+        /// </summary>
+        /// <param name="warehouseno">搴撳尯</param>
+        /// <param name="type">鍙枡绫诲瀷</param>
+        /// <returns></returns>
+        public void GetPalletNo(Pallnetmsg pallnetmsg)
+        {
+            DataStockDetail pallet = new DataStockDetail();
+            SysStorageLocat locat = new SysStorageLocat();
+            try
+            {
+                var house = "W01";//鍙《浣嶇疆锛屽悗缁牴鎹綅缃叧鑱旀垨瀹氫箟锛屾潵鍒ゆ柇鍙《浣嶇疆鎵�灞炶溅闂�
+                var url = "";//鍥炰紶MES鐨勬帴鍙e湴鍧�
+                var noticeno = "";//鍑哄叆搴撳崟鎹槑缁咺D
+                var sql = "select PalletNo,LocatNo from DataStockDetail where Status = '0'";
+                switch (pallnetmsg.Type)
+                {
+                    case "0"://鍙噣妗�
+                        List<DataStockDetail> jtpallet = new List<DataStockDetail>();
+                        sql += $"and  WareHouseNo = '{house}' and AreaNo like '%01' order by CreateTime desc";
+                        jtpallet = Db.Ado.SqlQuery<DataStockDetail>(sql).ToList();
+                        if (jtpallet.Count == 0)
+                        {
+                            throw new Exception("鏆傛棤鍑�妗跺彲鍒嗛厤");
+                        }
+                        //閬嶅巻搴撳瓨鍑�妗讹紝杩斿洖MES楠岃瘉
+                        foreach (var item in jtpallet)
+                        {
+                            string jsonReq = JsonConvert.SerializeObject(item.PalletNo);
+                            var response = HttpHelper.DoPost(url, jsonReq, "鍥炰紶MES鍑�妗剁紪鍙�", "RCS").ToString();
+                            var obj = JsonConvert.DeserializeObject<ReMes>(response);//瑙f瀽杩斿洖鏁版嵁
+                            if (obj.Success == "0")
+                            {
+                                if (obj.Result != "0")//鍙敤锛岀敓鎴愬皬杞︽媺鍑�妗朵换鍔�
+                                {
+                                    //鏌ユ壘鏄惁鏈夌┖浣欒剰妗朵綅
+                                    sql = $"select LocatNo from SysStorageLocat where AreaNo like '%04' and WareHouseNo = wareno and Status = 0";
+                                    locat = Db.Ado.SqlQuery<SysStorageLocat>(sql).FirstOrDefault();
+                                    if (locat == null)
+                                    {
+                                        Db.Ado.BeginTran();
+                                        //鑴忔《鍖烘棤绌轰綑搴撲綅锛屽垯閿佸畾姝ゅ噣妗跺簱瀛�,骞堕噸鏂伴亶鍘�
+                                        sql = $"UPDATE DataStockDetail Set Status = '5' Where locat ='{locat.LocatNo}'";
+                                        Db.Ado.ExecuteCommand(sql);
+                                        Db.Ado.CommitTran();
+                                        break;
+                                    }
+                                    //鐢熸垚璋冨害灏忚溅鍑�妗跺幓鑴忔《鍖轰换鍔�
+                                    var ztask = new TaskDetial
+                                    {
+                                        Startport = item.LocatNo,
+                                        Endport = locat.LocatNo,
+                                        Pallno = item.PalletNo,
+                                        Type = "4",
+                                        Crtype = "0",
+                                        Noticedetailno = int.Parse(noticeno),
+                                    };
+                                    CreateLotTask(ztask);
+                                    return;
+                                }
+                            }
+                            else
+                            {
+                                throw new Exception("鍥炰紶MES鍑�妗剁紪鍙峰け璐ワ紒");
+                            }
+                        }
+                        break;
+                    case "1"://鍙枡妗讹紙娣锋枡锛�
+                        //鍒ゆ柇鏄惁鏈夋壒娆″彿
+                        if (string.IsNullOrWhiteSpace(pallnetmsg.LotNo))
+                        {
+                            throw new Exception("鎵规鍙蜂负绌猴紒");
+                        }
+                        /*BllArrivalNoticeDetail ArriveMes = new BllArrivalNoticeDetail();
+                        //鍒ゆ柇璇ユ壒娆℃槸鍚︽湁瀵瑰簲鍏ュ簱鍗�
+                        sql = $"select * from BllArrivalNoticeDetail where LotNo =  '{pallnetmsg.LotNo}' order by CreateTime desc";
+                        ArriveMes = Db.Ado.SqlQuery<BllArrivalNoticeDetail>(sql).FirstOrDefault();
+                        if (ArriveMes == null)
+                        {
+                            throw new Exception("璇ユ壒娆℃病鏈夊搴旂殑鍏ュ簱鍗�");
+                        }
+                        noticeno = ArriveMes.Id.ToString();*/
+                        //鏌ユ壘搴撳瓨涓槸鍚︽湁鍙敤鐨勬鎵规鐨勬贩鏂欐《
+                        sql= $"select PalletNo,LocatNo from DataStockDetail where a.LotNo =  '{pallnetmsg.LotNo}'and AreaNo like '%02' " +
+                            $"and a.WareHouseNo = '{house}' and b.status = '0' order by CompleteTime desc";
+                        pallet = Db.Ado.SqlQuery<DataStockDetail>(sql).FirstOrDefault();
+                        if (pallet == null)
+                        {
+                            throw new Exception("鏆傛棤娣锋枡妗跺彲鍒嗛厤");
+                        }
+
+                        break;
+                    case "2"://鍙枡妗讹紙涓嬫枡锛�
+                        BllExportNoticeDetail bllSo = new BllExportNoticeDetail();
+                        //鍒ゆ柇鏄惁鏈夋壒娆″彿
+                        if (string.IsNullOrWhiteSpace(pallnetmsg.LotNo))
+                        {
+                            throw new Exception("鎵规鍙蜂负绌猴紒");
+                        }
+                        //鏌ユ壘鏄惁鏈夋鎵规鍑哄簱鍗�
+                        sql = $"select * from BllSoNoticeDetail where LotNo =  '{pallnetmsg.LotNo}' order by CreateTime desc";
+                        bllSo = Db.Ado.SqlQuery<BllExportNoticeDetail>(sql).FirstOrDefault();
+                        if (bllSo == null)
+                        {
+                            throw new Exception("璇ユ壒娆℃病鏈夊搴旂殑鍑哄簱鍗�");
+                        }
+                        noticeno = bllSo.Id.ToString();
+                        //鏌ユ壘搴撳瓨涓槸鍚︽湁姝ゆ壒娆$殑涓嬫枡妗�
+                        sql += $"select PalletNo,LocatNo from DataStockDetail where a.LotNo =  '{pallnetmsg.LotNo}'and AreaNo like '%03' " +
+                            $"and a.WareHouseNo = '{house}' and b.status = '0' order by CompleteTime desc";
+                        pallet = Db.Ado.SqlQuery<DataStockDetail>(sql).FirstOrDefault();
+                        if (pallet == null)
+                        {
+                            throw new Exception("鏆傛棤涓嬫枡妗跺彲鍒嗛厤");
+                        }
+
+                        break;
+                    case "3"://鍙剰妗�
+                        sql += $"and WareHouseNo = '{house}' and AreaNo like '%04' order by CreateTime desc";
+                        pallet = Db.Ado.SqlQuery<DataStockDetail>(sql).FirstOrDefault();
+                        if (pallet == null)
+                        {
+                            throw new Exception("鏆傛棤鑴忔《鍙垎閰�");
+                        }
+
+                        break;
+                }
+                //涓嬪彂灏忚溅浠诲姟
+                var task = new TaskDetial
+                {
+                    Startport = pallnetmsg.Location,
+                    Endport = pallet.LocatNo,
+                    Pallno = pallet.PackagNo,
+                    Type = pallnetmsg.Type,
+                    Crtype = "1",
+                    Noticedetailno = int.Parse(noticeno),
+                };
+                CreateLotTask(task);
+                return;
+            }
+            catch (Exception ex)
+            {
+                Db.Ado.RollbackTran();
+                throw ex;
+            }
+        }
+
+        /// <summary>
+        /// 鐢宠鍌ㄤ綅
+        /// </summary>
+        /// <param name="PalletNo"></param>
+        /// <returns></returns>
+        public void ApplyLocatNo(Pallnetmsg pallnetmsg)
+        {
+            var sql = $"select LocatNo from SysStorageLocat where status = '0'";
+            var sql2 = "";
+            var house = "W01";//鈥︹�﹀彨妗朵綅缃紝鍚庣画鏍规嵁浣嶇疆鍏宠仈鎴栧畾涔夛紝鏉ュ垽鏂彨妗朵綅缃墍灞炶溅闂�
+            var noticeno = "";//鍑哄叆搴撳崟鎹槑缁咺D
+            SysStorageLocat loction = new SysStorageLocat();
+            BllArrivalNoticeDetail ArriveMes = new BllArrivalNoticeDetail();
+            try
+            {
+                switch (pallnetmsg.Type)
+                {
+                    case "0"://鍑�妗剁敵璇峰偍浣�
+                        sql += $"and AreaNo like '%01' and WareHouseNo = {house}";
+                        loction = Db.Ado.SqlQuery<SysStorageLocat>(sql).OrderByDescending(a => a.CreateTime).FirstOrDefault();
+                        if (loction == null)
+                        {
+                            throw new Exception("搴撳唴鏆傛棤绌轰綑鍑�妗跺偍浣�");
+                        }
+                        break;
+                    case "1"://娣锋枡妗剁敵璇峰偍浣�
+                        //鍒ゆ柇鏄惁鏈夋壒娆″彿
+                        if (string.IsNullOrWhiteSpace(pallnetmsg.LotNo))
+                        {
+                            throw new Exception("鎵规鍙蜂负绌猴紒");
+                        }
+                        //鏍¢獙姝ゆ壒娆℃槸鍚︽湁鍏ュ簱鍗�
+                        /*sql2 = $"select * from BllArrivalNoticeDetail where LotNo =  '{pallnetmsg.LotNo}' order by CreateTime desc";
+                        ArriveMes = Db.Ado.SqlQuery<BllArrivalNoticeDetail>(sql).FirstOrDefault();
+                        if (ArriveMes == null)
+                        {
+                            throw new Exception("璇ユ壒娆℃病鏈夊搴旂殑鍏ュ簱鍗�");
+                        }
+                        noticeno = ArriveMes.Id.ToString();*/
+                        sql += $"and AreaNo  like '%02' and WareHouseNo = {house}";
+                        loction = Db.Ado.SqlQuery<SysStorageLocat>(sql).OrderByDescending(a => a.CreateTime).FirstOrDefault();
+                        if (loction == null)
+                        {
+                            throw new Exception("搴撳唴鏆傛棤绌轰綑娣锋枡妗跺偍浣�");
+                        }
+                        break;
+                    case "2"://鍗婃垚鍝佹《鐢宠鍌ㄤ綅
+                        //鍒ゆ柇鏄惁鏈夋壒娆″彿
+                        if (string.IsNullOrWhiteSpace(pallnetmsg.LotNo))
+                        {
+                            throw new Exception("鎵规鍙蜂负绌猴紒");
+                        }
+                        //鏍¢獙姝ゆ壒娆℃槸鍚︽湁鍏ュ簱鍗�
+                        sql2 = $"select * from BllArrivalNoticeDetail where LotNo =  '{pallnetmsg.LotNo}' order by CreateTime desc";
+                        ArriveMes = Db.Ado.SqlQuery<BllArrivalNoticeDetail>(sql).FirstOrDefault();
+                        if (ArriveMes == null)
+                        {
+                            throw new Exception("璇ユ壒娆℃病鏈夊搴旂殑鍏ュ簱鍗�");
+                        }
+                        noticeno = ArriveMes.Id.ToString();
+                        sql += $"and AreaNo like '%03' and WareHouseNo = {house}";
+                        loction = Db.Ado.SqlQuery<SysStorageLocat>(sql).OrderByDescending(a => a.CreateTime).FirstOrDefault();
+                        if (loction == null)
+                        {
+                            throw new Exception("搴撳唴鏆傛棤绌轰綑鍗婃垚鍝佹《鍌ㄤ綅");
+                        }
+                        break;
+                    case "3"://鑴忔《鐢宠鍌ㄤ綅
+                        sql += $"and AreaNo like '%04' and WareHouseNo = {house}";
+                        loction = Db.Ado.SqlQuery<SysStorageLocat>(sql).OrderByDescending(a => a.CreateTime).FirstOrDefault();
+                        if (loction == null)
+                        {
+                            throw new Exception("搴撳唴鏆傛棤绌轰綑鑴忔《鍌ㄤ綅");
+                        }
+                        break;
+                }
+                var task = new TaskDetial
+                {
+                    Startport = pallnetmsg.Location,
+                    Endport = loction.LocatNo,
+                    Pallno = pallnetmsg.Palletno,
+                    Type = pallnetmsg.Type,
+                    Crtype = "0",
+                    Noticedetailno = int.Parse(noticeno),
+                };
+                CreateLotTask(task);
+            }
+            catch (Exception)
+            {
+
+                throw;
+            }
+        }
+
+
+        /// <summary>
+        /// RCS鐢熸垚浠诲姟
+        /// </summary>
+        /// <param name="taskCode"></param>
+        /// <param name="taskType"></param>
+        /// <param name="startPos">璧峰浣嶇疆</param>
+        /// <param name="endPos">鐩殑浣嶇疆</param>
+        /// <param name="agvCode"></param>
+        /// <param name="url">RCS鍦板潃</param>
+        /// <returns></returns>
+        public genAgvSchedulingTaskRep genAgvSchedulingTask(AgvTask agv,string url)
+        {
+            try
+            {
+                genAgvSchedulingTaskRep cbrep = new genAgvSchedulingTaskRep();
+                PositionCodePath pcd1 = new PositionCodePath()
+                {
+                    positionCode = agv.startPos.ToString(),
+                    type = "00",
+                };
+                PositionCodePath pcd2 = new PositionCodePath()
+                {
+                    positionCode = agv.endPos.ToString(),
+                    type = "00",
+                };
+                List<PositionCodePath> lst = new List<PositionCodePath>();
+                lst.Add(pcd1);
+                lst.Add(pcd2);
+                genAgvSchedulingTaskReq cbreq = new genAgvSchedulingTaskReq()
+                {
+                    reqCode = agv.taskCode.ToString(),
+                    taskCode = agv.taskCode.ToString(),
+                    taskTyp = agv.taskType,
+                    positionCodePath = lst,
+                    podCode = "",
+                    agvCode = agv.agvCode,
+                    ctnrTyp = "1",
+                    //ctnrCode="2",
+
+                };
+                cbrep = genAgvSchedulingTask(cbreq,url);
+                
+
+                return cbrep;
+            }
+            catch (Exception ex) { throw ex; }
+        }
+
+        /// <summary>
+        /// 鐢熸垚浠诲姟鍗�
+        /// </summary>
+        /// <param name="req"></param>
+        /// <returns></returns>
+        private genAgvSchedulingTaskRep genAgvSchedulingTask(genAgvSchedulingTaskReq req,string url)
+        {
+            genAgvSchedulingTaskRep rep = new genAgvSchedulingTaskRep()
+            {
+                code = "-1",
+                message = "鐢熶骇浠诲姟鍗曞け璐�"
+            };
+            if (req == null)
+            {
+                return rep;
+            }
+            try
+            {
+                string jsonReq = JsonConvert.SerializeObject(req);
+
+                string jsonRep = HttpHelper.DoPost(url + "/genAgvSchedulingTask", jsonReq,"灏忚溅浠诲姟涓嬪彂","RCS");
+                jsonRep = jsonRep.TrimStart('\"');
+                jsonRep = jsonRep.TrimEnd('\"');
+                jsonRep = jsonRep.Replace("\\", "");
+                rep = JsonConvert.DeserializeObject<genAgvSchedulingTaskRep>(jsonRep);
+                return rep;
+            }
+            catch (Exception ex)
+            {
+                throw ex;
+            }
+        }
+
+
+        /// <summary>
+        /// 鐢熸垚浠诲姟鍗曞埌LogTask
+        /// </summary>
+        /// <param name="req"></param>
+        /// <returns></returns>
+        public void CreateLotTask(TaskDetial taskDetial)
+        {
+            try
+            {
+                var na = taskDetial.Crtype == "0"?"鍏ュ簱":"鍑哄簱";
+                var sql = "";
+                var sql2 = "";
+                //寮�鍚簨鍔�
+                Db.BeginTran();
+                //鍒ゆ柇浠诲姟鏄惁涓烘柊浠诲姟
+                if (string.IsNullOrWhiteSpace(taskDetial.Taskno))
+                {
+                    taskDetial.Taskno = new Common().GetMaxNo("T");
+                
+                    var logTaskEntry = new LogTask    
+                    {
+                        TaskNo = taskDetial.Taskno,
+                        Sender = "WMS",
+                        Receiver = "RCS",
+                        //IsSuccess = 1, //鏄惁涓嬪彂鎴愬姛 0澶辫触 1鎴愬姛
+                        SendDate = DateTime.Now,  //鍙戦�佹椂闂�
+                        //BackDate = DateTime.Now,  //杩斿洖鏃堕棿
+                        StartLocat = taskDetial.Startport,//璧峰浣嶇疆
+                        EndLocat = taskDetial.Endport,//鐩爣浣嶇疆
+                        PalletNo = taskDetial.Pallno,//鎵樼洏鐮�
+                        IsSend = 1,//鏄惁鍙啀娆′笅鍙�
+                        IsCancel = 1,//鏄惁鍙彇娑�
+                        IsFinish = 1,//鏄惁鍙畬鎴�
+                        Type = taskDetial.Crtype,//浠诲姟绫诲瀷 0 鍏ュ簱浠诲姟 1 鍑哄簱浠诲姟  2 绉诲簱浠诲姟
+                        Status = "0",//浠诲姟鐘舵��0锛氱瓑寰呮墽琛�1姝e湪鎵ц2鎵ц瀹屾垚
+                        NoticeDetailNo = int.Parse(taskDetial.Noticedetailno.ToString()),
+                        Msg = $"{taskDetial.Endport}鐨剓na}浠诲姟", //鍏抽敭淇℃伅
+                    };
+                    Db.Insertable(logTaskEntry).ExecuteCommand();
+                }
+                var Agv = new AgvTask
+                {
+                    taskCode = taskDetial.Taskno,
+                    taskType = taskDetial.Type,
+                    startPos = taskDetial.Startport,
+                    endPos = taskDetial.Endport,
+                    agvCode = "1",//鈥︹�﹂渶瑕佸拰AGV纭姝ゅ瓧娈靛��
+                };
+                //璇锋眰灏忚溅
+                genAgvSchedulingTaskRep chrep =genAgvSchedulingTask(Agv, "url");
+                if (chrep.code != "0")
+                {
+                    sql = $"UPDATE LogTask SET IsSuccess = '0' WHERE TaskNo = {taskDetial.Taskno}";
+                    Db.Ado.ExecuteCommand(sql);
+                    Db.CommitTran();
+                    throw new Exception("RCS浠诲姟涓嬪彂閿欒锛孯CS杩斿洖娑堟伅锛�"+chrep.message);
+                }
+                sql = $"UPDATE LogTask SET IsSuccess = 1,BackDate = {DateTime.Now},Status = '1' WHERE TaskNo = {taskDetial.Taskno}";
+                Db.Ado.ExecuteCommand(sql);
+                if (taskDetial.Crtype == "0")
+                {
+                    sql = $"UPDATE SysStorageLocat SET Status = '2' where LocatNo = '{taskDetial.Endport}'";
+                    sql2 = $"UPDATE DataStockDetail SET PalletStatus = '{taskDetial.Type}' where PalletNo = '{taskDetial.Pallno}'";
+                    Db.Ado.ExecuteCommand(sql2);
+                }
+                else
+                {
+                    sql = $"UPDATE SysStorageLocat SET Status = '3' where LocatNo = '{taskDetial.Startport}' ";
+                }
+                Db.Ado.ExecuteCommand(sql);
+                //鎻愪氦浜嬪姟
+                Db.CommitTran();
+            }
+            catch (Exception ex)
+            {
+                Db.RollbackTran();
+                throw ex;
+            }
+
+        }
     }
 }

--
Gitblit v1.8.0