service/Warehouse/MesDepotSectionsManager.cs
@@ -3,86 +3,90 @@
using NewPdaSqlServer.entity;
using SqlSugar;
namespace NewPdaSqlServer.service.Warehouse
namespace NewPdaSqlServer.service.Warehouse;
public class MesDepotSectionsManager : Repository<MesDepotSections>
{
    public class MesDepotSectionsManager : Repository<MesDepotSections>
    public dynamic GetSectionName(WarehouseQuery query)
    {
        public string GetSectionName(WarehouseQuery query)
        {
            var sectionName = Db.Queryable<MesDepotSections, MesDepots>(
                    (a, b) =>
                        new JoinQueryInfos(JoinType.Inner,
                            a.DepotGuid == b.Guid))
                .Where((a, b) => a.DepotSectionCode == query.sectionCode)
                .Select((a, b) => a.DepotSectionName).Single();
            if (sectionName == null)
                throw new Exception("库位编码 " + query.sectionCode + " 不存在,请确认!");
            return sectionName;
        }
        //库位变更
        public MesDepotSections ScanInDepotSectionsName(WarehouseQuery query)
        {
            if (string.IsNullOrEmpty(query.sectionCode))
                throw new Exception("请扫库位条码!");
            var mesDepotSections = Db.Queryable<MesDepotSections, MesDepots>(
                    (a, b) =>
                        new JoinQueryInfos(JoinType.Inner,
                            a.DepotGuid == b.Guid))
                .Where((a, b) => a.DepotSectionCode == query.sectionCode)
                .Select((a, b) => new MesDepotSections
                {
                    DepotSectionName = a.DepotSectionName,
                    DepotCode = b.DepotCode,
                    DepotName = b.DepotName
                }).First();
            if (mesDepotSections == null)
                throw new Exception("库位编码" + query.sectionCode + " 不存在,请确认!");
            return mesDepotSections;
        }
        //库位变更
        public MesInvItemStocks ScanEditBarcode(WarehouseQuery entity)
        {
            if (string.IsNullOrEmpty(entity.sectionCode))
                throw new Exception("请扫库位条码!");
            var mesDepotSections = Db.Queryable<MesDepotSections, MesDepots>(
                    (a, b) =>
                        new JoinQueryInfos(JoinType.Inner,
                            a.Zuid.ToString() == b.Zuid))
                .Where((a, b) => a.DepotSectionCode == entity.sectionCode)
                .First();
            if (mesDepotSections == null)
                throw new Exception("库位编码" + entity.sectionCode + " 不存在,请确认!");
            var mesInvItemStocks = Db.Queryable<MesInvItemStocks>()
                .Where(s => s.ItemBarcode == entity.barcode).First();
            if (mesInvItemStocks == null) throw new Exception("条码在仓库中不存在,请核对!");
            if (mesInvItemStocks.DepotsCode != mesDepotSections.DepotCode)
                throw new Exception(
                    "条码原仓库:" + mesInvItemStocks.DepotsCode + " 与目标仓库:" +
                    mesDepotSections.DepotCode + " 不一致,库位变更失败!");
            UseTransaction(db =>
        var sectionName = Db.Queryable<MesDepotSections, MesDepots>(
                (a, b) =>
                    new JoinQueryInfos(JoinType.Inner,
                        a.DepotGuid == b.Guid))  // 确保Guid类型正确比较
            .Where((a, b) => a.DepotSectionCode == query.sectionCode)
            .Select((a, b) => new  // 改用匿名类型替代dynamic
            {
                return db.Updateable<MesInvItemStocks>()
                    .SetColumns(s => s.DepotsCode == mesDepotSections.DepotCode)
                    .SetColumns(s => s.DepotSectionsCode == entity.sectionCode)
                    .Where(s => s.ItemBarcode == entity.barcode)
                    .ExecuteCommand();
            });
                a.DepotSectionName,
                DepotName = b.DepotName,  // 显式指定字段别名
                DepotCode = b.DepotCode
            }).Single();
            return mesInvItemStocks;
        }
        if (sectionName == null)
            throw new Exception("库位编码 " + query.sectionCode + " 不存在,请确认!");
        //调拨入库
        return sectionName;
    }
    //库位变更
    public MesDepotSections ScanInDepotSectionsName(WarehouseQuery query)
    {
        if (string.IsNullOrEmpty(query.sectionCode))
            throw new Exception("请扫库位条码!");
        var mesDepotSections = Db.Queryable<MesDepotSections, MesDepots>(
                (a, b) =>
                    new JoinQueryInfos(JoinType.Inner,
                        a.DepotGuid == b.Guid))
            .Where((a, b) => a.DepotSectionCode == query.sectionCode)
            .Select((a, b) => new MesDepotSections
            {
                DepotSectionName = a.DepotSectionName,
                DepotCode = b.DepotCode,
                DepotName = b.DepotName
            }).First();
        if (mesDepotSections == null)
            throw new Exception("库位编码" + query.sectionCode + " 不存在,请确认!");
        return mesDepotSections;
    }
    //库位变更
    public MesInvItemStocks ScanEditBarcode(WarehouseQuery entity)
    {
        if (string.IsNullOrEmpty(entity.sectionCode))
            throw new Exception("请扫库位条码!");
        var mesDepotSections = Db.Queryable<MesDepotSections, MesDepots>(
                (a, b) =>
                    new JoinQueryInfos(JoinType.Inner,
                        a.Zuid.ToString() == b.Zuid))
            .Where((a, b) => a.DepotSectionCode == entity.sectionCode)
            .First();
        if (mesDepotSections == null)
            throw new Exception("库位编码" + entity.sectionCode + " 不存在,请确认!");
        var mesInvItemStocks = Db.Queryable<MesInvItemStocks>()
            .Where(s => s.ItemBarcode == entity.barcode).First();
        if (mesInvItemStocks == null) throw new Exception("条码在仓库中不存在,请核对!");
        if (mesInvItemStocks.DepotsCode != mesDepotSections.DepotCode)
            throw new Exception(
                "条码原仓库:" + mesInvItemStocks.DepotsCode + " 与目标仓库:" +
                mesDepotSections.DepotCode + " 不一致,库位变更失败!");
        UseTransaction(db =>
        {
            return db.Updateable<MesInvItemStocks>()
                .SetColumns(s => s.DepotsCode == mesDepotSections.DepotCode)
                .SetColumns(s => s.DepotSectionsCode == entity.sectionCode)
                .Where(s => s.ItemBarcode == entity.barcode)
                .ExecuteCommand();
        });
        return mesInvItemStocks;
    }
    //调拨入库
}