From 88cf209956e5cb2bf5e4048c793c8bb5048808d4 Mon Sep 17 00:00:00 2001
From: yl <ykxkd@outlook.com>
Date: 星期一, 06 三月 2023 17:53:32 +0800
Subject: [PATCH] 新增MES工单报废补单选源单接口

---
 VueWebApi/DLL/DAL/ProductionManagementDAL.cs | 1344 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 1,319 insertions(+), 25 deletions(-)

diff --git a/VueWebApi/DLL/DAL/ProductionManagementDAL.cs b/VueWebApi/DLL/DAL/ProductionManagementDAL.cs
index 848865f..583d18b 100644
--- a/VueWebApi/DLL/DAL/ProductionManagementDAL.cs
+++ b/VueWebApi/DLL/DAL/ProductionManagementDAL.cs
@@ -145,13 +145,13 @@
                             parm = new
                             {
                                 wo_code = wo,
-                                wotype="PO",
+                                wotype = "PO",
                                 status = "NEW",
                                 wkshp_code = wkshopcode,
                                 plan_qty = cdqty + (decimal.Parse(markqty) - sumqty),  //鏈崟涓嬪崟鏁伴噺=鍒囧垎鏁伴噺+(涓嬪崟鏁伴噺-绱鍒囧垎涓嬪崟鏁伴噺)
                                 stck_code = warehousecode,
                                 materiel_code = partcode,
-                                sourceid=erporderid,
+                                sourceid = erporderid,
                                 m_po = erpordercode,
                                 username = username,
                                 CreateDate = DateTime.Now.ToString()
@@ -300,7 +300,7 @@
 
 
         #region[MES宸ュ崟鏌ヨ]
-        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)
+        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 = "";
@@ -368,6 +368,82 @@
                             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
+                            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'  and A.status='START' and A.wotype='PO' " + search;
                 var data = DapperHelper.GetPageList<object>(sql, dynamicParams, prop, order, startNum, endNum, out total);
                 mes.code = "200";
                 mes.Message = "鏌ヨ鎴愬姛!";
@@ -476,8 +552,75 @@
         }
         #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 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)
+        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)
         {
             var sql = "";
             var dynamicParams = new DynamicParameters();
@@ -534,6 +677,32 @@
                             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
+                        }
+                    });
+
+                    //if (is_aps == "Y")  //鏄惁鎺掔▼
+                    //{
+                    //    string sql1 = "select id from TBom_Main  where materiel_code='" + PartNumber + "' and status='Y' and version='" + VsionId + "'";
+                    //    DataTable dt1 = DBHelper.GetTable(sql1);
+                    //}
+
+                    //澧炲姞宸ュ崟鐢ㄦ枡琛紙瀛愪欢锛�  
 
                     bool aa = DapperHelper.DoTransaction(list);
                     if (aa)
@@ -594,6 +763,35 @@
                             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)
                     {
@@ -631,7 +829,7 @@
             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')";
                 dynamicParams.Add("@wocode", wocode);
                 var data = DapperHelper.selectdata(sql, dynamicParams);
                 if (data.Rows.Count > 0)
@@ -648,13 +846,13 @@
                         {
                             //鍥炲啓璁㈠崟琛ㄧ姸鎬佸強宸蹭笅鍗曟暟閲�
                             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 } });
+                            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 and id=@souceid";
-                            list.Add(new { str = sql, parm = new { m_po = m_po, souceid= souceid, orderqty = decimal.Parse(orderqty) } });
+                            list.Add(new { str = sql, parm = new { m_po = m_po, souceid = souceid, orderqty = decimal.Parse(orderqty) } });
                         }
                     }
                     //鍒犻櫎宸ュ崟宸ュ簭琛�
@@ -700,7 +898,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>();
@@ -711,8 +909,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)
                 {
@@ -752,7 +950,7 @@
                          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";
+                        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";
@@ -1146,7 +1344,7 @@
                     }
                     if (isend == "Y")  //褰撳墠宸ュ簭鏄湯閬撳伐搴�
                     {
-                        rt.nextstepcode ="";//璧嬬┖
+                        rt.nextstepcode = "";//璧嬬┖
                         rt.nextstepname = "";//璧嬬┖
                     }
                 }
@@ -1367,7 +1565,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";
@@ -1554,7 +1752,7 @@
         #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 remarks, string username)
+        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 remarks, string username)
         {
             var sql = "";
             string[] arra = new string[] { };
@@ -1618,7 +1816,7 @@
                         {
                             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 } });
+                            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 } });
 
                         }
                     }
@@ -1671,7 +1869,7 @@
                         {
                             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 } });
+                            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 } });
 
                         }
                     }
@@ -1791,7 +1989,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 remarks, 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[] { };
@@ -1822,7 +2020,7 @@
                 dynamicParams.Add("@step_code", stepcode);
                 dynamicParams.Add("@wx_code", wxcode);
                 var da = DapperHelper.selectdata(sql, dynamicParams);
-                if (da.Rows.Count<=0) 
+                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
@@ -1834,7 +2032,7 @@
                     string wxstring = (string.Join(",", dr.Select(x => x.ToString()).ToArray()));
                     mes.code = "300";
                     mes.count = 0;
-                    mes.Message = "鎿嶄綔澶辫触,褰撳墠鏀舵枡澶栧崗渚涙柟涓庡彂鏂欏鍗忎緵搴斿晢涓嶅尮閰�,搴斾负锛氥��"+wxstring+"銆�!";
+                    mes.Message = "鎿嶄綔澶辫触,褰撳墠鏀舵枡澶栧崗渚涙柟涓庡彂鏂欏鍗忎緵搴斿晢涓嶅尮閰�,搴斾负锛氥��" + wxstring + "銆�!";
                     mes.data = null;
                     return mes;
                 }
@@ -1842,7 +2040,7 @@
                 {
                     mes.code = "300";
                     mes.count = 0;
-                    mes.Message = "鎿嶄綔澶辫触,褰撳墠鏀舵枡鏁伴噺+涓嶈壇鏁伴噺,涓嶈兘澶т簬鍙戞枡鏁伴噺:"+da.Rows[0]["FQTY"].ToString()+"!";
+                    mes.Message = "鎿嶄綔澶辫触,褰撳墠鏀舵枡鏁伴噺+涓嶈壇鏁伴噺,涓嶈兘澶т簬鍙戞枡鏁伴噺:" + da.Rows[0]["FQTY"].ToString() + "!";
                     mes.data = null;
                     return mes;
                 }
@@ -1862,7 +2060,7 @@
                     {
                         mes.code = "300";
                         mes.count = 0;
-                        mes.Message = "鎿嶄綔澶辫触,褰撳墠鏀舵枡鏁伴噺+涓嶈壇鏁伴噺,涓嶈兘澶т簬寰呮敹鏁伴噺:" + (sum_fqty- sum_sqty) + "!";
+                        mes.Message = "鎿嶄綔澶辫触,褰撳墠鏀舵枡鏁伴噺+涓嶈壇鏁伴噺,涓嶈兘澶т簬寰呮敹鏁伴噺:" + (sum_fqty - sum_sqty) + "!";
                         mes.data = null;
                         return mes;
                     }
@@ -1888,7 +2086,7 @@
                         {
                             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 } });
+                            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 } });
 
                         }
                     }
@@ -1937,7 +2135,7 @@
                         {
                             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 } });
+                            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 } });
 
                         }
                     }
@@ -2272,7 +2470,7 @@
         #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)
+        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[] { };
@@ -2302,7 +2500,7 @@
                 //鍐欏叆宸ュ簭妫�楠岃褰曚富琛�
                 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 } });
+                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";
@@ -2342,5 +2540,1101 @@
         }
         #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 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), B.planenddate, 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 TKimp_Ewo B on A.m_po=B.wo
+	                         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='NEW' and A.isaps='Y'";
+                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]["WKSHOP"].ToString() == wkshpcode)  //宸ュ崟鍒涘缓杞﹂棿鏄惁绛変簬鎺掍骇璁惧 杞﹂棿
+                        {
+                            sy = "1";
+                            break;
+                        }
+                        else
+                        {
+                            sy = "0";
+                        }
+                    }
+                    if (sy == "0")
+                    {
+                        mes.code = "300";
+                        mes.count = 0;
+                        mes.Message = "鎺掍骇璁惧杞﹂棿涓庡伐鍗曞垱寤虹殑杞﹂棿涓嶅悓锛�";
+                        mes.data = null;
+                    }
+                    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='CJ001'and E.eqp_typecode='SBLX001'  and E.ClassType='D'
+		                                        and CONVERT(varchar(100), F.wkdate, 23)='2022-10-11' 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();
+            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 = 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.partname as part_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
+                               where  B.eqp_code in(@s1) 
+	                           and convert(varchar(100),B.Time_Start,21)>=@startdate and convert(varchar(100),B.Time_End,21)<=@enddate order by time_end";
+                dynamicParams.Add("@s1", s1);
+                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")   //宸ュ簭浠诲姟鐨勭姸鎬佸凡缁忔淳鍙戯紙瀹℃牳锛�
+                {
+                    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=''";
+                    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, invcode, qty,wo_code,pn_type)
+                        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 mes_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