/**  版本信息模板在安装目录下，可自行修改。
* base_stock.cs
*
* 功 能： N/A
* 类 名： base_stock
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2024-07-22 11:33:40   N/A    初版
*
* Copyright (c) 2012 Maticsoft Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Jpsoft.DAL
{
	/// <summary>
	/// 数据访问类:base_stock
	/// </summary>
	public partial class Stock
	{
		public Stock()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string id_)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from base_stock");
			strSql.Append(" where id_=@id_ ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@id_", MySqlDbType.VarChar,36)			};
			parameters[0].Value = id_;

			return DbHelperMySQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Jpsoft.Model.Stock model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into base_stock(");
			strSql.Append("id_,work_id,machine_name,stock_number,length_,tag_length,flaw_,status_,del_flag,create_by,create_time,update_by,update_time)");
			strSql.Append(" values (");
			strSql.Append("@id_,@work_id,@machine_name,@stock_number,@length_,@tag_length,@flaw_,@status_,@del_flag,@create_by,@create_time,@update_by,@update_time)");
			MySqlParameter[] parameters = {
					new MySqlParameter("@id_", MySqlDbType.VarChar,36),
					new MySqlParameter("@work_id", MySqlDbType.VarChar,36),
					new MySqlParameter("@machine_name", MySqlDbType.VarChar,100),
					new MySqlParameter("@stock_number", MySqlDbType.Int32,11),
					new MySqlParameter("@length_", MySqlDbType.Decimal,10),
					new MySqlParameter("@tag_length", MySqlDbType.Decimal,10),
					new MySqlParameter("@flaw_", MySqlDbType.VarChar,255),
					new MySqlParameter("@status_", MySqlDbType.Int32,1),
					new MySqlParameter("@del_flag", MySqlDbType.Bit),
					new MySqlParameter("@create_by", MySqlDbType.VarChar,36),
					new MySqlParameter("@create_time", MySqlDbType.Timestamp),
					new MySqlParameter("@update_by", MySqlDbType.VarChar,36),
					new MySqlParameter("@update_time", MySqlDbType.Timestamp)};
			parameters[0].Value = model.id_;
			parameters[1].Value = model.work_id;
			parameters[2].Value = model.machine_name;
			parameters[3].Value = model.stock_number;
			parameters[4].Value = model.length_;
			parameters[5].Value = model.tag_length;
			parameters[6].Value = model.flaw_;
			parameters[7].Value = model.status_;
			parameters[8].Value = model.del_flag;
			parameters[9].Value = model.create_by;
			parameters[10].Value = model.create_time;
			parameters[11].Value = model.update_by;
			parameters[12].Value = model.update_time;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Jpsoft.Model.Stock model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update base_stock set ");
			strSql.Append("work_id=@work_id,");
			strSql.Append("machine_name=@machine_name,");
			strSql.Append("stock_number=@stock_number,");
			strSql.Append("length_=@length_,");
			strSql.Append("tag_length=@tag_length,");
			strSql.Append("flaw_=@flaw_,");
			strSql.Append("status_=@status_,");
			strSql.Append("del_flag=@del_flag,");
			strSql.Append("create_by=@create_by,");
			strSql.Append("update_by=@update_by");
			strSql.Append(" where id_=@id_ ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@work_id", MySqlDbType.VarChar,36),
					new MySqlParameter("@machine_name", MySqlDbType.VarChar,100),
					new MySqlParameter("@stock_number", MySqlDbType.Int32,11),
					new MySqlParameter("@length_", MySqlDbType.Decimal,10),
					new MySqlParameter("@tag_length", MySqlDbType.Decimal,10),
					new MySqlParameter("@flaw_", MySqlDbType.VarChar,255),
					new MySqlParameter("@status_", MySqlDbType.Int32,1),
					new MySqlParameter("@del_flag", MySqlDbType.Bit),
					new MySqlParameter("@create_by", MySqlDbType.VarChar,36),
					new MySqlParameter("@update_by", MySqlDbType.VarChar,36),
					new MySqlParameter("@id_", MySqlDbType.VarChar,36)};
			parameters[0].Value = model.work_id;
			parameters[1].Value = model.machine_name;
			parameters[2].Value = model.stock_number;
			parameters[3].Value = model.length_;
			parameters[4].Value = model.tag_length;
			parameters[5].Value = model.flaw_;
			parameters[6].Value = model.status_;
			parameters[7].Value = model.del_flag;
			parameters[8].Value = model.create_by;
			parameters[9].Value = model.update_by;
			parameters[10].Value = model.id_;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string id_)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from base_stock ");
			strSql.Append(" where id_=@id_ ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@id_", MySqlDbType.VarChar,36)			};
			parameters[0].Value = id_;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string id_list )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from base_stock ");
			strSql.Append(" where id_ in ("+id_list + ")  ");
			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Jpsoft.Model.Stock GetModel(string id_)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id_,work_id,machine_name,stock_number,length_,tag_length,flaw_,status_,del_flag,create_by,create_time,update_by,update_time from base_stock ");
			strSql.Append(" where id_=@id_ ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@id_", MySqlDbType.VarChar,36)			};
			parameters[0].Value = id_;

			Jpsoft.Model.Stock model=new Jpsoft.Model.Stock();
			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Jpsoft.Model.Stock DataRowToModel(DataRow row)
		{
			Jpsoft.Model.Stock model=new Jpsoft.Model.Stock();
			if (row != null)
			{
				if(row["id_"]!=null)
				{
					model.id_=row["id_"].ToString();
				}
				if(row["work_id"]!=null)
				{
					model.work_id=row["work_id"].ToString();
				}
				if(row["machine_name"]!=null)
				{
					model.machine_name=row["machine_name"].ToString();
				}
				if(row["stock_number"]!=null && row["stock_number"].ToString()!="")
				{
					model.stock_number=int.Parse(row["stock_number"].ToString());
				}
				if(row["length_"]!=null && row["length_"].ToString()!="")
				{
					model.length_=decimal.Parse(row["length_"].ToString());
				}
				if(row["tag_length"]!=null && row["tag_length"].ToString()!="")
				{
					model.tag_length=decimal.Parse(row["tag_length"].ToString());
				}
				if(row["flaw_"]!=null)
				{
					model.flaw_=row["flaw_"].ToString();
				}
				if(row["status_"]!=null && row["status_"].ToString()!="")
				{
					model.status_=int.Parse(row["status_"].ToString());
				}
				if(row["del_flag"]!=null && row["del_flag"].ToString()!="")
				{
					if((row["del_flag"].ToString()=="1")||(row["del_flag"].ToString().ToLower()=="true"))
					{
						model.del_flag=true;
					}
					else
					{
						model.del_flag=false;
					}
				}
				if(row["create_by"]!=null)
				{
					model.create_by=row["create_by"].ToString();
				}
				if(row["create_time"]!=null && row["create_time"].ToString()!="")
				{
					model.create_time=DateTime.Parse(row["create_time"].ToString());
				}
				if(row["update_by"]!=null)
				{
					model.update_by=row["update_by"].ToString();
				}
				if(row["update_time"]!=null && row["update_time"].ToString()!="")
				{
					model.update_time=DateTime.Parse(row["update_time"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id_,work_id,machine_name,stock_number,length_,tag_length,flaw_,status_,del_flag,create_by,create_time,update_by,update_time ");
			strSql.Append(" FROM base_stock ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM base_stock ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperMySQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id_ desc");
			}
			strSql.Append(")AS Row, T.*  from base_stock T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			MySqlParameter[] parameters = {
					new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@PageSize", MySqlDbType.Int32),
					new MySqlParameter("@PageIndex", MySqlDbType.Int32),
					new MySqlParameter("@IsReCount", MySqlDbType.Bit),
					new MySqlParameter("@OrderType", MySqlDbType.Bit),
					new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
					};
			parameters[0].Value = "base_stock";
			parameters[1].Value = "id_";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

