/// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(BatchNoHisMDL model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update BatchNohis set ");
            strSql.Append("MaterialCode=@MaterialCode,");
            strSql.Append("BatchNo=@BatchNo,");
            strSql.Append("BatchNum=@BatchNum,");
            strSql.Append("Supplier=@Supplier,");
            strSql.Append("CreateTime=@CreateTime");
            strSql.Append(" where TID=@TID");
            SqlParameter[] parameters =
            {
                new SqlParameter("@MaterialCode", model.MaterialCode),
                new SqlParameter("@BatchNo",      model.BatchNo),
                new SqlParameter("@BatchNum",     model.BatchNum),
                new SqlParameter("@Supplier",     model.Supplier),
                new SqlParameter("@CreateTime",   model.CreateTime),
                new SqlParameter("@TID",          model.TID)
            };

            int rows = helper.ExecuteSql(strSql.ToString(), parameters);

            if (rows > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        /// <summary>
        /// 更新批次信息
        /// </summary>
        private void Update(BatchNoMDL model = null)
        {
            try
            {
                if (model == null)//Insert 方法传过来的
                {
                    this.Insert();
                    return;
                }
                else
                {
                    bool flag = DataDAL.Update(model);
                    if (flag)
                    {
                        ReturnData.Code = "1";
                        ReturnData.Msg  = "OK";
                    }

                    BatchNoHisDAL HisDAL   = new BatchNoHisDAL();
                    BatchNoHisMDL HisModel = new BatchNoHisMDL();
                    HisModel.MaterialCode = model.MaterialCode;
                    HisModel.BatchNo      = model.BatchNo;
                    HisModel.BatchNum     = model.BatchNum;
                    HisModel.Supplier     = model.Supplier;
                    HisModel.CreateTime   = DateTime.Now;
                    flag = HisDAL.Add(HisModel);
                }
            }
            catch (Exception ex)
            {
                CLog.WriteErrLog(ex.Message + ex.StackTrace);
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BatchNoHisMDL DataRowToModel(DataRow row)
        {
            BatchNoHisMDL model = new BatchNoHisMDL();

            if (row != null)
            {
                if (row["TID"] != null && row["TID"].ToString() != "")
                {
                    model.TID = long.Parse(row["TID"].ToString());
                }
                if (row["MaterialCode"] != null)
                {
                    model.MaterialCode = row["MaterialCode"].ToString();
                }
                if (row["BatchNo"] != null)
                {
                    model.BatchNo = row["BatchNo"].ToString();
                }
                if (row["BatchNum"] != null && row["BatchNum"].ToString() != "")
                {
                    model.BatchNum = int.Parse(row["BatchNum"].ToString());
                }
                if (row["Supplier"] != null)
                {
                    model.Supplier = row["Supplier"].ToString();
                }
                if (row["CreateTime"] != null && row["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(row["CreateTime"].ToString());
                }
            }
            return(model);
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(BatchNoHisMDL model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("insert into BatchNohis(");
            strSql.Append("MaterialCode,BatchNo,BatchNum,Supplier,CreateTime)");
            strSql.Append(" values (");
            strSql.Append("@MaterialCode,@BatchNo,@BatchNum,@Supplier,@CreateTime)");
            SqlParameter[] parameters =
            {
                new SqlParameter("@MaterialCode", model.MaterialCode),
                new SqlParameter("@BatchNo",      model.BatchNo),
                new SqlParameter("@BatchNum",     model.BatchNum),
                new SqlParameter("@Supplier",     model.Supplier),
                new SqlParameter("@CreateTime",   model.CreateTime)
            };

            int rows = helper.ExecuteSql(strSql.ToString(), parameters);

            if (rows > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
 /// <summary>
 /// 获取历史批次信息
 /// </summary>
 private void Select()
 {
     try
     {
         string method = context.Request.Params["method"].ToString();
         if (method == "model")
         {
             string TID = context.Request.Params["TID"].ToString();
             string sql = " 1=1 ";
             if (!string.IsNullOrEmpty(TID))
             {
                 sql += string.Format(" AND TID = {0}", TID);
             }
             BatchNoHisMDL model = DataDAL.GetModel(sql);
             if (model != null)
             {
                 ReturnData.Code = "1";
                 ReturnData.Msg  = "OK";
                 ReturnData.Data = model;
             }
         }
         else if (method == "search")
         {
             string sql = " 1=1 ";
             //string ProductType = context.Request.Params["ProductType"].ToString();
             //string ProductCode = context.Request.Params["ProductCode"].ToString();
             //CLog.WriteErrLog(ProductType);
             //if (!string.IsNullOrEmpty(ProductCode))
             //{
             //    sql += string.Format(" AND ProductCode = '{0}'", ProductCode);
             //}
             //if (!string.IsNullOrEmpty(ProductType))
             //{
             //    sql += string.Format(" AND ProductType = '{0}'", ProductType);
             //}
             sql += " ORDER BY TID ASC";
             //CLog.WriteErrLog(sql);
             DataSet set = DataDAL.GetList(sql);
             if (set != null && set.Tables.Count > 0)
             {
                 DataTable table = set.Tables[0];
                 if (table != null && table.Rows.Count > 0)
                 {
                     object obj = TableHelper.TableToObj(table);
                     ReturnData.Code = "1";
                     ReturnData.Msg  = "OK";
                     ReturnData.Data = obj;
                 }
             }
         }
     }
     catch (Exception ex)
     {
         CLog.WriteErrLog(ex.Message);
     }
 }
        /// <summary>
        /// 添加历史批次信息
        /// </summary>
        private void Insert()
        {
            try
            {
                string MaterialCode = context.Request.Params["MaterialCode"].ToString();
                string BatchNo      = context.Request.Params["BatchNo"].ToString();
                string BatchNumStr  = context.Request.Params["BatchNum"].ToString();
                string Supplier     = context.Request.Params["Supplier"].ToString();
                int    BatchNum     = 0;
                bool   b            = int.TryParse(BatchNumStr, out BatchNum);
                string CreateTime   = context.Request.Params["CreateTime"].ToString();
                if (string.IsNullOrEmpty(MaterialCode) || string.IsNullOrEmpty(BatchNo))
                {
                    ReturnData.Msg = "参数错误";
                }
                else
                {
                    BatchNoHisMDL Model = new BatchNoHisMDL();
                    Model.MaterialCode = MaterialCode;
                    Model.BatchNo      = BatchNo;
                    Model.Supplier     = Supplier;

                    if (b)
                    {
                        Model.BatchNum = BatchNum;
                    }

                    if (!string.IsNullOrEmpty(CreateTime))
                    {
                        Model.CreateTime = DateTime.Parse(CreateTime);
                    }
                    else
                    {
                        Model.CreateTime = DateTime.Now;
                    }
                    bool flag = DataDAL.Add(Model);
                    if (flag)
                    {
                        ReturnData.Code = "1";
                        ReturnData.Msg  = "OK";
                    }
                }
            }
            catch (Exception ex)
            {
                CLog.WriteErrLog(ex.Message);
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BatchNoHisMDL GetModel(string where)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select TID,MaterialCode,BatchNo,BatchNum,Supplier,CreateTime from BatchNohis ");
            if (!string.IsNullOrEmpty(where))
            {
                strSql.Append(" where " + where);
            }
            BatchNoHisMDL model = new BatchNoHisMDL();
            DataSet       ds    = helper.Query(strSql.ToString());

            if (ds.Tables[0].Rows.Count > 0)
            {
                return(DataRowToModel(ds.Tables[0].Rows[0]));
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BatchNoHisMDL GetModel(long TID)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select TID,MaterialCode,BatchNo,BatchNum,Supplier,CreateTime from BatchNohis ");
            strSql.Append(" where TID=@TID");
            SqlParameter[] parameters =
            {
                new SqlParameter("@TID", TID)
            };

            BatchNoHisMDL model = new BatchNoHisMDL();
            DataSet       ds    = helper.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                return(DataRowToModel(ds.Tables[0].Rows[0]));
            }
            else
            {
                return(null);
            }
        }
 /// <summary>
 /// 上传数据
 /// </summary>
 private void Upload()
 {
     try
     {
         string    Param        = context.Request.Params["Param"].ToString();
         DataTable ReceiveTable = JsonHelper.JsonDeSerializer <DataTable>(Param);
         int       UploadCount  = 0;
         int       RowCount     = 0;
         string    ids          = "";//上传成功的ID
         if (ReceiveTable != null && ReceiveTable.Rows.Count > 0)
         {
             RowCount = ReceiveTable.Rows.Count;
             foreach (DataRow row in ReceiveTable.Rows)
             {
                 BatchNoHisMDL Model = DataDAL.DataRowToModel(row);
                 bool          flag  = DataDAL.Add(Model);
                 if (flag)
                 {
                     UploadCount++;
                     ids += string.Format("{0},", Model.TID);
                 }
             }
         }
         if (ids.Length > 0)
         {
             ids = ids.Substring(0, ids.Length - 1);
         }
         ReturnData.Code = "1";
         ReturnData.Msg  = "OK";
         ReturnData.Data = ids;
     }
     catch (Exception ex)
     {
         CLog.WriteErrLog(ex.Message);
     }
 }
        /// <summary>
        /// 添加批次信息
        /// </summary>
        private void Insert()
        {
            try
            {
                //string ProductCode = context.Request.Params["ProductCode"].ToString();
                string ProductType  = context.Request.Params["ProductType"].ToString();
                string MaterialCode = context.Request.Params["MaterialCode"].ToString();
                string BatchNo      = context.Request.Params["BatchNo"].ToString();
                string BatchNumStr  = context.Request.Params["BatchNum"].ToString();
                string Supplier     = context.Request.Params["Supplier"].ToString();
                int    BatchNum     = 0;
                bool   b            = int.TryParse(BatchNumStr, out BatchNum);

                BatchNoMDL Model = null;
                string     sql   = string.Format("materialcode='{0}'", MaterialCode);
                Model = DataDAL.GetModel(sql);
                if (Model != null)
                {
                    Model.BatchNo  = BatchNo;
                    Model.BatchNum = (b && BatchNum > 0) ? BatchNum : Model.BatchNum;
                    Model.StockNum = Model.BatchNum;
                    Model.Supplier = Supplier;

                    this.Update(Model);
                    return;
                }
                else
                {
                    Model = new BatchNoMDL();
                    sql   = string.Format("producttype='{0}' AND materialcode='{1}'", ProductType, MaterialCode);
                    ProductBomInfoDAL pb   = new ProductBomInfoDAL();
                    ProductBomInfoMDL info = pb.GetModel(sql);

                    if (b && BatchNum > 0)
                    {
                        Model.BatchNum = BatchNum;
                    }
                    else
                    {
                        if (info != null || info.BatchNum.ToString().Trim() != "")
                        {
                            Model.BatchNum = int.Parse(info.BatchNum.ToString());
                        }
                        else
                        {
                            Model.BatchNum = 1;
                        }
                    }
                    Model.StockNum     = Model.BatchNum;
                    Model.MaterialCode = MaterialCode;
                    Model.MaterialName = info.MaterialName;
                    Model.BatchNo      = BatchNo;
                    Model.Supplier     = Supplier;

                    bool flag = DataDAL.Add(Model);
                    if (flag)
                    {
                        ReturnData.Code = "1";
                        ReturnData.Msg  = "OK";
                    }

                    BatchNoHisDAL HisDAL   = new BatchNoHisDAL();
                    BatchNoHisMDL HisModel = new BatchNoHisMDL();
                    HisModel.MaterialCode = Model.MaterialCode;
                    HisModel.BatchNo      = BatchNo;
                    HisModel.BatchNum     = Model.BatchNum;
                    HisModel.Supplier     = Model.Supplier;
                    HisModel.CreateTime   = DateTime.Now;
                    flag = HisDAL.Add(HisModel);
                }
            }
            catch (Exception ex)
            {
                CLog.WriteErrLog(ex.Message + ex.StackTrace);
            }
        }