From ce9ce75d289be1f45d6e7e8d40e95ec7c8f357b5 Mon Sep 17 00:00:00 2001
From: 快乐的昕的电脑 <快乐的昕的电脑@DESKTOP-C2BQPQU>
Date: 星期一, 01 十二月 2025 17:15:55 +0800
Subject: [PATCH] 物料单位换算信息

---
 MES.Service/Dto/webApi/ErpUnitMatrixing.cs                         |   17 +
 MES.Service/service/BasicData/MesUnitMatrixingManager.cs           |  247 ++++++++++++++++++++++++
 MES.Service/Modes/MesUnitMatrixing.cs                              |   78 +++++++
 MESApplication/Controllers/BasicData/MesUnitMatrixingController.cs |  256 +++++++++++++++++++++++++
 4 files changed, 598 insertions(+), 0 deletions(-)

diff --git a/MES.Service/Dto/webApi/ErpUnitMatrixing.cs b/MES.Service/Dto/webApi/ErpUnitMatrixing.cs
new file mode 100644
index 0000000..70c9020
--- /dev/null
+++ b/MES.Service/Dto/webApi/ErpUnitMatrixing.cs
@@ -0,0 +1,17 @@
+锘縩amespace MES.Service.Dto.webApi;
+
+public class ErpUnitMatrixing
+{
+    public string? Type { get; set; }
+    public string? Id { get; set; }
+    public string FNumber { get; set; }
+    public decimal? FConvertMolecules { get; set; }
+    public decimal? FConvertDenominator { get; set; }
+    public string? FCurrentUnitId { get; set; }
+    public string? FForbidStatus { get; set; }
+    public string? remark1 { get; set; }
+    public string? remark2 { get; set; }
+    public string? remark3 { get; set; }
+    public string? remark4 { get; set; }
+    public string? remark5 { get; set; }
+}
\ No newline at end of file
diff --git a/MES.Service/Modes/MesUnitMatrixing.cs b/MES.Service/Modes/MesUnitMatrixing.cs
new file mode 100644
index 0000000..89b4e99
--- /dev/null
+++ b/MES.Service/Modes/MesUnitMatrixing.cs
@@ -0,0 +1,78 @@
+锘縰sing SqlSugar;
+
+namespace MES.Service.Modes;
+
+/// <summary>
+///     鐗╂枡鍗曚綅鎹㈢畻淇℃伅
+/// </summary>
+[SugarTable("MES_UNIT_MATRIXING")]
+public class MesUnitMatrixing
+{
+    /// <summary>
+    ///     BASEINFO
+    /// </summary>
+    [SugarColumn(ColumnName = "ID", IsPrimaryKey = true)]
+    public decimal Id { get; set; }
+
+    /// <summary>
+    ///     鐗╂枡缂栫爜
+    /// </summary>
+    [SugarColumn(ColumnName = "ITEM_NO")]
+    public string? ItemNo { get; set; }
+
+    /// <summary>
+    ///     鎹㈢畻鍒嗗瓙
+    /// </summary>
+    [SugarColumn(ColumnName = "CONVMOL")]
+    public decimal? CONVMOL { get; set; }
+
+    /// <summary>
+    ///     鎹㈢畻鍒嗘瘝
+    /// </summary>
+    [SugarColumn(ColumnName = "CONVDEN")]
+    public decimal? CONVDEN { get; set; }
+
+    /// <summary>
+    ///     鎹㈢畻鍗曚綅
+    /// </summary>
+    [SugarColumn(ColumnName = "ALT_UNITS")]
+    public string? AltUnits { get; set; }
+
+    /// <summary>
+    ///     鏈夋晥鐮�(Y鏈夋晥, N鏃犳晥)
+    /// </summary>
+    [SugarColumn(ColumnName = "VAL_STATES")]
+    public string? ValStates { get; set; }
+
+    /// <summary>
+    ///     鑷畾涔夊瓧娈�1
+    /// </summary>
+    [SugarColumn(ColumnName = "remark1")]
+    public string? remark1 { get; set; }
+
+    /// <summary>
+    ///     鑷畾涔夊瓧娈�2
+    /// </summary>
+    [SugarColumn(ColumnName = "remark2")]
+    public string? remark2 { get; set; }
+
+    /// <summary>
+    ///     鑷畾涔夊瓧娈�3
+    /// </summary>
+    [SugarColumn(ColumnName = "remark3")]
+    public string? remark3 { get; set; }
+
+    /// <summary>
+    ///     鑷畾涔夊瓧娈�4
+    /// </summary>
+    [SugarColumn(ColumnName = "remark4")]
+    public string? remark4 { get; set; }
+
+    /// <summary>
+    ///     鑷畾涔夊瓧娈�5
+    /// </summary>
+    [SugarColumn(ColumnName = "remark5")]
+    public string? remark5 { get; set; }
+
+    [SugarColumn(IsIgnore = true)] public string? Type { get; set; }
+}
\ No newline at end of file
diff --git a/MES.Service/service/BasicData/MesUnitMatrixingManager.cs b/MES.Service/service/BasicData/MesUnitMatrixingManager.cs
new file mode 100644
index 0000000..1fe562d
--- /dev/null
+++ b/MES.Service/service/BasicData/MesUnitMatrixingManager.cs
@@ -0,0 +1,247 @@
+锘縰sing AngleSharp.Dom;
+using MES.Service.DB;
+using MES.Service.Dto.webApi;
+using MES.Service.Modes;
+using SqlSugar;
+
+namespace MES.Service.service.BasicData;
+
+public class MesUnitMatrixingManager : Repository<MesUnitMatrixing>
+{
+    public bool Save(ErpUnitMatrixing unit)
+    {
+        var entity = GetMesUnitMatrixing(unit);
+
+        return UseTransaction(db =>
+        {
+            switch (unit.Type)
+            {
+                case "0":
+                    if (UpdateUnitStatus(db, entity.Id, "A"))
+                        return 1;
+                    break;
+                case "1":
+                    if (UpdateUnitStatus(db, entity.Id, "B"))
+                        return 1;
+                    break;
+                case "3":
+                    if (DeleteUnit(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<ErpUnitMatrixing> units)
+    {
+        var list = units.Select(GetMesUnitMatrixing).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 (!UpdateUnitStatusBatch(db, unitGroup.Value, "A"))
+                            throw new NotImplementedException("鍚敤澶辫触");
+                        break;
+                    case "1":
+                        if (!UpdateUnitStatusBatch(db, unitGroup.Value, "B"))
+                            throw new NotImplementedException("绂佺敤澶辫触");
+                        break;
+                    case "3":
+                        if (!DeleteUnitBatch(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 UpdateUnitStatus(SqlSugarScope db, decimal unitId,
+        string status)
+    {
+        var result = db.Updateable<MesUnitMatrixing>()
+            .SetColumns(s => s.ValStates == status)
+            .Where(s => s.Id == unitId).ExecuteCommand();
+
+        if (result > 0)
+            return true;
+
+        throw new NotImplementedException(status == "A" ? "鍚敤澶辫触" : "绂佺敤澶辫触");
+    }
+
+    private bool InsertUnit(SqlSugarScope db, MesUnitMatrixing entity)
+    {
+        var insert = db.Insertable(entity).ExecuteCommand();
+        if (insert > 0)
+            return true;
+
+        throw new NotImplementedException("鎻掑叆澶辫触");
+    }
+
+    private bool DeleteUnit(SqlSugarScope db, decimal unitId)
+    {
+        var deleteById = db.Deleteable<MesUnitMatrixing>()
+            .Where(s => s.Id == unitId).ExecuteCommand();
+        if (deleteById > 0)
+            return true;
+
+        throw new NotImplementedException("鍒犻櫎澶辫触");
+    }
+
+    private MesUnitMatrixing GetMesUnitMatrixing(ErpUnitMatrixing unit)
+    {
+        // 鏌ユ壘鏄惁宸插瓨鍦ㄧ浉鍚岀紪鐮佺殑璁板綍銆�
+        var existingCustomer = Db.Queryable<MesUnitMatrixing>()
+            .Where(s => s.ItemNo == unit.FNumber)
+            .First();
+
+        var entity = new MesUnitMatrixing
+        {
+            // 濡傛灉瀛樺湪锛屼娇鐢ㄧ幇鏈夌殑ID锛屽悗缁皢鍒犻櫎鍚庨噸鏂版彃鍏�
+            // 濡傛灉涓嶅瓨鍦紝璁句负0锛孖nsertOrUpdate鏂规硶灏嗙敓鎴愭柊ID
+            Id = existingCustomer?.Id ?? 0,
+            //Id = Convert.ToDecimal(unit.Id),
+            Type = unit.Type,
+            ItemNo=unit.FNumber,
+            CONVMOL=unit.FConvertMolecules,
+            CONVDEN=unit.FConvertDenominator,
+            AltUnits=unit.FCurrentUnitId,
+            ValStates=unit.FForbidStatus
+        };
+
+        // ERP: 0=鏈鐢�, 1=绂佺敤
+        // MES: A=鏈鐢�, B=绂佺敤
+        if (string.IsNullOrEmpty(unit.FForbidStatus))
+        {
+            entity.ValStates = "A";
+        }
+        else
+        {
+            //鎴戞湡鏈涚殑鍊兼槸A=鍚�,B=鏄�
+            //瀹為檯缁欐垜鐨勫�兼槸0鎴�1锛屾垜甯屾湜涓烘垜杞崲浠嶢鍜孊鐨勬柟寮�
+            entity.ValStates = unit.FForbidStatus == "1" ? "B" : "A";
+        }
+
+        return entity;
+    }
+
+    /// <summary>
+    /// 鐢熸垚鏂扮殑ID锛岀‘淇濅笉閲嶅
+    /// </summary>
+    private decimal GenerateNewId()
+    {
+        // 澶勭悊绌鸿〃鐨勬儏鍐碉紝浠�1寮�濮�
+        var maxId = Db.Queryable<MesUnitMatrixing>().Max(x => (decimal?)x.Id) ?? 0;
+        var newId = maxId + 1;
+
+        // 鍙岄噸妫�鏌ワ紝纭繚鐢熸垚鐨処D涓嶅瓨鍦�
+        while (Db.Queryable<MesUnitMatrixing>().Where(x => x.Id == newId).Any())
+        {
+            newId++;
+        }
+
+        return newId;
+    }
+
+    private bool UpdateUnitStatusBatch(SqlSugarScope db, List<MesUnitMatrixing> unitList,
+        string status)
+    {
+        var ids = unitList.Select(it => it.Id).ToArray();
+        var result = db.Updateable<MesUnitMatrixing>()
+            .SetColumns(s => s.ValStates == status)
+            .Where(s => ids.Contains(s.Id)).ExecuteCommand();
+
+        if (result > 0)
+            return true;
+
+        throw new NotImplementedException(status == "A" ? "鍚敤澶辫触" : "绂佺敤澶辫触");
+    }
+
+    private bool InsertUnitBatch(SqlSugarScope db, List<MesUnitMatrixing> unitList)
+    {
+        var insertRange = db.Insertable(unitList).ExecuteCommand();
+        if (insertRange > 0)
+            return true;
+
+        throw new NotImplementedException("鎻掑叆澶辫触");
+    }
+
+    private bool DeleteUnitBatch(SqlSugarScope db, List<MesUnitMatrixing> unitList)
+    {
+        var ids = unitList.Select(it => it.Id).ToArray();
+        var deleteByIds = db.Deleteable<MesUnitMatrixing>()
+            .Where(s => ids.Contains(s.Id)).ExecuteCommand();
+        return deleteByIds > 0;
+    }
+
+    /// <summary>
+    /// 鍚屾鐗╂枡鍗曚綅淇℃伅new_0/4
+    /// </summary>
+    /// <param name="db"></param>
+    /// <param name="entity"></param>
+    /// <returns></returns>
+    private bool InsertOrUpdate(SqlSugarScope db, MesUnitMatrixing entity)
+    {
+        if (entity.Id == 0)
+        {
+            // 鏂板鎯呭喌锛氱敓鎴愭柊ID骞舵彃鍏�
+            var newId = GenerateNewId();
+            entity.Id = newId;
+            return db.Insertable(entity).ExecuteCommand() > 0;
+        }
+        else
+        {
+            // 鏇存柊鎯呭喌锛氬垹闄ゅ悗閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塈D
+            var originalId = entity.Id;
+
+            // 鍏堝垹闄ゅ師璁板綍锛堝鏋滃瓨鍦級
+            db.Deleteable<MesUnitMatrixing>().Where(s => s.Id == originalId).ExecuteCommand();
+
+            // 閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塈D
+            entity.Id = originalId;
+            return db.Insertable(entity).ExecuteCommand() > 0;
+        }
+    }
+
+    /// <summary>
+    /// 鍚屾鐗╂枡鍗曚綅淇℃伅old_0/4
+    /// </summary>
+    /// <param name="db"></param>
+    /// <param name="entity"></param>
+    /// <returns></returns>
+    //private bool InsertOrUpdate(SqlSugarScope db, MesUnitMatrixing entity)
+    //{
+    //    db.Deleteable<MesUnitMatrixing>()
+    //        .Where(s => s.Id == entity.Id).ExecuteCommand();
+    //    var insert = db.Insertable(entity).ExecuteCommand();
+    //    return insert > 0;
+    //}
+
+    private bool InsertOrUpdateBatch(SqlSugarScope db, List<MesUnitMatrixing> unitList)
+    {
+        return unitList.All(entity => InsertOrUpdate(db, entity));
+    }
+}
\ No newline at end of file
diff --git a/MESApplication/Controllers/BasicData/MesUnitMatrixingController.cs b/MESApplication/Controllers/BasicData/MesUnitMatrixingController.cs
new file mode 100644
index 0000000..d9a977b
--- /dev/null
+++ b/MESApplication/Controllers/BasicData/MesUnitMatrixingController.cs
@@ -0,0 +1,256 @@
+锘縰sing 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 MesUnitMatrixingController : ControllerBase
+{
+    private readonly MessageCenterManager _manager = new();
+    private readonly MesUnitMatrixingManager m = new();
+
+    private readonly string METHOD = "POST";
+
+    private readonly string TableName = "MES_UNIT_MATRIXING";
+
+    private readonly string URL = "http://localhost:10054/api/MesUnitMatrixing/";
+
+    [HttpPost("Save")]
+    public ResponseResult Save(ErpUnitMatrixing unit)
+    {
+        var entity = new MessageCenter();
+        entity.TableName = TableName;
+        entity.Url = URL + "Save";
+        entity.Method = METHOD;
+        entity.Data = JsonConvert.SerializeObject(unit);
+        entity.Status = 1;
+        entity.CreateBy = "PL017";
+        entity.Route = unit.FNumber;
+        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<ErpUnitMatrixing> 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 = "PL017";
+        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] MesUnitMatrixing 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] MesUnitMatrixing 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] MesUnitMatrixing 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);
+        }
+    }
+}
\ No newline at end of file

--
Gitblit v1.9.3