From 91489875fc8c9fd2b44c3791ca6f67dcf559675f Mon Sep 17 00:00:00 2001
From: zyf <1071160500@qq.com>
Date: 星期三, 18 六月 2025 18:52:56 +0800
Subject: [PATCH] 修改入库检更新工单完工条件

---
 MES.Service/service/QC/RKJService.cs |  354 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 338 insertions(+), 16 deletions(-)

diff --git a/MES.Service/service/QC/RKJService.cs b/MES.Service/service/QC/RKJService.cs
index 3adbcee..43f0696 100644
--- a/MES.Service/service/QC/RKJService.cs
+++ b/MES.Service/service/QC/RKJService.cs
@@ -1,4 +1,5 @@
-锘縰sing MES.Service.DB;
+锘縰sing Masuit.Tools.Models;
+using MES.Service.DB;
 using MES.Service.Dto.service;
 using MES.Service.Modes;
 using MES.Service.util;
@@ -57,11 +58,10 @@
     public List<RkDaa002> GetDaa001s(string lineNo)
     {
         var db = SqlSugarHelper.GetInstance();
-        // return db.Queryable<RKJDaa001>()
-        //     .Where(t => t.LineNo == lineNo)
-        //     .OrderBy(t => t.BillNo, OrderByType.Desc)
-        //     .ToList();
-        return null;
+        return db.Queryable<RkDaa002>()
+            .Where(t => lineNo.Contains(t.Daa020))
+            .OrderBy(t => t.BillNo, OrderByType.Desc)
+            .ToList();
     }
 
     //鏍规嵁妫�楠屾爣鍑嗘潵璁$畻妫�楠屼釜鏁�
@@ -314,6 +314,22 @@
 
     public int SetQSItemDetail(QsItemOqcItemDetail detail)
     {
+        var db = SqlSugarHelper.GetInstance();
+
+        var qsItemOqcReq = db.Queryable<QsItemOqcReq>()
+            .Where(o => o.Id == detail.Gid)
+            .First();
+
+        if (qsItemOqcReq == null)
+        {
+            throw new Exception("璇ユ楠屽崟涓嶅瓨鍦紝璇峰洖鍒板叆搴撴鍒楄〃閲嶆柊鏌ョ湅");
+        }
+
+        if (!string.IsNullOrEmpty(qsItemOqcReq.FcheckResu))
+        {
+            throw new Exception("璇ユ楠屽崟宸�" + qsItemOqcReq.FcheckResu + "锛屼笉鍏佽淇敼缁撴灉");
+        }
+
         var oracle = SqlSugarHelper.UseTransactionWithOracle(db =>
         {
             List<QsItemOqcItemDetail> result = new();
@@ -418,7 +434,131 @@
         //     //鑷姩鐢熸垚鍏ュ簱妫�寮傚父瀵圭瓥
         //     saveDetect02(detail.Gid, detail.CreateBy);
 
+        setBarCodes(FcheckResu, detail, db);
+
         return useTransactionWithOracle;
+    }
+
+    /// <summary>
+    ///  褰撴楠屽崟涓嶅悎鏍兼椂锛�
+    /// 鍙栨秷mes_inv_item_barcodes 鐨勬姤宸ユ爣璇� WORK_FLG
+    /// womdaa 宸茬敓浜ч噺 DAA011鍔犲洖鏉$爜鎶ュ伐鏁伴噺
+    /// MES_SCGD_GX 妫�楠屾爣璇� CHECK_FLAG锛�('鈭�','脳')
+    /// 閫氳繃mes_inv_item_in_c_details2鐨凪ES_SCGD_GX_ID杩炴帴
+    /// 
+    /// 20250602鏇存柊 鐜板湪涓嶅悎鏍间笉浼氭洿鏂板伐鍗曠敓浜ф暟閲忥紝鍚堟牸浼氬鍔犲搴斿伐鍗曠敓浜ф暟閲�
+    ///</summary>
+    private void setBarCodes(string? FcheckResu, QsItemOqcItemDetail detail,
+        SqlSugarClient db)
+    {
+        var oqcReqList = db.Queryable<QsItemOqcReq, MesInvItemIns, Womdaa>(
+                (a, b, c) =>
+                    new JoinQueryInfos(
+                        JoinType.Left, a.BillNo == b.BillNo,
+                        JoinType.Left, c.Daa001 == b.RbillNo
+                    ))
+            .Where(a => a.Id == detail.Gid)
+            .Select((a, b, c) =>
+                new
+                {
+                    a.Id, a.BillNo, c.Daa001
+                })
+            .ToList();
+
+        if (oqcReqList.Count <= 0)
+        {
+            return;
+        }
+
+        var qsItemOqcReq = oqcReqList.First(); // 鑾峰彇绗竴涓厓绱�  
+
+        if (qsItemOqcReq.Id is null or 0) return;
+
+        var mesInvItemInsList = db
+            .Queryable<MesInvItemIns, MesInvItemInCDetails2>((a, b) =>
+                new JoinQueryInfos(
+                    JoinType.Left, a.Id == b.ItemInId))
+            .Where((a, b) => a.BillNo == qsItemOqcReq.BillNo)
+            .Select((a, b) => new
+                { b.ItemBarcode, b.Quantity, b.MesScgdGxId })
+            .ToList();
+
+        var barCodes = mesInvItemInsList.Select(s => s.ItemBarcode).ToList();
+
+        var count = db.Queryable<MesInvItemBarcodes>()
+            .Where(s => barCodes.Contains(s.ItemBarcode))
+            .Count();
+
+        var gxId = mesInvItemInsList.Select(s => s.MesScgdGxId).ToList();
+
+        var sumQty = mesInvItemInsList.Sum(s => s.Quantity);
+
+        if ("鍚堟牸".Equals(FcheckResu))
+        {
+            //鏍规嵁宸ュ簭id鏇存柊宸ュ簭琛ㄧ殑妫�楠岀粨鏋�
+            db.Updateable<MesScgdGx>()
+                .SetColumns(s => s.CheckFlag == "鈭�")
+                .Where(s => gxId.Contains(s.Id))
+                .ExecuteCommand();
+
+            if (count <= 0) return;
+            
+            //鏇存柊mes_inv_item_barcodes 鐨勬姤宸ユ爣璇� WORK_FLG 浣跨敤鏉$爜
+            var executeCommand = db.Updateable<MesInvItemBarcodes>()
+                .SetColumns(a => a.WorkFlg == 1)
+                .Where(a => barCodes.Contains(a.ItemBarcode))
+                .ExecuteCommand();
+
+            if (executeCommand > 0)
+            {
+                //womdaa 宸茬敓浜ч噺 DAA011鍔犱笂鏉$爜鎶ュ伐鏁伴噺
+                db.Updateable<Womdaa>()
+                    .SetColumns(b => b.Daa011 == SqlFunc.IsNull(b.Daa011, 0) + sumQty)
+                    .Where(b => b.Daa001 == qsItemOqcReq.Daa001)
+                    .ExecuteCommand();
+
+                //鍒ゆ柇鏄惁瀹屽伐
+                var complete = db.Queryable<Womdaa>()
+                            .Where(a => a.Daa008 == a.Daa011 && a.Daa001== qsItemOqcReq.Daa001)
+                            .Count();
+
+                var WqtQty = db.Queryable<Womdab>()
+                            .Where(a => a.Dab001 == qsItemOqcReq.Daa001)
+                            .Sum(a => a.Dab006- SqlFunc.IsNull(a.Dab007, 0));
+
+                if (complete > 0 && WqtQty == 0)  //榻愬涓斿伐鍗曠敓浜ф暟閲�=宸ュ崟鏁伴噺锛屽伐鍗曟洿鏀逛负瀹屽伐
+                {
+                    db.Updateable<Womdaa>()
+                    .SetColumns(b => b.Daa018 == "瀹屽伐")
+                    .SetColumns(b => b.Daa017 == DateTime.Now)
+                    .Where(b => b.Daa001 == qsItemOqcReq.Daa001)
+                    .ExecuteCommand();
+                }
+            }
+        }
+        else if ("涓嶅悎鏍�".Equals(FcheckResu))
+        {
+            //鏍规嵁宸ュ簭id鏇存柊宸ュ簭琛ㄧ殑妫�楠岀粨鏋�
+            db.Updateable<MesScgdGx>()
+                .SetColumns(s => s.CheckFlag == "脳")
+                .Where(s => gxId.Contains(s.Id))
+                .ExecuteCommand();
+
+            //鍙栨秷mes_inv_item_barcodes 鐨勬姤宸ユ爣璇� WORK_FLG 浣跨敤鏉$爜
+            var executeCommand = db.Updateable<MesInvItemBarcodes>()
+                .SetColumns(a => a.WorkFlg == 0)
+                .Where(a => barCodes.Contains(a.ItemBarcode))
+                .ExecuteCommand();
+
+            /*if (executeCommand > 0)
+            {
+                //womdaa 宸茬敓浜ч噺 DAA011鎵i櫎鏉$爜鎶ュ伐鏁伴噺
+                db.Updateable<Womdaa>()
+                    .SetColumns(b => b.Daa011 == b.Daa011 - sumQty)
+                    .Where(b => b.Daa001 == qsItemOqcReq.Daa001)
+                    .ExecuteCommand();
+            }*/
+        }
     }
 
     public int saveDetect02(decimal? gid, string? createBy)
@@ -480,8 +620,8 @@
 
         var qsItemOqcReqs = db
             .Queryable<QsItemOqcReq, MesItems, MesInvItemIns, MesInvTransaction,
-                RKJDaa001, Womdaa>(
-                (a, b, c, ca, m, da) => new JoinQueryInfos(
+                RKJDaa001, Womdaa, VLineUser,MesDeptUser, Womcaa, ExZzCaaFiled>(
+                (a, b, c, ca, m, da, z,d, caa,zf) => new JoinQueryInfos(
                     JoinType.Left, a.ItemNo == b.ItemNo,
                     JoinType.Left, a.BillNo == c.BillNo,
                     JoinType.Left,
@@ -489,24 +629,35 @@
                     c.Company == ca.Company &&
                     c.Factory == ca.Factory,
                     JoinType.Left, c.Id == m.ItemInId && a.ItemNo == m.ItemNo,
-                    JoinType.Left, da.Daa001 == c.RbillNo
+                    JoinType.Left, da.Daa001 == c.RbillNo,
+                    JoinType.Left, da.Daa015 == z.LineNo,
+                    JoinType.Left,c.Bgr == d.UserNo,
+                    JoinType.Left, da.Daa021 == caa.Caa020,
+                    JoinType.Left, caa.Erpid == zf.Erpid
                 ))
             // .WhereIF(lineNo != null && lineNo.Length > 0,
             //     (a, b, c, ca, m, da) => lineNo.Contains(da.Daa015))
-            .Where((a, b, c, ca, m, da) => lineNo.Contains(da.Daa015))
+            .WhereIF(!"PL017".Equals(queryObj.createUser),
+                (a, b, c, ca, m, da, z, d, caa, zf) => lineNo.Contains(da.Daa015))
             .WhereIF(!string.IsNullOrEmpty(queryObj.id),
-                (a, b, c, ca, m, da) => a.Id.ToString() == queryObj.id)
+                (a, b, c, ca, m, da, z, d, caa, zf) => a.Id.ToString() == queryObj.id)
             .WhereIF(
                 StringUtil.IsNotNullOrEmpty(queryObj.result) &&
                 "鏈畬鎴�".Equals(queryObj.result),
-                (a, b, c, ca, m, da) => a.FcheckResu == null)
+                (a, b, c, ca, m, da, z, d, caa, zf) => a.FcheckResu == null)
             .WhereIF(
                 StringUtil.IsNotNullOrEmpty(queryObj.result) &&
                 !"鏈畬鎴�".Equals(queryObj.result),
-                (a, b, c, ca, m, da) => a.FcheckResu != null)
-            .Select((a, b, c, ca, m, da) => new QsItemOqcReq
+                (a, b, c, ca, m, da, z, d, caa, zf) => a.FcheckResu != null)
+            //鍔犵瓫閫夋潯浠�,鏍规嵁渚涘簲鍟嗭紝鐗╂枡缂栫爜锛岀墿鏂欏悕绉版悳绱�
+            //.WhereIF(queryObj.SearchValue!=null && queryObj.SearchValue!="", (a) => a.SuppName == queryObj.SearchValue|| a.ItemName == queryObj.SearchValue || a.ItemNo == queryObj.SearchValue )
+            .WhereIF(queryObj.SearchValue != null && queryObj.SearchValue != "",
+            (a, b, c, ca, m, da, z, d, caa, zf) => b.ItemName.ToLower().Contains(queryObj.SearchValue.ToLower())
+            || a.ItemNo.ToLower().Contains(queryObj.SearchValue.ToLower()))
+            .Select((a, b, c, ca, m, da, z, d, caa, zf) => new QsItemOqcReq
             {
                 BillNo = a.BillNo,
+                Fsubmit = a.Fsubmit,
                 Remarks = a.Remarks,
                 Id = a.Id,
                 CreateDate = a.CreateDate,
@@ -522,7 +673,10 @@
                 ItemInId = m.ItemInId,
                 Daa015 = da.Daa015,
                 LineNo = c.LineNo,
-                Quantity = m.Quantity
+                Quantity = m.Quantity,
+                Caa015 = caa.Caa015,
+                cust = da.Cust == null ? zf.Cust : da.Cust,
+                Bgr = d.UserName
             })
             .OrderBy(a => a.CreateDate, OrderByType.Desc)
             .ToPageList(queryObj.PageIndex, queryObj.Limit, ref totalCount);
@@ -592,6 +746,22 @@
     {
         if (isRk(detail.ItemInId)) throw new Exception("宸叉湁鏉$爜鍏ュ簱锛屼笉鍏佽淇敼妫�楠岀粨鏋�");
 
+        var db = SqlSugarHelper.GetInstance();
+
+        var qsItemOqcReq = db.Queryable<QsItemOqcReq>()
+            .Where(o => o.Id == detail.Gid)
+            .First();
+
+        if (qsItemOqcReq == null)
+        {
+            throw new Exception("璇ユ楠屽崟涓嶅瓨鍦紝璇峰洖鍒板叆搴撴鍒楄〃閲嶆柊鏌ョ湅");
+        }
+
+        if (!string.IsNullOrEmpty(qsItemOqcReq.FcheckResu))
+        {
+            throw new Exception("璇ユ楠屽崟宸�" + qsItemOqcReq.FcheckResu + "锛屼笉鍏佽淇敼缁撴灉");
+        }
+
         var withOracle = SqlSugarHelper.UseTransactionWithOracle(db =>
         {
             return db.Updateable<QsItemOqcItemDetail>()
@@ -660,7 +830,7 @@
         });
     }
 
-    public int saveItem(RKJDto rkjDto)
+    public int saveItem(RKJDto rkjDto) //鑷姩鐢熸垚鍚堟牸鐨勬楠岄」
     {
         var items = rkjDto.items;
         var userNo = rkjDto.userNo;
@@ -688,6 +858,158 @@
             SetQSItemDetail(detail);
         });
 
+        //鍥犱负榛樿鍚堟牸锛屽湪杩欓噷澧炲姞宸ュ崟鐢熶骇鏁伴噺
+        var db = SqlSugarHelper.GetInstance();
+        var oqcReqList = db.Queryable<QsItemOqcReq, MesInvItemIns, Womdaa>(
+               (a, b, c) =>
+                   new JoinQueryInfos(
+                       JoinType.Left, a.BillNo == b.BillNo,
+                       JoinType.Left, c.Daa001 == b.RbillNo
+               ))
+           .Where(a => a.Id == rkjDto.gid)
+           .Select((a, b, c) =>
+               new
+               {
+                   a.Id,
+                   a.BillNo,
+                   c.Daa001
+               })
+           .First();
+
+        if (oqcReqList.Id is null or 0) return 0;
+
+        //鏌ヨ妫�楠屾暟閲�
+        var sumQty = db
+            .Queryable<MesInvItemIns, MesInvItemInCDetails2>((a, b) =>
+                new JoinQueryInfos(
+                    JoinType.Left, a.Id == b.ItemInId))
+            .Where((a, b) => a.BillNo == oqcReqList.BillNo)
+            .Select((a, b) => new {b.Quantity })
+            .MergeTable().Sum( a=> a.Quantity);
+
+        /*
+        //鏇存柊鐢熶骇閲�
+        db.Updateable<Womdaa>()
+                    .SetColumns(b => b.Daa011 == SqlFunc.IsNull(b.Daa011, 0) + sumQty)
+                    .Where(b => b.Daa001 == oqcReqList.Daa001)
+                    .ExecuteCommand();
+
+        //鍒ゆ柇鏄惁瀹屽伐
+        var complete = db.Queryable<Womdaa>()
+                    .Where(a => a.Daa008 == a.Daa011 && a.Daa001 == oqcReqList.Daa001)
+                    .Count();
+
+        if (complete > 0)
+        {
+            db.Updateable<Womdaa>()
+            .SetColumns(b => b.Daa018 == "瀹屽伐")
+            .Where(b => b.Daa001 == oqcReqList.Daa001)
+            .ExecuteCommand();
+        }
+
+        */
+
         return Convert.ToInt32(rkjDto.gid);
     }
+
+    public int cleanReqResult(RKJDto dto) //娓呴櫎妫�楠岀粨鏋�
+    {
+        if (isRk(dto.gid)) throw new Exception("宸叉湁鏉$爜鍏ュ簱锛屼笉鍏佽娓呴櫎妫�楠岀粨鏋�");
+
+        var db = SqlSugarHelper.GetInstance();
+        //鏌ヨ妫�楠岀粨鏋�
+        var oqcReqList = db.Queryable<QsItemOqcReq, MesInvItemIns, Womdaa>(
+               (a, b, c) =>
+                   new JoinQueryInfos(
+                       JoinType.Left, a.BillNo == b.BillNo,
+                       JoinType.Left, c.Daa001 == b.RbillNo
+               ))
+           .Where(a => a.Id == dto.gid)
+           .Select((a, b, c) =>
+               new
+               {
+                   a.FcheckResu,
+                   a.Id,
+                   a.BillNo,
+                   c.Daa001
+               })
+           .First();
+
+        if (oqcReqList.Id is null or 0) return 0;
+
+        var FcheckResu = oqcReqList.FcheckResu; //妫�楠岀粨鏋�
+
+        //鏌ヨ妫�楠屾暟閲�
+        var mesInvItemInsList = db
+            .Queryable<MesInvItemIns, MesInvItemInCDetails2>((a, b) =>
+                new JoinQueryInfos(
+                    JoinType.Left, a.Id == b.ItemInId))
+            .Where((a, b) => a.BillNo == oqcReqList.BillNo)
+            .Select((a, b) => new
+            { b.ItemBarcode, b.Quantity, b.MesScgdGxId })
+            .ToList();
+
+        var barCodes = mesInvItemInsList.Select(s => s.ItemBarcode).ToList();
+
+        var count = db.Queryable<MesInvItemBarcodes>()
+            .Where(s => barCodes.Contains(s.ItemBarcode))
+            .Count();
+
+        var gxId = mesInvItemInsList.Select(s => s.MesScgdGxId).ToList();
+
+        var sumQty = mesInvItemInsList.Sum(s => s.Quantity);
+
+
+        //鏍规嵁宸ュ簭id鏇存柊宸ュ簭琛ㄧ殑妫�楠岀粨鏋�
+        db.Updateable<MesScgdGx>()
+            .SetColumns(s => s.CheckFlag == "鏈楠�")
+            .Where(s => gxId.Contains(s.Id))
+            .ExecuteCommand();
+
+        if (count <= 0) return 0;
+
+        //鏇存柊mes_inv_item_barcodes 鐨勬姤宸ユ爣璇� WORK_FLG 涓哄凡鎶ュ伐锛堜富瑕佹槸閽堝涓嶅悎鏍肩殑妫�楠岀粨鏋滐級
+        var executeCommand = db.Updateable<MesInvItemBarcodes>()
+            .SetColumns(a => a.WorkFlg == 1)
+            .Where(a => barCodes.Contains(a.ItemBarcode))
+            .ExecuteCommand();
+
+
+        if ("鍚堟牸".Equals(FcheckResu)) //妫�楠屽悎鏍肩殑鍒欏洖閫�鐢熶骇鏁伴噺
+        {
+            //womdaa 宸茬敓浜ч噺 DAA011鎵i櫎鏉$爜鎶ュ伐鏁伴噺
+            db.Updateable<Womdaa>()
+                .SetColumns(b => b.Daa011 == SqlFunc.IsNull(b.Daa011, 0) - sumQty)
+                .Where(b => b.Daa001 == oqcReqList.Daa001)
+                .ExecuteCommand();
+
+            //宸ュ崟鐘舵�佹洿鏀逛负寮�宸�
+            db.Updateable<Womdaa>()
+                .SetColumns(b => b.Daa018 == "寮�宸�")
+                .SetColumns(b => b.Daa017 == null)
+                .Where(b => b.Daa001 == oqcReqList.Daa001)
+                .ExecuteCommand();
+        }
+
+
+        return SqlSugarHelper.UseTransactionWithOracle(db =>
+        {
+            return db.Updateable<QsItemOqcReq>()
+                .SetColumns(s => s.FcheckResu == null)
+                .Where(s => s.Id == dto.gid).ExecuteCommand();
+        });
+    }
+
+    private bool isRk(int? gid)
+    {
+        var db = SqlSugarHelper.GetInstance();
+        return db.Queryable<QsItemOqcReq, MesInvItemIns, MesInvItemInCDetails2,MesInvItemStocks>(
+                (a, b, c,d) => new JoinQueryInfos(
+                    JoinType.Inner, a.BillNo == b.BillNo,
+                    JoinType.Inner, b.Id == c.ItemInId,
+                    JoinType.Inner,c.ItemBarcode == d.ItemBarcode
+                ))
+            .Where((a, b, c,d) => a.Id == gid)
+            .Count() > 0;
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.3