From 4f57c3e602eef9ec929a2315183fa28b21cd0987 Mon Sep 17 00:00:00 2001
From: yl <ykxkd@outlook.com>
Date: 星期三, 29 三月 2023 10:51:33 +0800
Subject: [PATCH] 订单下达获取工单语句修改、生产工单手工创建获取单据号接口

---
 VueWebApi/DLL/DAL/ProductionManagementDAL.cs | 2652 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 2,508 insertions(+), 144 deletions(-)

diff --git a/VueWebApi/DLL/DAL/ProductionManagementDAL.cs b/VueWebApi/DLL/DAL/ProductionManagementDAL.cs
index fef2858..745bc95 100644
--- a/VueWebApi/DLL/DAL/ProductionManagementDAL.cs
+++ b/VueWebApi/DLL/DAL/ProductionManagementDAL.cs
@@ -1,10 +1,12 @@
 锘縰sing Dapper;
+using Newtonsoft.Json.Linq;
 using System;
 using System.Collections.Generic;
 using System.Data;
 using System.Data.SqlClient;
 using System.Linq;
 using System.Web;
+using VueWebApi.Models;
 using VueWebApi.Tools;
 
 namespace VueWebApi.DLL.DAL
@@ -20,7 +22,7 @@
 
 
         #region[ERP璁㈠崟鏌ヨ]
-        public static ToMessage ErpOrderSearch(string erporderstus, string erpordercode, string partcode, string partname, string partspec, int startNum, string paydatestartdate, string paydateenddate, string creatuser, string createstartdate, string createenddate, int endNum, string prop, string order)
+        public static ToMessage ErpOrderSearch(string erporderstus, string erpordercode, string partcode, string partname, string partspec, int startNum, string paydatestartdate, string paydateenddate, string paydatestartdate1, string paydateenddate2, string creatuser, int endNum, string prop, string order)
         {
             var dynamicParams = new DynamicParameters();
             string search = "";
@@ -53,19 +55,19 @@
                 }
                 if (paydatestartdate != "" && paydatestartdate != null)
                 {
-                    search += "and A.paydate between @paydatestartdate and @paydateenddate ";
-                    dynamicParams.Add("@paydatestartdate", paydatestartdate+" 00:00:00");
+                    search += "and A.planstartdate between @paydatestartdate and @paydateenddate ";
+                    dynamicParams.Add("@paydatestartdate", paydatestartdate + " 00:00:00");
                     dynamicParams.Add("@paydateenddate", paydateenddate + " 23:59:59");
                 }
-                if (createstartdate != "" && createstartdate != null)
+                if (paydatestartdate1 != "" && paydatestartdate1 != null)
                 {
-                    search += "and A.createdate between @createstartdate and @createenddate ";
-                    dynamicParams.Add("@createstartdate", createstartdate);
-                    dynamicParams.Add("@createenddate", createenddate + " 23:59:59");
+                    search += "and A.planenddate between @paydatestartdate1 and @paydateenddate2 ";
+                    dynamicParams.Add("@paydatestartdate1", paydatestartdate1);
+                    dynamicParams.Add("@paydateenddate2", paydateenddate2 + " 23:59:59");
                 }
                 if (creatuser != "" && creatuser != null)
                 {
-                    search += "and A.createuser like '%'+@creatuser+'%' ";
+                    search += "and U.username like '%'+@creatuser+'%' ";
                     dynamicParams.Add("@creatuser", creatuser);
                 }
 
@@ -75,12 +77,14 @@
                 }
                 // --------------鏌ヨ鎸囧畾鏁版嵁--------------
                 var total = 0; //鎬绘潯鏁�
-                var sql = @"select A.status,A.wo,A.materiel_code as partcode,B.partname,B.partspec,A.qty,A.relse_qty,A.wkshp_code,C.org_name as wkshp_name,
-                            A.stck_code,D.name as stck_name,A.paydate,A.createuser,A.createdate 
+                var sql = @"select A.id, A.status,A.wo,A.materiel_code as partcode,B.partname,B.partspec,A.qty,A.relse_qty,A.wkshp_code,C.org_name as wkshp_name,
+                            A.stck_code,D.name as stck_name,saleOrderDeliveryDate,A.planstartdate,A.planenddate,U.username as createuser,A.createdate 
                             from TKimp_Ewo A
                             left join TMateriel_Info B on A.materiel_code=B.partcode
                             left join TOrganization C on A.wkshp_code=C.org_code
-                            left join T_Sec_Stck D on A.stck_code=D.code where A.is_delete<>'1' " + search;
+                            left join T_Sec_Stck D on A.stck_code=D.code 
+                            left join TUser U on A.createuser=U.usercode 
+                            where A.is_delete<>'1' " + search;
                 var data = DapperHelper.GetPageList<object>(sql, dynamicParams, prop, order, startNum, endNum, out total);
                 mes.code = "200";
                 mes.Message = "鏌ヨ鎴愬姛!";
@@ -99,11 +103,12 @@
         #endregion
 
         #region[ERP璁㈠崟涓嬭揪]
-        public static ToMessage MarkSaveErpOrder(string erpordercode, string partcode, string wkshopcode, string warehousecode, string erpqty, string markqty, string ordernum, string relse_qty, string username)
+        public static ToMessage MarkSaveErpOrder(string erporderid, string erpordercode, string partcode, string wkshopcode, string warehousecode, string erpqty, string markqty, string ordernum, string relse_qty, string saleOrderDeliveryDate, string username)
         {
             var sql = "";
             List<object> list = new List<object>();
             var dynamicParams = new DynamicParameters();
+
             try
             {
                 list.Clear();
@@ -122,7 +127,8 @@
                     //鑾峰彇鏈�澶у崟鎹彿
                     if (i == 1)  //棣栧崟鑾峰彇宸ュ崟鍙�
                     {
-                        sql = @"select isnull(max(substring(wo_code,charindex('_',wo_code)+1,len(wo_code)-charindex('_',wo_code))),0)+1 as worknumb from TK_Wrk_Man where m_po=@erpordercode";
+                        sql = @"select isnull(max(cast(substring(wo_code,charindex('_',wo_code)+1,len(wo_code)-charindex('_',wo_code)) as numeric)),0)+1 as worknumb   
+                                from TK_Wrk_Man where m_po=@erpordercode";
                         dynamicParams.Add("@erpordercode", erpordercode);
                         var data = DapperHelper.selectdata(sql, dynamicParams);
                         num = Convert.ToInt32(data.Rows[0]["WORKNUMB"].ToString());
@@ -135,21 +141,24 @@
                     }
                     if (i == Convert.ToInt32(ordernum))  //鏈�鍚庝竴鍗曟椂
                     {
-                        sql = @"insert into TK_Wrk_Man(wo_code,status,wkshp_code,plan_qty,stck_code,materiel_code,m_po,lm_user,lm_date) values(@wo_code,@status,@wkshp_code,@plan_qty,@stck_code,@materiel_code,@m_po,@username,@CreateDate)";
+                        sql = @"insert into TK_Wrk_Man(wo_code,wotype,status,wkshp_code,plan_qty,stck_code,materiel_code,sourceid,m_po,lm_user,lm_date,saleOrderDeliveryDate) values(@wo_code,@wotype,@status,@wkshp_code,@plan_qty,@stck_code,@materiel_code,@sourceid,@m_po,@username,@CreateDate,@saleOrderDeliveryDate)";
                         list.Add(new
                         {
                             str = sql,
                             parm = new
                             {
                                 wo_code = wo,
+                                wotype = "PO",
                                 status = "NEW",
                                 wkshp_code = wkshopcode,
                                 plan_qty = cdqty + (decimal.Parse(markqty) - sumqty),  //鏈崟涓嬪崟鏁伴噺=鍒囧垎鏁伴噺+(涓嬪崟鏁伴噺-绱鍒囧垎涓嬪崟鏁伴噺)
                                 stck_code = warehousecode,
                                 materiel_code = partcode,
+                                sourceid = erporderid,
                                 m_po = erpordercode,
                                 username = username,
-                                CreateDate = DateTime.Now.ToString()
+                                CreateDate = DateTime.Now.ToString(),
+                                saleOrderDeliveryDate = Convert.ToDateTime(saleOrderDeliveryDate)
                             }
                         });
                         sumqty = sumqty + (decimal.Parse(markqty) - sumqty);
@@ -157,48 +166,55 @@
                     else
                     {
 
-                        sql = @"insert into TK_Wrk_Man(wo_code,status,wkshp_code,plan_qty,stck_code,materiel_code,m_po,lm_user,lm_date) values(@wo_code,@status,@wkshp_code,@plan_qty,@stck_code,@materiel_code,@m_po,@username,@CreateDate)";
+                        sql = @"insert into TK_Wrk_Man(wo_code,wotype,status,wkshp_code,plan_qty,stck_code,materiel_code,sourceid,m_po,lm_user,lm_date,saleOrderDeliveryDate) values(@wo_code,@wotype,@status,@wkshp_code,@plan_qty,@stck_code,@materiel_code,@sourceid,@m_po,@username,@CreateDate,@saleOrderDeliveryDate)";
                         list.Add(new
                         {
                             str = sql,
                             parm = new
                             {
                                 wo_code = wo,
+                                wotype = "PO",
                                 status = "NEW",
                                 wkshp_code = wkshopcode,
                                 plan_qty = cdqty,
                                 stck_code = warehousecode,
                                 materiel_code = partcode,
+                                sourceid = erporderid,
                                 m_po = erpordercode,
                                 username = username,
-                                CreateDate = DateTime.Now.ToString()
+                                CreateDate = DateTime.Now.ToString(),
+                                saleOrderDeliveryDate = Convert.ToDateTime(saleOrderDeliveryDate)
                             }
                         });
                     }
                 }
                 if (decimal.Parse(erpqty) == decimal.Parse(markqty) + decimal.Parse(relse_qty))   //濡傛灉ERP璁㈠崟=涓嬪崟鏁伴噺+宸蹭笅鍗曟暟閲�,鍒欐洿鏂癊RP璁㈠崟琛ㄧ姸鎬佷负CREATED:宸插垱寤�
                 {
-                    sql = @"update  TKimp_Ewo set status='CREATED',relse_qty=relse_qty+@sumqty where wo=@wo";
+                    sql = @"update  TKimp_Ewo set status='CREATED',saleOrderDeliveryDate=@saleOrderDeliveryDate,relse_qty=relse_qty+@sumqty where wo=@wo and id=@erporderid";
                     list.Add(new
                     {
                         str = sql,
                         parm = new
                         {
                             wo = erpordercode,
-                            sumqty = sumqty
+                            erporderid = erporderid,
+                            sumqty = sumqty,
+                            saleOrderDeliveryDate = Convert.ToDateTime(saleOrderDeliveryDate)
                         }
                     });
                 }
                 else   //鏇存柊ERP璁㈠崟琛ㄧ姸鎬佷负CREATING:鍒涘缓涓�
                 {
-                    sql = @"update  TKimp_Ewo set status='CREATING',relse_qty=relse_qty+@sumqty where wo=@wo";
+                    sql = @"update  TKimp_Ewo set status='CREATING',saleOrderDeliveryDate=@saleOrderDeliveryDate,relse_qty=relse_qty+@sumqty where wo=@wo and id=@erporderid";
                     list.Add(new
                     {
                         str = sql,
                         parm = new
                         {
                             wo = erpordercode,
-                            sumqty = sumqty
+                            erporderid = erporderid,
+                            sumqty = sumqty,
+                            saleOrderDeliveryDate = Convert.ToDateTime(saleOrderDeliveryDate)
                         }
                     });
                 }
@@ -230,7 +246,7 @@
         #endregion
 
         #region[ERP璁㈠崟鍏抽棴]
-        public static ToMessage ClosedErpOrder(string erpordercode, string username)
+        public static ToMessage ClosedErpOrder(string erporderid, string erpordercode, string username)
         {
             var sql = "";
             List<object> list = new List<object>();
@@ -250,13 +266,14 @@
                 else
                 {
                     //鍏抽棴璁㈠崟
-                    sql = @"update  TKimp_Ewo set status='CLOSED' where wo=@wo";
+                    sql = @"update  TKimp_Ewo set status='CLOSED' where wo=@wo and id=@erporderid";
                     list.Add(new
                     {
                         str = sql,
                         parm = new
                         {
-                            wo = erpordercode
+                            wo = erpordercode,
+                            erporderid = erporderid
                         }
                     });
                 }
@@ -290,7 +307,7 @@
 
 
         #region[MES宸ュ崟鏌ヨ]
-        public static ToMessage ErpOrderSearch(string mesorderstus, string mesordercode, string partcode, string partname, string partspec, int startNum, string creatuser, string createdate, int endNum, string prop, string order)
+        public static ToMessage MesOrderSearch(string mesorderstus, string mesordercode, string sourceorder, string ordertype, string partcode, string partname, string partspec, int startNum, string creatuser, string createdate, int endNum, string prop, string order)
         {
             var dynamicParams = new DynamicParameters();
             string search = "";
@@ -305,6 +322,16 @@
                 {
                     search += "and A.wo_code like '%'+@mesordercode+'%' ";
                     dynamicParams.Add("@mesordercode", mesordercode);
+                }
+                if (sourceorder != "" && sourceorder != null)
+                {
+                    search += "and A.m_po like '%'+@sourceorder+'%' ";
+                    dynamicParams.Add("@sourceorder", sourceorder);
+                }
+                if (ordertype != "" && ordertype != null)
+                {
+                    search += "and A.wotype like '%'+@ordertype+'%' ";
+                    dynamicParams.Add("@ordertype", ordertype);
                 }
                 if (partcode != "" && partcode != null)
                 {
@@ -328,7 +355,7 @@
                 }
                 if (creatuser != "" && creatuser != null)
                 {
-                    search += "and A.lm_user like '%'+@creatuser+'%' ";
+                    search += "and U.username like '%'+@creatuser+'%' ";
                     dynamicParams.Add("@creatuser", creatuser);
                 }
 
@@ -338,14 +365,93 @@
                 }
                 // --------------鏌ヨ鎸囧畾鏁版嵁--------------
                 var total = 0; //鎬绘潯鏁�
-                var sql = @"select A.status,A.wo_code,A.materiel_code as partcode,B.partname,B.partspec,A.plan_qty,A.wkshp_code,C.org_name as wkshp_name,
-                            A.route_code,E.name as route_name,A.stck_code,F.name as stck_name,A.plan_startdate,A.plan_enddate,A.piroque,A.m_po,A.lm_user,A.lm_date
+                var sql = @"select A.id, A.status,A.wotype,A.wo_code,A.materiel_code as partcode,B.partname,B.partspec,A.plan_qty,A.wkshp_code,C.org_name as wkshp_name,
+                            A.route_code,E.name as route_name,A.stck_code,F.name as stck_name,A.plan_startdate,A.plan_enddate,A.piroque,A.sourceid,A.m_po,U.username as lm_user,A.lm_date
                             from TK_Wrk_Man A
                             left join TMateriel_Info B on A.materiel_code=B.partcode
                             left join TOrganization C on A.wkshp_code=C.org_code
                             left join T_Sec_Stck D on A.stck_code=D.code 
                             left join TFlw_Rout E on A.route_code=E.code
-                            left join T_Sec_Stck F on A.stck_code=F.code where A.is_delete<>'1' " + search;
+                            left join T_Sec_Stck F on A.stck_code=F.code 
+                            left join TUser U on A.lm_user=U.usercode 
+                            where A.is_delete<>'1' " + search;
+                var data = DapperHelper.GetPageList<object>(sql, dynamicParams, prop, order, startNum, endNum, out total);
+                mes.code = "200";
+                mes.Message = "鏌ヨ鎴愬姛!";
+                mes.count = total;
+                mes.data = data.ToList();
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return mes;
+        }
+        #endregion
+
+        #region[MES鎶ュ簾琛ュ崟宸ュ崟鏌ヨ]
+        public static ToMessage MesBadOrderSearch(string mesordercode, string sourceorder, string partcode, string partname, string partspec, int startNum, string creatuser, string createdate, int endNum, string prop, string order)
+        {
+            var dynamicParams = new DynamicParameters();
+            string search = "";
+            try
+            {
+                if (mesordercode != "" && mesordercode != null)
+                {
+                    search += "and A.wo_code like '%'+@mesordercode+'%' ";
+                    dynamicParams.Add("@mesordercode", mesordercode);
+                }
+                if (sourceorder != "" && sourceorder != null)
+                {
+                    search += "and A.m_po like '%'+@sourceorder+'%' ";
+                    dynamicParams.Add("@sourceorder", sourceorder);
+                }
+                if (partcode != "" && partcode != null)
+                {
+                    search += "and A.materiel_code like '%'+@partcode+'%' ";
+                    dynamicParams.Add("@partcode", partcode);
+                }
+                if (partname != "" && partname != null)
+                {
+                    search += "and B.partname like '%'+@partname+'%' ";
+                    dynamicParams.Add("@partname", partname);
+                }
+                if (partspec != "" && partspec != null)
+                {
+                    search += "and B.partspec like '%'+@partspec+'%' ";
+                    dynamicParams.Add("@partspec", partspec);
+                }
+                if (createdate != "" && createdate != null)
+                {
+                    search += "and CONVERT(varchar(100),A.lm_date,23)=@createdate ";
+                    dynamicParams.Add("@createdate", createdate);
+                }
+                if (creatuser != "" && creatuser != null)
+                {
+                    search += "and U.username like '%'+@creatuser+'%' ";
+                    dynamicParams.Add("@creatuser", creatuser);
+                }
+
+                if (search == "")
+                {
+                    search = "and 1=1 ";
+                }
+                // --------------鏌ヨ鎸囧畾鏁版嵁--------------
+                var total = 0; //鎬绘潯鏁�
+                var sql = @"select A.id, A.status,A.wotype,A.wo_code,A.materiel_code as partcode,B.partname,B.partspec,A.plan_qty,A.wkshp_code,C.org_name as wkshp_name,
+                            A.route_code,E.name as route_name,A.stck_code,F.name as stck_name,A.plan_startdate,A.plan_enddate,A.piroque,A.sourceid,A.m_po,U.username as lm_user,A.lm_date,S.bad_qty
+                            from TK_Wrk_Man A
+                            left join (select wo_code,sum(bad_qty) as bad_qty from  TK_Wrk_Step where bad_qty>0 group by wo_code) S on A.wo_code=S.wo_code
+                            left join TMateriel_Info B on A.materiel_code=B.partcode
+                            left join TOrganization C on A.wkshp_code=C.org_code
+                            left join T_Sec_Stck D on A.stck_code=D.code 
+                            left join TFlw_Rout E on A.route_code=E.code
+                            left join T_Sec_Stck F on A.stck_code=F.code 
+                            left join TUser U on A.lm_user=U.usercode 
+                            where A.is_delete<>'1'  and A.status='START' and A.wotype='PO' and S.bad_qty>0 " + search;
                 var data = DapperHelper.GetPageList<object>(sql, dynamicParams, prop, order, startNum, endNum, out total);
                 mes.code = "200";
                 mes.Message = "鏌ヨ鎴愬姛!";
@@ -371,7 +477,7 @@
             try
             {
                 //鑾峰彇杞﹂棿涓嬫媺妗嗘暟鎹�
-                sql = @"select R.code,R.name  from TMateriel_Info A
+                sql = @"select R.code,R.name,A.default_route  from TMateriel_Info A
                         inner join  TMateriel_Route M on A.partcode=M.materiel_code
                         inner join  TFlw_Rout R on M.route_code=R.code
                         where A.partcode=@partcode ";
@@ -454,26 +560,195 @@
         }
         #endregion
 
-        #region[MES宸ュ崟鏂板銆佺紪杈戞彁浜
-        public static ToMessage AddUpdateMesOrder(string mesorderstus, string mesordercode, string partcode, string mesqty, string routecode, string wkshopcode, string planstartdate, string planenddate, string orderlev, string username, string opertype)
+
+        #region銆愮敓浜х鐞嗐�佸伐鍗曟柊澧炪�佺紪杈戞椂,閫夋嫨鎺掔▼鏄椂鑾峰彇鐗╂枡娓呭崟鐗堟湰鍙枫��
+        public static ToMessage JobCreationSonAddVison(string partnumber)
         {
-            var sql = "";
+            string sql = "";
+            var dynamicParams = new DynamicParameters();
+            try
+            {
+                sql = @" select id,version  from TBom_Main where materiel_code=@partnumber and status='Y' order by version desc ";
+                dynamicParams.Add("@partnumber", partnumber);
+                var data = DapperHelper.selectdata(sql, dynamicParams);
+                mes.code = "200";
+                mes.Message = "鏌ヨ鎴愬姛!";
+                mes.data = data;
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return mes;
+        }
+        #endregion
+
+        #region[宸ュ崟娲惧彂閫夋嫨宸ヨ壓璺嚎鎴栭�夋嫨鐢熶骇杞﹂棿鏃跺垽鏂粦瀹氭潯浠禲
+        public static ToMessage SelectRouteOrWkshop(string partcode, string routecode, string wkshopcode)
+        {
+            string sql = "";
+            var dynamicParams = new DynamicParameters();
+            try
+            {
+                mes.code = "200";
+                mes.Message = "";
+                mes.data = null;
+                //鍒ゆ柇宸ヨ壓璺嚎瀵瑰簲宸ュ簭鏄惁閮芥湁鍏宠仈宸ヤ綔绔�
+                sql = @"select B.step_code,C.eqp_code from TFlw_Rout A
+                        inner join TFlw_Rtdt B on A.code=B.rout_code
+                        left join TFlw_Rteqp C on B.step_code=C.step_code
+                        where A.code=@routecode";
+                dynamicParams.Add("@routecode", routecode);
+                var dtck = DapperHelper.selectdata(sql, dynamicParams);
+                for (int i = 0; i < dtck.Rows.Count; i++)
+                {
+                    if (dtck.Rows[i].IsNull("eqp_code"))
+                    {
+                        mes.code = "300";
+                        mes.count = 0;
+                        mes.Message = "褰撳墠宸ヨ壓璺嚎瀵瑰簲宸ュ簭銆�" + dtck.Rows[i]["step_code"].ToString() + "銆戯紝鏈叧鑱旇澶囷紝璇峰叧鑱旀墍鏈夊伐搴忓搴旇澶�!";
+                        mes.data = null;
+                        return mes;
+                    }
+                }
+                //鍒ゆ柇宸ヨ壓璺嚎瀵瑰簲宸ュ簭鍏宠仈鐨勫伐浣滅珯鏄惁鏈夎缃妭鎷嶅伐浠�
+                sql = @"select AA.step_code,AA.eqp_code,AA.flwtype,S.unprice  from (
+                        select A.code,B.step_code,C.eqp_code,S.flwtype from TFlw_Rout A
+                        inner join TFlw_Rtdt B on A.code=B.rout_code
+                        left join TFlw_Rteqp C on B.step_code=C.step_code
+                        left join TStep S on B.step_code=S.stepcode
+                        where A.code=@route_code
+                        ) as AA
+                        left join (select * from TPrteEqp_Stad   where materiel_code=@partcode and route_code=@route_code) as S on
+                        AA.code=S.route_code and AA.step_code=S.step_code and AA.eqp_code=S.eqp_code";
+                dynamicParams.Add("@partcode", partcode);
+                dynamicParams.Add("@route_code", routecode);
+                var dtc = DapperHelper.selectdata(sql, dynamicParams);
+                for (int i = 0; i < dtc.Rows.Count; i++)
+                {
+                    if (dtc.Rows[i]["flwtype"].ToString() == "Z") //鍒ゆ柇宸ュ簭鏄惁涓鸿嚜鍒�
+                    {
+                        if (dtc.Rows[i].IsNull("unprice") || decimal.Parse(dtc.Rows[i]["unprice"].ToString()) == 0)
+                        {
+                            mes.code = "300";
+                            mes.count = 0;
+                            mes.Message = "鑺傛媿宸ヤ环涓細褰撳墠浜у搧銆�" + partcode + "銆戯紝瀵瑰簲宸ヨ壓璺嚎銆�" + routecode + "銆戞湭璁剧疆(鎴栨湭璁剧疆鍏�)鎴栧伐浠峰皬浜庣瓑浜�0!";
+                            mes.data = null;
+                            return mes;
+                        }
+                    }
+                }
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return mes;
+        }
+        #endregion
+
+        #region[MES宸ュ崟鏂板銆佽幏鍙栧伐鍗曞彿]
+        public static ToMessage AddMesOrderCodeSearch()
+        {
+            string sql = "";
+            string wo_code = "";
+            var dynamicParams = new DynamicParameters();
+            try
+            {
+                //鑾峰彇鍗曟嵁鍙�
+                sql = @"SELECT 'SGPO'+CONVERT(varchar(12) , getdate(), 112 )+'_'+cast(isnull(max(cast(substring(wo_code,charindex('_',wo_code)+1,len(wo_code)-charindex('_',wo_code)) as numeric)),0)+1 as varchar) as numct
+                        FROM TK_Wrk_Man where wo_code like '%SGPO%'";
+                var data = DapperHelper.selecttable(sql);
+                mes.code = "200";
+                mes.Message = "鏌ヨ鎴愬姛!";
+                mes.data = data.Rows[0]["numct"].ToString();
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return mes;
+        }
+        #endregion
+
+        #region[MES宸ュ崟鏂板銆佺紪杈戞彁浜
+        public static ToMessage AddUpdateMesOrder(string mesorderstus, string sourceorder, string ordertype, string mesordercode, string partcode, string mesqty, string routecode, string wkshopcode, string planstartdate, string planenddate, string orderlev, string username, string opertype, string is_aps, string bom_id)
+        {
+            string sql = "", wo_status = "", wrk_status = "";
             var dynamicParams = new DynamicParameters();
             List<object> list = new List<object>();
             try
             {
+                //鑾峰彇宸ヨ壓璺嚎棣栭亾宸ュ簭ID
+                sql = @"select distinct B.id  from TFlw_Rout  A
+                                 left join TFlw_Rtdt B on A.code=B.rout_code and b.first_choke='Y'
+                                 where A.code=@routecode";
+                dynamicParams.Add("@routecode", routecode);
+                var dtck = DapperHelper.selectdata(sql, dynamicParams);
+                if (dtck.Rows.Count <= 0)
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "宸ュ崟瀵瑰簲宸ヨ壓璺嚎娌℃湁缁戝畾宸ュ簭!";
+                    mes.data = null;
+                    return mes;
+                }
+
                 if (opertype == "Add")
                 {
+                    if (is_aps == "Y")  //鏄惁鎺掔▼(鏄�)
+                    {
+                        //鍒ゆ柇鐗╂枡娓呭崟
+                        sql = "select id from TBom_Main  where materiel_code=@partcode and status='Y' and id=@bom_id";
+                        dynamicParams.Add("@partcode", partcode);
+                        dynamicParams.Add("@bom_id", bom_id);
+                        var data = DapperHelper.selectdata(sql, dynamicParams);
+                        if (data.Rows.Count <= 0)
+                        {
+                            mes.code = "300";
+                            mes.count = 0;
+                            mes.Message = "宸ュ崟瀵瑰簲鐨勪骇鍝佹病鏈夊缓绔婤OM鎴朆OM鏈鏍革紒";
+                            mes.data = null;
+                            return mes;
+                        }
+                        //澧炲姞宸ュ崟鐢ㄦ枡琛紙瀛愪欢锛�
+                        sql = @"insert into TK_Wrk_Allo(wo_code, seq, materiel_code, basqty, qty,bom_id,materieltype,stck_code,stopfeed,base_quantity,loss_quantity,m_quantity)
+                                select @wocode,A.seq,A.smateriel_code,A.total_quantity,(convert(decimal(18, 0), @uomqty)*A.Base_Quantity*(1+A.LOSS_QUANTITY/100))/C.quantity,
+                                A.m_id,A.pn_type,'','N',A.base_quantity,A.loss_quantity,C.quantity  
+                                from TBom_Deta A  left join  TBom_Main C on A.m_Id=C.id 
+                                left join TMateriel_Info B on A.smateriel_code = B.partcode
+                                where A.m_id=@bom_id";
+                        list.Add(new { str = sql, parm = new { wocode = mesordercode, uomqty = mesqty, bom_id = bom_id } });
+
+                        wo_status = "NOSCHED";
+                        wrk_status = "NOSCHED";
+                    }
+                    else //鍚�
+                    {
+                        wo_status = "ALLO";
+                        wrk_status = "ALLO";
+                    }
                     //鍐欏叆宸ュ崟琛�
-                    sql = @"insert into TK_Wrk_Man(wo_code,status,wkshp_code,plan_qty,plan_startdate,plan_enddate,route_code,stck_code,lm_user,lm_date,materiel_code,m_po,piroque)
-                                values(@mesordercode,@mesorderstus,@wkshopcode,@mesqty,@planstartdate,@planenddate,@routecode,@stck_code,@username,@CreateDate,@materiel_code,@m_po,@orderlev)";
+                    sql = @"insert into TK_Wrk_Man(wo_code,wotype,status,wkshp_code,plan_qty,plan_startdate,plan_enddate,route_code,stck_code,lm_user,lm_date,materiel_code,m_po,piroque,isaps)
+                                values(@mesordercode,@wotype,@mesorderstus,@wkshopcode,@mesqty,@planstartdate,@planenddate,@routecode,@stck_code,@username,@CreateDate,@materiel_code,@m_po,@orderlev,@isaps)";
                     list.Add(new
                     {
                         str = sql,
                         parm = new
                         {
                             mesordercode = mesordercode,
-                            mesorderstus = "ALLO", //娲惧彂
+                            wotype = ordertype,
+                            m_po = sourceorder,
+                            mesorderstus = wo_status, //鍗曟嵁鐘舵��
                             wkshopcode = wkshopcode,
                             mesqty = mesqty,
                             planstartdate = planstartdate,
@@ -483,8 +758,8 @@
                             username = username,
                             CreateDate = DateTime.Now.ToString(),
                             materiel_code = partcode,
-                            m_po = "",
-                            orderlev = orderlev
+                            orderlev = orderlev,
+                            isaps = is_aps
                         }
                     });
                     //鍐欏叆宸ュ簭浠诲姟琛�
@@ -506,9 +781,27 @@
                             planstartdate = planstartdate,
                             planenddate = planenddate,
                             routecode = routecode,
-                            status = "ALLO",  //娲惧彂
+                            status = wrk_status,  //宸ュ簭鐘舵��
                             username = username,
                             CreateDate = DateTime.Now.ToString()
+                        }
+                    });
+
+                    //鍐欏叆宸ュ崟宸ヨ壓璺嚎宸ュ簭宸ヤ环澶嶅埗琛�
+                    sql = @"insert into TWoPrteEqp_Stad(wo,materiel_code,eqp_code,stand_value,opc_conver,route_code,unprice,eqp_value,cavity_qty,wkspcode,lm_user,lm_date,torg_code,is_delete,step_code)
+                           select distinct @mesordercode as wo,S.materiel_code,S.eqp_code,S.stand_value,S.opc_conver,S.route_code,S.unprice,
+                           S.eqp_value,S.cavity_qty,S.wkspcode,S.lm_user,S.lm_date,S.torg_code,S.is_delete,S.step_code
+                           from TFlw_Rtdt A
+                           inner join TFlw_Rteqp C on A.step_code=C.step_code
+                           inner join (select *   from TPrteEqp_Stad  where materiel_code=@materiel_code and route_code=@routecode) as S on C.step_code=S.step_code and C.eqp_code=S.eqp_code";
+                    list.Add(new
+                    {
+                        str = sql,
+                        parm = new
+                        {
+                            mesordercode = mesordercode,
+                            materiel_code = partcode,
+                            routecode = routecode
                         }
                     });
 
@@ -530,7 +823,39 @@
                 }
                 if (opertype == "Update")
                 {
-                    sql = @"update TK_Wrk_Man set route_code=@routecode,wkshp_code=@wkshopcode,plan_startdate=@planstartdate,plan_enddate=@planenddate,status=@status,piroque=@orderlev,lm_user=@username,lm_date=@CreateDate where wo_code=@mesordercode";
+                    if (is_aps == "Y")  //鏄惁鎺掔▼(鏄�)
+                    {
+                        //鍒ゆ柇鐗╂枡娓呭崟
+                        sql = "select id from TBom_Main  where materiel_code=@partcode and status='Y' and id=@bom_id";
+                        dynamicParams.Add("@partcode", partcode);
+                        dynamicParams.Add("@bom_id", bom_id);
+                        var data = DapperHelper.selectdata(sql, dynamicParams);
+                        if (data.Rows.Count <= 0)
+                        {
+                            mes.code = "300";
+                            mes.count = 0;
+                            mes.Message = "宸ュ崟瀵瑰簲鐨勪骇鍝佹病鏈夊缓绔婤OM鎴朆OM鏈鏍革紒";
+                            mes.data = null;
+                            return mes;
+                        }
+                        //澧炲姞宸ュ崟鐢ㄦ枡琛紙瀛愪欢锛�
+                        sql = @"insert into TK_Wrk_Allo(wo_code, seq, materiel_code, basqty, qty,bom_id,materieltype,stck_code,stopfeed,base_quantity,loss_quantity,m_quantity)
+                                select @wocode,A.seq,A.smateriel_code,A.total_quantity,(convert(decimal(18, 0), @uomqty)*A.Base_Quantity*(1+A.LOSS_QUANTITY/100))/C.quantity,
+                                A.m_id,A.pn_type,'','N',A.base_quantity,A.loss_quantity,C.quantity  
+                                from TBom_Deta A  left join  TBom_Main C on A.m_Id=C.id 
+                                left join TMateriel_Info B on A.smateriel_code = B.partcode
+                                where A.m_id=@bom_id";
+                        list.Add(new { str = sql, parm = new { wocode = mesordercode, uomqty = mesqty, bom_id = bom_id } });
+
+                        wo_status = "NOSCHED";
+                        wrk_status = "NOSCHED";
+                    }
+                    else //鍚�
+                    {
+                        wo_status = "ALLO";
+                        wrk_status = "ALLO";
+                    }
+                    sql = @"update TK_Wrk_Man set route_code=@routecode,wkshp_code=@wkshopcode,plan_startdate=@planstartdate,plan_enddate=@planenddate,status=@status,piroque=@orderlev,lm_user=@username,lm_date=@CreateDate,isaps=@isaps where wo_code=@mesordercode";
                     list.Add(new
                     {
                         str = sql,
@@ -540,11 +865,12 @@
                             wkshopcode = wkshopcode,
                             planstartdate = planstartdate,
                             planenddate = planenddate,
-                            status = "ALLO",  //娲惧彂
+                            status = wo_status,
                             routecode = routecode,
                             username = username,
                             CreateDate = DateTime.Now.ToString(),
-                            orderlev = orderlev
+                            orderlev = orderlev,
+                            isaps = is_aps
                         }
                     });
                     //鍐欏叆宸ュ簭浠诲姟琛�
@@ -565,12 +891,41 @@
                             mesqty = mesqty,
                             planstartdate = planstartdate,
                             planenddate = planenddate,
-                            status = "ALLO",  //娲惧彂
+                            status = wo_status,
                             username = username,
-                            routecode= routecode,
+                            routecode = routecode,
                             CreateDate = DateTime.Now.ToString()
                         }
                     });
+
+                    //鍒犻櫎宸ュ崟宸ヨ壓璺嚎宸ュ簭宸ヤ环澶嶅埗琛�
+                    sql = @"delete TWoPrteEqp_Stad where wo=@mesordercode";
+                    list.Add(new
+                    {
+                        str = sql,
+                        parm = new
+                        {
+                            mesordercode = mesordercode
+                        }
+                    });
+                    //鍐欏叆宸ュ崟宸ヨ壓璺嚎宸ュ簭宸ヤ环澶嶅埗琛�
+                    sql = @"insert into TWoPrteEqp_Stad(wo,materiel_code,eqp_code,stand_value,opc_conver,route_code,unprice,eqp_value,cavity_qty,wkspcode,lm_user,lm_date,torg_code,is_delete,step_code)
+                           select distinct @mesordercode as wo,S.materiel_code,S.eqp_code,S.stand_value,S.opc_conver,S.route_code,S.unprice,
+                           S.eqp_value,S.cavity_qty,S.wkspcode,S.lm_user,S.lm_date,S.torg_code,S.is_delete,S.step_code
+                           from TFlw_Rtdt A
+                           inner join TFlw_Rteqp C on A.step_code=C.step_code
+                           inner join (select *   from TPrteEqp_Stad  where materiel_code=@materiel_code and route_code=@routecode) as S on C.step_code=S.step_code and C.eqp_code=S.eqp_code";
+                    list.Add(new
+                    {
+                        str = sql,
+                        parm = new
+                        {
+                            mesordercode = mesordercode,
+                            materiel_code = partcode,
+                            routecode = routecode
+                        }
+                    });
+
                     bool aa = DapperHelper.DoTransaction(list);
                     if (aa)
                     {
@@ -600,15 +955,15 @@
         #endregion
 
         #region[MES宸ュ崟鍒犻櫎]
-        public static ToMessage DeleteMesOrder(string wocode, string m_po, string orderqty)
+        public static ToMessage DeleteMesOrder(string souceid, string wocode, string m_po, string orderqty)
         {
             var sql = "";
             List<object> list = new List<object>();
             var dynamicParams = new DynamicParameters();
             try
             {
-                //鍒ゆ柇宸ュ崟鏄惁涓烘湭寮�濮嬬姸鎬佹垨鑰呭凡娲惧彂鐘舵��(婊¤冻鍏朵腑涓�绉嶉兘鍙垹闄わ紝鍚﹀垯涓嶅厑璁稿垹闄�)
-                sql = @"select *  from TK_Wrk_Man where wo_code=@wocode and status='NEW' or status='ALLO'";
+                //鍒ゆ柇宸ュ崟鏄惁涓烘湭寮�濮嬬姸鎬佹垨鑰呭凡娲惧彂鎴栧緟鎺掔▼鐘舵��(婊¤冻鍏朵腑涓�绉嶉兘鍙垹闄わ紝鍚﹀垯涓嶅厑璁稿垹闄�)
+                sql = @"select *  from TK_Wrk_Man where wo_code=@wocode and status in('NEW','ALLO','NOSCHED')";
                 dynamicParams.Add("@wocode", wocode);
                 var data = DapperHelper.selectdata(sql, dynamicParams);
                 if (data.Rows.Count > 0)
@@ -616,25 +971,30 @@
                     if (m_po != "" && m_po != null)
                     {
                         //鏌ヨ鐢熶骇璁㈠崟琛ㄦ暟鎹�
-                        sql = @"select *  from TKimp_Ewo where wo=@m_po";
+                        sql = @"select *  from TKimp_Ewo where wo=@m_po and id=@souceid";
                         dynamicParams.Add("@m_po", m_po);
+                        dynamicParams.Add("@souceid", souceid);
                         var data0 = DapperHelper.selectdata(sql, dynamicParams);
                         decimal relse_qty = decimal.Parse(data0.Rows[0]["RELSE_QTY"].ToString());//浠ヤ笅鍗曟暟閲�
                         if ((relse_qty - decimal.Parse(orderqty)) == 0)  //鍏ㄩ儴鎾ら攢 璁㈠崟鐘舵�佸洖鍐欐湭寮�濮�,宸蹭笅鍗曟暟閲忎负0
                         {
                             //鍥炲啓璁㈠崟琛ㄧ姸鎬佸強宸蹭笅鍗曟暟閲�
-                            sql = @"update TKimp_Ewo set status='NEW',relse_qty=0  where wo=@m_po";
-                            list.Add(new { str = sql, parm = new { m_po = m_po } });
+                            sql = @"update TKimp_Ewo set status='NEW',relse_qty=0  where wo=@m_po and id=@souceid";
+                            list.Add(new { str = sql, parm = new { m_po = m_po, souceid = souceid } });
                         }
                         else
                         {
                             //鍥炲啓璁㈠崟琛ㄧ姸鎬佸強宸蹭笅鍗曟暟閲�
-                            sql = @"update TKimp_Ewo set status='CREATING',relse_qty=relse_qty-@orderqty  where wo=@m_po";
-                            list.Add(new { str = sql, parm = new { m_po = m_po, orderqty = decimal.Parse(orderqty) } });
+                            sql = @"update TKimp_Ewo set status='CREATING',relse_qty=relse_qty-@orderqty  where wo=@m_po and id=@souceid";
+                            list.Add(new { str = sql, parm = new { m_po = m_po, souceid = souceid, orderqty = decimal.Parse(orderqty) } });
                         }
                     }
                     //鍒犻櫎宸ュ崟宸ュ簭琛�
                     sql = @"delete TK_Wrk_Step  where wo_code=@wocode";
+                    list.Add(new { str = sql, parm = new { wocode = wocode } });
+
+                    //鍒犻櫎鍔犲伐鍗曠敤鏂欒〃(瀛愪欢)
+                    sql = @"delete TK_Wrk_Allo  where wo_code=@wocode";
                     list.Add(new { str = sql, parm = new { wocode = wocode } });
 
                     //鍒犻櫎宸ュ崟琛�
@@ -676,7 +1036,7 @@
         #endregion
 
         #region[MES宸ュ崟鍏抽棴]
-        public static ToMessage ClosedMesOrder(string wocode, string m_po)
+        public static ToMessage ClosedMesOrder(string username, string wocode, string m_po)
         {
             var sql = "";
             List<object> list = new List<object>();
@@ -687,8 +1047,8 @@
                 sql = @"update TK_Wrk_Step set status='CLOSED'  where wo_code=@wocode";
                 list.Add(new { str = sql, parm = new { wocode = wocode } });
                 //鍥炲啓宸ュ崟琛ㄧ姸鎬佷负(鍏抽棴)
-                sql = @"update TK_Wrk_Man set status='CLOSED'  where wo_code=@wocode";
-                list.Add(new { str = sql, parm = new { wocode = wocode } });
+                sql = @"update TK_Wrk_Man set status='CLOSED',closeuser=@username,closedate=@closedate  where wo_code=@wocode";
+                list.Add(new { str = sql, parm = new { wocode = wocode, username = username, closedate = DateTime.Now.ToString() } });
                 bool aa = DapperHelper.DoTransaction(list);
                 if (aa)
                 {
@@ -724,9 +1084,11 @@
             try
             {
                 //鑾峰彇宸ュ簭浠诲姟淇℃伅
-                sql = @"select A.wo_code,A.seq,B.stepcode,B.stepname,A.plan_qty,A.good_qty,A.ng_qty  from  TK_Wrk_Step A
-                        left join TStep B on A.step_code=B.stepcode
-                        where A.wo_code=@wo_code";
+                sql = @"select A.wo_code,P.partcode,P.partname,P.partspec,A.seq,B.stepcode,B.stepname,A.plan_qty,A.good_qty,A.ng_qty  from  TK_Wrk_Step A
+                         left join TStep B on A.step_code=B.stepcode
+                         left join TK_Wrk_Man M on A.wo_code=M.wo_code
+                         left join TMateriel_Info P on M.materiel_code=P.partcode
+                        where A.wo_code=@wo_code order by A.seq";
                 dynamicParams.Add("@wo_code", wo_code);
                 var data = DapperHelper.selectdata(sql, dynamicParams);
                 mes.code = "200";
@@ -802,9 +1164,9 @@
                     sql = @"select *  from TStep  where stepcode=@stepcode";
                     dynamicParams.Add("@stepcode", stepcode);
                     var data0 = DapperHelper.selectdata(sql, dynamicParams);
-                    if (data0.Rows.Count > 0) 
+                    if (data0.Rows.Count > 0)
                     {
-                        if (data0.Rows[0]["FLWTYPE"].ToString() == "W") 
+                        if (data0.Rows[0]["FLWTYPE"].ToString() == "W")
                         {
                             mes.code = "300";
                             mes.count = 0;
@@ -816,12 +1178,12 @@
                 }
                 //鏍规嵁鏉′欢鏌ヨ宸ュ崟宸ュ簭浠诲姟(鑷埗宸ュ簭)
                 sql = @"select A.status,A.wo_code,B.route_code,M.partcode,M.partname,M.partspec,A.seq,A.isbott,A.isend,
-                            S.stepcode,S.stepname,S.descr,A.plan_qty,A.good_qty,A.ng_qty,A.plan_startdate
+                            S.stepcode,S.stepname,S.descr,A.plan_qty,A.good_qty,A.ng_qty,A.bad_qty,A.plan_startdate,A.plan_enddate
                             from TK_Wrk_Step A
                             left join TK_Wrk_Man B on A.wo_code=B.wo_code
                             left join TMateriel_Info M on B.materiel_code=M.partcode
                             left join TStep S on A.step_code=S.stepcode
-                            where A.status<>'CLOSED' and S.flwtype='Z'  " + search;
+                            where A.status in('ALLO','START') and S.flwtype='Z'  " + search;
                 var data = DapperHelper.GetPageList<object>(sql, dynamicParams, prop, order, startNum, endNum, out total);
                 if (data.ToList().Count > 0)
                 {
@@ -920,12 +1282,12 @@
                 }
                 //鏍规嵁鏉′欢鏌ヨ宸ュ崟宸ュ簭浠诲姟(鑷埗宸ュ簭)
                 sql = @"select A.status,A.wo_code,B.route_code,M.partcode,M.partname,M.partspec,A.seq,A.isbott,A.isend,
-                            S.stepcode,S.stepname,S.descr,A.plan_qty,A.good_qty,A.ng_qty,A.plan_startdate
+                            S.stepcode,S.stepname,S.descr,A.plan_qty,A.good_qty,A.ng_qty,A.bad_qty,A.plan_startdate,A.plan_enddate
                             from TK_Wrk_Step A
                             left join TK_Wrk_Man B on A.wo_code=B.wo_code
                             left join TMateriel_Info M on B.materiel_code=M.partcode
                             left join TStep S on A.step_code=S.stepcode
-                            where A.status<>'CLOSED' and S.flwtype='W'  " + search;
+                            where A.status in('ALLO','START') and S.flwtype='W'  " + search;
                 var data = DapperHelper.GetPageList<object>(sql, dynamicParams, prop, order, startNum, endNum, out total);
                 if (data.ToList().Count > 0)
                 {
@@ -942,6 +1304,217 @@
                     mes.Message = "鏃犲彲鎵ц鐨勭敓浜т换鍔�,浠诲姟宸插畬鎴愭垨宸插叧闂�!";
                     mes.data = null;
                     return mes;
+                }
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return mes;
+        }
+        #endregion
+
+        #region[鐢熶骇寮�鎶ュ伐鎵爜鑾峰彇宸ュ崟瀵瑰簲宸ュ簭浠诲姟(涓嶈壇)]
+        public static ToMessage MesOrderNgStepSearch(string orderstepqrcode, int startNum, int endNum, string prop, string order)
+        {
+            var sql = "";
+            string search = "";
+            string ordercode = "";
+            string stepcode = "";
+            var dynamicParams = new DynamicParameters();
+            var total = 0; //鎬绘潯鏁�
+            try
+            {
+                if (orderstepqrcode != "" && orderstepqrcode != null)
+                {
+                    string[] arra = orderstepqrcode.Split(';');
+                    if (arra.Length == 1) //宸ュ崟鍙蜂簩缁寸爜
+                    {
+                        ordercode = arra[0]; //鑾峰彇鎸囧畾瀛楃涓插墠闈㈢殑瀛楃
+                        mes.code = "300";
+                        mes.count = 0;
+                        mes.Message = "璇锋壂鎻忓伐搴忎簩缁寸爜!";
+                        mes.data = null;
+                        return mes;
+                    }
+                    if (arra.Length == 2) //宸ュ崟鍙�+宸ュ簭鍙蜂簩缁寸爜
+                    {
+                        ordercode = arra[0]; //鑾峰彇鎸囧畾瀛楃涓插墠闈㈢殑瀛楃
+                        stepcode = arra[1]; //鑾峰彇鎸囧畾瀛楃涓插墠闈㈢殑瀛楃
+                    }
+                    if (ordercode != "" && ordercode != null) //宸ュ崟鍙蜂笉涓虹┖,宸ュ簭鍙蜂负绌�
+                    {
+                        search += "and A.wo_code=@ordercode ";
+                        dynamicParams.Add("@ordercode", ordercode);
+                    }
+                    if (ordercode != "" && stepcode != "") //宸ュ崟鍙蜂笉涓虹┖,宸ュ簭鍙蜂笉涓虹┖
+                    {
+                        search += "and A.wo_code=@ordercode ";
+                        dynamicParams.Add("@ordercode", ordercode);
+                        search += "and S.stepcode=@stepcode ";
+                        dynamicParams.Add("@stepcode", stepcode);
+                    }
+                }
+                if (stepcode != "")
+                {
+                    //鏌ユ壘褰撳墠宸ュ簭浠诲姟
+                    sql = @"select *  from TK_Wrk_Step  where step_code=@stepcode and wo_code=@ordercode";
+                    dynamicParams.Add("@stepcode", stepcode);
+                    dynamicParams.Add("@ordercode", ordercode);
+                    var data0 = DapperHelper.selectdata(sql, dynamicParams);
+                    if (data0.Rows.Count <= 0)
+                    {
+                        mes.code = "300";
+                        mes.count = 0;
+                        mes.Message = "褰撳墠宸ュ簭浠诲姟涓嶅瓨鍦�,鏃犳晥鏉$爜!";
+                        mes.data = null;
+                        return mes;
+                    }
+                }
+                //鏍规嵁鏉′欢鏌ヨ宸ュ崟宸ュ簭浠诲姟,涓斾笉鑹暟閲忓ぇ浜�0
+                sql = @"select A.wo_code,M.partcode,M.partname,M.partspec,A.seq,A.isend,
+                            S.stepcode,S.stepname,S.descr,A.plan_qty,A.good_qty,A.ng_qty,A.bad_qty
+                            from TK_Wrk_Step A
+                            left join TK_Wrk_Man B on A.wo_code=B.wo_code
+                            left join TMateriel_Info M on B.materiel_code=M.partcode
+                            left join TStep S on A.step_code=S.stepcode
+                            where A.status in('ALLO','START') and A.ng_qty>0 " + search;
+                var data = DapperHelper.GetPageList<object>(sql, dynamicParams, prop, order, startNum, endNum, out total);
+                if (data.ToList().Count > 0)
+                {
+                    mes.code = "200";
+                    mes.count = total;
+                    mes.Message = "鏌ヨ鎴愬姛!";
+                    mes.data = data.ToList();
+                    return mes;
+                }
+                else
+                {
+                    mes.code = "301";
+                    mes.count = 0;
+                    mes.Message = "鏃犲彲鎵ц鐨勭敓浜т换鍔�,浠诲姟宸插畬鎴愭垨宸插叧闂�!";
+                    mes.data = null;
+                    return mes;
+                }
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return mes;
+        }
+        #endregion
+
+        #region[鐢熶骇寮�鎶ュ伐鎵爜鑾峰彇宸ュ崟瀵瑰簲宸ュ簭浠诲姟(涓嶈壇鏄庣粏)]
+        public static ToMessage MesOrderNgSubStepSearch(string orderstepqrcode)
+        {
+            var sql = "";
+            string search = "";
+            string ordercode = "";
+            string stepcode = "";
+            var dynamicParams = new DynamicParameters();
+            var dynamicParams1 = new DynamicParameters();
+            Dictionary<string, object> list = new Dictionary<string, object>();
+            ScanStartReportData rt = new ScanStartReportData();
+            var total = 0; //鎬绘潯鏁�
+            try
+            {
+                if (orderstepqrcode != "" && orderstepqrcode != null)
+                {
+                    string[] arra = orderstepqrcode.Split(';');
+                    if (arra.Length == 1) //宸ュ崟鍙蜂簩缁寸爜
+                    {
+                        ordercode = arra[0]; //鑾峰彇鎸囧畾瀛楃涓插墠闈㈢殑瀛楃
+                        mes.code = "300";
+                        mes.count = 0;
+                        mes.Message = "璇锋壂鎻忓伐搴忎簩缁寸爜!";
+                        mes.data = null;
+                        return mes;
+                    }
+                    if (arra.Length == 2) //宸ュ崟鍙�+宸ュ簭鍙蜂簩缁寸爜
+                    {
+                        ordercode = arra[0]; //鑾峰彇鎸囧畾瀛楃涓插墠闈㈢殑瀛楃
+                        stepcode = arra[1]; //鑾峰彇鎸囧畾瀛楃涓插墠闈㈢殑瀛楃
+                    }
+                }
+                //1.鏍规嵁宸ュ崟+宸ュ簭鏌ユ壘褰撳墠宸ュ簭鏄惁棣栭亾宸ュ簭
+                sql = @"select A.wo_code,P.partcode,P.partname,P.partspec, T.stepcode,T.stepname,A.seq,T.flwtype,T.descr,A.status,A.plan_qty,A.good_qty,A.ng_qty,A.bad_qty,A.isbott,A.isend  
+                        from TK_Wrk_Step A
+                        left join  TStep T on A.step_code=T.stepcode
+                        left join  TK_Wrk_Man M on A.wo_code=M.wo_code
+                        left join  TMateriel_Info P on M.materiel_code=P.partcode
+                        where A.wo_code=@ordercode and A.step_code=@stepcode";
+                dynamicParams.Add("@ordercode", ordercode);
+                dynamicParams.Add("@stepcode", stepcode);
+                var data = DapperHelper.selectdata(sql, dynamicParams);
+                if (data.Rows.Count > 0)
+                {
+                    rt.wo_code = data.Rows[0]["WO_CODE"].ToString(); //宸ュ崟鍙�
+                    rt.partnumber = data.Rows[0]["PARTCODE"].ToString(); //浜у搧缂栫爜
+                    rt.partname = data.Rows[0]["PARTNAME"].ToString(); //浜у搧鍚嶇О
+                    rt.partspec = data.Rows[0]["PARTSPEC"].ToString(); //浜у搧瑙勬牸
+                    rt.stepcode = data.Rows[0]["STEPCODE"].ToString(); //宸ュ簭缂栫爜
+                    rt.stepname = data.Rows[0]["STEPNAME"].ToString(); //宸ュ簭鍚嶇О
+                    rt.stepdesc = data.Rows[0]["DESCR"].ToString(); //宸ュ簭鎻忚堪
+                    rt.planqty = decimal.Parse(data.Rows[0]["PLAN_QTY"].ToString()); //浠诲姟鏁伴噺
+                    rt.noreportqty = decimal.Parse(data.Rows[0]["GOOD_QTY"].ToString()); //鎶ュ伐鏁伴噺
+                    rt.noputqty = decimal.Parse(data.Rows[0]["NG_QTY"].ToString()); //涓嶈壇鏁伴噺
+                    string isend = data.Rows[0]["ISEND"].ToString();//鏈亾宸ュ簭
+                    rt.seq = data.Rows[0]["SEQ"].ToString();//宸ュ簭搴忓彿
+
+                    //鑾峰彇褰撳墠宸ュ簭涓嬮亾宸ュ簭
+                    sql = @"select A.isbott,A.isend,T.stepcode,T.stepname from TK_Wrk_Step A
+                            left join  TStep T on A.step_code=T.stepcode
+                            where A.wo_code=@ordercode and A.seq=@seq+1 ";
+                    dynamicParams.Add("@ordercode", ordercode);
+                    dynamicParams.Add("@seq", decimal.Parse(data.Rows[0]["SEQ"].ToString()));
+                    var dt0 = DapperHelper.selectdata(sql, dynamicParams);
+                    if (dt0.Rows.Count > 0) //鏈変笅閬撳伐搴�
+                    {
+                        rt.nextstepcode = dt0.Rows[0]["STEPCODE"].ToString();//涓嬮亾宸ュ簭缂栫爜
+                        rt.nextstepname = dt0.Rows[0]["STEPNAME"].ToString();//涓嬮亾宸ュ簭鍚嶇О
+                    }
+                    if (isend == "Y")  //褰撳墠宸ュ簭鏄湯閬撳伐搴�
+                    {
+                        rt.nextstepcode = "";//璧嬬┖
+                        rt.nextstepname = "";//璧嬬┖
+                    }
+                }
+                else
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "褰撳墠鎵爜宸ュ簭浠诲姟涓嶅瓨鍦�!";
+                    mes.data = null;
+                    return mes;
+                }
+                //鏍规嵁鏉′欢鏌ヨ宸ュ崟宸ュ簭鎶ュ伐(鏀舵枡)璁板綍,涓斾笉鑹暟閲忓ぇ浜�0
+                //瀛樺偍杩囩▼鍚�
+                sql = @"h_p_IFCLD_MesReportDefectHandleSelect";
+                dynamicParams1.Add("@ordercode", ordercode);
+                dynamicParams1.Add("@stepcode", stepcode);
+                DataTable dt = DapperHelper.selectProcedure(sql, dynamicParams1);
+                if (dt.Rows.Count > 0)
+                {
+                    list.Add("data1", rt);
+                    list.Add("data2", dt);
+                    mes.code = "200";
+                    mes.count = 0;
+                    mes.Message = "鏌ヨ鎴愬姛!";
+                    mes.data = list;
+                }
+                else
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "鏃犲彲鎵ц鐨勭敓浜т换鍔�,浠诲姟宸插畬鎴愭垨宸插叧闂�!";
+                    mes.data = null;
                 }
             }
             catch (Exception e)
@@ -1130,7 +1703,7 @@
                     dynamicParams.Add("@usercode", usercode);
                 }
                 //鐝粍鑾峰彇浜哄憳鍒楄〃
-                sql = @"select usercode,username  from TUser where is_delete<>'1' "+search;
+                sql = @"select usercode,username  from TUser where is_delete<>'1' and usercode<>'9999'" + search;
                 dynamicParams.Add("@usercode", usercode);
                 var data = DapperHelper.selectdata(sql, dynamicParams);
                 mes.code = "200";
@@ -1183,7 +1756,7 @@
                         left join TCustomer C on A.eqp_code=C.code
                         where A.step_code=@stepcode and A.style='W'";
                 dynamicParams.Add("@stepcode", stepcode);
-                var data = DapperHelper.selectdata(sql,dynamicParams);
+                var data = DapperHelper.selectdata(sql, dynamicParams);
                 mes.code = "200";
                 mes.Message = "鏌ヨ鎴愬姛!";
                 mes.data = data;
@@ -1282,7 +1855,9 @@
                 sql = @"update TK_Wrk_Step set status='START'  where wo_code=@mesordercode and step_code=@stepcode";
                 list.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepcode = stepcode } });
 
-
+                //鍥炲啓宸ュ崟宸ュ簭琛ㄧ姸鎬佷负宸插紑宸�
+                sql = @"update TK_Wrk_Step set status='START'  where wo_code=@mesordercode and step_code=@stepcode";
+                list.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepcode = stepcode } });
                 //鍥炲啓宸ュ崟琛ㄧ姸鎬佷负锛� 寮�宸ワ細START 
                 sql = @"update TK_Wrk_Man set status='START'  where wo_code=@mesordercode";
                 list.Add(new { str = sql, parm = new { mesordercode = mesordercode } });
@@ -1315,18 +1890,18 @@
         #endregion
 
         #region[鐢熶骇寮�鎶ュ伐,鎶ュ伐鎻愪氦]
-        public static ToMessage SavaMesOrderStepReport(string mesordercode, string partcode, string stepseq, string stepcode, string eqpcode, string usergroupcode, string reportuser, string taskqty, string startqty, string reportqty, string ngqty, string badcode, string username)
+        public static ToMessage SavaMesOrderStepReport(string mesordercode, string partcode, string stepseq, string stepcode, string eqpcode, string reckway, string usergroupcode, string reportuser, string taskqty, string startqty, string reportqty, string ngqty, string badcode, string remarks, string username)
         {
             var sql = "";
-            string[] arra = new string[] { };
+            //string[] arra = new string[] { };
             string[] arra1 = new string[] { };
             List<object> list = new List<object>();
             var dynamicParams = new DynamicParameters();
             try
             {
                 string date = DateTime.Now.ToString(); //鑾峰彇绯荤粺鏃堕棿
-                //鎴彇鎶ュ伐浜哄憳
-                arra = reportuser.Split(';');
+                //鑾峰彇鎶ュ伐浜哄憳銆佸垎閰嶆瘮渚�
+                JArray arra = (JArray)Newtonsoft.Json.JsonConvert.DeserializeObject(reportuser);
                 if (ngqty != "" || ngqty != "0")
                 {
                     //鎴彇涓嶈壇鍘熷洜
@@ -1337,38 +1912,27 @@
                 //鍒ゆ柇鏄惁鏈夋姤宸ヨ褰�(鏈夛細淇敼 鏃狅細鏂板)
                 sql = @"select *  from TK_Wrk_Record where wo_code=@wo_code and step_code=@step_code and style='B'";
                 dynamicParams.Add("@wo_code", mesordercode);
-                dynamicParams.Add("@stepcode", stepcode);
+                dynamicParams.Add("@step_code", stepcode);
                 var data = DapperHelper.selectdata(sql, dynamicParams);
+                //鑾峰彇寮�宸ヨ褰曠殑榛樿閫変腑鐨勮澶�(浜х嚎)涓庢姤宸ユ椂鐨勮澶囦骇绾垮仛瀵规瘮鍒ゆ柇
+                sql = @"select A.eqp_code,B.name  from TK_Wrk_Record A
+                        inner join TEqpInfo B on A.eqp_code=B.code
+                        where A.wo_code=@wo_code and A.step_code=@step_code and A.style='S'";
+                dynamicParams.Add("@wo_code", mesordercode);
+                dynamicParams.Add("@step_code", stepcode);
+                var da = DapperHelper.selectdata(sql, dynamicParams);
+                if (da.Rows[0]["EQP_CODE"].ToString() != eqpcode)
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "鎿嶄綔澶辫触,褰撳墠鎶ュ伐浜х嚎搴斾负锛�" + da.Rows[0]["NAME"].ToString() + "!";
+                    mes.data = null;
+                    return mes;
+                }
                 if (data.Rows.Count > 0)
                 {
-                    //淇敼鎶ュ伐璁板綍
-                    sql = @"update TK_Wrk_Record set good_qty=good+@reportqty,ng_qty=ng_qty+@ngqty
-                                lm_user=@username,lm_date=@CreateDate where wo_code=@mesordercode and step_code=@stepcode";
-                    list.Add(new { str = sql, parm = new { reportqty = decimal.Parse(reportqty), ngqty = decimal.Parse(ngqty), mesordercode = mesordercode, stepcode = stepcode, username = username, CreateDate = date } });
-                    //鍐欏叆瀛愯〃
-                    for (int i = 0; i < arra.Length; i++)
-                    {
-                        sql = @"insert into  TK_Wrk_RecordSub(m_id,eqp_code,report_person,report_date,report_qty,usergroup_code,ng_qty,style,lm_user,lm_date) 
-                                values(@m_id,@eqp_code,@report_person,@report_date,@report_qty,@usergroup_code,@ng_qty,@style,@lm_user,@lm_date)";
-                        list.Add(new { str = sql, parm = new { m_id = int.Parse(data.Rows[0]["ID"].ToString()), eqp_code = eqpcode, report_person = arra[i], report_date = date, report_qty = reportqty, usergroup_code = usergroupcode, ng_qty = ngqty, style = "B", lm_user = username, lm_date = date } });
-
-                    }
-                    if (badcode != "" || badcode != null)
-                    {
-                        //鍐欏叆缂洪櫡璁板綍琛�
-                        for (int i = 0; i < arra1.Length; i++)
-                        {
-                            sql = @"insert into  CSR_WorkRecord_Defect(record_id,wo_code,partnumber,step_seq,step_code,defect_qty,defect_code,style,lm_user,lm_date) 
-                                values(@record_id,@wo_code,@partcode,@stepseq,@stepcode,@ngqty,@defect_code,@style,@lm_user,@lm_date)";
-                            list.Add(new { str = sql, parm = new { record_id = int.Parse(data.Rows[0]["ID"].ToString()), wo_code = mesordercode, partcode = partcode, stepseq = stepseq, stepcode = stepcode, ngqty = ngqty, defect_code = arra1[i], style = "B", lm_user = username, lm_date = date } });
-
-                        }
-                    }
-                }
-                else
-                {
                     //鑾峰彇涓昏〃鏈�澶D
-                    sql = @"select IDENT_CURRENT('TK_Wrk_Record')+1 as id";
+                    sql = @"select ISNULL(IDENT_CURRENT('TK_Wrk_Record')+1,1) as id";
                     var dt = DapperHelper.selecttable(sql);
                     //鍐欏叆寮�鎶ュ伐璁板綍琛�
                     sql = @"insert into  TK_Wrk_Record(wo_code,step_seq,step_code,eqp_code,materiel_code,task_qty,start_qty,good_qty,ng_qty,style,lm_user,lm_date) 
@@ -1376,21 +1940,110 @@
                     list.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepseq = stepseq, stepcode = stepcode, eqpcode = eqpcode, partcode = partcode, taskqty = taskqty, startqty = startqty, reportqty = reportqty, ngqty = ngqty, style = "B", lm_user = username, lm_date = date } });
 
                     //鍐欏叆瀛愯〃
-                    for (int i = 0; i < arra.Length; i++)
+                    for (int i = 0; i < arra.Count; i++)
                     {
-                        sql = @"insert into  TK_Wrk_RecordSub(m_id,eqp_code,report_person,report_date,report_qty,usergroup_code,ng_qty,style,lm_user,lm_date) 
-                                values(@m_id,@eqp_code,@report_person,@report_date,@report_qty,@usergroup_code,@ng_qty,@style,@lm_user,@lm_date)";
-                        list.Add(new { str = sql, parm = new { m_id = int.Parse(dt.Rows[0]["ID"].ToString()), eqp_code = eqpcode, report_person = arra[i], report_date = date, report_qty = reportqty, usergroup_code = usergroupcode, ng_qty = ngqty, style = "B", lm_user = username, lm_date = date } });
+                        sql = @"insert into  TK_Wrk_RecordSub(m_id,eqp_code,report_person,report_date,report_qty,reckway,usergroup_code,ratio,ng_qty,style,lm_user,lm_date) 
+                                values(@m_id,@eqp_code,@report_person,@report_date,@report_qty,@reckway,@usergroup_code,@ratio,@ng_qty,@style,@lm_user,@lm_date)";
+                        list.Add(new
+                        {
+                            str = sql,
+                            parm = new
+                            {
+                                m_id = int.Parse(dt.Rows[0]["ID"].ToString()),
+                                eqp_code = eqpcode,
+                                report_person = arra[i]["usercode"].ToString(),
+                                report_date = date,
+                                report_qty = reportqty,
+                                reckway = reckway,
+                                usergroup_code = usergroupcode,
+                                ratio = decimal.Parse(arra[i]["ratio"].ToString()),
+                                ng_qty = ngqty,
+                                style = "B",
+                                lm_user = username,
+                                lm_date = date
+                            }
+                        });
 
                     }
-                    if (badcode != "" || badcode != null)
+                    if (badcode != "" && ngqty != "0")
                     {
                         //鍐欏叆缂洪櫡璁板綍琛�
                         for (int i = 0; i < arra1.Length; i++)
                         {
-                            sql = @"insert into  CSR_WorkRecord_Defect(record_id,wo_code,partnumber,step_seq,step_code,defect_qty,defect_code,style,lm_user,lm_date) 
-                                values(@record_id,@wo_code,@partcode,@stepseq,@stepcode,@ngqty,@defect_code,@style,@lm_user,@lm_date)";
-                            list.Add(new { str = sql, parm = new { record_id = int.Parse(dt.Rows[0]["ID"].ToString()), wo_code = mesordercode, partcode = partcode, stepseq = stepseq, stepcode = stepcode, ngqty = ngqty, defect_code = arra1[i], style = "B", lm_user = username, lm_date = date } });
+                            sql = @"insert into  CSR_WorkRecord_Defect(record_id,wo_code,partnumber,step_seq,step_code,defect_qty,defect_code,remarks,style,lm_user,lm_date) 
+                                values(@record_id,@wo_code,@partcode,@stepseq,@stepcode,@ngqty,@defect_code,@remarks,@style,@lm_user,@lm_date)";
+                            list.Add(new { str = sql, parm = new { record_id = int.Parse(dt.Rows[0]["ID"].ToString()), wo_code = mesordercode, partcode = partcode, stepseq = stepseq, stepcode = stepcode, ngqty = ngqty, defect_code = arra1[i], remarks = remarks, style = "B", lm_user = username, lm_date = date } });
+
+                        }
+                    }
+                    ////淇敼鎶ュ伐璁板綍
+                    //sql = @"update TK_Wrk_Record set good_qty=good_qty+@reportqty,ng_qty=ng_qty+@ngqty,
+                    //            lm_user=@username,lm_date=@CreateDate where wo_code=@mesordercode and step_code=@stepcode and style='B'";
+                    //list.Add(new { str = sql, parm = new { reportqty = decimal.Parse(reportqty), ngqty = decimal.Parse(ngqty), mesordercode = mesordercode, stepcode = stepcode, username = username, CreateDate = date } });
+                    ////鍐欏叆瀛愯〃
+                    //for (int i = 0; i < arra.Length; i++)
+                    //{
+                    //    sql = @"insert into  TK_Wrk_RecordSub(m_id,eqp_code,report_person,report_date,report_qty,usergroup_code,ng_qty,style,lm_user,lm_date) 
+                    //            values(@m_id,@eqp_code,@report_person,@report_date,@report_qty,@usergroup_code,@ng_qty,@style,@lm_user,@lm_date)";
+                    //    list.Add(new { str = sql, parm = new { m_id = int.Parse(data.Rows[0]["ID"].ToString()), eqp_code = eqpcode, report_person = arra[i], report_date = date, report_qty = reportqty, usergroup_code = usergroupcode, ng_qty = ngqty, style = "B", lm_user = username, lm_date = date } });
+
+                    //}
+                    //if (badcode != "" && ngqty != "0")
+                    //{
+                    //    //鍐欏叆缂洪櫡璁板綍琛�
+                    //    for (int i = 0; i < arra1.Length; i++)
+                    //    {
+                    //        sql = @"insert into  CSR_WorkRecord_Defect(record_id,wo_code,partnumber,step_seq,step_code,defect_qty,defect_code,style,lm_user,lm_date) 
+                    //            values(@record_id,@wo_code,@partcode,@stepseq,@stepcode,@ngqty,@defect_code,@style,@lm_user,@lm_date)";
+                    //        list.Add(new { str = sql, parm = new { record_id = int.Parse(data.Rows[0]["ID"].ToString()), wo_code = mesordercode, partcode = partcode, stepseq = stepseq, stepcode = stepcode, ngqty = ngqty, defect_code = arra1[i], style = "B", lm_user = username, lm_date = date } });
+
+                    //    }
+                    //}
+                }
+                else
+                {
+                    //鑾峰彇涓昏〃鏈�澶D
+                    sql = @"select ISNULL(IDENT_CURRENT('TK_Wrk_Record')+1,1) as id";
+                    var dt = DapperHelper.selecttable(sql);
+                    //鍐欏叆寮�鎶ュ伐璁板綍琛�
+                    sql = @"insert into  TK_Wrk_Record(wo_code,step_seq,step_code,eqp_code,materiel_code,task_qty,start_qty,good_qty,ng_qty,style,lm_user,lm_date) 
+                                values(@mesordercode,@stepseq,@stepcode,@eqpcode,@partcode,@taskqty,@startqty,@reportqty,@ngqty,@style,@lm_user,@lm_date)";
+                    list.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepseq = stepseq, stepcode = stepcode, eqpcode = eqpcode, partcode = partcode, taskqty = taskqty, startqty = startqty, reportqty = reportqty, ngqty = ngqty, style = "B", lm_user = username, lm_date = date } });
+
+                    //鍐欏叆瀛愯〃
+                    for (int i = 0; i < arra.Count; i++)
+                    {
+                        sql = @"insert into  TK_Wrk_RecordSub(m_id,eqp_code,report_person,report_date,report_qty,reckway,usergroup_code,ratio,ng_qty,style,lm_user,lm_date) 
+                                values(@m_id,@eqp_code,@report_person,@report_date,@report_qty,@reckway,@usergroup_code,@ratio,@ng_qty,@style,@lm_user,@lm_date)";
+                        list.Add(new
+                        {
+                            str = sql,
+                            parm = new
+                            {
+                                m_id = int.Parse(dt.Rows[0]["ID"].ToString()),
+                                eqp_code = eqpcode,
+                                report_person = arra[i]["usercode"].ToString(),
+                                report_date = date,
+                                report_qty = reportqty,
+                                reckway = reckway,
+                                usergroup_code = usergroupcode,
+                                ratio = decimal.Parse(arra[i]["ratio"].ToString()),
+                                ng_qty = ngqty,
+                                style = "B",
+                                lm_user = username,
+                                lm_date = date
+                            }
+                        });
+
+                    }
+                    if (badcode != "" && ngqty != "0")
+                    {
+                        //鍐欏叆缂洪櫡璁板綍琛�
+                        for (int i = 0; i < arra1.Length; i++)
+                        {
+                            sql = @"insert into  CSR_WorkRecord_Defect(record_id,wo_code,partnumber,step_seq,step_code,defect_qty,defect_code,remarks,style,lm_user,lm_date) 
+                                values(@record_id,@wo_code,@partcode,@stepseq,@stepcode,@ngqty,@defect_code,@remarks,@style,@lm_user,@lm_date)";
+                            list.Add(new { str = sql, parm = new { record_id = int.Parse(dt.Rows[0]["ID"].ToString()), wo_code = mesordercode, partcode = partcode, stepseq = stepseq, stepcode = stepcode, ngqty = ngqty, defect_code = arra1[i], remarks = remarks, style = "B", lm_user = username, lm_date = date } });
 
                         }
                     }
@@ -1399,11 +2052,11 @@
 
                 //鍥炲啓宸ュ崟宸ュ簭琛ㄥ悎鏍兼暟閲忋�佷笉鑹暟閲�
                 sql = @"update TK_Wrk_Step set good_qty=good_qty+@reportqty,ng_qty=ng_qty+@ngqty  where wo_code=@mesordercode and step_code=@stepcode";
-                list.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepcode = stepcode,reportqty=reportqty,ngqty=ngqty } });
+                list.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepcode = stepcode, reportqty = reportqty, ngqty = ngqty } });
 
                 //鍥炲啓宸ュ崟琛ㄥ悎鏍兼暟閲忋�佷笉鑹暟閲�
-                sql = @"update TK_Wrk_Man set good_qty=good_qty+@reportqty,ng_qty=ng_qty+@ngqty  where wo_code=@mesordercode";
-                list.Add(new { str = sql, parm = new { mesordercode = mesordercode, reportqty = reportqty, ngqty = ngqty } });
+                //sql = @"update TK_Wrk_Man set good_qty=good_qty+@reportqty,ng_qty=ng_qty+@ngqty  where wo_code=@mesordercode";
+                //list.Add(new { str = sql, parm = new { mesordercode = mesordercode, reportqty = reportqty, ngqty = ngqty } });
 
                 bool aa = DapperHelper.DoTransaction(list);
                 if (aa)
@@ -1452,17 +2105,17 @@
                 {
                     //淇敼鍙戞枡璁板綍
                     sql = @"update TK_Wrk_OutRecord set fqty=fqty+@fqty,lm_user=@username,lm_date=@CreateDate
-                             where wo_code=@mesordercode and step_code=@stepcode and wx_code=@wx_code";
+                             where wo_code=@mesordercode and step_code=@stepcode and wx_code=@wx_code and style='F'";
                     list.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepcode = stepcode, wx_code = wxcode, fqty = decimal.Parse(fqty), username = username, CreateDate = date } });
                     //鍐欏叆瀛愯〃
-                    sql = @"insert into  TK_Wrk_OutRecordSub(m_id,wx_code,out_person,out_time,fqty,syle,lm_user,lm_date) 
-                                values(@m_id,@wx_code,@out_person,@out_time,@fqty,@lm_user,@lm_date)";
+                    sql = @"insert into  TK_Wrk_OutRecordSub(m_id,wx_code,out_person,out_time,fqty,style,lm_user,lm_date) 
+                                values(@m_id,@wx_code,@out_person,@out_time,@fqty,@style,@lm_user,@lm_date)";
                     list.Add(new { str = sql, parm = new { m_id = int.Parse(data.Rows[0]["ID"].ToString()), wx_code = wxcode, out_person = outuser, out_time = date, fqty = fqty, style = 'F', lm_user = username, lm_date = date } });
                 }
                 else
                 {
                     //鑾峰彇涓昏〃鏈�澶D
-                    sql = @"select IDENT_CURRENT('TK_Wrk_OutRecord')+1 as id";
+                    sql = @"select ISNULL(IDENT_CURRENT('TK_Wrk_OutRecord')+1,1) as id";
                     var dt = DapperHelper.selecttable(sql);
                     //鍐欏叆澶栧崗璁板綍涓昏〃
                     sql = @"insert into  TK_Wrk_OutRecord(wo_code,step_seq,step_code,wx_code,materiel_code,style,fqty,lm_user,lm_date) 
@@ -1510,7 +2163,7 @@
         #endregion
 
         #region[鐢熶骇寮�鎶ュ伐, 鏀舵枡鎻愪氦]
-        public static ToMessage SavaMesOrderStepIn(string mesordercode, string partcode, string stepseq, string stepcode, string wxcode, string inuser, string taskqty, string sqty, string ngqty, string badcode, string username)
+        public static ToMessage SavaMesOrderStepIn(string mesordercode, string partcode, string stepseq, string stepcode, string wxcode, string inuser, string taskqty, string sqty, string ngqty, string badcode, string remarks, string username)
         {
             var sql = "";
             string[] arra1 = new string[] { };
@@ -1528,38 +2181,67 @@
 
                 list.Clear();
                 //鍒ゆ柇鏄惁鏈夋敹鏂欒褰�(鏈夛細锛堝悓宸ュ崟+宸ュ簭+澶栧崗渚涙柟淇敼锛� 鏃狅細鏂板)
-                sql = @"select *  from TK_Wrk_OutRecord where wo_code=@wo_code and step_code=@step_code and wx_code=@wxcode and style='F'";
+                sql = @"select *  from TK_Wrk_OutRecord where wo_code=@wo_code and step_code=@step_code and wx_code=@wx_code and style='S'";
                 dynamicParams.Add("@wo_code", mesordercode);
                 dynamicParams.Add("@step_code", stepcode);
                 dynamicParams.Add("@wx_code", wxcode);
                 var data = DapperHelper.selectdata(sql, dynamicParams);
+                //鑾峰彇鍙戞枡璁板綍鐨勯粯璁ら�変腑鐨勫鍗忎緵搴斿晢涓庢敹鏂欐椂鐨勫鍗忎緵搴斿晢鍋氬姣斿垽鏂�
+                sql = @"select A.wx_code,B.name,A.fqty   from TK_Wrk_OutRecord A
+                        inner join TCustomer B on A.wx_code=B.code
+                        where A.wo_code=@wo_code and A.step_code=@step_code and wx_code=@wx_code and A.style='F' ";
+                dynamicParams.Add("@wo_code", mesordercode);
+                dynamicParams.Add("@step_code", stepcode);
+                dynamicParams.Add("@wx_code", wxcode);
+                var da = DapperHelper.selectdata(sql, dynamicParams);
+                if (da.Rows.Count <= 0)
+                {
+                    sql = @"select A.wx_code,B.name,A.fqty   from TK_Wrk_OutRecord A
+                        inner join TCustomer B on A.wx_code=B.code
+                        where A.wo_code=@wo_code and A.step_code=@step_code and A.style='F' ";
+                    dynamicParams.Add("@wo_code", mesordercode);
+                    dynamicParams.Add("@step_code", stepcode);
+                    var da1 = DapperHelper.selectdata(sql, dynamicParams);
+                    var dr = da1.AsEnumerable().ToList().Select(x => x.Field<string>("NAME")).ToList();
+                    string wxstring = (string.Join(",", dr.Select(x => x.ToString()).ToArray()));
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "鎿嶄綔澶辫触,褰撳墠鏀舵枡澶栧崗渚涙柟涓庡彂鏂欏鍗忎緵搴斿晢涓嶅尮閰�,搴斾负锛氥��" + wxstring + "銆�!";
+                    mes.data = null;
+                    return mes;
+                }
+                if ((decimal.Parse(sqty) + decimal.Parse(ngqty)) > decimal.Parse(da.Rows[0]["FQTY"].ToString()))  //鏀舵枡鏁伴噺+涓嶈壇鏁伴噺>鍙戞枡鏁伴噺
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "鎿嶄綔澶辫触,褰撳墠鏀舵枡鏁伴噺+涓嶈壇鏁伴噺,涓嶈兘澶т簬鍙戞枡鏁伴噺:" + da.Rows[0]["FQTY"].ToString() + "!";
+                    mes.data = null;
+                    return mes;
+                }
+                //if (da.Rows[0]["WX_CODE"].ToString() != wxcode)
+                //{
+                //    mes.code = "300";
+                //    mes.count = 0;
+                //    mes.Message = "鎿嶄綔澶辫触,褰撳墠鏀舵枡澶栧崗渚涙柟搴斾负锛�" + da.Rows[0]["NAME"].ToString() + "!";
+                //    mes.data = null;
+                //    return mes;
+                //}
                 if (data.Rows.Count > 0)
                 {
-                    //淇敼澶栧崗璁板綍涓昏〃
-                    sql = @"update TK_Wrk_OutRecord set sqty=sqty+@sqty,ng_qty=@ngqty,lm_user=@username,lm_date=@CreateDate
-                             where wo_code=@mesordercode and step_code=@stepcode and wx_code=@wx_code";
-                    list.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepcode = stepcode, wx_code = wxcode, sqty = decimal.Parse(sqty), ngqty = decimal.Parse(ngqty), username = username, CreateDate = date } });
-                    //鍐欏叆澶栧崗璁板綍瀛愯〃
-                    sql = @"insert into  TK_Wrk_OutRecordSub(m_id,wx_code,in_person,in_time,sqty,ng_qty,syle,lm_user,lm_date) 
-                                values(@m_id,@wx_code,@in_person,@in_time,@sqty,@ngqty,@lm_user,@lm_date)";
-                    list.Add(new { str = sql, parm = new { m_id = int.Parse(data.Rows[0]["ID"].ToString()), wx_code = wxcode, in_person = inuser, in_time = date, sqty = sqty, ngqty = ngqty, style = 'S', lm_user = username, lm_date = date } });
-
-                    if (badcode != "" || badcode != null)
+                    decimal sum_sqty = data.AsEnumerable().Select(d => d.Field<decimal>("SQTY")).Sum();  //鑾峰彇鍚屽崟鍙�,鍚屽伐搴�,鍚屽鍗忎緵搴斿晢鏀舵枡鎬绘暟閲�
+                    decimal sum_fqty = da.AsEnumerable().Select(d => d.Field<decimal>("FQTY")).Sum();  //鑾峰彇鍚屽崟鍙�,鍚屽伐搴�,鍚屽鍗忎緵搴斿晢鍙戞枡鎬绘暟閲�
+                    if ((sum_sqty + decimal.Parse(sqty) + decimal.Parse(ngqty)) > sum_fqty) //宸叉敹鏂欐�绘暟+褰撳墠鏀舵枡鏁伴噺+涓嶈壇鏁伴噺>鎬诲彂鏂欐暟閲�
                     {
-                        //鍐欏叆缂洪櫡璁板綍琛�
-                        for (int i = 0; i < arra1.Length; i++)
-                        {
-                            sql = @"insert into  CSR_WorkRecord_Defect(record_id,wo_code,partnumber,step_seq,step_code,defect_qty,defect_code,style,lm_user,lm_date) 
-                                values(@record_id,@wo_code,@partcode,@stepseq,@stepcode,@ngqty,@defect_code,@stepcode,@style,@lm_user,@lm_date)";
-                            list.Add(new { str = sql, parm = new { record_id = int.Parse(data.Rows[0]["ID"].ToString()), wo_code = mesordercode, partcode = partcode, stepseq = stepseq, stepcode = stepcode, ngqty = ngqty, defect_code = arra1[i], style = "S", lm_user = username, lm_date = date } });
-
-                        }
+                        mes.code = "300";
+                        mes.count = 0;
+                        mes.Message = "鎿嶄綔澶辫触,褰撳墠鏀舵枡鏁伴噺+涓嶈壇鏁伴噺,涓嶈兘澶т簬寰呮敹鏁伴噺:" + (sum_fqty - sum_sqty) + "!";
+                        mes.data = null;
+                        return mes;
                     }
-                }
-                else
-                {
+
+
                     //鑾峰彇涓昏〃鏈�澶D
-                    sql = @"select IDENT_CURRENT('TK_Wrk_OutRecord')+1 as id";
+                    sql = @"select ISNULL(IDENT_CURRENT('TK_Wrk_OutRecord')+1,1) as id";
                     var dt = DapperHelper.selecttable(sql);
                     //鍐欏叆澶栧崗璁板綍涓昏〃
                     sql = @"insert into  TK_Wrk_OutRecord(wo_code,step_seq,step_code,wx_code,materiel_code,style,sqty,ng_qty,lm_user,lm_date) 
@@ -1571,14 +2253,63 @@
                                 values(@m_id,@wxcode,@in_person,@in_time,@sqty,@ng_qty,@style,@lm_user,@lm_date)";
                     list.Add(new { str = sql, parm = new { m_id = int.Parse(dt.Rows[0]["ID"].ToString()), wxcode = wxcode, in_person = inuser, in_time = date, sqty = sqty, ng_qty = ngqty, style = "S", lm_user = username, lm_date = date } });
 
-                    if (badcode != "" || badcode != null)
+                    if (badcode != "" && ngqty != "0")
                     {
                         //鍐欏叆缂洪櫡璁板綍琛�
                         for (int i = 0; i < arra1.Length; i++)
                         {
-                            sql = @"insert into  CSR_WorkRecord_Defect(record_id,wo_code,partnumber,step_seq,step_code,defect_qty,defect_code,style,lm_user,lm_date) 
-                                values(@record_id,@wo_code,@partcode,@stepseq,@stepcode,@ngqty,@defect_code,@stepcode,@style,@lm_user,@lm_date)";
-                            list.Add(new { str = sql, parm = new { record_id = int.Parse(dt.Rows[0]["ID"].ToString()), wo_code = mesordercode, partcode = partcode, stepseq = stepseq, stepcode = stepcode, ngqty = ngqty, defect_code = arra1[i], style = "S", lm_user = username, lm_date = date } });
+                            sql = @"insert into  CSR_WorkRecord_Defect(record_id,wo_code,partnumber,step_seq,step_code,defect_qty,defect_code,remarks,style,lm_user,lm_date) 
+                                values(@record_id,@wo_code,@partcode,@stepseq,@stepcode,@ngqty,@defect_code,@remarks,@style,@lm_user,@lm_date)";
+                            list.Add(new { str = sql, parm = new { record_id = int.Parse(dt.Rows[0]["ID"].ToString()), wo_code = mesordercode, partcode = partcode, stepseq = stepseq, stepcode = stepcode, ngqty = ngqty, defect_code = arra1[i], remarks = remarks, style = "S", lm_user = username, lm_date = date } });
+
+                        }
+                    }
+
+
+                    ////淇敼澶栧崗璁板綍涓昏〃
+                    //sql = @"update TK_Wrk_OutRecord set sqty=sqty+@sqty,ng_qty=ng_qty+@ngqty,lm_user=@username,lm_date=@CreateDate
+                    //         where wo_code=@mesordercode and step_code=@stepcode and wx_code=@wx_code and style='S'";
+                    //list.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepcode = stepcode, wx_code = wxcode, sqty = decimal.Parse(sqty), ngqty = decimal.Parse(ngqty), username = username, CreateDate = date } });
+                    ////鍐欏叆澶栧崗璁板綍瀛愯〃
+                    //sql = @"insert into  TK_Wrk_OutRecordSub(m_id,wx_code,in_person,in_time,sqty,ng_qty,style,lm_user,lm_date) 
+                    //            values(@m_id,@wx_code,@in_person,@in_time,@sqty,@ngqty,@style,@lm_user,@lm_date)";
+                    //list.Add(new { str = sql, parm = new { m_id = int.Parse(data.Rows[0]["ID"].ToString()), wx_code = wxcode, in_person = inuser, in_time = date, sqty = sqty, ngqty = ngqty, style = 'S', lm_user = username, lm_date = date } });
+
+                    //if (badcode != "" && ngqty != "0")
+                    //{
+                    //    //鍐欏叆缂洪櫡璁板綍琛�
+                    //    for (int i = 0; i < arra1.Length; i++)
+                    //    {
+                    //        sql = @"insert into  CSR_WorkRecord_Defect(record_id,wo_code,partnumber,step_seq,step_code,defect_qty,defect_code,style,lm_user,lm_date) 
+                    //            values(@record_id,@wo_code,@partcode,@stepseq,@stepcode,@ngqty,@defect_code,@style,@lm_user,@lm_date)";
+                    //        list.Add(new { str = sql, parm = new { record_id = int.Parse(data.Rows[0]["ID"].ToString()), wo_code = mesordercode, partcode = partcode, stepseq = stepseq, stepcode = stepcode, ngqty = ngqty, defect_code = arra1[i], style = "S", lm_user = username, lm_date = date } });
+
+                    //    }
+                    //}
+                }
+                else
+                {
+                    //鑾峰彇涓昏〃鏈�澶D
+                    sql = @"select ISNULL(IDENT_CURRENT('TK_Wrk_OutRecord')+1,1) as id";
+                    var dt = DapperHelper.selecttable(sql);
+                    //鍐欏叆澶栧崗璁板綍涓昏〃
+                    sql = @"insert into  TK_Wrk_OutRecord(wo_code,step_seq,step_code,wx_code,materiel_code,style,sqty,ng_qty,lm_user,lm_date) 
+                                values(@mesordercode,@stepseq,@stepcode,@wx_code,@partcode,@style,@sqty,@ngqty,@lm_user,@lm_date)";
+                    list.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepseq = stepseq, stepcode = stepcode, wx_code = wxcode, partcode = partcode, style = 'S', sqty = sqty, ngqty = ngqty, lm_user = username, lm_date = date } });
+
+                    //鍐欏叆澶栧崗璁板綍瀛愯〃
+                    sql = @"insert into  TK_Wrk_OutRecordSub(m_id,wx_code,in_person,in_time,sqty,ng_qty,style,lm_user,lm_date) 
+                                values(@m_id,@wxcode,@in_person,@in_time,@sqty,@ng_qty,@style,@lm_user,@lm_date)";
+                    list.Add(new { str = sql, parm = new { m_id = int.Parse(dt.Rows[0]["ID"].ToString()), wxcode = wxcode, in_person = inuser, in_time = date, sqty = sqty, ng_qty = ngqty, style = "S", lm_user = username, lm_date = date } });
+
+                    if (badcode != "" && ngqty != "0")
+                    {
+                        //鍐欏叆缂洪櫡璁板綍琛�
+                        for (int i = 0; i < arra1.Length; i++)
+                        {
+                            sql = @"insert into  CSR_WorkRecord_Defect(record_id,wo_code,partnumber,step_seq,step_code,defect_qty,defect_code,remarks,style,lm_user,lm_date) 
+                                values(@record_id,@wo_code,@partcode,@stepseq,@stepcode,@ngqty,@defect_code,@remarks,@style,@lm_user,@lm_date)";
+                            list.Add(new { str = sql, parm = new { record_id = int.Parse(dt.Rows[0]["ID"].ToString()), wo_code = mesordercode, partcode = partcode, stepseq = stepseq, stepcode = stepcode, ngqty = ngqty, defect_code = arra1[i], remarks = remarks, style = "S", lm_user = username, lm_date = date } });
 
                         }
                     }
@@ -1588,8 +2319,8 @@
                 list.Add(new { str = sql, parm = new { mesordercode = mesordercode, stepcode = stepcode, sqty = sqty, ngqty = ngqty } });
 
                 //鍥炲啓宸ュ崟琛ㄥ悎鏍兼暟閲忋�佷笉鑹暟閲�
-                sql = @"update TK_Wrk_Man set good_qty=good_qty+@sqty,ng_qty=ng_qty+@ngqty  where wo_code=@mesordercode";
-                list.Add(new { str = sql, parm = new { mesordercode = mesordercode, sqty = sqty, ngqty = ngqty } });
+                //sql = @"update TK_Wrk_Man set good_qty=good_qty+@sqty,ng_qty=ng_qty+@ngqty  where wo_code=@mesordercode";
+                //list.Add(new { str = sql, parm = new { mesordercode = mesordercode, sqty = sqty, ngqty = ngqty } });
                 bool aa = DapperHelper.DoTransaction(list);
                 if (aa)
                 {
@@ -1617,5 +2348,1638 @@
         }
         #endregion'
 
+        #region[涓嶈壇澶勭悊,鎻愪氦]
+        public static ToMessage EditOrderNgStepSeave(ReportDefectHandle json, string username)
+        {
+            var sql = "";
+            string[] arra1 = new string[] { };
+            List<object> list = new List<object>();
+            var dynamicParams = new DynamicParameters();
+            decimal sumrepair_qty = 0, sumbad_qty = 0;  //绱缁翠慨鏁伴噺銆佺疮璁℃姤搴熸暟閲�
+            try
+            {
+                string date = DateTime.Now.ToString(); //鑾峰彇绯荤粺鏃堕棿
+                list.Clear();
+
+
+                //寰幆json鏁版嵁
+                for (int i = 0; i < json.Data.Rows.Count; i++)
+                {
+                    //鑷埗宸ュ簭
+                    if (json.Data.Rows[i]["STYLE"].ToString() == "Z")
+                    {
+                        //鍥炲啓瀵瑰簲鐨勬姤宸ヨ褰曞瓙琛ㄥ悎鏍兼暟閲忋�佷笉鑹暟閲忋�佹姤搴熸暟閲�
+                        sql = @"update TK_Wrk_RecordSub set report_qty=report_qty+@repair_qty,ng_qty=ng_qty-@repair_qty-@bad_qty,bad_qty=bad_qty+@bad_qty
+                                where  m_id=@m_id and style='B'";
+                        list.Add(new
+                        {
+                            str = sql,
+                            parm = new
+                            {
+                                m_id = int.Parse(json.Data.Rows[i]["ID"].ToString()),
+                                repair_qty = decimal.Parse(json.Data.Rows[i]["REPAIR_QTY"].ToString()),
+                                bad_qty = decimal.Parse(json.Data.Rows[i]["BAD_QTY"].ToString())
+                            }
+                        });
+                        //鍥炲啓瀵瑰簲鐨勬姤宸ヨ褰曚富琛ㄥ悎鏍兼暟閲忋�佷笉鑹暟閲忋�佹姤搴熸暟閲�
+                        sql = @"update TK_Wrk_Record set good_qty=good_qty+@repair_qty,ng_qty=ng_qty-@repair_qty-@bad_qty,bad_qty=bad_qty+@bad_qty
+                        where wo_code=@wo_code and step_code=@step_code and id=@id and style='B'";
+                        list.Add(new
+                        {
+                            str = sql,
+                            parm = new
+                            {
+                                repair_qty = decimal.Parse(json.Data.Rows[i]["REPAIR_QTY"].ToString()),
+                                bad_qty = decimal.Parse(json.Data.Rows[i]["BAD_QTY"].ToString()),
+                                wo_code = json.Data.Rows[i]["WO_CODE"].ToString(),
+                                step_code = json.Data.Rows[i]["STEP_CODE"].ToString(),
+                                id = int.Parse(json.Data.Rows[i]["ID"].ToString())
+                            }
+                        });
+                        //鍐欏叆鎶ュ伐缂洪櫡澶勭悊璁板綍琛�
+                        sql = @"insert into  CSR_WorkRecord_DefectHandle(record_subid,wo_code,partnumber,step_seq,step_code,repair_qty,bad_qty,defect_code,style,lm_user,lm_date) 
+                                values(@record_subid,@wo_code,@partcode,@stepseq,@stepcode,@repair_qty,@bad_qty,@defect_code,@style,@lm_user,@lm_date)";
+                        list.Add(new
+                        {
+                            str = sql,
+                            parm = new
+                            {
+                                record_subid = int.Parse(json.Data.Rows[i]["M_ID"].ToString()),
+                                wo_code = json.Data.Rows[i]["WO_CODE"].ToString(),
+                                partcode = json.Data.Rows[i]["MATERIEL_CODE"].ToString(),
+                                stepseq = json.Data.Rows[i]["SEQ"].ToString(),
+                                stepcode = json.Data.Rows[i]["STEP_CODE"].ToString(),
+                                repair_qty = decimal.Parse(json.Data.Rows[i]["REPAIR_QTY"].ToString()),
+                                bad_qty = decimal.Parse(json.Data.Rows[i]["BAD_QTY"].ToString()),
+                                defect_code = json.Data.Rows[i]["DEFECT_CODE"].ToString(),
+                                style = "B",
+                                lm_user = username,
+                                lm_date = date
+                            }
+                        });
+
+                        sumrepair_qty = sumrepair_qty + decimal.Parse(json.Data.Rows[i]["REPAIR_QTY"].ToString());
+                        sumbad_qty = sumbad_qty + decimal.Parse(json.Data.Rows[i]["BAD_QTY"].ToString());
+                    }
+                    //澶栧崗宸ュ簭
+                    if (json.Data.Rows[i]["STYLE"].ToString() == "S")
+                    {
+                        //鍥炲啓瀵瑰簲鐨勬敹鏂欒褰曞瓙琛ㄦ敹鏂欐暟閲忋�佷笉鑹暟閲忋�佹姤搴熸暟閲�
+                        sql = @"update TK_Wrk_OutRecordSub set sqty=sqty+@repair_qty,ng_qty=ng_qty-@repair_qty-@bad_qty,bad_qty=bad_qty+@bad_qty
+                                where  m_id=@m_id and style='S'";
+                        list.Add(new
+                        {
+                            str = sql,
+                            parm = new
+                            {
+                                m_id = int.Parse(json.Data.Rows[i]["ID"].ToString()),
+                                repair_qty = decimal.Parse(json.Data.Rows[i]["REPAIR_QTY"].ToString()),
+                                bad_qty = decimal.Parse(json.Data.Rows[i]["BAD_QTY"].ToString())
+                            }
+                        });
+                        //鍥炲啓瀵瑰簲鐨勬敹鏂欒褰曚富琛ㄥ悎鏍兼暟閲忋�佷笉鑹暟閲忋�佹姤搴熸暟閲�
+                        sql = @"update TK_Wrk_OutRecord set sqty=sqty+@repair_qty,ng_qty=ng_qty-@repair_qty-@bad_qty,bad_qty=bad_qty+@bad_qty
+                        where wo_code=@wo_code and step_code=@step_code and id=@id and style='S'";
+                        list.Add(new
+                        {
+                            str = sql,
+                            parm = new
+                            {
+                                repair_qty = decimal.Parse(json.Data.Rows[i]["REPAIR_QTY"].ToString()),
+                                bad_qty = decimal.Parse(json.Data.Rows[i]["BAD_QTY"].ToString()),
+                                wo_code = json.Data.Rows[i]["WO_CODE"].ToString(),
+                                step_code = json.Data.Rows[i]["STEP_CODE"].ToString(),
+                                id = int.Parse(json.Data.Rows[i]["ID"].ToString())
+                            }
+                        });
+                        //鍐欏叆鎶ュ伐缂洪櫡澶勭悊璁板綍琛�
+                        sql = @"insert into  CSR_WorkRecord_DefectHandle(record_subid,wo_code,partnumber,step_seq,step_code,repair_qty,bad_qty,defect_code,style,lm_user,lm_date) 
+                                values(@record_subid,@wo_code,@partcode,@stepseq,@stepcode,@repair_qty,@bad_qty,@defect_code,@style,@lm_user,@lm_date)";
+                        list.Add(new
+                        {
+                            str = sql,
+                            parm = new
+                            {
+                                record_subid = int.Parse(json.Data.Rows[i]["M_ID"].ToString()),
+                                wo_code = json.Data.Rows[i]["WO_CODE"].ToString(),
+                                partcode = json.Data.Rows[i]["MATERIEL_CODE"].ToString(),
+                                stepseq = json.Data.Rows[i]["SEQ"].ToString(),
+                                stepcode = json.Data.Rows[i]["STEP_CODE"].ToString(),
+                                repair_qty = decimal.Parse(json.Data.Rows[i]["REPAIR_QTY"].ToString()),
+                                bad_qty = decimal.Parse(json.Data.Rows[i]["BAD_QTY"].ToString()),
+                                defect_code = json.Data.Rows[i]["DEFECT_CODE"].ToString(),
+                                style = "S",
+                                lm_user = username,
+                                lm_date = date
+                            }
+                        });
+                        sumrepair_qty = sumrepair_qty + decimal.Parse(json.Data.Rows[i]["REPAIR_QTY"].ToString());
+                        sumbad_qty = sumbad_qty + decimal.Parse(json.Data.Rows[i]["BAD_QTY"].ToString());
+                    }
+                }
+
+                //鍥炲啓宸ュ崟宸ュ簭琛ㄥ悎鏍兼暟閲忋�佷笉鑹暟閲�
+                sql = @"update TK_Wrk_Step set good_qty=good_qty+@sumrepair_qty,ng_qty=ng_qty-@sumrepair_qty-@sumbad_qty,bad_qty=bad_qty+@sumbad_qty  where wo_code=@wo_code and step_code=@stepcode";
+                list.Add(new
+                {
+                    str = sql,
+                    parm = new
+                    {
+                        wo_code = json.Data.Rows[0]["WO_CODE"].ToString(),
+                        stepcode = json.Data.Rows[0]["STEP_CODE"].ToString(),
+                        sumrepair_qty = sumrepair_qty,
+                        sumbad_qty = sumbad_qty
+                    }
+                });
+                bool aa = DapperHelper.DoTransaction(list);
+                if (aa)
+                {
+                    mes.code = "200";
+                    mes.count = 0;
+                    mes.Message = "鎿嶄綔鎴愬姛!";
+                    mes.data = null;
+                }
+                else
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "鎿嶄綔澶辫触!";
+                    mes.data = null;
+                }
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return mes;
+        }
+        #endregion
+
+        #region[鐢熶骇寮�鎶ュ伐,宸ュ簭妫�楠屾壂鐮佽幏鍙栦换鍔′俊鎭痌
+        public static ToMessage MesOrderStepCheckSearch(string orderstepqrcode)
+        {
+            var sql = "";
+            string ordercode = "";
+            string stepcode = "";
+            var dynamicParams = new DynamicParameters();
+            try
+            {
+                if (orderstepqrcode != "" && orderstepqrcode != null)
+                {
+                    string[] arra = orderstepqrcode.Split(';');
+                    if (arra.Length == 1) //宸ュ崟鍙蜂簩缁寸爜
+                    {
+                        mes.code = "300";
+                        mes.count = 0;
+                        mes.Message = "璇锋壂鎻忓伐搴忔潯鐮�!";
+                        mes.data = null;
+                        return mes;
+                    }
+                    if (arra.Length == 2) //宸ュ崟鍙�+宸ュ簭鍙蜂簩缁寸爜
+                    {
+                        ordercode = arra[0]; //鑾峰彇鎸囧畾瀛楃涓插墠闈㈢殑瀛楃
+                        stepcode = arra[1]; //鑾峰彇鎸囧畾瀛楃涓插墠闈㈢殑瀛楃
+                    }
+                    //閫氳繃鎵弿浜岀淮鐮佷俊鎭煡鎵句换鍔′俊鎭�
+                    sql = @"select A.wo_code,M.partcode,M.partname,M.partspec,S.stepcode,S.stepname  
+                            from TK_Wrk_Step A
+                            inner join TK_Wrk_Man P on A.wo_code=P.wo_code
+                            left join TMateriel_Info M on P.materiel_code=M.partcode
+                            left join TStep S on A.step_code=S.stepcode
+                            where A.wo_code=@wo_code and A.step_code=@stepcode";
+                    dynamicParams.Add("@wo_code", ordercode);
+                    dynamicParams.Add("@stepcode", stepcode);
+                    var data = DapperHelper.selectdata(sql, dynamicParams);
+                    if (data.Rows.Count > 0)
+                    {
+                        mes.code = "200";
+                        mes.count = 0;
+                        mes.Message = "鏌ヨ鎴愬姛!";
+                        mes.data = data;
+                        return mes;
+                    }
+                    else
+                    {
+                        mes.code = "300";
+                        mes.count = 0;
+                        mes.Message = "鐢熶骇浠诲姟涓嶅瓨鍦�!";
+                        mes.data = null;
+                        return mes;
+                    }
+                }
+                else
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "浜岀淮鐮佷俊鎭负绌�!";
+                    mes.data = null;
+                    return mes;
+                }
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return mes;
+        }
+        #endregion
+
+        #region[鐢熶骇寮�鎶ュ伐,宸ュ簭妫�楠岃幏鍙栨楠屾爣鍑嗕笅鎷夋鏁版嵁]
+        public static ToMessage MesOrderStepCheckSelect()
+        {
+            string sql = "";
+            var dynamicParams = new DynamicParameters();
+            try
+            {
+                //鑾峰彇宸ュ簭妫�楠屾爣鍑�
+                sql = @"select code,name  from TStepCheckStandard where is_delete<>'1'";
+                var data = DapperHelper.selecttable(sql);
+                mes.code = "200";
+                mes.Message = "鏌ヨ鎴愬姛!";
+                mes.data = data;
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return mes;
+        }
+        #endregion
+
+        #region[鐢熶骇寮�鎶ュ伐,宸ュ簭妫�楠屾牴鎹楠屾爣鍑嗚幏鍙栨楠岄」鐩垪琛╙
+        public static ToMessage MesOrderStepCheckItemList(string checkstandcode)
+        {
+            string sql = "";
+            var dynamicParams = new DynamicParameters();
+            try
+            {
+                //鏍规嵁妫�楠屾爣鍑嗙紪鐮佽幏鍙栧伐搴忔楠岄」鐩�
+                sql = @"select B.code,B.name,A.stepcheckitem_desc,A.stepcheckitem_seq   from TStepCheckStandardSub A
+                        left join TStepCheckItem B on A.stepcheckitem_code=B.code
+                        where B.is_delete<>'1' and A.stepstaned_code=@checkstandcode";
+                dynamicParams.Add("@checkstandcode", checkstandcode);
+                var data = DapperHelper.selectdata(sql, dynamicParams);
+                mes.code = "200";
+                mes.Message = "鏌ヨ鎴愬姛!";
+                mes.data = data;
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return mes;
+        }
+        #endregion
+
+        #region[鐢熶骇寮�鎶ュ伐,宸ュ簭妫�楠屾彁浜や繚瀛榏
+        public static ToMessage SaveMesOrderStepCheckItem(string mesordercode, string partcode, string stepcode, string checkstanedcode, string checkusercode, string checktypecode, string checkresult, string checkdescr, string checkqty, string username, List<StepCheck> json)
+        {
+            var sql = "";
+            string[] arra = new string[] { };
+            string[] arra1 = new string[] { };
+            List<object> list = new List<object>();
+            string checktypename = "";
+            var dynamicParams = new DynamicParameters();
+            try
+            {
+                string date = DateTime.Now.ToString(); //鑾峰彇绯荤粺鏃堕棿
+
+                list.Clear();
+                switch (checktypecode)
+                {
+                    case "FirstCheck":
+                        checktypename = "棣栨";
+                        break;
+                    case "PatroCheck":
+                        checktypename = "宸℃";
+                        break;
+                    case "EndCheck":
+                        checktypename = "瀹屽伐妫�楠�";
+                        break;
+                    default:
+                        break;
+                }
+                //鍐欏叆宸ュ簭妫�楠岃褰曚富琛�
+                sql = @"insert into  TStepCheckRecord(wo_code,partcode,step_code,checkstaned_code,check_user,check_type,check_typename,check_result,check_descr,check_qty,lm_user,lm_date) 
+                                values(@mesordercode,@partcode,@stepcode,@checkstanedcode,@checkusercode,@checktypecode,@checktypename,@checkresult,@checkdescr,@check_qty,@lm_user,@lm_date)";
+                list.Add(new { str = sql, parm = new { mesordercode = mesordercode, partcode = partcode, stepcode = stepcode, checkstanedcode = checkstanedcode, checkusercode = checkusercode, checktypecode = checktypecode, checktypename = checktypename, checkresult = checkresult, checkdescr = checkdescr, check_qty = checkqty, lm_user = username, lm_date = date } });
+                //鍐欏叆宸ュ簭妫�楠岃褰曞瓙琛�
+                //鑾峰彇涓昏〃鏈�澶D
+                sql = @"select ISNULL(IDENT_CURRENT('TStepCheckRecord')+1,1) as id";
+                var dt = DapperHelper.selecttable(sql);
+                for (int i = 0; i < json.Count; i++)
+                {
+                    sql = @"insert into  TStepCheckRecordSub(m_id,checkitem_seq,checkitem_code,checkitem_name,checkitem_descr,check_result,lm_user,lm_date) 
+                                values(@m_id,@checkiem_seq,@checkitem_code,@checkitem_name,@checkitem_descr,@check_result,@lm_user,@lm_date)";
+                    list.Add(new { str = sql, parm = new { m_id = int.Parse(dt.Rows[0]["ID"].ToString()), checkiem_seq = int.Parse(json[i].seq), checkitem_code = json[i].code, checkitem_name = json[i].name, checkitem_descr = json[i].descr, check_result = json[i].checkresult, lm_user = username, lm_date = date } });
+
+                }
+
+                bool aa = DapperHelper.DoTransaction(list);
+                if (aa)
+                {
+                    mes.code = "200";
+                    mes.count = 0;
+                    mes.Message = "妫�楠屾垚鍔�!";
+                    mes.data = null;
+                }
+                else
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "妫�楠屽け璐�!";
+                    mes.data = null;
+                }
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return mes;
+        }
+        #endregion
+
+
+        #region[MES宸ュ崟鎵归噺鍏抽棴鏌ヨ]
+        public static ToMessage MesOrderBitchClosedSearch(string mesorderstus, string mesordercode, string sourceorder, string ordertype, string partcode, string partname, string partspec, int startNum, string creatuser, string createdate, int endNum, string prop, string order)
+        {
+            var dynamicParams = new DynamicParameters();
+            string search = "";
+            try
+            {
+                if (mesorderstus != "" && mesorderstus != null)
+                {
+                    search += "and A.status=@mesorderstus ";
+                    dynamicParams.Add("@mesorderstus", mesorderstus);
+                }
+                if (mesordercode != "" && mesordercode != null)
+                {
+                    search += "and A.wo_code like '%'+@mesordercode+'%' ";
+                    dynamicParams.Add("@mesordercode", mesordercode);
+                }
+                if (sourceorder != "" && sourceorder != null)
+                {
+                    search += "and A.m_po like '%'+@sourceorder+'%' ";
+                    dynamicParams.Add("@sourceorder", sourceorder);
+                }
+                if (ordertype != "" && ordertype != null)
+                {
+                    search += "and A.wotype like '%'+@ordertype+'%' ";
+                    dynamicParams.Add("@ordertype", ordertype);
+                }
+                if (partcode != "" && partcode != null)
+                {
+                    search += "and A.materiel_code like '%'+@partcode+'%' ";
+                    dynamicParams.Add("@partcode", partcode);
+                }
+                if (partname != "" && partname != null)
+                {
+                    search += "and B.partname like '%'+@partname+'%' ";
+                    dynamicParams.Add("@partname", partname);
+                }
+                if (partspec != "" && partspec != null)
+                {
+                    search += "and B.partspec like '%'+@partspec+'%' ";
+                    dynamicParams.Add("@partspec", partspec);
+                }
+                if (createdate != "" && createdate != null)
+                {
+                    search += "and CONVERT(varchar(100),A.lm_date,23)=@createdate ";
+                    dynamicParams.Add("@createdate", createdate);
+                }
+                if (creatuser != "" && creatuser != null)
+                {
+                    search += "and U.username like '%'+@creatuser+'%' ";
+                    dynamicParams.Add("@creatuser", creatuser);
+                }
+
+                if (search == "")
+                {
+                    search = "and 1=1 ";
+                }
+                // --------------鏌ヨ鎸囧畾鏁版嵁--------------
+                var total = 0; //鎬绘潯鏁�
+                var sql = @"select A.id, A.status,A.wotype,A.wo_code,A.materiel_code as partcode,B.partname,B.partspec,A.plan_qty,A.wkshp_code,C.org_name as wkshp_name,
+                            A.route_code,E.name as route_name,A.stck_code,F.name as stck_name,A.plan_startdate,A.plan_enddate,A.piroque,A.sourceid,A.m_po,U.username as lm_user,A.lm_date
+                            ,(select sum(S.good_qty)  from TK_Wrk_Step S where S.wo_code=A.wo_code and S.isend='Y') as good_qty
+                            ,(select sum(S.ng_qty)  from TK_Wrk_Step S where S.wo_code=A.wo_code and S.isend='Y') as ng_qty
+                            ,(select sum(S.bad_qty)  from TK_Wrk_Step S where S.wo_code=A.wo_code and S.isend='Y') as bad_qty
+                            from TK_Wrk_Man A
+                            left join TMateriel_Info B on A.materiel_code=B.partcode
+                            left join TOrganization C on A.wkshp_code=C.org_code
+                            left join T_Sec_Stck D on A.stck_code=D.code 
+                            left join TFlw_Rout E on A.route_code=E.code
+                            left join T_Sec_Stck F on A.stck_code=F.code 
+                            left join TUser U on A.lm_user=U.usercode 
+                            where A.is_delete<>'1' " + search;
+                var data = DapperHelper.GetPageList<object>(sql, dynamicParams, prop, order, startNum, endNum, out total);
+                mes.code = "200";
+                mes.Message = "鏌ヨ鎴愬姛!";
+                mes.count = total;
+                mes.data = data.ToList();
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return mes;
+        }
+        #endregion
+
+        #region[MES宸ュ崟鎵归噺鍏抽棴鎻愪氦]
+        public static ToMessage MesOrderBitchClosedSeave(string username, DataTable dt)
+        {
+            var sql = "";
+            List<object> list = new List<object>();
+            var dynamicParams = new DynamicParameters();
+            try
+            {
+                for (int i = 0; i < dt.Rows.Count; i++)
+                {
+                    //鍏抽棴宸ュ崟瀵瑰簲宸ュ簭浠诲姟
+                    sql = @"update TK_Wrk_Step set status='CLOSED'  where wo_code=@wocode";
+                    list.Add(new { str = sql, parm = new { wocode = dt.Rows[i]["WO_CODE"].ToString() } });
+                    //鍥炲啓宸ュ崟琛ㄧ姸鎬佷负(鍏抽棴)
+                    sql = @"update TK_Wrk_Man set status='CLOSED',closeuser=@username,closedate=@closedate  where wo_code=@wocode";
+                    list.Add(new { str = sql, parm = new { wocode = dt.Rows[i]["WO_CODE"].ToString(), username = username, closedate = DateTime.Now.ToString() } });
+                }
+                bool aa = DapperHelper.DoTransaction(list);
+                if (aa)
+                {
+                    mes.code = "200";
+                    mes.count = 0;
+                    mes.Message = "宸ュ崟鍏抽棴鎴愬姛!";
+                    mes.data = null;
+                }
+                else
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "宸ュ崟鍏抽棴澶辫触!";
+                    mes.data = null;
+                }
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return mes;
+        }
+        #endregion
+
+        #region[MES宸ュ崟鎵归噺鍙嶅叧闂璢
+        public static ToMessage MesOrderBitchAntiClosedSeave(string username, DataTable dt)
+        {
+            var sql = "";
+            List<object> list = new List<object>();
+            var dynamicParams = new DynamicParameters();
+            try
+            {
+                for (int i = 0; i < dt.Rows.Count; i++)
+                {
+                    //鍏抽棴宸ュ崟瀵瑰簲宸ュ簭浠诲姟
+                    sql = @"update TK_Wrk_Step set status='START'  where wo_code=@wocode";
+                    list.Add(new { str = sql, parm = new { wocode = dt.Rows[i]["WO_CODE"].ToString() } });
+                    //鍥炲啓宸ュ崟琛ㄧ姸鎬佷负(鍏抽棴)
+                    sql = @"update TK_Wrk_Man set status='START',closeuser=@username,closedate=@closedate  where wo_code=@wocode";
+                    list.Add(new { str = sql, parm = new { wocode = dt.Rows[i]["WO_CODE"].ToString(), username = username, closedate = DateTime.Now.ToString() } });
+                }
+                bool aa = DapperHelper.DoTransaction(list);
+                if (aa)
+                {
+                    mes.code = "200";
+                    mes.count = 0;
+                    mes.Message = "宸ュ崟鍏抽棴鎴愬姛!";
+                    mes.data = null;
+                }
+                else
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "宸ュ崟鍏抽棴澶辫触!";
+                    mes.data = null;
+                }
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return mes;
+        }
+        #endregion
+
+
+
+        #region[浜ц兘瑙勫垝璁剧疆鏌ヨ]
+        public static ToMessage CapacityPlanningSetupSearch()
+        {
+            string sql = "";
+            try
+            {
+                sql = @"select code,name,wktme1_start,wktme2_start,wktme3_start,wktme4_start,wktme5_start,
+                        lm_user,lm_date,duration  
+                        from  TBas_wkshift_info";
+                var data = DapperHelper.selecttable(sql);
+                mes.code = "200";
+                mes.Message = "鏌ヨ鎴愬姛!";
+                mes.data = data;
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return mes;
+        }
+        #endregion
+
+        #region[浜ц兘瑙勫垝璁剧疆鏂板]
+        public static ToMessage CapacityPlanningSetupAddUpdate(string username, List<CapacityPlanningSetup> objs)
+        {
+            var sql = "";
+            List<object> list = new List<object>();
+            var dynamicParams = new DynamicParameters();
+            try
+            {
+                //鍒ゆ柇缂栫爜鏄惁閲嶅
+                sql = @"select *  from TBas_wkshift_info where code=@code";
+                dynamicParams.Add("@code", objs[0].CapSetupCode);
+                var data = DapperHelper.selectdata(sql, dynamicParams);
+                if (data.Rows.Count > 0)
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "缂栫爜宸插瓨鍦�,涓嶈兘閲嶅!";
+                    mes.data = null;
+                    return mes;
+                }
+                //鍒ゆ柇鍚嶇О鏄惁閲嶅
+                sql = @"select *  from TBas_wkshift_info where name=@name";
+                dynamicParams.Add("@name", objs[0].CapSetupName);
+                var data0 = DapperHelper.selectdata(sql, dynamicParams);
+                if (data0.Rows.Count > 0)
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "鍚嶇О宸插瓨鍦�,涓嶈兘閲嶅!";
+                    mes.data = null;
+                    return mes;
+                }
+                list.Clear();
+                //寰幆鍐欏叆璁惧绫诲瀷琛�
+                for (int i = 0; i < objs.Count; i++)
+                {
+                    sql = @"insert into TBas_wkshift_info(code, name, wktme1_start, wktme2_start, wktme3_start, wktme4_start, wktme5_start, lm_user, lm_date, duration)
+                            values(@code,@name,@wktme1_start,@wktme2_start,@wktme3_start,@wktme4_start,@wktme5_start,@lm_user,@lm_date,@duration)";
+                    list.Add(new
+                    {
+                        str = sql,
+                        parm = new
+                        {
+                            code = objs[i].CapSetupCode,
+                            name = objs[i].CapSetupName,
+                            wktme1_start = objs[i].OneStartDate,
+                            wktme2_start = objs[i].TwoStartDate,
+                            wktme3_start = objs[i].ThreeStartDate,
+                            wktme4_start = objs[i].FourStartDate,
+                            wktme5_start = objs[i].FiveStartDate,
+                            lm_user = username,
+                            lm_date = DateTime.Now.ToString(),
+                            duration = objs[i].Duration
+                        }
+                    });
+                }
+                bool aa = DapperHelper.DoTransaction(list);
+                if (aa)
+                {
+                    mes.code = "200";
+                    mes.count = 0;
+                    mes.Message = "鎿嶄綔鎴愬姛!";
+                    mes.data = null;
+                }
+                else
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "鎿嶄綔澶辫触!";
+                    mes.data = null;
+                }
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return mes;
+        }
+        #endregion
+
+        #region[浜ц兘瑙勫垝鏂规璁剧疆鍒犻櫎]
+        public static ToMessage CapacityPlanningSetupDelete(string username, string capsetupcode)
+        {
+            var sql = "";
+            List<object> list = new List<object>();
+            var dynamicParams = new DynamicParameters();
+            try
+            {
+                //鍒ゆ柇鏂规鏄惁琚紩鐢�
+                sql = @"select *  from TWkm_capac_plan where wkshift_code=@capsetupcode";
+                dynamicParams.Add("@capsetupcode", capsetupcode);
+                var data = DapperHelper.selectdata(sql, dynamicParams);
+                if (data.Rows.Count > 0)
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "鍒犻櫎澶辫触锛�,璇ユ柟妗堝凡缁忚浜ц兘瑙勫垝璧勬簮寮曠敤!";
+                    mes.data = null;
+                    return mes;
+                }
+                list.Clear();
+                //鍒犻櫎鏂规
+
+                sql = @"delete TBas_wkshift_info where code=@code";
+                list.Add(new
+                {
+                    str = sql,
+                    parm = new
+                    {
+                        code = capsetupcode
+                    }
+                });
+
+                bool aa = DapperHelper.DoTransaction(list);
+                if (aa)
+                {
+                    mes.code = "200";
+                    mes.count = 0;
+                    mes.Message = "鍒犻櫎鎴愬姛!";
+                    mes.data = null;
+                }
+                else
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "鍒犻櫎澶辫触!";
+                    mes.data = null;
+                }
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return mes;
+        }
+        #endregion
+
+        #region[浜ц兘瑙勫垝鏌ヨ]
+        public static ToMessage CapacityPlanningSearch(string workshop, string devicetype, string stustype, int startNum, int endNum, string prop, string order)
+        {
+            var dynamicParams = new DynamicParameters();
+            string search = "";
+            try
+            {
+                if (workshop != "" && workshop != null)
+                {
+                    search += "and A.wksp_code=@workshop ";
+                    dynamicParams.Add("@workshop", workshop);
+                }
+                if (devicetype != "" && devicetype != null)
+                {
+                    search += "and A.code=@devicetype ";
+                    dynamicParams.Add("@devicetype", devicetype);
+                }
+                if (stustype != "" && stustype != null)
+                {
+                    search += "and B.enable=@stustype ";
+                    dynamicParams.Add("@stustype", stustype);
+                }
+                if (search == "")
+                {
+                    search = "and 1=1 ";
+                }
+                // --------------鏌ヨ鎸囧畾鏁版嵁--------------
+                var total = 0; //鎬绘潯鏁�
+                var sql = @"select 
+                            B.id             CaptPlanId, 
+                            A.wksp_code      CaptPlanWorkShopid,
+                            C.org_name       CaptPlanWorkShop,
+                            A.code           CaptPlanDeviceTypeid,
+                            A.name           CaptPlanDeviceType,
+                            A.ClassType      CaptPlanType,
+                            E.aa             CaptPlanShopCalendar,
+                            E.RR             CaptPlanShopMaxDate,
+                            E.TT             CaptPlanShopCalendarList,
+                            B.wkshift_code   CaptPlanWorkShiftCode,
+                            D.name           CaptPlanWorkShift,
+                            D.duration       CaptPlanDuration, 
+                            B.lm_date        CaptPlanDate,
+                            B.ENABLE         CaptPlanStus,                     
+                            G.username       CaptPlanUser
+                            FROM( 
+                            select distinct B.wksp_code,A.code,A.name,'D' ClassType   from TEqpType A
+                            left join TEqpInfo B on A.code=B.eqptype_code
+                            left join TFlw_Rteqp C on B.code=C.eqp_code
+                            left join TFlw_Rtdt  D on C.step_code=D.step_code
+                            where D.first_choke='Y'  --order by A.code
+                            )A
+                            left join TOrganization C on A.wksp_code=C.org_code
+                            left join TWkm_capac_plan B ON A.wksp_code=B.wkshop and A.code=B.eqp_typecode 
+                            left join TBas_wkshift_info D on B.wkshift_code=D.code
+                            left join TUser  G   on B.lm_user=G.usercode
+                            left join (
+                               select m_id, 
+                               min(CONVERT(varchar(100), wkdate, 23))+'~'+max(CONVERT(varchar(100), wkdate, 23)) aa,
+                               max(CONVERT(varchar(100), wkdate, 23))RR,
+                               STUFF((
+                                    SELECT ',' + CONVERT(varchar(100),t1.wkdate, 23)
+                                      FROM TWkm_capac_plan_sub t1
+                                      WHERE t1.m_id = t0.m_id
+                                      ORDER BY t1.m_id
+                                      FOR XML PATH('')), 1, LEN(','), '') AS TT
+                                      FROM TWkm_capac_plan_sub t0 where CONVERT(varchar(100), wkdate, 23)>=CONVERT(varchar(100),getdate(), 23) 
+                                      GROUP BY t0.m_id  
+                             ) E on B.id=E.m_id
+                            where C.description='W' " + search;
+                var data = DapperHelper.GetPageList<object>(sql, dynamicParams, prop, order, startNum, endNum, out total);
+                mes.code = "200";
+                mes.Message = "鏌ヨ鎴愬姛!";
+                mes.count = total;
+                mes.data = data.ToList();
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return mes;
+        }
+        #endregion
+
+        #region[浜ц兘瑙勫垝榛樿鏂规淇濆瓨鎻愪氦]
+        public static ToMessage CapacityPlanSubmit(string type, string captplanid, string wkshopcode, string capunitcode, string capsetupcode, string captplantype, string username)
+        {
+            var dynamicParams = new DynamicParameters();
+            try
+            {
+                if (type == "Add")
+                {
+                    var sql = @"insert into TWkm_capac_plan(wkshop, eqp_typecode, wkshift_code, enable, lm_user, lm_date,classtype) 
+                            values(@wkshop,@eqp_typecode,@wkshift_code,@enable,@lm_user,@lm_date,@classtype)";
+                    dynamicParams.Add("@wkshop", wkshopcode);
+                    dynamicParams.Add("@eqp_typecode", capunitcode);
+                    dynamicParams.Add("@wkshift_code", capsetupcode);
+                    dynamicParams.Add("@enable", "Y");
+                    dynamicParams.Add("@lm_user", username);
+                    dynamicParams.Add("@lm_date", DateTime.Now.ToString());
+                    dynamicParams.Add("@classtype", captplantype);
+                    int cont = DapperHelper.SQL(sql, dynamicParams);
+                    if (cont > 0)
+                    {
+                        mes.code = "200";
+                        mes.count = 0;
+                        mes.Message = "鏂板鎿嶄綔鎴愬姛!";
+                        mes.data = null;
+                    }
+                    else
+                    {
+                        mes.code = "300";
+                        mes.count = 0;
+                        mes.Message = "鏂板鎿嶄綔澶辫触!";
+                        mes.data = null;
+                    }
+                }
+                if (type == "Update")
+                {
+                    var sql = @"update TWkm_capac_plan set wkshift_code=@wkshift_code,lm_user=@username,lm_date=@CreateDate where id=@captplanid";
+                    dynamicParams.Add("@captplanid", captplanid);
+                    dynamicParams.Add("@wkshift_code", capsetupcode);
+                    dynamicParams.Add("@username", username);
+                    dynamicParams.Add("@CreateDate", DateTime.Now.ToString());
+                    int cont = DapperHelper.SQL(sql, dynamicParams);
+                    if (cont > 0)
+                    {
+                        mes.code = "200";
+                        mes.count = 0;
+                        mes.Message = "淇敼鎿嶄綔鎴愬姛!";
+                        mes.data = null;
+                    }
+                    else
+                    {
+                        mes.code = "300";
+                        mes.count = 0;
+                        mes.Message = "淇敼鎿嶄綔澶辫触!";
+                        mes.data = null;
+                    }
+                }
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return mes;
+        }
+        #endregion
+
+        #region[浜ц兘瑙勫垝鐐瑰嚮鏃ュ巻缁勪欢鏌ユ壘宸插叧鑱旂殑鏃ユ湡鏂规]
+        public static ToMessage CapacityPlanningCalendar(string captplanid)
+        {
+            string sql = "";
+            var dynamicParams = new DynamicParameters();
+            List<CapaPlan> list = new List<CapaPlan>();
+            try
+            {
+                sql = @"select CONVERT(varchar(100), wkdate, 23) DataTime,wkshift_code   from TWkm_capac_plan_sub where m_id=@captplanid";
+                dynamicParams.Add("@captplanid", captplanid);
+                var data = DapperHelper.selectdata(sql, dynamicParams);
+                for (int i = 0; i < data.Rows.Count; i++)
+                {
+                    string DataTime = data.Rows[i]["DataTime"].ToString();//鏃ユ湡
+                    string key = data.Rows[i]["wkshift_code"].ToString(); //鏂规缂栫爜
+                    CapaPlan cmp = new CapaPlan();
+                    cmp.name = DataTime;
+                    cmp.key = key;
+                    list.Add(cmp);
+                }
+                mes.code = "200";
+                mes.Message = "鏌ヨ鎴愬姛!";
+                mes.data = list;
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return mes;
+        }
+        #endregion
+
+        #region[浜ц兘瑙勫垝鏃ュ巻鍙屽嚮鏃跺甫鍑哄搴旂殑鏂规]
+        public static ToMessage CapacityPlanningOnclickSelect(string captplanid, string datetime)
+        {
+            string sql = "";
+            var dynamicParams = new DynamicParameters();
+            List<CapacityPlanSect> list = new List<CapacityPlanSect>();
+            try
+            {
+                sql = @"select distinct AA.code,AA.name,(case  when AA.Stus is null then 'N' else 'Y' end) flag
+                        from
+                        (
+                        select A.code,A.name,B.wkshift_code Stus  from TBas_wkshift_info A
+                        left join TWkm_capac_plan_sub B on A.code= B.wkshift_code and B.m_id=@captplanid and CONVERT(varchar(100), wkdate, 23)=@datetime
+                        ) AA  order by code";
+                dynamicParams.Add("@captplanid", captplanid);
+                dynamicParams.Add("@datetime", datetime);
+                var data = DapperHelper.selectdata(sql, dynamicParams);
+                for (int i = 0; i < data.Rows.Count; i++)
+                {
+                    string code = data.Rows[i]["code"].ToString();//鏂规缂栫爜
+                    string name = data.Rows[i]["name"].ToString();//鏂规鍚嶇О
+                    string flag = data.Rows[i]["flag"].ToString();   //閫変腑鏂规鏍囪瘑
+                    CapacityPlanSect cmp = new CapacityPlanSect();
+                    cmp.CapCode = code;
+                    cmp.CapName = name;
+                    cmp.CapStus = flag;
+                    cmp.list = new List<object>();
+
+                    string sql1 = @"select wktme1_start,wktme2_start,wktme3_start,wktme4_start,wktme5_start  from TBas_wkshift_info where code=@code";
+                    dynamicParams.Add("@code", code);
+                    var db = DapperHelper.selectdata(sql1, dynamicParams);
+
+                    for (int j = 0; j < db.Columns.Count; j++) //鏃堕棿娈�
+                    {
+                        if (db.Rows[0][j].ToString().Trim() != null && db.Rows[0][j].ToString().Trim() != "")
+                        {
+                            cmp.list.Add(db.Rows[0][j].ToString());
+                        }
+                    }
+                    list.Add(cmp);
+                }
+                mes.code = "200";
+                mes.Message = "鏌ヨ鎴愬姛!";
+                mes.data = list;
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return mes;
+        }
+        #endregion
+
+        #region[浜ц兘瑙勫垝宸ヤ綔鏃ュ巻閫夋嫨鎻愪氦]
+        public static ToMessage CapacityPlanningGivePlanSubmit(string captplanid, string wkshopcode, string capunitcode, string capsetupcode, string captplantype, List<CapaPlan> objs, string type, string username)
+        {
+            string sql = "";
+            List<object> list = new List<object>();
+            var dynamicParams = new DynamicParameters();
+            try
+            {
+                list.Clear();
+                if (capsetupcode == "" || capsetupcode == null)
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "榛樿鏂规閫夋嫨涓嶈兘涓虹┖!";
+                    mes.data = null;
+                    return mes;
+                }
+                if (objs.Count <= 0)
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "宸ヤ綔鏃ュ巻閫夋嫨涓嶈兘涓虹┖!";
+                    mes.data = null;
+                    return mes;
+                }
+                if (type == "Add")
+                {
+                    for (int i = 0; i < objs.Count; i++)
+                    {
+                        sql = @"insert into TWkm_capac_plan_sub(m_id, wkdate,wkshift_code) 
+                                     values(@m_id,@wkdate,@wkshift_code)";
+                        list.Add(new
+                        {
+                            str = sql,
+                            parm = new
+                            {
+                                m_id = captplanid,
+                                wkdate = objs[i].name,
+                                wkshift_code = objs[i].key
+                            }
+                        });
+                    }
+                    bool aa = DapperHelper.DoTransaction(list);
+                    if (aa)
+                    {
+                        mes.code = "200";
+                        mes.count = 0;
+                        mes.Message = "鎿嶄綔鎴愬姛!";
+                        mes.data = null;
+                    }
+                    else
+                    {
+                        mes.code = "200";
+                        mes.count = 0;
+                        mes.Message = "鎿嶄綔澶辫触!";
+                        mes.data = null;
+                    }
+
+                }
+                if (type == "Update")
+                {
+                    //瀹氫箟涓や釜鏁扮粍鐢ㄦ潵瀵规瘮淇敼鍓嶅悗鏃ユ湡鍙婃柟妗堢殑鍙樺寲
+                    List<CapaPlan> ids = new List<CapaPlan>();
+                    List<CapaPlan> ids1 = new List<CapaPlan>();
+                    for (int i = 0; i < objs.Count; i++)
+                    {
+                        ids.Add(objs[i]);
+                    }
+
+                    //鏍规嵁id鏌ユ壘鏃ュ巻鏃堕棿
+                    string sql2 = @"select CONVERT(varchar(100), wkdate, 23) wkdate,wkshift_code  from TWkm_capac_plan_sub where m_id=@m_id";
+                    dynamicParams.Add("@m_id", captplanid);
+                    var dt2 = DapperHelper.selectdata(sql2, dynamicParams);
+                    if (dt2.Rows.Count > 0)
+                    {
+                        for (int k = 0; k < dt2.Rows.Count; k++)
+                        {
+                            CapaPlan ids2 = new CapaPlan();
+                            ids2.name = dt2.Rows[k]["wkdate"].ToString();
+                            ids2.key = dt2.Rows[k]["wkshift_code"].ToString();
+                            ids1.Add(ids2);
+                        }
+                    }
+
+                    List<CapaPlan> list3 = ids.Except(ids1).ToList(); //琛ㄧずids涓摢浜涘�兼槸ids1涓墍涓嶅瓨鍦ㄧ殑;
+                    List<CapaPlan> list4 = ids1.Except(ids).ToList(); //琛ㄧずids1涓摢浜涘�兼槸ids涓墍涓嶅瓨鍦ㄧ殑;
+                    list4 = list3.Union(list4).ToList();
+                    //鏌ユ壘鍏抽敭宸ュ簭璁惧銆佸伐浣嶇兢缁勭殑鎵�鏈夎澶�
+                    string sql3 = @"select distinct A.code,A.name  from TEqpInfo A 
+                                  left join TFlw_Rteqp B on A.code=B.eqp_code
+                                  left join TFlw_Rtdt D on B.step_code=D.step_code                 
+                                  where A.wksp_code=@wkshopcode  and D.first_choke='Y'  order by A.code";
+                    dynamicParams.Add("@wkshopcode", wkshopcode);
+                    var dt3 = DapperHelper.selectdata(sql3, dynamicParams);
+                    if (list4.Count > 0)  //濡傛灉鏃ユ湡鏈夊彉鍔�
+                    {
+                        if (dt3.Rows.Count > 0)
+                        {
+                            for (int m = 0; m < list4.Count; m++)
+                            {
+                                for (int n = 0; n < dt3.Rows.Count; n++)
+                                {
+                                    string sql4 = @"select  A.wo_code,A.eqp_code,A.Time_Start,A.time_end, 
+                                                     (case  B.Status when 'SCHED' then '宸叉帓绋�' when 'ALLOC' then '宸叉淳鍙�' when 'START' then '宸插紑宸�' end ) status   
+                                                      from TK_Wrk_EqpAps  A
+                                                      left join TK_Wrk_Man B on A.wo_code=B.wo_code
+                                                      left join TEqpInfo C on A.eqp_code=C.code
+                                                      where CONVERT(varchar(100), time_start, 23)=@time_start and A.eqp_code=@eqp_code and B.isaps='Y' and B.status IN('SCHED','ALLOC','START')";
+                                    dynamicParams.Add("@time_start", list4[m].name);
+                                    dynamicParams.Add("@eqp_code", dt3.Rows[n]["CODE"]);
+                                    var dt4 = DapperHelper.selectdata(sql4, dynamicParams);
+                                    if (dt4.Rows.Count > 0)
+                                    {
+                                        for (int g = 0; g < dt4.Rows.Count; g++)
+                                        {
+                                            mes.code = "300";
+                                            mes.count = 0;
+                                            mes.Message = "淇敼澶辫触锛�,鏃ユ湡锛�" + dt4.Rows[g]["TIME_START"] + "鏈夈��" + dt4.Rows[g]["STATUS"] + "銆戝伐鍗曪細" + dt4.Rows[g]["WO_CODE"] + "";
+                                            mes.data = null;
+                                            return mes;
+                                        }
+                                    }
+                                }
+                            }
+                        }
+                    }
+                    //鏇存柊鎺掍骇鐢熶骇璧勬簮涓昏〃
+                    sql = @"update TWkm_capac_plan set wkshift_code=@wkshift_code,classtype=@classtype,lm_user=@lm_user,lm_date=@lm_time  where id=@id";
+                    list.Add(new
+                    {
+                        str = sql,
+                        parm = new
+                        {
+                            id = captplanid,
+                            wkshift_code = capsetupcode,
+                            classtype = captplantype,
+                            lm_user = username,
+                            lm_time = DateTime.Now.ToString()
+                        }
+                    });
+                    //鍒犻櫎鎺掍骇鐢熶骇璧勬簮瀛愯〃
+                    sql = @"delete TWkm_capac_plan_sub  where m_id=@id";
+                    list.Add(new
+                    {
+                        str = sql,
+                        parm = new
+                        {
+                            id = captplanid
+                        }
+                    });
+                    //寰幆鍐欏叆鎺掍骇鐢熶骇璧勬簮瀛愯〃
+                    for (int i = 0; i < objs.Count; i++)
+                    {
+                        sql = @"insert into TWkm_capac_plan_sub(m_id,wkdate,wkshift_code) values(@m_id,@wkdate,@wkshift_code)";
+                        list.Add(new
+                        {
+                            str = sql,
+                            parm = new
+                            {
+                                m_id = captplanid,
+                                wkdate = objs[i].name,
+                                wkshift_code = objs[i].key
+                            }
+                        });
+                    }
+                    bool aa = DapperHelper.DoTransaction(list);
+                    if (aa)
+                    {
+                        mes.code = "200";
+                        mes.count = 0;
+                        mes.Message = "鎿嶄綔鎴愬姛!";
+                        mes.data = null;
+                    }
+                    else
+                    {
+                        mes.code = "300";
+                        mes.count = 0;
+                        mes.Message = "鎿嶄綔澶辫触!";
+                        mes.data = null;
+                    }
+                }
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return mes;
+        }
+        #endregion
+
+
+
+        #region[鑷姩鎺掔▼宸ュ崟鏌ヨ]
+        public static ToMessage AdvancedSchedulingSearch(string workshop, string wocode, string partcode, string partname, int startNum, int endNum, string prop, string order)
+        {
+            var dynamicParams = new DynamicParameters();
+            string search = "";
+            try
+            {
+                if (workshop != "" && workshop != null)
+                {
+                    search += "and A.wkshp_code=@workshop ";
+                    dynamicParams.Add("@workshop", workshop);
+                }
+                if (wocode != "" && wocode != null)
+                {
+                    search += "and A.wo_code like '%'+@wocode+'%' ";
+                    dynamicParams.Add("@wocode", wocode);
+                }
+                if (partcode != "" && partcode != null)
+                {
+                    search += "and A.materiel_code like '%'+@partcode+'%' ";
+                    dynamicParams.Add("@partcode", partcode);
+                }
+                if (partname != "" && partname != null)
+                {
+                    search += "and C.partname like '%'+@partname+'%' ";
+                    dynamicParams.Add("@partname", partname);
+                }
+                if (search == "")
+                {
+                    search = "and 1=1 ";
+                }
+                // --------------鏌ヨ鎸囧畾鏁版嵁--------------
+                var total = 0; //鎬绘潯鏁�
+                var sql = @"select
+                             A.id              AdvaScheId, 
+                             A.status          AdvaScheStus,
+                             A.wo_code         AdvaScheWorkCode,
+                             A.wkshp_code      AdvaScheWorkShopid,
+                             M.org_name        AdvaScheWorkShop,
+                             A.materiel_code   AdvaSchePartNumber,
+                             C.partname        AdvaSchePartName,
+                             C.partspec        AdvaSchePartSpec,
+                             D.name            AdvaSchePartModel,
+                             A.plan_qty        AdvaScheQty,
+                             U.name            AdvaScheUom,
+                             isnull(E.sched_qty,0)       AdvaScheYPQty,
+	                         CONVERT(varchar(100), A.saleOrderDeliveryDate, 23) AdvaScheEndDate,
+	                         convert(varchar(20),isnull(E.sched_qty,0))+'/'+convert(varchar(20),isnull(A.plan_qty,0)) AdvaScheSpeed,
+                             A.route_code    AdvaScheRoutid,
+                             F.name        AdvaScheRoutName, 
+                             E.step_code   AdvaScheBotProcid,
+                             G.stepname        AdvaScheBotProcName,
+	                         CONVERT(varchar(100), H.maxtime, 23) AdvaSchePCEndDate,
+                             CONVERT(varchar(100), H.mintime, 23) AdvaSchePCStartDate,
+                            (case  when CONVERT(varchar(100), H.maxtime, 23)<=CONVERT(varchar(100), E.plan_enddate, 23) then 'Y'  when H.MAXTime is null  then 'Y'  else 'N' end) Flag,
+                            (case when A.PiroQue='1' then '鐗规��' when A.PiroQue='2' then '绱ф��' when A.PiroQue='3' then '姝e父' end) AdvaSchePiroQue 
+	                         from TK_Wrk_Man A
+	                         left join TMateriel_Info C on C.partcode= A.materiel_code
+                             left join T_Dict D on C.stocktype_code= d.code
+                             left join TK_Wrk_Step E on E.wo_code=A.wo_code
+                             left join TFlw_Rout F  on A.route_code=F.code
+                             left join TStep G  on E.step_code=G.stepcode
+                             left join (select wo_code, max(TIME_END) MAXTime,min(time_start) MINTime  from TK_Wrk_EqpAps group by wo_code) H on A.wo_code=H.wo_code
+	                         left join TUom U on C.uom_code=U.code
+                             left join TOrganization M on A.wkshp_code=M.org_code
+	                         where E.isbott = 'Y' and A.status='NOSCHED' and A.isaps='Y' " + search;
+                var data = DapperHelper.GetPageList<object>(sql, dynamicParams, prop, order, startNum, endNum, out total);
+                mes.code = "200";
+                mes.Message = "鏌ヨ鎴愬姛!";
+                mes.count = total;
+                mes.data = data.ToList();
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return mes;
+        }
+        #endregion
+
+        #region[鎺掍骇璁惧淇℃伅]
+        public static List<AdvancedSchedulingDevice> OnclickAdvancedSchedulingDevice(string wocode, string wkshpcode, string partcode, string startdate, string enddate, ref ToMessage mes)
+        {
+            string sql = "", sql0 = "";
+            var dynamicParams = new DynamicParameters();
+            List<AdvancedSchedulingDevice> list = new List<AdvancedSchedulingDevice>();
+            DataTable dt, dt1;
+
+            DateTime beginDate = Convert.ToDateTime(startdate);
+            DateTime endDate = DateTime.Parse(enddate);
+            try
+            {
+                if (beginDate > endDate)
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "瑕佹眰浜や粯鏃堕棿涓嶈兘灏忎簬褰撳墠绯荤粺鏃堕棿锛�";
+                    mes.data = null;
+                    return list;
+                }
+                bool IsCap = false;    //鏄惁璁剧疆浜ц兘
+                //閫氳繃宸ュ崟鏌ユ壘宸ヨ壓璺嚎瀵瑰簲鍏抽敭宸ュ簭鎵�鍏宠仈鐨勮澶囨槸鍚﹀彲鐢�
+                sql = @"select distinct C.eqp_code ,D.Enable  from TK_Wrk_Man A
+                       left join TFlw_Rout K on A.route_code=K.code
+                       left join TFlw_Rtdt B on K.code=B.rout_code and B.first_choke='Y'
+                       left join TFlw_Rteqp C on B.step_code= C.step_code
+                       left join TEqpInfo D on C.eqp_code=D.code
+                       where A.wo_code=@wocode and A.materiel_code=@partcode";
+                dynamicParams.Add("@wocode", wocode);
+                dynamicParams.Add("@partcode", partcode);
+                var dt_0 = DapperHelper.selectdata(sql, dynamicParams);
+                if (dt_0.Rows.Count > 0)
+                {
+                    int cout = dt_0.Rows.Count;
+                    int num = 0;
+                    for (int m = 0; m < dt_0.Rows.Count; m++)
+                    {
+                        if (dt_0.Rows[m]["Enable"].ToString() == "N")
+                        {
+                            num = num + 1;
+                        }
+                    }
+                    if (num == cout)
+                    {
+                        mes.code = "300";
+                        mes.count = 0;
+                        mes.Message = "璁惧涓嶅彲鐢紒";
+                        mes.data = null;
+                        return list;
+                    }
+                }
+                else
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "宸ヨ壓璺嚎鏈叧鑱旇澶囷紒";
+                    mes.data = null;
+                    return list;
+                }
+
+                //閫氳繃宸ュ崟鏌ユ壘宸ヨ壓璺嚎瀵瑰簲鍏抽敭宸ュ簭鎵�鍏宠仈鐨勮澶囨墍灞炶溅闂�
+                sql0 = @"select distinct D.wksp_code  from TK_Wrk_Man A
+                         left join TFlw_Rout K on A.route_code=K.code
+                         left join TFlw_Rtdt B on K.code=B.rout_code and B.first_choke='Y'
+                         left join TFlw_Rteqp C on B.step_code= C.step_code
+                         left join TEqpInfo D on C.eqp_code=D.code
+                         where A.wo_code=@wocode and A.materiel_code=@partcode ";
+                dynamicParams.Add("@wocode", wocode);
+                dynamicParams.Add("@partcode", partcode);
+                var dt0 = DapperHelper.selectdata(sql0, dynamicParams);
+                if (dt0.Rows.Count > 0)
+                {
+                    string sy = "0";
+                    for (int i = 0; i < dt0.Rows.Count; i++)
+                    {
+                        if (dt0.Rows[i]["WKSP_CODE"].ToString() == wkshpcode)  //宸ュ崟鍒涘缓杞﹂棿鏄惁绛変簬鎺掍骇璁惧 杞﹂棿
+                        {
+                            sy = "1";
+                            break;
+                        }
+                        else
+                        {
+                            sy = "0";
+                        }
+                    }
+                    if (sy == "0")
+                    {
+                        mes.code = "300";
+                        mes.count = 0;
+                        mes.Message = "鎺掍骇璁惧杞﹂棿涓庡伐鍗曞垱寤虹殑杞﹂棿涓嶅悓锛�";
+                        mes.data = null;
+                        return list;
+                    }
+                    else
+                    {
+                        List<APSList> listData = SchedulingMethod.SchedulingMethodTF(wocode, wkshpcode, partcode);
+                        for (DateTime date = beginDate; date <= endDate; date = date.AddDays(1))
+                        {
+                            AdvancedSchedulingDevice tbj = new AdvancedSchedulingDevice();
+                            tbj.YearDate = date.ToString("yyyy-MM-dd");
+                            tbj.children = new List<AdvancedSchedulingDeviceCont>();
+                            for (int j = 0; j < listData.Count; j++)
+                            {
+                                if (listData[j].AdvaDevicCropMob.ToString() == "0" || listData[j].AdvaDevicRhythm.ToString() == "")
+                                {
+                                    mes.code = "300";
+                                    mes.count = 0;
+                                    mes.Message = "鎺掔▼璁惧" + listData[j].eqp_id.ToString() + "绋煎姩鐜囦笉鑳戒负0鎴栦负绌猴紒";
+                                    mes.data = null;
+                                    return list;
+                                }
+                                if (listData[j].AdvaDevicRhythm.ToString() == "0" || listData[j].AdvaDevicRhythm.ToString() == "")
+                                {
+                                    mes.code = "300";
+                                    mes.count = 0;
+                                    mes.Message = "鎺掔▼璁惧" + listData[j].eqp_id.ToString() + "鏈缃妭鎷嶏紒";
+                                    mes.data = null;
+                                    return list;
+                                }
+                                string sql1 = @"select wktme1_start,wktme2_start,wktme3_start,wktme4_start,wktme5_start,G.name  
+		                                        from  TWkm_capac_plan  E 
+                                                left join TWkm_capac_plan_sub F on E.id=F.m_id
+                                                left join TBas_wkshift_info G on F.wkshift_code=G.code
+                                                where E.wkshop=@wkshop and E.eqp_typecode=@eqp_typecode  and E.ClassType=@classtype
+		                                        and CONVERT(varchar(100), F.wkdate, 23)=@wkdate and E.enable='Y'";
+                                dynamicParams.Add("@wkshop", wkshpcode);
+                                dynamicParams.Add("@eqp_typecode", listData[j].Style.ToString());
+                                dynamicParams.Add("@classtype", listData[j].ClassType.ToString());
+                                dynamicParams.Add("@wkdate", date.ToString("yyyy-MM-dd"));
+                                dt1 = DapperHelper.selectdata(sql1, dynamicParams);
+
+                                AdvancedSchedulingDeviceCont tbjson = new AdvancedSchedulingDeviceCont();
+                                tbjson.AdvaDevicNumber = listData[j].eqp_id.ToString();
+                                tbjson.AdvaDevicName = listData[j].name.ToString();
+                                tbjson.AdvaDevicCropMob = listData[j].AdvaDevicCropMob.ToString();  //绋煎姩鐜�
+                                tbjson.AdvaDevicRhythm = listData[j].AdvaDevicRhythm.ToString();    //鐢熶骇鑺傛媿
+                                if (dt1.Rows.Count > 0)
+                                {
+                                    tbjson.OneStartDate = dt1.Rows[0]["wktme1_start"].ToString();
+                                    tbjson.TwoStartDate = dt1.Rows[0]["wktme2_start"].ToString();
+                                    tbjson.ThreeStartDate = dt1.Rows[0]["wktme3_start"].ToString();
+                                    tbjson.FourStartDate = dt1.Rows[0]["wktme4_start"].ToString();
+                                    tbjson.FiveStartDate = dt1.Rows[0]["wktme5_start"].ToString();
+                                    tbj.children.Add(tbjson);
+                                    IsCap = true;
+                                }
+                                else
+                                {
+
+                                    tbjson.OneStartDate = "";
+                                    tbjson.TwoStartDate = "";
+                                    tbjson.ThreeStartDate = "";
+                                    tbjson.FourStartDate = "";
+                                    tbjson.FiveStartDate = "";
+                                    tbj.children.Add(tbjson);
+                                }
+                            }
+
+                            list.Add(tbj);
+                        }
+                        if (list.Select(p => p.children).ToList().Count > 0)
+                        {
+                            int one = list.Where(t => t.children.Select(s => s.OneStartDate).Any(x => x != "")).ToList().Count;
+                            int two = list.Where(t => t.children.Select(s => s.TwoStartDate).Any(x => x != "")).ToList().Count;
+                            int three = list.Where(t => t.children.Select(s => s.ThreeStartDate).Any(x => x != "")).ToList().Count;
+                            int four = list.Where(t => t.children.Select(s => s.FourStartDate).Any(x => x != "")).ToList().Count;
+                            int five = list.Where(t => t.children.Select(s => s.FiveStartDate).Any(x => x != "")).ToList().Count;
+                            if (one <= 0 && two <= 0 && three <= 0 && four <= 0 && five <= 0)
+                            {
+                                mes.code = "300";
+                                mes.count = 0;
+                                mes.Message = "褰撳墠宸ュ崟鍔犲伐浜у搧瀵瑰簲宸ヨ壓璺嚎璁惧鏈缃骇鑳芥垨鏈缃敓浜ц妭鎷嶏紒";
+                                mes.data = null;
+                            }
+                        }
+                    }
+                }
+                else
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "璁惧娌℃湁鍏宠仈杞﹂棿锛�";
+                    mes.data = null;
+                }
+                return list;
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return list;
+        }
+        #endregion
+
+        #region[璁惧宸叉帓绋嬩俊鎭痌
+        public static DataTable AlreadyScheduling(string wocode, string wkshpcode, string partcode, string botproccode, string startdate, string enddate)
+        {
+            var dynamicParams = new DynamicParameters();
+            DataTable dt;
+            try
+            {
+                List<APSList> listData = SchedulingMethod.SchedulingMethodTF(wocode, wkshpcode, partcode);
+                string[] empIds = listData.Select(a => a.eqp_id).ToArray();
+                string str = string.Join(",", empIds);
+                string[] s1 = Array.ConvertAll<string, string>(str.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries), s => s.ToString()); //string鍒嗗壊杞瑂tring[] 
+                //string s1 = string.Format("'{0}'", str.Replace(",", "','"));
+
+                string sql = @"select B.wo_code,  B.eqp_code,B.time_start,B.time_end, 'S' status , B.alloc_qty,D.partcode as part_code,D.partname as part_name,T.name as uom_name
+	                           from TK_Wrk_EqpAps B 
+                               left join TK_Wrk_Man C on B.wo_code=C.wo_code
+                               left join  TMateriel_Info D on C.materiel_code=D.partcode
+                               left join TUom T on D.uom_code=T.code
+                               where  B.eqp_code in @eqpcode
+	                           and convert(varchar(100),B.Time_Start,21)>=@startdate and convert(varchar(100),B.Time_End,21)<=@enddate order by time_end";
+                dt = DapperHelper.selectlist(sql, new { eqpcode = s1.ToArray(), startdate = startdate + " 00:00:00", enddate = enddate + " 23:59:59" });
+                //dynamicParams.Add("@s1", new { shopcode = s1.ToArray() });
+                //dynamicParams.Add("@startdate", startdate + " 00:00:00");
+                //dynamicParams.Add("@enddate", enddate + " 23:59:59");
+                //var dt_0 = DapperHelper.selectdata(sql, dynamicParams);
+                if (dt == null || dt.Rows.Count == 0)
+                {
+                    return null;
+                }
+            }
+            catch (Exception ex)
+            {
+                throw new Exception(ex.Message);
+            }
+
+            return dt;
+        }
+        #endregion
+
+        #region[鎺掔▼鏁版嵁鎻愪氦]
+        public static ToMessage SubmitAlreadyScheduling(string username, string wocode, string botprocecode, List<AlreadyScheduling> objs)
+        {
+            var sql = "";
+            List<object> list = new List<object>();
+            var dynamicParams = new DynamicParameters();
+            try
+            {
+                string maxTime = objs.Max(t => Convert.ToDateTime(t.AlreEndDate).ToString("yyyy-MM-dd HH:mm"));  //鏈�澶у��
+                string minTime = objs.Min(t => Convert.ToDateTime(t.AlreEndDate).ToString("yyyy-MM-dd HH:mm"));  //鏈�灏忓��
+                sql = @"select 
+                        A.id, A.status,
+                        '0' BottFrointv,  convert(varchar(100),B.plan_enddate-0,21) Plan_end ,
+                        convert(varchar(100),B.plan_startdate+0,21) plan_start
+                        from TK_Wrk_Step A
+                        left join TK_Wrk_Man B on A.wo_code=B.wo_code  
+                        where A.wo_code=@wocode and A.step_code=@botprocecode and A.isbott='Y'";
+                dynamicParams.Add("@wocode", wocode);
+                dynamicParams.Add("@botprocecode", botprocecode);
+                var dt = DapperHelper.selectdata(sql, dynamicParams);
+                string ID = dt.Rows[0]["ID"].ToString();     // mes_tk_wrk_step 琛� 鐡跺緞宸ュ簭琛孖D
+                string status = dt.Rows[0]["STATUS"].ToString();
+                Decimal nm = 0;                                                     //鐡跺緞宸ュ簭鐨勫墠缃ぉ鏁�
+                Decimal nn = Decimal.Parse(dt.Rows[0]["BottFrointv"].ToString());   //鐡跺緞宸ュ簭鐨勫悗缃ぉ鏁�
+                if (status != "NEW" && status != "SCHED" && status != "NOSCHED")   //宸ュ簭浠诲姟鐨勭姸鎬佸凡缁忔淳鍙戯紙瀹℃牳锛�
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "褰撳墠鎺掔▼浠诲姟宸茬粡娲惧彂,鎻愪氦鍙栨秷锛�";
+                    mes.data = null;
+                    return mes;
+                }
+                if (status == "SCHED")   //宸ュ簭浠诲姟鐨勭姸鎬佸凡缁忔帓绋�
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "褰撳墠鎺掔▼浠诲姟宸茬粡鎺掔▼,鎻愪氦鍙栨秷锛�";
+                    mes.data = null;
+                    return mes;
+                }
+
+                list.Clear();
+                //鎸夊伐鍗曞垹闄よ澶囦换鍔¤〃(鏃ヤ换鍔¤〃锛�
+                sql = @"delete TK_Wrk_EqpAps  where wo_code=@wocode";
+                list.Add(new { str = sql, parm = new { wocode = wocode } });
+                //鎸夊伐鍗曞垹闄よ澶囦换鍔¤〃(姹囨�昏〃锛�
+                sql = @"delete TK_Wrk_EqpApsSum  where wo_code=@wocode";
+                list.Add(new { str = sql, parm = new { wocode = wocode } });
+                //鎸夊伐鍗曞垹闄よ澶囦换鍔¤〃(姹囨�昏〃锛夌墿鏂欒〃
+                sql = @"delete TK_Wrk_EqpSum_Allo  where wo_code=@wocode";
+                list.Add(new { str = sql, parm = new { wocode = wocode } });
+
+                float n = 0;  //绱鎺掍骇鎬绘暟
+
+                for (int i = 0; i < objs.Count; i++)        //寰幆娣诲姞姣忎釜璁惧鐨勬満鍙颁换鍔�
+                {
+                    sql = @"insert into TK_Wrk_EqpAps (wo_code,step_taid,eqp_code,time_start,time_end,alloc_qty,status)
+                            values(@wo_code,@step_taid,@eqp_code,@time_start,@time_end,@alloc_qty,@status)";
+                    list.Add(new { str = sql, parm = new { wo_code = wocode, step_taid = ID, eqp_code = objs[i].AlreDevicNumber, time_start = objs[i].AlreStartDate, time_end = objs[i].AlreEndDate, alloc_qty = objs[i].AlreQty, status = "NEW" } });
+                    n = n + float.Parse(objs[i].AlreQty.ToString());
+                }
+
+                bool aa = DapperHelper.DoTransaction(list); //鎻愪氦璁惧浠诲姟
+                if (!aa)
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "鍏堥鎺�,鍐嶇偣鍑绘彁浜わ紒";
+                    mes.data = null;
+                    return mes;
+                }
+                list.Clear();
+                //鐢熸垚璁惧浠诲姟姹囨�昏〃  锛堟牴鎹澶囨棩浠诲姟琛級
+                sql = "select distinct eqp_code from TK_Wrk_EqpAps where wo_code=@wocode";
+                dynamicParams.Add("@wocode", wocode);
+                var dt1 = DapperHelper.selectdata(sql, dynamicParams);
+
+                for (int i = 0; i < dt1.Rows.Count; i++)
+                {
+                    sql = @"insert into  TK_Wrk_EqpApsSum (wo_code,eqp_code,step_taid,p_date, t_date, qty,status)
+                           select  min(wo_code),min(eqp_code),min(step_taid),min(time_start),max(time_end),sum(Alloc_Qty),'NEW' from TK_Wrk_EqpAps  
+                           where wo_code=@wocode and eqp_code=@eqp_code";
+                    list.Add(new { str = sql, parm = new { wocode = wocode, eqp_code = dt1.Rows[i]["EQP_CODE"].ToString() } });
+                }
+
+                bool aa1 = DapperHelper.DoTransaction(list); //鎻愪氦璁惧浠诲姟
+                if (!aa1)
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "鐢熸垚璁惧浠诲姟姹囨�昏〃鍑洪敊锛屾帓浜уけ璐ワ紒";
+                    mes.data = null;
+                    return mes;
+                }
+                list.Clear();
+                //鍐欏叆璁惧浠诲姟(姹囨�昏〃)鐢ㄦ枡  璁″垝鏁伴噺*瀛愪欢鍩烘湰鐢ㄩ噺*锛�1+鎹熻�楃巼锛�/姣嶄欢鍩烘湰鐢ㄩ噺
+                sql = @"insert into TK_Wrk_EqpSum_Allo(m_id, seq, materiel_code, qty,wo_code,materieltype)
+                        select A.id M_id, B.seq,B.materiel_code,(round(A.qty,2)*BE.Base_Quantity*(1+BE.LOSS_QUANTITY/100))/BM.quantity  qty,A.wo_code,B.materieltype  
+                        from TK_Wrk_EqpApsSum A 
+                        left join TK_Wrk_Allo B on A.Wo_Code= B.Wo_Code 
+                        left join TBom_Deta BE  ON B.bom_id=BE.m_id and B.materiel_code=BE.smateriel_code
+                        left join  TBom_Main BM on BE.m_Id=BM.id  where A.wo_code=@wocode";
+                list.Add(new { str = sql, parm = new { wocode = wocode } });
+
+                //鏇存柊 宸ュ簭浠诲姟鍗曠殑銆愮摱寰勫伐搴忋�� 鎺掍骇棰勫紑宸ユ棩鏈熴�佹帓浜ч瀹屽伐鏃ユ湡銆佺姸鎬侊細NEW===>SCHED 銆佸凡鎺掍骇鏁伴噺
+                sql = @"update TK_Wrk_Step   set plan_startdate =convert(varchar(100),@plan_startdate,21),  plan_enddate =convert(varchar(100),@plan_enddate,21),  status = 'SCHED', sched_qty =@sched_qty where id =@id";
+                list.Add(new { str = sql, parm = new { plan_startdate = minTime, plan_enddate = maxTime, sched_qty = n, id = ID } });
+
+                //宸ュ崟宸ュ簭鐨勨�滆鍒掑紑鏈烘棩鏈� = 鐡跺緞宸ュ簭鐨勯寮�宸ユ棩鏈� - 鐡跺緞宸ュ簭鐨勫墠缃棩鏈燂級    涓诲伐鍗曪細璁″垝瀹屽伐鏃ユ湡 = 鐡跺緞宸ュ簭鐨勯瀹屽伐鏃ユ湡 + 鐡跺緞宸ュ簭鐨勫悗缃棩鏈�
+                sql = @"update TK_Wrk_Step   set plan_startdate =convert(varchar(100),@plan_startdate,21),  plan_enddate =convert(varchar(100),@plan_enddate,21),  status = 'SCHED', sched_qty =@sched_qty where wo_code =@wocode and isbott='N'";
+                list.Add(new
+                {
+                    str = sql,
+                    parm = new
+                    {
+                        plan_startdate = Convert.ToDateTime(minTime).AddDays(Convert.ToDouble(-nm)).ToString("yyyy-MM-dd"),
+                        plan_enddate = Convert.ToDateTime(maxTime).AddDays(Convert.ToDouble(nn)).ToString("yyyy-MM-dd"),
+                        sched_qty = n,
+                        wocode = wocode
+                    }
+                });
+                //涓诲伐鍗曠殑鈥滆鍒掑紑鏈烘棩鏈� = 鐡跺緞宸ュ簭鐨勯寮�宸ユ棩鏈� - 鐡跺緞宸ュ簭鐨勫墠缃棩鏈燂級    涓诲伐鍗曪細璁″垝瀹屽伐鏃ユ湡 = 鐡跺緞宸ュ簭鐨勯瀹屽伐鏃ユ湡 + 鐡跺緞宸ュ簭鐨勫悗缃棩鏈�
+                sql = @"update TK_Wrk_Man set status='SCHED',plan_startdate =convert(varchar(100),@plan_startdate,21),  plan_enddate =convert(varchar(100),@plan_enddate,21), exchag='Y',allocfag='N' where wo_code =@wocode";
+                list.Add(new
+                {
+                    str = sql,
+                    parm = new
+                    {
+                        plan_startdate = Convert.ToDateTime(minTime).AddDays(Convert.ToDouble(-nm)).ToString("yyyy-MM-dd"),
+                        plan_enddate = Convert.ToDateTime(maxTime).AddDays(Convert.ToDouble(nn)).ToString("yyyy-MM-dd"),
+                        sched_qty = n,
+                        wocode = wocode
+                    }
+                });
+                bool aa2 = DapperHelper.DoTransaction(list); //鎻愪氦璁惧浠诲姟
+                if (aa2)
+                {
+                    mes.code = "200";
+                    mes.count = 0;
+                    mes.Message = "鎻愪氦鎺掔▼鎴愬姛锛�";
+                }
+                else
+                {
+                    mes.code = "300";
+                    mes.count = 0;
+                    mes.Message = "鎻愪氦鎺掔▼澶辫触锛�";
+                    mes.data = null;
+                }
+            }
+            catch (Exception e)
+            {
+                mes.code = "300";
+                mes.count = 0;
+                mes.Message = e.Message;
+                mes.data = null;
+            }
+            return mes;
+        }
+        #endregion
+
     }
 }
\ No newline at end of file

--
Gitblit v1.9.3