using Newtonsoft.Json.Linq;
using Pub_Class;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.Http;
using WebAPI.Models;
namespace WebAPI.Controllers
{
    public class LMESController : ApiController
    {
        public DBUtility.ClsPub.Enum_BillStatus BillStatus;
        private json objJsonResult = new json();
        SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
        /// 
        /// 返回进站扫描列表|工序进站接收单列表 
        ///参数:string sql。
        ///返回值:object。    
        /// 
        [Route("LEMS/MES_StationInBillList_Json")]
        [HttpGet]
        public object MES_StationInBillList_Json(string sWhere)
        {
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (sWhere == null || sWhere.Equals(""))
                {
                    ds = oCN.RunProcReturn("select * from h_v_MES_StationInBillList", "h_v_MES_StationInBillList");
                }
                else 
                {
                    string sql1 = "select * from h_v_MES_StationInBillList where 1 = 1 ";
                    string sql = sql1 + sWhere;
                    ds = oCN.RunProcReturn(sql, "h_v_MES_StationInBillList");
                }
                
            }
            catch (Exception e)
            {
                ds = null;
            }
            return GetObjectJson(ds);
        }
        /// 
        /// 返回工序流转卡维护列表主表
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/MES_Sc_ProcessExchangeBillQuery_Json")]
        [HttpGet]
        public object MES_Sc_ProcessExchangeBillList_Json(string sWhere,string user)
        {
            DataSet ds;
            try
            {
                //判断是否有查询权限
                if (!DBUtility.ClsPub.Security_Log("Sc_ProcessExchangeBill_Query", 1, false, user))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无查询权限!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                ds = oCN.RunProcReturn("select * from h_v_Sc_ProcessExchangeBillQuery where 1 = 1 " + sWhere+ " order by hmainid desc ", "h_v_Sc_ProcessExchangeBillQuery");
                //if (ds.Tables[0].Rows.Count != 0 || ds != null)
                //{
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = ds.Tables[0];
                return objJsonResult;
                //}
                //else
                //{
                //objJsonResult.code = "0";
                //objJsonResult.count = 0;
                //objJsonResult.Message = "无数据";
                //objJsonResult.data = null;
                //return objJsonResult;
                //}
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "异常!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        /// 
        /// 返回工序流转卡维护列表从表
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/MES_Sc_ProcessExchangeBillQuerySub_Json")]
        [HttpGet]
        public object MES_Sc_ProcessExchangeBillSub_Json(string sWhere)
        {
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                string sql = "select * from h_v_Sc_ProcessExchangeBillQuerySub where hmainid = ";
                string sql1 = sql + sWhere;
                ds = oCN.RunProcReturn(sql1, "h_v_Sc_ProcessExchangeBillQuerySub");
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "没有返回任何记录!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
            return GetObjectJson(ds);
        }
        /// 
        /// 返回工序出站汇报单列表
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/MES_StationOutBillList_Json")]
        [HttpGet]
        public object MES_StationOutBillList_Json(string sWhere)
        {
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (sWhere == null || sWhere.Equals(""))
                {
                    ds = oCN.RunProcReturn("select * from h_v_MES_StationOutBillList ", "h_v_MES_StationInBillList");
                }
                else
                {
                    string sql1 = "select * from h_v_MES_StationOutBillList where 1 = 1 ";
                    string sql = sql1 + sWhere;
                    ds = oCN.RunProcReturn(sql, "h_v_MES_StationOutBillList");
                }
            }
            catch (Exception e)
            {
                ds = null;
            }
            return GetObjectJson(ds);
        }
        /// 
        /// 返回工序委外发出单列表
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/MES_StationEntrustInBillList_Json")]
        [HttpGet]
        public object MES_StationEntrustInBillList_Json(string sWhere)
        {
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (sWhere == null || sWhere.Equals(""))
                {
                    ds = oCN.RunProcReturn("select * from h_v_MES_StationEntrustInBillList ", "h_v_MES_StationEntrustInBillList");
                }
                else
                {
                    string sql1 = "select * from h_v_MES_StationEntrustInBillList where 1 = 1 ";
                    string sql = sql1 + sWhere;
                    ds = oCN.RunProcReturn(sql, "h_v_MES_StationEntrustInBillList");
                }
            }
            catch (Exception e)
            {
                ds = null;
            }
            return GetObjectJson(ds);
        }
        #region 工艺路线
        #region 工艺路线列表
        /// 
        /// 返回工艺路线列表
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/MES_Gy_RoutingBillList_Json")]
        [HttpGet]
        public object MES_Gy_RoutingBillList_Json(string sWhere,string user)
        {
            DataSet ds;
            try
            {
                //判断是否有查询权限
                //if (!DBUtility.ClsPub.Security_Log("Gy_RoutingBill_Query", 1, false, user))
                //{
                //    objJsonResult.code = "0";
                //    objJsonResult.count = 0;
                //    objJsonResult.Message = "无权限查询!";
                //    objJsonResult.data = null;
                //    return objJsonResult;
                //}
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (sWhere == null || sWhere.Equals(""))
                {
                    ds = oCN.RunProcReturn("select * from h_v_Gy_RoutingBillList order by hmainid desc", "h_v_Gy_RoutingBillList");
                }
                else
                {
                    string sql1 = "select * from h_v_Gy_RoutingBillList where 1 = 1 ";
                    string sql = sql1 + sWhere+ " order by hmainid desc";
                    ds = oCN.RunProcReturn(sql, "h_v_Gy_RoutingBillList");
                }
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "没有返回任何记录!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
            return GetObjectJson(ds);
        }
        #endregion
        #region 工艺路线  删除
        /// 
        /// 工艺路线删除功能
        /// 
        /// 
        [Route("LEMS/DeltetGy_RoutingBillSub")]
        [HttpGet]
        public object DeltetGy_RoutingBillSub(Int64 lngBillKey,string user)
        {
            DataSet ds;
            string ModRightNameCheck = "Gy_RoutingBill_Drop";
            try
            {
                //删除权限
                if (!DBUtility.ClsPub.Security_Log(ModRightNameCheck, 1, false, user))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "删除失败!无权限!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (lngBillKey == 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "单据id为空!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                oCN.BeginTran();//开始事务
                ds = oCN.RunProcReturn("select * from Gy_RoutingBillmain where HInterID=" + lngBillKey, "Gy_RoutingBillmain");
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "没有数据,无法删除!";
                    objJsonResult.data = null;
                    return objJsonResult; ;
                }
                string HChecker = Convert.ToString(ds.Tables[0].Rows[0]["HChecker"]);
                //string hdeleteman = Convert.ToString(ds.Tables[0].Rows[0]["hdeleteman"]);
                if (HChecker != "")
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "单据已经审核,不能删除!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                //if (hdeleteman != "")
                //{
                //    objJsonResult.code = "0";
                //    objJsonResult.count = 0;
                //    objJsonResult.Message = "单据已经作废,不能删除!";
                //    objJsonResult.data = null;
                //    return objJsonResult;
                //}
                oCN.RunProc("delete from Gy_RoutingBillmain where HInterID=" + lngBillKey);
                oCN.RunProc("delete from Gy_RoutingBillSub where HInterID=" + lngBillKey);
                oCN.Commit();//提交事务
                objJsonResult.code = "0";
                objJsonResult.count = 1;
                objJsonResult.Message = "* 数据删除成功!";
                objJsonResult.data = null;
                return objJsonResult; ;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "删除失败!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
        #endregion
        /// 
        /// 返回生产任务单列表
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/MES_IF_ICMOBillList_Json")]
        [HttpGet]
        public object MES_IF_ICMOBillList_Json(string sWhere,string user)
        {
            DataSet ds;
            try
            {
                //判断是否有查询权限
                if (!DBUtility.ClsPub.Security_Log("Sc_ICMOBillQuery", 1, false, user))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无权限查询!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                ds = oCN.RunProcReturn("select * from h_v_IF_ICMOBillList where 1 = 1 " + sWhere + " order by hmainid desc", "h_v_IF_ICMOBillList");
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "没有返回任何记录!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
            return GetObjectJson(ds);
        }
        /// 
        /// 返回工序委外接收单列表
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/MES_StationEntrustOutBillList_Json")]
        [HttpGet]
        public object MES_StationEntrustOutBillList_Json(string sWhere)
        {
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (sWhere == null || sWhere.Equals(""))
                {
                    ds = oCN.RunProcReturn("select * from h_v_MES_StationEntrustOutBillList ", "h_v_MES_StationEntrustOutBillList");
                }
                else
                {
                    string sql1 = "select * from h_v_MES_StationEntrustOutBillList where 1 = 1 ";
                    string sql = sql1 + sWhere;
                    ds = oCN.RunProcReturn(sql, "h_v_MES_StationEntrustOutBillList");
                }
            }
            catch (Exception e)
            {
                ds = null;
            }
            return GetObjectJson(ds);
        }
        ///
        ///封装状态码及返回信息的公用方法。
        ///参数:DataSet。
        ///返回值:json。
        ///
        public object GetObjectJson(DataSet ds)
        {
            try
            {
                //if (ds.Tables[0].Rows.Count != 0 || ds != null)
                //{
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = ds.Tables[0];
                return objJsonResult;
                //}
                //else
                //{
                //objJsonResult.code = "0";
                //objJsonResult.count = 0;
                //objJsonResult.Message = "无数据";
                //objJsonResult.data = null;
                //return objJsonResult;
                //}
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "没有返回任何记录!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        ///
        ///自定义错误信息方法。
        ///参数:string。
        ///返回值:object。
        ///
        public object CustomError(string msg)
        {
            objJsonResult.code = "0";
            objJsonResult.count = 0;
            objJsonResult.Message = msg;
            objJsonResult.data = null;
            return objJsonResult;
        }
        ///
        ///统一正确信息方法。
        ///参数:string。
        ///返回值:object。
        ///
        public object CustomCorrect(DataSet ds)
        {
            if (ds == null || ds.Tables[0].Rows.Count <= 0)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "没有返回任何记录!";
                objJsonResult.data = null;
                return objJsonResult;
            }
            else
            {
                objJsonResult.code = "0";
                objJsonResult.count = 1;
                objJsonResult.Message = "获取信息成功!";
                objJsonResult.data = ds.Tables[0];
                return objJsonResult;
            }
        }
        /// 
        /// 返回选择生产班组列表
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/MES_GetProductionTeamList_Json")]
        [HttpGet]
        public object MES_GetProductionTeamList_Json(string sWhere)
        {
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                ds = oCN.RunProcReturn("Select HItemID,HNumber 班组代码,HName 班组 from h_v_IF_Group where HStopflag=0 " + sWhere + " Order by HItemID ", "h_v_IF_Group");
            }
            catch (Exception e)
            {
                ds = null;
            }
            return GetObjectJson(ds);
        }
        /// 
        /// 返回选择生产资源列表
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/MES_GetProductionResourcesList_Json")]
        [HttpGet]
        public object MES_GetProductionResourcesList_Json(string sWhere)
        {
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                ds = oCN.RunProcReturn("Select HItemID,HNumber 生产资源代码,HName 生产资源 from Gy_Source where HStopflag=0 " + sWhere + " Order by HItemID ", "Gy_Source");
            }
            catch (Exception e)
            {
                ds = null;
            }
            return GetObjectJson(ds);
        }
        /// 
        /// 扫条码方法(进站接收单、出站汇报单)
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/TxtHBarCode_KeyDown")]
        [HttpGet]
        public object TxtHBarCode_KeyDown(string sBillBarCode)
        {
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (sBillBarCode == null || sBillBarCode.Equals(""))
                {
                    return CustomError("未输入条形码!");
                }
                else
                {
                    //拆分条形码
                    string[] NewBarCode;
                    if (sBillBarCode.CompareTo("#") > 0)
                    {
                        NewBarCode = sBillBarCode.Split(Convert.ToChar("#"));
                        sBillBarCode = NewBarCode[0];
                    }
                    string sBillNo = sBillBarCode;
                    ds = oCN.RunProcReturn("select top 1 * from h_v_Sc_ProcessExchangeBillList  where 单据号= '" + sBillNo + "'", "h_v_Sc_ProcessExchangeBillList");
                    if (ds == null || ds.Tables[0].Rows.Count == 0)
                    {
                        return CustomError("不存在此流转卡号!");
                    }
                }
            }
            catch (Exception e)
            {
                ds = null;
            }
            return CustomCorrect(ds);
        }
        /// 
        /// 流水号回车方法(进站接收单、出站汇报单)
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/TxtHProcNo_KeyDown")]
        [HttpGet]
        public object TxtHProcNo_KeyDown(string sProcNo, string sBillNo)
        {
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (sProcNo == null || sProcNo.Equals("") || sBillNo == null || sBillNo.Equals(""))
                {
                    return CustomError("流水号或流转卡号为空!");
                }
                else
                {
                    ds = oCN.RunProcReturn("select top 1 * from h_v_Sc_ProcessExchangeBillList  where 单据号= '" + sBillNo + "' and 下道流转工序<>'转' and 委外标记=0 and 流转标记=1  and 工序号='" + sProcNo + "'", "h_v_Sc_ProcessExchangeBillList");
                }
            }
            catch (Exception e)
            {
                ds = null;
            }
            return CustomCorrect(ds);
        }
        /// 
        /// 扫流转卡号方法(产线包装单、产线组装追溯单)--根据流转卡号保存到temp表
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/SaveCirculationCard_Json")]
        [HttpGet]
        public object SaveCirculationCard_Json(string HBillNo, string HInterID, string HBillNo2, string HBarCode,
            string HNumber, string HName, string HModel)
        {
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (HBillNo == null || HBillNo.Equals("") || HInterID == null || HInterID.Equals("") || HBillNo2 == null || HBillNo2.Equals(""))
                {
                    return CustomError("流转卡号不能为空!");
                }
                else
                {
                    if (HBarCode == null || HBarCode.Equals(""))
                    {
                        HBarCode = " ";
                    }
                    ds = oCN.RunProcReturn("exec Save_Sc_ProcessExchangeBillMainToTmp '" + HBillNo + "','" + HInterID + "','" + HBillNo2 + "','" + HBarCode + "','" + HNumber + "','" + HName + "','" + HModel + "'", "Save_Sc_ProcessExchangeBillMainToTmp");
                }
            }
            catch (Exception e)
            {
                ds = null;
            }
            return CustomCorrect(ds);
            //return ds.Tables[0].Rows;
        }
        /// 
        /// 扫子件条码方法(产线包装单、产线组装追溯单)--根据子件条码保存到temp表
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/SaveSubBarcode_Json")]
        [HttpGet]
        public object SaveSubBarcode_Json(string HBillNo, string SubBarcode, string HInterID, string HBillNo2,
            string HBillType, string sMaker, string HBarCode, string HNumber, string HName, string HModel)
        {
            DataSet ds1;
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (HBillNo == null || HBillNo.Equals("") || SubBarcode == null || SubBarcode.Equals("") || HInterID == null || HInterID.Equals("") || HBillNo2 == null || HBillNo2.Equals("") || HBillType == null || HBillType.Equals(""))
                {
                    return CustomError("流转卡号或子件条码不能为空!");
                }
                else
                {
                    ds1 = oCN.RunProcReturn("select * from KF_PonderationBillMain_Temp where HSourceBillNo = '" + SubBarcode + "' and HBillType = '" + HBillType + "'", "KF_PonderationBillMain_Temp");
                    if (ds1.Tables[0].Rows.Count > 0)
                    {
                        return CustomError("该子件条码已存在");
                    }
                    if (HBarCode == null || HBarCode.Equals(""))
                    {
                        HBarCode = " ";
                    }
                    ds = oCN.RunProcReturn("exec h_p_Save_SubBarcodeToTmp '" + HBillNo + "','" + SubBarcode + "','" + HInterID + "','" + HBillNo2 + "','" + HBillType + "','" + sMaker + "','" + HBarCode + "','" + HNumber + "','" + HName + "','" + HModel + "'", "h_p_Save_SubBarcodeToTmp");
                }
            }
            catch (Exception e)
            {
                ds = null;
            }
            return CustomCorrect(ds);
            //return ds.Tables[0].Rows;
        }
        /// 
        /// 查询条码档案表里的镭雕条码的合计数量方法(产线包装单)
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/Select_Gy_BarCodeBill_HQty")]
        [HttpGet]
        public object Select_Gy_BarCodeBill_HQty(string HBarCode)
        {
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (HBarCode == null || HBarCode.Equals(""))
                {
                    return CustomError("唯一条码不能为空!");
                }
                else
                {
                    ds = oCN.RunProcReturn("select HQty from Gy_BarCodeBill where HBarCode = '" + HBarCode + "'", "Gy_BarCodeBill");
                }
            }
            catch (Exception e)
            {
                ds = null;
            }
            return CustomCorrect(ds);
            //return ds.Tables[0].Rows;
        }
        /// 
        /// 更新条码档案表里的镭雕条码的合计数量方法(产线包装单)
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/Update_Gy_BarCodeBill_HQty")]
        [HttpGet]
        public object Update_Gy_BarCodeBill_HQty(string HBarCode,string HQty)
        {
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (HBarCode == null || HBarCode.Equals(""))
                {
                    return CustomError("唯一条码不能为空!");//h_p_Update_Gy_BarCodeBill_HQty
                }
                else
                {
                    ds = oCN.RunProcReturn("exec h_p_Update_Gy_BarCodeBill_HQty " + HBarCode + "," + HQty + "", "Gy_BarCodeBill");
                }
            }
            catch (Exception e)
            {
                ds = null;
            }
            return CustomCorrect(ds);
            //return ds.Tables[0].Rows;
        }
        /// 
        /// 更新扫码记录列表方法(产线包装单、产线组装追溯单)
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/Update_HBillNo_SubBarcodeList_Json")]
        [HttpGet]
        public object Update_HBillNo_SubBarcodeList_Json(string HBillNo)
        {
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (HBillNo == null || HBillNo.Equals(""))
                {
                    return CustomError("流转卡号不能为空!");
                }
                else
                {
                    ds = oCN.RunProcReturn("exec h_v_KF_PonderationBillMain_TempAddP '" + HBillNo + "'", "h_v_KF_PonderationBillMain_TempAddP");
                }
            }
            catch (Exception e)
            {
                ds = null;
            }
            return CustomCorrect(ds);
            //return ds.Tables[0].Rows;
        }
        /// 
        /// 提交-主表方法(产线包装单)--根据流传卡号从temp表保存到主表
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/SaveToSc_PackUnionBillMain")]
        [HttpGet]
        public object SaveToSc_PackUnionBillMain(string HBillNo, string HRemark)
        {
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (HBillNo == null || HBillNo.Equals(""))
                {
                    return CustomError("流转卡号不能为空!");
                }
                else
                {
                    if (HRemark == null || HRemark.Equals(""))
                    {
                        ds = oCN.RunProcReturn("exec h_p_Save_KF_PonderationBillMain_TempToSc_PackUnionBillMain '" + HBillNo + "',''", "h_p_Save_KF_PonderationBillMain_TempToSc_PackUnionBillMain");
                    }
                    else 
                    {
                        ds = oCN.RunProcReturn("exec h_p_Save_KF_PonderationBillMain_TempToSc_PackUnionBillMain '" + HBillNo + "','" + HRemark + "'", "h_p_Save_KF_PonderationBillMain_TempToSc_PackUnionBillMain");
                    }
                }
            }
            catch (Exception e)
            {
                ds = null;
            }
            return CustomCorrect(ds);
            //return ds.Tables[0].Rows;
        }
        /// 
        /// 提交-子表方法(产线包装单)--根据子件条码从temp表保存到子表
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/SaveToSc_PackUnionBillSub")]
        [HttpGet]
        public object SaveToSc_PackUnionBillSub(string HitemID, string HRemark)
        {
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (HitemID == null || HitemID.Equals(""))
                {
                    return CustomError("更新失败HitemID为空!");
                }
                else
                {
                    ds = oCN.RunProcReturn("exec h_p_Save_KF_PonderationBillMain_TempToSc_PackUnionBillSub '" + HitemID + "','" + HRemark + "'", "h_p_Save_KF_PonderationBillMain_TempToSc_PackUnionBillSub");
                }
            }
            catch (Exception e)
            {
                ds = null;
            }
            return CustomCorrect(ds);
            //return ds.Tables[0].Rows;
        }
        /// 
        /// 更新temp表的标记字段方法(产线包装单)
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/Update_Flag_HRelationInterID")]
        [HttpGet]
        public object Update_Flag_HRelationInterID(string HitemID)
        {
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (HitemID == null || HitemID.Equals(""))
                {
                    return CustomError("更新失败HitemID为空!");
                }
                else
                {
                    ds = oCN.RunProcReturn("exec h_p_Update_Flag_HRelationInterID '" + HitemID + "'", "h_p_Update_Flag_HRelationInterID");
                }
            }
            catch (Exception e)
            {
                ds = null;
            }
            return CustomCorrect(ds);
            //return ds.Tables[0].Rows;
        }
        /// 
        /// 提交-主表方法(产线组装追溯单)--根据流传卡号从temp表保存到主表
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/SaveToSc_AssemblyBillMain")]
        [HttpGet]
        public object SaveToSc_AssemblyBillMain(string HBillNo)
        {
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (HBillNo == null || HBillNo.Equals(""))
                {
                    return CustomError("流转卡号不能为空!");
                }
                else
                {
                    ds = oCN.RunProcReturn("exec h_p_Save_KF_PonderationBillMain_TempToSc_AssemblyBillMain '" + HBillNo + "'", "h_p_Save_KF_PonderationBillMain_TempToSc_AssemblyBillMain");
                }
            }
            catch (Exception e)
            {
                ds = null;
            }
            return CustomCorrect(ds);
            //return ds.Tables[0].Rows;
        }
        /// 
        /// 提交-子表方法(产线组装追溯单)--根据子件条码从temp表保存到子表
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/SaveToSc_AssemblyBillSub")]
        [HttpGet]
        public object SaveToSc_AssemblyBillSub(string HitemID)
        {
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (HitemID == null || HitemID.Equals(""))
                {
                    return CustomError("更新失败HitemID为空!");
                }
                else
                {
                    ds = oCN.RunProcReturn("exec h_p_Save_KF_PonderationBillMain_TempToSc_AssemblyBillSub '" + HitemID + "'", "h_p_Save_KF_PonderationBillMain_TempToSc_AssemblyBillSub");
                }
            }
            catch (Exception e)
            {
                ds = null;
            }
            return CustomCorrect(ds);
            //return ds.Tables[0].Rows;
        }
        /// 
        /// 生成唯一条码方法(产线包装单)--保存时生成唯一条码
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/SaveBarCode_json")]
        [HttpGet]
        public object SaveBarCode_json(string sHMaterID, string sHUnitID, string sHBarcodeNo, string sHMaterName)
        {
            DataSet ds;
            string sTMNumber = "";
            string aDate = System.DateTime.Now.ToString();
            string sDate = System.DateTime.Now.ToShortDateString();
            string sYear = ClsPub.isDate(sDate).Year.ToString().Substring(2, 2);
            string sPeriod = "0" + ClsPub.isDate(sDate).Month.ToString();
            sPeriod = sPeriod.Substring(sPeriod.Length - 2, 2);
            string sDay = "0" + ClsPub.isDate(sDate).Day.ToString();
            sDay = sDay.Substring(sDay.Length - 2, 2);
            int LSH = 0;
            try
            {
                SQLHelper.ClsCN oCn = new SQLHelper.ClsCN();
                sTMNumber = sHMaterID + sYear + sPeriod + sDay;
                ds = oCn.RunProcReturn("exec h_p_WMS_GetMaxNo '" + sTMNumber + "'", "h_p_WMS_GetMaxNo");    //获取最大流水号
                oCn.RunProc("exec h_p_WMS_SetMaxNo '" + sTMNumber + "'");
                LSH = ClsPub.isInt(ds.Tables[0].Rows[0][0]);
                LSH = LSH + 1;
                string LSH2 = LSH.ToString();
                while (LSH2.Length < 6)  //如果流水号小于6位数前面补0
                {
                    LSH2 = "0" + LSH2;
                }
                string TM = sTMNumber + LSH2;
                oCn.RunProc("exec h_p_WMS_SetMaxNo '" + sTMNumber + "'");
                string HBarCode = TM;
                string HBarCodeType = "唯一条码";
                int HMaterID = ClsPub.isInt(sHMaterID);
                int HUnitID = ClsPub.isInt(sHUnitID);
                int HQty = 0;
                string HBatchNo = "";
                string HSupID = "0";
                string HGroupID = "0";
                string HMaker = ClsPub.CurUserName;
                double HPrintQty = 0;
                double HinitQty = 0;
                string HSourceInterID = "0";
                string HSourceEntryID = "0";
                string HSourceBillNo = sHBarcodeNo;
                string HSourceBillType = "";
                double HEndQty = 0;
                int HBarcodeQtys = 0;
                int HBarcodeNo = 0;
                string HDeptID = "0";
                string HWhID = "0";
                string HSPID = "0";
                string HRemark = "";
                string HCusID = "0";
                string HCusType = "";
                string HEndDate = sDate;
                string HWorkLineName = "";
                string HBarCodeDate = sDate;
                string HSTOCKORGID = "0";
                string HOWNERID = "0";
                string HSeOrderBillNo = "";
                string HMaterName = sHMaterName;
                string HMaterModel = "";
                string HPinfan = "";
                string HAuxPropID = "0";
                string HMTONo = "";
                oCn.RunProc("insert into Gy_BarCodeBill "
                    + " (HBarCode,HBarCodeType,HMaterID,HUnitID,HQty,"
                    + " HBatchNo,HSupID,HGroupID,HMaker,HMakeDate,"
                    + " HPrintQty,HinitQty,HSourceInterID,HSourceEntryID,HSourceBillNo,"
                    + " HSourceBillType,HEndQty,HBarcodeQtys,HBarcodeNo,HDeptID,"
                    + " HWhID,HSPID,HRemark,HCusID,HCusType,"
                    + " HEndDate,HWorkLineName,HBarCodeDate,HSTOCKORGID,HOWNERID,"
                    + "HSeOrderBillNo,HMaterName,HMaterModel,HPinfan,HAuxPropID,HMTONo "
                    + ") values ('" + HBarCode + "','" + HBarCodeType + "','" + sHMaterID + "','" + sHUnitID + "','" + HQty + "','"
                        + HBatchNo + "','" + HSupID + "','" + HGroupID + "','" + HMaker + "',getdate(),'"
                        + HPrintQty + "','" + HinitQty + "','" + HSourceInterID + "','" + HSourceEntryID + "','" + HSourceBillNo + "','"
                        + HSourceBillType + "','" + HEndQty + "','" + HBarcodeQtys + "','" + HBarcodeNo + "','" + HDeptID + "','"
                        + HWhID + "','" + HSPID + "','" + HRemark + "','" + HCusID + "','" + HCusType + "','"
                        + HEndDate + "','" + HWorkLineName + "','" + HBarCodeDate + "','" + HSTOCKORGID + "'," + HOWNERID + ",'"
                        + HSeOrderBillNo + "','" + HMaterName + "','" + HMaterModel + "','" + HPinfan + "','" + HAuxPropID + "','" + HMTONo + "')"
                );
                ds = oCn.RunProcReturn("select HBarCode from Gy_BarCodeBill where HSourceBillNo = '" + sHBarcodeNo + "'  order by HMakeDate desc", "Gy_BarCodeBill");
            }
            catch (Exception e)
            {
                ds = null;
            }
            return CustomCorrect(ds);
        }
        /// 
        /// 返回产线包装单列表
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/MES_ProductionLinePackagingList_Json")]
        [HttpGet]
        public object MES_ProductionLinePackagingList_Json(string sWhere)
        {
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (sWhere == null || sWhere.Equals(""))
                {
                    ds = oCN.RunProcReturn("select * from h_v_ProductionLinePackagingList order by 唯一条码 desc", "h_v_ProductionLinePackagingList");
                }
                else
                {
                    string sql1 = "select * from h_v_ProductionLinePackagingList where 1 = 1 ";
                    string sql = sql1 + sWhere;
                    string sql2 = " order by 唯一条码 desc";
                    sql = sql + sql2;
                    ds = oCN.RunProcReturn(sql, "h_v_ProductionLinePackagingList");
                }
            }
            catch (Exception e)
            {
                ds = null;
            }
            return GetObjectJson(ds);
        }
        /// 
        /// 扫批次条码方法(产线组装追溯单)--根据批次条码到条码档案表中获取数据保存到temp表
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/Save_Gy_BarCodeBillToTmp_Json")]
        [HttpGet]
        public object Save_Gy_BarCodeBillToTmp_Json(string HInterID, string HBillNo2, string HBarCode)
        {
            DataSet ds1;
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (HInterID == null || HInterID.Equals("") || HBillNo2 == null || HBillNo2.Equals("") || HBarCode == null || HBarCode.Equals(""))
                {
                    return CustomError("流转卡号或子件条码不能为空!");
                }
                else
                {
                    ds1 = oCN.RunProcReturn("select top 1 HBarCode from Gy_BarCodeBill where HBarCode = '" + HBarCode + "'", "Gy_BarCodeBill");
                    if (ds1.Tables[0].Rows.Count == 0)
                    {
                        return CustomError("无此批次条码");
                    }
                    ds = oCN.RunProcReturn("exec h_v_Save_Gy_BarCodeBillToTmp '" + HInterID + "','" + HBillNo2 + "','" + HBarCode + "'", "h_v_Save_Gy_BarCodeBillToTmp");
                }
            }
            catch (Exception e)
            {
                ds = null;
            }
            return CustomCorrect(ds);
            //return ds.Tables[0].Rows;
        }
        /// 
        /// 返回产线组装追溯单列表
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/MES_AssemblyTraceabilityList_Json")]
        [HttpGet]
        public object MES_AssemblyTraceabilityList_Json(string sWhere)
        {
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (sWhere == null || sWhere.Equals(""))
                {
                    ds = oCN.RunProcReturn("select * from h_v_AssemblyTraceabilityList order by 生产任务单单号 desc,条码类型排序字段 ", "h_v_ProductionLinePackagingList");
                }
                else
                {
                    string sql1 = "select * from h_v_AssemblyTraceabilityList where 1 = 1 ";
                    string sql = sql1 + sWhere;
                    string sql2 = " order by 生产任务单单号 desc,条码类型排序字段 ";
                    sql = sql + sql2;
                    ds = oCN.RunProcReturn(sql, "h_v_AssemblyTraceabilityList");
                }
            }
            catch (Exception e)
            {
                ds = null;
            }
            return GetObjectJson(ds);
        }
        /// 
        /// 返回产线组装追溯单列表--过滤条件为镭雕条码的专用方法
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/MES_AssemblyTraceabilityList_E_Json")]
        [HttpGet]
        public object MES_AssemblyTraceabilityList_E_Json(string HICMOBillNo)
        {
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (HICMOBillNo == null || HICMOBillNo.Equals(""))
                {
                    return CustomError("流转卡号或子件条码不能为空!");
                }
                else
                {
                    ds = oCN.RunProcReturn("exec h_v_AssemblyTraceabilityList_E " + HICMOBillNo, "h_v_AssemblyTraceabilityList_E");
                }
            }
            catch (Exception e)
            {
                ds = null;
            }
            return GetObjectJson(ds);
        }
        /// 
        /// 返回墙咔工资统计报表
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("LEMS/MES_Qk_OutProcessList_Json")]
        [HttpGet]
        public object MES_Qk_OutProcessList_Json(string sWhere)
        {
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                ds = oCN.RunProcReturn("select * from (select min(a.HDate) 起始时间, max(a.HDate) 截至时间, b.HName 工序名称, c.HName 班组名称, d.HName 汇报人, sum(a.HQty) 合计汇报数量, sum(b.HFixPrice * a.HQty) 合计工资 from Sc_StationOutBillMain a left join Gy_Process b on a.HProcID = b.HItemID Left join Gy_Group c on a.HGroupID = c.HItemID left join Gy_Employee d on a.HEmpID = d.HItemID where 1=1 group by b.HName,c.HName,d.HName) t where 1=1 " + sWhere, "Sc_StationOutBillMain");
            }
            catch (Exception e)
            {
                ds = null;
            }
            return GetObjectJson(ds);
        }
        //单行删除进站接收单列表
        [Route("LEMS/StationInBillListDelete_Json")]
        [HttpGet]
        public object StationInBillListDelete_Json(long sHInterID)
        {
            try
            {
                SQLHelper.ClsCN oCn = new SQLHelper.ClsCN();
                oCn.RunProc("Delete from Sc_StationInBillMain where HInterID=" + sHInterID, ref DBUtility.ClsPub.sExeReturnInfo);
                objJsonResult.code = "200";
                objJsonResult.count = 1;
                objJsonResult.Message = "删除成功!";
                objJsonResult.data = null;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "000";
                objJsonResult.count = 0;
                objJsonResult.Message = "删除失败!";
                objJsonResult.data = e.ToString();
                return objJsonResult;
            }
        }
        //单行删除出站汇报单列表
        [Route("LEMS/StationOutBillListDelete_Json")]
        [HttpGet]
        public object StationOutBillListDelete_Json(long sHInterID)
        {
            try
            {
                SQLHelper.ClsCN oCn = new SQLHelper.ClsCN();
                oCn.RunProc("Delete from Sc_StationOutBillMain where HInterID=" + sHInterID, ref DBUtility.ClsPub.sExeReturnInfo);
                objJsonResult.code = "200";
                objJsonResult.count = 1;
                objJsonResult.Message = "删除成功!";
                objJsonResult.data = null;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "000";
                objJsonResult.count = 0;
                objJsonResult.Message = "删除失败!";
                objJsonResult.data = e.ToString();
                return objJsonResult;
            }
        }
        
    }
}