From 87bdc068d2135c27fa23732bab14ac297787e6e5 Mon Sep 17 00:00:00 2001
From: 如洲 陈 <1278080563@qq.com>
Date: 星期四, 30 十月 2025 08:35:11 +0800
Subject: [PATCH] 单位转换接口和委外工单变更修改

---
 MES.Service/Dto/webApi/ErpUnitZh.cs                         |   66 ++++++
 MES.Service/Modes/MesUnitZh.cs                              |   79 +++++++
 MES.Service/service/BasicData/MesUnitZhManager.cs           |  183 ++++++++++++++++
 MESApplication/Controllers/BasicData/MesUnitZhController.cs |  259 +++++++++++++++++++++++
 MES.Service/service/BasicData/ProductionOrderManager.cs     |   54 ++++
 5 files changed, 640 insertions(+), 1 deletions(-)

diff --git a/MES.Service/Dto/webApi/ErpUnitZh.cs b/MES.Service/Dto/webApi/ErpUnitZh.cs
new file mode 100644
index 0000000..41b9795
--- /dev/null
+++ b/MES.Service/Dto/webApi/ErpUnitZh.cs
@@ -0,0 +1,66 @@
+namespace MES.Service.Dto.webApi;
+
+/// <summary>
+///     ERP鍗曚綅杞崲鏁版嵁鎺ユ敹瀵硅薄
+/// </summary>
+public class ErpUnitZh
+{
+    /// <summary>
+    ///     鐗╂枡id (FDWA001)
+    /// </summary>
+    public string? FDWA001 { get; set; }
+
+    /// <summary>
+    ///     鐗╂枡鍚嶇О (FDWA002)
+    /// </summary>
+    public string? FDWA002 { get; set; }
+
+    /// <summary>
+    ///     鐗╂枡缂栫爜 (FDWA003)
+    /// </summary>
+    public string? FDWA003 { get; set; }
+
+    /// <summary>
+    ///     鎹㈢畻鍒嗗瓙 (FDWA004)
+    /// </summary>
+    public decimal? FDWA004 { get; set; }
+
+    /// <summary>
+    ///     杞崲鍗曚綅id (FDWA005)
+    /// </summary>
+    public string? FDWA005 { get; set; }
+
+    /// <summary>
+    ///     杞崲鍗曚綅 (FDWA006)
+    /// </summary>
+    public string? FDWA006 { get; set; }
+
+    /// <summary>
+    ///     鎹㈢畻鍏崇郴 (FDWA007)
+    /// </summary>
+    public decimal? FDWA007 { get; set; }
+
+    /// <summary>
+    ///     鍩哄噯鍗曚綅id (FDWA008)
+    /// </summary>
+    public string? FDWA008 { get; set; }
+
+    /// <summary>
+    ///     鍩哄噯鍗曚綅 (FDWA009)
+    /// </summary>
+    public string? FDWA009 { get; set; }
+
+    /// <summary>
+    ///     ERPID (FDWA010)
+    /// </summary>
+    public string? FDWA010 { get; set; }
+
+    /// <summary>
+    ///     鎿嶄綔绫诲瀷 (0=鍚敤, 1=绂佺敤, 2=鏂板, 3=鍒犻櫎, 4=鏇存柊)
+    /// </summary>
+    public string? Type { get; set; }
+}
+
+
+
+
diff --git a/MES.Service/Modes/MesUnitZh.cs b/MES.Service/Modes/MesUnitZh.cs
new file mode 100644
index 0000000..ea68203
--- /dev/null
+++ b/MES.Service/Modes/MesUnitZh.cs
@@ -0,0 +1,79 @@
+using SqlSugar;
+
+namespace MES.Service.Modes;
+
+/// <summary>
+///     鍗曚綅杞崲琛�
+/// </summary>
+[SugarTable("MES_UNIT_ZH")]
+public class MesUnitZh
+{
+    /// <summary>
+    ///     涓婚敭ID
+    /// </summary>
+    [SugarColumn(ColumnName = "ID", IsPrimaryKey = true)]
+    public decimal Id { get; set; }
+
+    /// <summary>
+    ///     鐗╂枡id
+    /// </summary>
+    [SugarColumn(ColumnName = "DWA001")]
+    public decimal? Dwa001 { get; set; }
+
+    /// <summary>
+    ///     鐗╂枡鍚嶇О
+    /// </summary>
+    [SugarColumn(ColumnName = "DWA002")]
+    public string? Dwa002 { get; set; }
+
+    /// <summary>
+    ///     鐗╂枡缂栫爜
+    /// </summary>
+    [SugarColumn(ColumnName = "DWA003")]
+    public string? Dwa003 { get; set; }
+
+    /// <summary>
+    ///     鎹㈢畻鍒嗗瓙
+    /// </summary>
+    [SugarColumn(ColumnName = "DWA004")]
+    public decimal? Dwa004 { get; set; }
+
+    /// <summary>
+    ///     杞崲鍗曚綅id
+    /// </summary>
+    [SugarColumn(ColumnName = "DWA005")]
+    public decimal? Dwa005 { get; set; }
+
+    /// <summary>
+    ///     杞崲鍗曚綅
+    /// </summary>
+    [SugarColumn(ColumnName = "DWA006")]
+    public string? Dwa006 { get; set; }
+
+    /// <summary>
+    ///     鎹㈢畻鍏崇郴
+    /// </summary>
+    [SugarColumn(ColumnName = "DWA007")]
+    public decimal? Dwa007 { get; set; }
+
+    /// <summary>
+    ///     鍩哄噯鍗曚綅id
+    /// </summary>
+    [SugarColumn(ColumnName = "DWA008")]
+    public decimal? Dwa008 { get; set; }
+
+    /// <summary>
+    ///     鍩哄噯鍗曚綅
+    /// </summary>
+    [SugarColumn(ColumnName = "DWA009")]
+    public string? Dwa009 { get; set; }
+
+    /// <summary>
+    ///     ERPID
+    /// </summary>
+    [SugarColumn(ColumnName = "ERPID")]
+    public decimal? Erpid { get; set; }
+
+    [SugarColumn(IsIgnore = true)] public string? Type { get; set; }
+}
+
diff --git a/MES.Service/service/BasicData/MesUnitZhManager.cs b/MES.Service/service/BasicData/MesUnitZhManager.cs
new file mode 100644
index 0000000..fa107d2
--- /dev/null
+++ b/MES.Service/service/BasicData/MesUnitZhManager.cs
@@ -0,0 +1,183 @@
+using MES.Service.DB;
+using MES.Service.Dto.webApi;
+using MES.Service.Modes;
+using SqlSugar;
+
+namespace MES.Service.service.BasicData;
+
+public class MesUnitZhManager : Repository<MesUnitZh>
+{
+    public bool Save(ErpUnitZh unit)
+    {
+        var entity = GetMesUnitZh(unit);
+
+        return UseTransaction(db =>
+        {
+            switch (unit.Type)
+            {
+                case "0":
+                    if (UpdateUnitZhStatus(db, entity.Id, "A"))
+                        return 1;
+                    break;
+                case "1":
+                    if (UpdateUnitZhStatus(db, entity.Id, "B"))
+                        return 1;
+                    break;
+                case "3":
+                    if (DeleteUnitZh(db, entity.Id))
+                        return 1;
+                    break;
+                case "2":
+                case "4":
+                    if (InsertOrUpdate(db, entity))
+                        return 1;
+                    break;
+                default:
+                    throw new ArgumentNullException(
+                        $"type娌℃湁{unit.Type}杩欎釜绫诲瀷鐨勫弬鏁�");
+            }
+
+            throw new NotImplementedException("鎿嶄綔澶辫触");
+        }) > 0;
+    }
+
+    public bool SaveList(List<ErpUnitZh> units)
+    {
+        var list = units.Select(GetMesUnitZh).ToList();
+        var groupBy = list.GroupBy(s => s.Type)
+            .ToDictionary(g => g.Key, g => g.ToList());
+
+        return UseTransaction(db =>
+        {
+            foreach (var unitGroup in groupBy)
+                switch (unitGroup.Key)
+                {
+                    case "0":
+                        if (!UpdateUnitZhStatusBatch(db, unitGroup.Value, "A"))
+                            throw new NotImplementedException("鍚敤澶辫触");
+                        break;
+                    case "1":
+                        if (!UpdateUnitZhStatusBatch(db, unitGroup.Value, "B"))
+                            throw new NotImplementedException("绂佺敤澶辫触");
+                        break;
+                    case "3":
+                        if (!DeleteUnitZhBatch(db, unitGroup.Value))
+                            throw new NotImplementedException("鍒犻櫎澶辫触");
+                        break;
+                    case "2":
+                    case "4":
+                        if (!InsertOrUpdateBatch(db, unitGroup.Value))
+                            throw new NotImplementedException("鍚屾澶辫触");
+                        break;
+                    default:
+                        throw new ArgumentNullException(
+                            $"type娌℃湁{unitGroup.Key}杩欎釜绫诲瀷鐨勫弬鏁�");
+                }
+
+            return 1;
+        }) > 0;
+    }
+
+    private bool UpdateUnitZhStatus(SqlSugarScope db, decimal unitId,
+        string status)
+    {
+        // 娉ㄦ剰锛氬鏋淢esUnitZh琛ㄦ病鏈塅forbidstatus瀛楁锛岄渶瑕佹牴鎹疄闄呮儏鍐佃皟鏁�
+        // 杩欓噷鍙兘闇�瑕侀�氳繃鍏朵粬鏂瑰紡鏇存柊鐘舵�侊紝鎴栬�呮殏鏃舵敞閲婃帀
+        var result = db.Updateable<MesUnitZh>()
+            .Where(s => s.Id == unitId).ExecuteCommand();
+
+        if (result > 0)
+            return true;
+
+        throw new NotImplementedException(status == "A" ? "鍚敤澶辫触" : "绂佺敤澶辫触");
+    }
+
+    private bool InsertUnitZh(SqlSugarScope db, MesUnitZh entity)
+    {
+        var insert = db.Insertable(entity).ExecuteCommand();
+        if (insert > 0)
+            return true;
+
+        throw new NotImplementedException("鎻掑叆澶辫触");
+    }
+
+    private bool DeleteUnitZh(SqlSugarScope db, decimal unitId)
+    {
+        var deleteById = db.Deleteable<MesUnitZh>().In(unitId).ExecuteCommand();
+        if (deleteById > 0)
+            return true;
+
+        throw new NotImplementedException("鍒犻櫎澶辫触");
+    }
+
+    private MesUnitZh GetMesUnitZh(ErpUnitZh unit)
+    {
+        return new MesUnitZh
+        {
+            Id = string.IsNullOrEmpty(unit.FDWA010) ? 0 : Convert.ToDecimal(unit.FDWA010),
+            Dwa001 = string.IsNullOrEmpty(unit.FDWA001) ? null : Convert.ToDecimal(unit.FDWA001),
+            Dwa002 = unit.FDWA002,
+            Dwa003 = unit.FDWA003,
+            Dwa004 = unit.FDWA004,
+            Dwa005 = string.IsNullOrEmpty(unit.FDWA005) ? null : Convert.ToDecimal(unit.FDWA005),
+            Dwa006 = unit.FDWA006,
+            Dwa007 = unit.FDWA007,
+            Dwa008 = string.IsNullOrEmpty(unit.FDWA008) ? null : Convert.ToDecimal(unit.FDWA008),
+            Dwa009 = unit.FDWA009,
+            Erpid = string.IsNullOrEmpty(unit.FDWA010) ? null : Convert.ToDecimal(unit.FDWA010),
+            Type = unit.Type
+        };
+    }
+
+    private bool UpdateUnitZhStatusBatch(SqlSugarScope db, List<MesUnitZh> unitList,
+        string status)
+    {
+        var ids = unitList.Select(it => it.Id).ToArray();
+        var result = db.Updateable<MesUnitZh>()
+            .Where(s => ids.Contains(s.Id)).ExecuteCommand();
+
+        if (result > 0)
+            return true;
+
+        throw new NotImplementedException(status == "A" ? "鍚敤澶辫触" : "绂佺敤澶辫触");
+    }
+
+    private bool InsertUnitZhBatch(SqlSugarScope db, List<MesUnitZh> unitList)
+    {
+        var insertRange = db.Insertable(unitList).ExecuteCommand();
+        if (insertRange > 0)
+            return true;
+
+        throw new NotImplementedException("鎻掑叆澶辫触");
+    }
+
+    private bool DeleteUnitZhBatch(SqlSugarScope db, List<MesUnitZh> unitList)
+    {
+        var ids = unitList.Select(it => it.Id).ToArray();
+        var deleteByIds = db.Deleteable<MesUnitZh>().In(ids).ExecuteCommand();
+        return deleteByIds > 0;
+    }
+
+    /// <summary>
+    /// 鎻掑叆鎴栨洿鏂扮殑鏂规硶锛氬厛鍒犻櫎鍐嶆彃鍏�
+    /// </summary>
+    private bool InsertOrUpdate(SqlSugarScope db, MesUnitZh entity)
+    {
+        db.Deleteable<MesUnitZh>()
+            .Where(s => s.Id == entity.Id)
+            .ExecuteCommand();
+
+        var insert = db.Insertable(entity).ExecuteCommand();
+        return insert > 0;
+    }
+
+    private bool InsertOrUpdateBatch(SqlSugarScope db, List<MesUnitZh> unitList)
+    {
+        foreach (var entity in unitList)
+            if (!InsertOrUpdate(db, entity))
+                return false;
+
+        return true;
+    }
+}
+
diff --git a/MES.Service/service/BasicData/ProductionOrderManager.cs b/MES.Service/service/BasicData/ProductionOrderManager.cs
index f78da56..03d8359 100644
--- a/MES.Service/service/BasicData/ProductionOrderManager.cs
+++ b/MES.Service/service/BasicData/ProductionOrderManager.cs
@@ -53,6 +53,31 @@
 
         womcaaWW.Caa001 = mesRohInDatas[0].Cab001;
         womcaaWW.Erpid = womcaaWW.FSUBENTRYID.ToInt32();
+
+        //瀹氫箟杈撳叆鍙傛暟
+        var inputParam1 = new SugarParameter("P_WORK_NO", womcaaWW.Caa001);
+        // 瀹氫箟杈撳嚭鍙傛暟
+        var outParam1 = new SugarParameter("c_Result", null, true);
+        var outParam2 = new SugarParameter("C_MSG", null, true);
+        // 浣跨敤 SqlSugar 鎵ц瀛樺偍杩囩▼
+        db.Ado.ExecuteCommand(
+            "BEGIN PRC_UPDATE_DAA(:P_WORK_NO,:c_Result,:C_MSG); END;",
+            inputParam1, outParam1, outParam2);
+        // 鑾峰彇杈撳嚭鍙傛暟鐨勫��
+        var result = int.Parse((string)outParam1.Value);
+        var message = outParam2.Value == DBNull.Value
+            ? string.Empty
+            : (string)outParam2.Value;
+        if (result == 1)
+        {
+            //瀛樺偍杩囩▼澶辫触鍒欎簨鍔¤繘琛屽洖婊�
+            db.Ado.RollbackTran();
+            throw new Exception(message);
+        }
+
+        // 鎻愪氦浜嬪姟
+        db.Ado.CommitTran();
+
         var decimals = mesRohInDatas.Select(s => s.Id).ToArray();
         var update = base.DeleteById(womcaaWW.Id);
         var insertOrUpdate = db
@@ -91,7 +116,34 @@
             .IgnoreColumnsNull()
             .ExecuteCommand() > 0;
 
-        if (orUpdate && baOrUpdate) return true;
+        if (orUpdate && baOrUpdate)
+        {
+            //瀹氫箟杈撳叆鍙傛暟
+            var inputParam1 = new SugarParameter("P_WORK_NO", mesWomcaa.Caa001);
+            // 瀹氫箟杈撳嚭鍙傛暟
+            var outParam1 = new SugarParameter("c_Result", null, true);
+            var outParam2 = new SugarParameter("C_MSG", null, true);
+            // 浣跨敤 SqlSugar 鎵ц瀛樺偍杩囩▼
+            Db.Ado.ExecuteCommand(
+                "BEGIN PRC_UPDATE_DAA(:P_WORK_NO,:c_Result,:C_MSG); END;",
+                inputParam1, outParam1, outParam2);
+            // 鑾峰彇杈撳嚭鍙傛暟鐨勫��
+            var result = int.Parse((string)outParam1.Value);
+            var message = outParam2.Value == DBNull.Value
+                ? string.Empty
+                : (string)outParam2.Value;
+            if (result == 1)
+            {
+                //瀛樺偍杩囩▼澶辫触鍒欎簨鍔¤繘琛屽洖婊�
+                db.Ado.RollbackTran();
+                throw new Exception(message);
+            }
+
+            // 鎻愪氦浜嬪姟
+            db.Ado.CommitTran();
+            return true;
+        }
+
 
         throw new NotImplementedException("鎻掑叆鎴栨洿鏂板け璐�");
     }
diff --git a/MESApplication/Controllers/BasicData/MesUnitZhController.cs b/MESApplication/Controllers/BasicData/MesUnitZhController.cs
new file mode 100644
index 0000000..0cb35fe
--- /dev/null
+++ b/MESApplication/Controllers/BasicData/MesUnitZhController.cs
@@ -0,0 +1,259 @@
+using System.Dynamic;
+using MES.Service.Dto.webApi;
+using MES.Service.Modes;
+using MES.Service.service;
+using MES.Service.service.BasicData;
+using MES.Service.util;
+using Microsoft.AspNetCore.Mvc;
+using Newtonsoft.Json;
+
+namespace MESApplication.Controllers.BasicData;
+
+[ApiController]
+[Route("api/[controller]")]
+public class MesUnitZhController : ControllerBase
+{
+    private readonly MessageCenterManager _manager = new();
+    private readonly MesUnitZhManager m = new();
+
+    private readonly string METHOD = "POST";
+
+    private readonly string TableName = "MES_UNIT_ZH";
+
+    private readonly string URL = "http://localhost:10054/api/MesUnitZh/";
+
+    [HttpPost("Save")]
+    public ResponseResult Save(ErpUnitZh unit)
+    {
+        var entity = new MessageCenter();
+        entity.TableName = TableName;
+        entity.Url = URL + "Save";
+        entity.Method = METHOD;
+        entity.Route = unit.FDWA003;
+        entity.Data = JsonConvert.SerializeObject(unit);
+        entity.Status = 1;
+        entity.CreateBy = "ERP";
+        try
+        {
+            dynamic resultInfos = new ExpandoObject();
+            var save = m.Save(unit);
+            resultInfos.tbBillList = save;
+
+            entity.Result = 0;
+            if (save) entity.Result = 1;
+
+            entity.DealWith = 1;
+            _manager.save(entity);
+
+            return new ResponseResult
+            {
+                status = 0,
+                message = "OK",
+                data = resultInfos
+            };
+        }
+        catch (Exception ex)
+        {
+            entity.Result = 0;
+
+            entity.DealWith = 0;
+
+            entity.ResultData = ex.Message;
+
+            _manager.save(entity);
+
+            return ResponseResult.ResponseError(ex);
+        }
+    }
+
+    [HttpPost("SaveList")]
+    public ResponseResult SaveList(List<ErpUnitZh> units)
+    {
+        var entity = new MessageCenter();
+        entity.TableName = TableName;
+        entity.Url = URL + "SaveList";
+        entity.Method = METHOD;
+        entity.Data = JsonConvert.SerializeObject(units);
+        entity.Status = 1;
+        entity.CreateBy = "ERP";
+        try
+        {
+            dynamic resultInfos = new ExpandoObject();
+            var save = m.SaveList(units);
+            resultInfos.tbBillList = save;
+
+            entity.Result = 0;
+            if (save) entity.Result = 1;
+
+            entity.DealWith = 1;
+            _manager.save(entity);
+
+            return new ResponseResult
+            {
+                status = 0,
+                message = "OK",
+                data = resultInfos
+            };
+        }
+        catch (Exception ex)
+        {
+            entity.Result = 0;
+
+            entity.DealWith = 0;
+
+            entity.ResultData = ex.Message;
+
+            _manager.save(entity);
+
+            return ResponseResult.ResponseError(ex);
+        }
+    }
+
+    /// <summary>
+    ///     鑾峰彇鎵�鏈�
+    /// </summary>
+    /// <returns></returns>
+    [HttpPost("GetList")]
+    public ResponseResult GetList()
+    {
+        try
+        {
+            dynamic resultInfos = new ExpandoObject();
+            resultInfos.tbBillList = m.GetList();
+            return new ResponseResult
+            {
+                status = 0,
+                message = "OK",
+                data = resultInfos
+            };
+        }
+        catch (Exception ex)
+        {
+            return ResponseResult.ResponseError(ex);
+        }
+    }
+
+    /// <summary>
+    ///     鏍规嵁涓婚敭鑾峰彇
+    /// </summary>
+    /// <returns></returns>
+    [HttpPost("GetById")]
+    public ResponseResult GetById(int id)
+    {
+        try
+        {
+            dynamic resultInfos = new ExpandoObject();
+            resultInfos.tbBillList = m.GetById(id);
+            return new ResponseResult
+            {
+                status = 0,
+                message = "OK",
+                data = resultInfos
+            };
+        }
+        catch (Exception ex)
+        {
+            return ResponseResult.ResponseError(ex);
+        }
+    }
+
+    /// <summary>
+    ///     鏍规嵁涓婚敭鍒犻櫎
+    /// </summary>
+    /// <returns></returns>
+    [HttpPost("DeleteByIds")]
+    public ResponseResult DeleteByIds([FromBody] object[] ids)
+    {
+        try
+        {
+            dynamic resultInfos = new ExpandoObject();
+            resultInfos.tbBillList = m.DeleteByIds(ids);
+            return new ResponseResult
+            {
+                status = 0,
+                message = "OK",
+                data = resultInfos
+            };
+        }
+        catch (Exception ex)
+        {
+            return ResponseResult.ResponseError(ex);
+        }
+    }
+
+    /// <summary>
+    ///     娣诲姞
+    /// </summary>
+    /// <returns></returns>
+    [HttpPost("Insert")]
+    public ResponseResult Add([FromBody] MesUnitZh data)
+    {
+        try
+        {
+            dynamic resultInfos = new ExpandoObject();
+            resultInfos.tbBillList = m.Insert(data);
+            return new ResponseResult
+            {
+                status = 0,
+                message = "OK",
+                data = resultInfos
+            };
+        }
+        catch (Exception ex)
+        {
+            return ResponseResult.ResponseError(ex);
+        }
+    }
+
+    /// <summary>
+    ///     娣诲姞杩斿洖鑷
+    /// </summary>
+    /// <returns></returns>
+    [HttpPost("InsertReturnIdentity")]
+    public ResponseResult InsertReturnIdentity([FromBody] MesUnitZh data)
+    {
+        try
+        {
+            dynamic resultInfos = new ExpandoObject();
+            resultInfos.tbBillList = m.InsertReturnIdentity(data);
+            return new ResponseResult
+            {
+                status = 0,
+                message = "OK",
+                data = resultInfos
+            };
+        }
+        catch (Exception ex)
+        {
+            return ResponseResult.ResponseError(ex);
+        }
+    }
+
+    /// <summary>
+    ///     淇敼
+    /// </summary>
+    /// <returns></returns>
+    [HttpPost("Update")]
+    public ResponseResult Update([FromBody] MesUnitZh data)
+    {
+        try
+        {
+            dynamic resultInfos = new ExpandoObject();
+            resultInfos.tbBillList = m.Update(data);
+            return new ResponseResult
+            {
+                status = 0,
+                message = "OK",
+                data = resultInfos
+            };
+        }
+        catch (Exception ex)
+        {
+            return ResponseResult.ResponseError(ex);
+        }
+    }
+}
+
+
+
+

--
Gitblit v1.9.3