using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;
using Dapper;
using PushPlatform.Data;

namespace PushPlatform.Data.DaoAchieve
{
    sealed partial class SysMenuDaoAchieve
    {
        
        
        
        
        #region Dao
        public int Save(SysMenu entity)
        {
            using(IDbConnection connection = new MySqlConnection(PushPlatformContext.connString))
            {
                var result = connection.Execute("Insert into sys_menu values (@Id,@Parent_Id,@Menu_Name,@Menu_Url,@Menu_Sort,@Create_User,@Create_Time,@Update_User,@Update_Time,@Is_Deleted,@Timestamp)",
                                   entity);
                return result;
            }
        }
        
        public int Save(List<SysMenu> entityList)
        {
            using(IDbConnection connection = new MySqlConnection(PushPlatformContext.connString))
            {
                var result = connection.Execute("Insert into sys_menu values (@Id,@Parent_Id,@Menu_Name,@Menu_Url,@Menu_Sort,@Create_User,@Create_Time,@Update_User,@Update_Time,@Is_Deleted,@Timestamp)",
                                   entityList);
                return result;
            }
        }
        
        
        public int Delete(string id)
        {
            using(IDbConnection connection = new MySqlConnection(PushPlatformContext.connString))
            {
                var result = connection.Execute("delete from sys_menu where Id=@id",
                                   new { id = id });
                return result;
            }
        }
        
        public int Update(SysMenu entity)
        {
            using(IDbConnection connection = new MySqlConnection(PushPlatformContext.connString))
            {
                var result = connection.Execute("update sys_menu set Parent_Id=@Parent_Id,Menu_Name=@Menu_Name,Menu_Url=@Menu_Url,Menu_Sort=@Menu_Sort,Create_User=@Create_User,Create_Time=@Create_Time,Update_User=@Update_User,Update_Time=@Update_Time,Is_Deleted=@Is_Deleted,Timestamp=@Timestamp where Id=@id",
                                   entity);
                return result;
            }
        }
        
        public int Update(List<SysMenu> entityList)
        {
            using(IDbConnection connection = new MySqlConnection(PushPlatformContext.connString))
            {
                var result = connection.Execute("update sys_menu set Parent_Id=@Parent_Id,Menu_Name=@Menu_Name,Menu_Url=@Menu_Url,Menu_Sort=@Menu_Sort,Create_User=@Create_User,Create_Time=@Create_Time,Update_User=@Update_User,Update_Time=@Update_Time,Is_Deleted=@Is_Deleted,Timestamp=@Timestamp where Id=@id",
                                   entityList);
                return result;
            }
        }
        
        public int SaveOrUpdate(IList<SysMenu> entityList)
        {
            if (entityList == null || entityList.Count == 0)
                return 0;
            List<SysMenu> saveList = new List<SysMenu>();
            List<SysMenu> updateList = new List<SysMenu>();
            foreach(var entity in entityList)
            {
                if(entity.IsEdit)
                {
                    updateList.Add(entity);
                }
                else
                {
                    saveList.Add(entity);
                }
            }
            int save = 0;
            int update = 0;
            using(IDbConnection connection = new MySqlConnection(PushPlatformContext.connString))
            {
		connection.Open();
                IDbTransaction transaction = connection.BeginTransaction();
                try
                {
                    if (updateList.Count > 0)
                    {
                        update = connection.Execute("update sys_menu set Parent_Id=@Parent_Id,Menu_Name=@Menu_Name,Menu_Url=@Menu_Url,Menu_Sort=@Menu_Sort,Create_User=@Create_User,Create_Time=@Create_Time,Update_User=@Update_User,Update_Time=@Update_Time,Is_Deleted=@Is_Deleted,Timestamp=@Timestamp where Id=@id",
                                       updateList, transaction, null, null);
                    }
                    if (saveList.Count > 0)
                    {
                        save = connection.Execute("Insert into sys_menu values (@Id,@Parent_Id,@Menu_Name,@Menu_Url,@Menu_Sort,@Create_User,@Create_Time,@Update_User,@Update_Time,@Is_Deleted,@Timestamp)",
                                       saveList, transaction, null, null);
                    }
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw new Exception(ex.Message);
                }
                return save + update;
            }
        }
        
        public int SaveOrUpdateWithTran(IList<SysMenu> entityList, IDbConnection connection, IDbTransaction transaction)
        {
            if (entityList == null || entityList.Count == 0)
                return 0;
            List<SysMenu> saveList = new List<SysMenu>();
            List<SysMenu> updateList = new List<SysMenu>();
            foreach(var entity in entityList)
            {
                if(entity.IsEdit)
                {
                    updateList.Add(entity);
                }
                else
                {
                    saveList.Add(entity);
                }
            }
            int save = 0;
            int update = 0;
            if (updateList.Count > 0)
            {
                update = connection.Execute("update sys_menu set Parent_Id=@Parent_Id,Menu_Name=@Menu_Name,Menu_Url=@Menu_Url,Menu_Sort=@Menu_Sort,Create_User=@Create_User,Create_Time=@Create_Time,Update_User=@Update_User,Update_Time=@Update_Time,Is_Deleted=@Is_Deleted,Timestamp=@Timestamp where Id=@id",
                               updateList, transaction, null, null);
            }
            if (saveList.Count > 0)
            {
                save = connection.Execute("Insert into sys_menu values (@Id,@Parent_Id,@Menu_Name,@Menu_Url,@Menu_Sort,@Create_User,@Create_Time,@Update_User,@Update_Time,@Is_Deleted,@Timestamp)",
                               saveList, transaction, null, null);
            }
            return save + update;
        }
        
        public SysMenu Get(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return null;
            }
            using(IDbConnection connection = new MySqlConnection(PushPlatformContext.connString))
            {
                var query = connection.Query<SysMenu>("select * from sys_menu where Id=@id", new { id = id });
                if (query.Count() > 0)
                    return query.FirstOrDefault();
                return null;
            }
        }
        
        public int GetCount(PushPlatform.Data.Criteria.CriteriaSysMenu mycriteria)
        {
            if(mycriteria.UsePage && (mycriteria.PageIndex <= 0 || mycriteria.PageSize < 1))
            {
                mycriteria.PageTotal = 0;
                return 0;
            }
            using(IDbConnection connection = new MySqlConnection(PushPlatformContext.connString))
            {
                if (mycriteria.UserWhere)
                {
                    if (mycriteria.WhereValue != null)
                    {
                        return connection.Query<int>("select count(1) from sys_menu " + mycriteria.Where, mycriteria.WhereValue).FirstOrDefault();
                    }
                    else
                    {
                        return connection.Query<int>("select count(1) from sys_menu " + mycriteria.Where).FirstOrDefault();
                    }
                }
                else
                {
                    return connection.Query<int>("select count(1) from sys_menu " + CreatAutoQuery(mycriteria)).FirstOrDefault(); 
                }
            }
        }
        
        public List<SysMenu> Fetch(PushPlatform.Data.Criteria.CriteriaSysMenu mycriteria)
        {
            if(mycriteria.UsePage && (mycriteria.PageIndex <= 0 || mycriteria.PageSize < 1))
            {
                mycriteria.PageTotal = 0;
                return new List<SysMenu>();
            }
            string orderby = "";
            foreach(var order in mycriteria.OrderList)
            {
                if(orderby=="")
                {
                    orderby = " order by ";
                }
                orderby += order+",";
            }
            if(orderby.Length>0)
            {
                orderby = orderby.Substring(0, orderby.Length - 1);
                if(!mycriteria.IsAsc)
                {
                    orderby += " desc ";
                }
            }
            int pageStart = (mycriteria.PageIndex - 1) * mycriteria.PageSize;
            using(IDbConnection connection = new MySqlConnection(PushPlatformContext.connString))
            {
                if (mycriteria.UserWhere)
                {
                    if(mycriteria.UsePage)
                    {
                        if (mycriteria.WhereValue != null)
                        {
                            mycriteria.PageTotal = connection.Query<int>("select count(1) from sys_menu " + mycriteria.Where + orderby, mycriteria.WhereValue).FirstOrDefault();
                            var query = connection.Query<SysMenu>("select * from sys_menu " + mycriteria.Where + orderby + " LIMIT " + pageStart.ToString() + "," + mycriteria.PageSize, mycriteria.WhereValue);
                            return query.ToList();
                        }
                        else
                        {
                            mycriteria.PageTotal = connection.Query<int>("select count(1) from sys_menu " + mycriteria.Where + orderby).FirstOrDefault();
                            var query = connection.Query<SysMenu>("select * from sys_menu " + mycriteria.Where + orderby + " LIMIT " + pageStart.ToString() + "," + mycriteria.PageSize);
                            return query.ToList();
                        }
                    }
                    else
                    {
                        if (mycriteria.WhereValue != null)
                        {
                            var query = connection.Query<SysMenu>("select * from sys_menu " + mycriteria.Where + orderby, mycriteria.WhereValue);
                            return query.ToList();
                        }
                        else
                        {
                            var query = connection.Query<SysMenu>("select * from sys_menu " + mycriteria.Where + orderby);
                            return query.ToList();
                        }
                    }
                }
                else
                {
                    if (mycriteria.UsePage)
                    {
                        string whereStr=CreatAutoQuery(mycriteria);
                        mycriteria.PageTotal = connection.Query<int>("select count(1) from sys_menu " + whereStr).FirstOrDefault();
                        string queryStr = "select * from sys_menu ";
                        queryStr += whereStr + orderby;
                        queryStr += " LIMIT " + pageStart.ToString() + "," + mycriteria.PageSize;
                        var query = connection.Query<SysMenu>(queryStr);
                        return query.ToList();
                    }
                    else
                    {
                        string queryStr = "select * from sys_menu ";
                        queryStr += CreatAutoQuery(mycriteria) + orderby;
                        var query = connection.Query<SysMenu>(queryStr);
                        return query.ToList();
                    }  
                }
            }
        }
        #endregion
        
        #region method
        private string CreatAutoQuery(Criteria.CriteriaSysMenu mycriteria)
        {
            string where = "";
            
            if (!string.IsNullOrEmpty(mycriteria.LikeId))
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Id like '%" + mycriteria.LikeId + "%'";
            }
            if (mycriteria.InId.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.InId)
                {
                    instr += "'"+str+"',";
                }
                instr = instr.Substring(0,instr.Length-1);
                where += " Id in (" + instr + ")";
            }
            if (mycriteria.NotId.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.NotId)
                {
                    instr += "'"+str+"',";
                }
                instr = instr.Substring(0,instr.Length-1);
                where += "Id not in (" + instr + ")";
            }
            if (mycriteria.OrEqId.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.OrEqId)
                {
                    if(instr=="")
                    {instr="(";}
                    instr += "Id='"+str+"' or ";
                }
                instr = instr.Substring(0,instr.Length-4);
                instr+=")";
                where += instr;
            }
            if (mycriteria.OrLikeId.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.OrLikeId)
                {
                    if(instr=="")
                    {instr="(";}
                    instr += "Id like '%"+str+"%' or ";
                }
                instr = instr.Substring(0,instr.Length-4);
                instr+=")";
                where += instr;
            }
            if (!string.IsNullOrEmpty(mycriteria.EqId))
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Id ='" + mycriteria.EqId + "'";
            }
            if (!string.IsNullOrEmpty(mycriteria.NeId))
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Id <>'" + mycriteria.NeId + "'";
            }
            if (mycriteria.NullId)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Id is null"; 
            }
            if (mycriteria.NotNullId)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Id is not null";
            }
            if (!string.IsNullOrEmpty(mycriteria.LikeParentId))
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Parent_Id like '%" + mycriteria.LikeParentId + "%'";
            }
            if (mycriteria.InParentId.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.InParentId)
                {
                    instr += "'"+str+"',";
                }
                instr = instr.Substring(0,instr.Length-1);
                where += " Parent_Id in (" + instr + ")";
            }
            if (mycriteria.NotParentId.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.NotParentId)
                {
                    instr += "'"+str+"',";
                }
                instr = instr.Substring(0,instr.Length-1);
                where += "Parent_Id not in (" + instr + ")";
            }
            if (mycriteria.OrEqParentId.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.OrEqParentId)
                {
                    if(instr=="")
                    {instr="(";}
                    instr += "Parent_Id='"+str+"' or ";
                }
                instr = instr.Substring(0,instr.Length-4);
                instr+=")";
                where += instr;
            }
            if (mycriteria.OrLikeParentId.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.OrLikeParentId)
                {
                    if(instr=="")
                    {instr="(";}
                    instr += "Parent_Id like '%"+str+"%' or ";
                }
                instr = instr.Substring(0,instr.Length-4);
                instr+=")";
                where += instr;
            }
            if (!string.IsNullOrEmpty(mycriteria.EqParentId))
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Parent_Id ='" + mycriteria.EqParentId + "'";
            }
            if (!string.IsNullOrEmpty(mycriteria.NeParentId))
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Parent_Id <>'" + mycriteria.NeParentId + "'";
            }
            if (mycriteria.NullParentId)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Parent_Id is null"; 
            }
            if (mycriteria.NotNullParentId)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Parent_Id is not null";
            }
            if (!string.IsNullOrEmpty(mycriteria.LikeMenuName))
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Menu_Name like '%" + mycriteria.LikeMenuName + "%'";
            }
            if (mycriteria.InMenuName.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.InMenuName)
                {
                    instr += "'"+str+"',";
                }
                instr = instr.Substring(0,instr.Length-1);
                where += " Menu_Name in (" + instr + ")";
            }
            if (mycriteria.NotMenuName.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.NotMenuName)
                {
                    instr += "'"+str+"',";
                }
                instr = instr.Substring(0,instr.Length-1);
                where += "Menu_Name not in (" + instr + ")";
            }
            if (mycriteria.OrEqMenuName.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.OrEqMenuName)
                {
                    if(instr=="")
                    {instr="(";}
                    instr += "Menu_Name='"+str+"' or ";
                }
                instr = instr.Substring(0,instr.Length-4);
                instr+=")";
                where += instr;
            }
            if (mycriteria.OrLikeMenuName.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.OrLikeMenuName)
                {
                    if(instr=="")
                    {instr="(";}
                    instr += "Menu_Name like '%"+str+"%' or ";
                }
                instr = instr.Substring(0,instr.Length-4);
                instr+=")";
                where += instr;
            }
            if (!string.IsNullOrEmpty(mycriteria.EqMenuName))
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Menu_Name ='" + mycriteria.EqMenuName + "'";
            }
            if (!string.IsNullOrEmpty(mycriteria.NeMenuName))
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Menu_Name <>'" + mycriteria.NeMenuName + "'";
            }
            if (mycriteria.NullMenuName)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Menu_Name is null"; 
            }
            if (mycriteria.NotNullMenuName)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Menu_Name is not null";
            }
            if (!string.IsNullOrEmpty(mycriteria.LikeMenuUrl))
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Menu_Url like '%" + mycriteria.LikeMenuUrl + "%'";
            }
            if (mycriteria.InMenuUrl.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.InMenuUrl)
                {
                    instr += "'"+str+"',";
                }
                instr = instr.Substring(0,instr.Length-1);
                where += " Menu_Url in (" + instr + ")";
            }
            if (mycriteria.NotMenuUrl.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.NotMenuUrl)
                {
                    instr += "'"+str+"',";
                }
                instr = instr.Substring(0,instr.Length-1);
                where += "Menu_Url not in (" + instr + ")";
            }
            if (mycriteria.OrEqMenuUrl.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.OrEqMenuUrl)
                {
                    if(instr=="")
                    {instr="(";}
                    instr += "Menu_Url='"+str+"' or ";
                }
                instr = instr.Substring(0,instr.Length-4);
                instr+=")";
                where += instr;
            }
            if (mycriteria.OrLikeMenuUrl.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.OrLikeMenuUrl)
                {
                    if(instr=="")
                    {instr="(";}
                    instr += "Menu_Url like '%"+str+"%' or ";
                }
                instr = instr.Substring(0,instr.Length-4);
                instr+=")";
                where += instr;
            }
            if (!string.IsNullOrEmpty(mycriteria.EqMenuUrl))
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Menu_Url ='" + mycriteria.EqMenuUrl + "'";
            }
            if (!string.IsNullOrEmpty(mycriteria.NeMenuUrl))
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Menu_Url <>'" + mycriteria.NeMenuUrl + "'";
            }
            if (mycriteria.NullMenuUrl)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Menu_Url is null"; 
            }
            if (mycriteria.NotNullMenuUrl)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Menu_Url is not null";
            }
            if (mycriteria.InMenuSort.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.InMenuSort)
                {
                    instr += str.ToString()+",";
                }
                instr = instr.Substring(0,instr.Length-1);
                where += " Menu_Sort in (" + instr + ")";
            }
            if (mycriteria.NotMenuSort.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.NotMenuSort)
                {
                    instr += str.ToString()+",";
                }
                instr = instr.Substring(0,instr.Length-1);
                where += "Menu_Sort not in (" + instr + ")";
            }
            if (mycriteria.OrEqMenuSort.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.OrEqMenuSort)
                {
                    if(instr=="")
                    {instr="(";}
                    instr += "Menu_Sort="+str.ToString()+" or ";
                }
                instr = instr.Substring(0,instr.Length-4);
                instr+=")";
                where += instr;
            }
            if (mycriteria.EqMenuSort!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Menu_Sort =" + mycriteria.EqMenuSort;
            }
            if (mycriteria.NeMenuSort!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Menu_Sort <>" + mycriteria.NeMenuSort;
            }
            if (mycriteria.LeMenuSort!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Menu_Sort <=" + mycriteria.LeMenuSort;
            }
            if (mycriteria.GeMenuSort!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Menu_Sort >=" + mycriteria.GeMenuSort;
            }
            if (mycriteria.LtMenuSort!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Menu_Sort <" + mycriteria.LtMenuSort;
            }
            if (mycriteria.GtMenuSort!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Menu_Sort >" + mycriteria.GtMenuSort;
            }
            if (mycriteria.NullMenuSort)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Menu_Sort is null"; 
            }
            if (mycriteria.NotNullMenuSort)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Menu_Sort is not null";
            }
            if (!string.IsNullOrEmpty(mycriteria.LikeCreateUser))
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Create_User like '%" + mycriteria.LikeCreateUser + "%'";
            }
            if (mycriteria.InCreateUser.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.InCreateUser)
                {
                    instr += "'"+str+"',";
                }
                instr = instr.Substring(0,instr.Length-1);
                where += " Create_User in (" + instr + ")";
            }
            if (mycriteria.NotCreateUser.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.NotCreateUser)
                {
                    instr += "'"+str+"',";
                }
                instr = instr.Substring(0,instr.Length-1);
                where += "Create_User not in (" + instr + ")";
            }
            if (mycriteria.OrEqCreateUser.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.OrEqCreateUser)
                {
                    if(instr=="")
                    {instr="(";}
                    instr += "Create_User='"+str+"' or ";
                }
                instr = instr.Substring(0,instr.Length-4);
                instr+=")";
                where += instr;
            }
            if (mycriteria.OrLikeCreateUser.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.OrLikeCreateUser)
                {
                    if(instr=="")
                    {instr="(";}
                    instr += "Create_User like '%"+str+"%' or ";
                }
                instr = instr.Substring(0,instr.Length-4);
                instr+=")";
                where += instr;
            }
            if (!string.IsNullOrEmpty(mycriteria.EqCreateUser))
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Create_User ='" + mycriteria.EqCreateUser + "'";
            }
            if (!string.IsNullOrEmpty(mycriteria.NeCreateUser))
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Create_User <>'" + mycriteria.NeCreateUser + "'";
            }
            if (mycriteria.NullCreateUser)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Create_User is null"; 
            }
            if (mycriteria.NotNullCreateUser)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Create_User is not null";
            }
            if (mycriteria.EqCreateTime!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Create_Time =" + mycriteria.EqCreateTime;
            }
            if (mycriteria.NeCreateTime!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Create_Time <>" + mycriteria.NeCreateTime;
            }
            if (mycriteria.LeCreateTime!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Create_Time <='" + mycriteria.LeCreateTime + "'";
            }
            if (mycriteria.GeCreateTime!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Create_Time >='" + mycriteria.GeCreateTime + "'";
            }
            if (mycriteria.LtCreateTime!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Create_Time <'" + mycriteria.LtCreateTime + "'";
            }
            if (mycriteria.GtCreateTime!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Create_Time >'" + mycriteria.GtCreateTime + "'";
            }
            if (mycriteria.NullCreateTime)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Create_Time is null"; 
            }
            if (mycriteria.NotNullCreateTime)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Create_Time is not null";
            }
            if (!string.IsNullOrEmpty(mycriteria.LikeUpdateUser))
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Update_User like '%" + mycriteria.LikeUpdateUser + "%'";
            }
            if (mycriteria.InUpdateUser.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.InUpdateUser)
                {
                    instr += "'"+str+"',";
                }
                instr = instr.Substring(0,instr.Length-1);
                where += " Update_User in (" + instr + ")";
            }
            if (mycriteria.NotUpdateUser.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.NotUpdateUser)
                {
                    instr += "'"+str+"',";
                }
                instr = instr.Substring(0,instr.Length-1);
                where += "Update_User not in (" + instr + ")";
            }
            if (mycriteria.OrEqUpdateUser.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.OrEqUpdateUser)
                {
                    if(instr=="")
                    {instr="(";}
                    instr += "Update_User='"+str+"' or ";
                }
                instr = instr.Substring(0,instr.Length-4);
                instr+=")";
                where += instr;
            }
            if (mycriteria.OrLikeUpdateUser.Count>0)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                string instr = "";
                foreach(var str in mycriteria.OrLikeUpdateUser)
                {
                    if(instr=="")
                    {instr="(";}
                    instr += "Update_User like '%"+str+"%' or ";
                }
                instr = instr.Substring(0,instr.Length-4);
                instr+=")";
                where += instr;
            }
            if (!string.IsNullOrEmpty(mycriteria.EqUpdateUser))
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Update_User ='" + mycriteria.EqUpdateUser + "'";
            }
            if (!string.IsNullOrEmpty(mycriteria.NeUpdateUser))
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Update_User <>'" + mycriteria.NeUpdateUser + "'";
            }
            if (mycriteria.NullUpdateUser)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Update_User is null"; 
            }
            if (mycriteria.NotNullUpdateUser)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Update_User is not null";
            }
            if (mycriteria.EqUpdateTime!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Update_Time =" + mycriteria.EqUpdateTime;
            }
            if (mycriteria.NeUpdateTime!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Update_Time <>" + mycriteria.NeUpdateTime;
            }
            if (mycriteria.LeUpdateTime!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Update_Time <='" + mycriteria.LeUpdateTime + "'";
            }
            if (mycriteria.GeUpdateTime!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Update_Time >='" + mycriteria.GeUpdateTime + "'";
            }
            if (mycriteria.LtUpdateTime!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Update_Time <" + mycriteria.LtUpdateTime;
            }
            if (mycriteria.GtUpdateTime!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Update_Time >" + mycriteria.GtUpdateTime;
            }
            if (mycriteria.NullUpdateTime)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Update_Time is null"; 
            }
            if (mycriteria.NotNullUpdateTime)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Update_Time is not null";
            }
            if (mycriteria.EqIsDeleted!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Is_Deleted =" + mycriteria.EqIsDeleted;
            }
            if (mycriteria.NeIsDeleted!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Is_Deleted <>" + mycriteria.NeIsDeleted;
            }
            if (mycriteria.LeIsDeleted!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Is_Deleted <=" + mycriteria.LeIsDeleted;
            }
            if (mycriteria.GeIsDeleted!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Is_Deleted >=" + mycriteria.GeIsDeleted;
            }
            if (mycriteria.LtIsDeleted!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Is_Deleted <" + mycriteria.LtIsDeleted;
            }
            if (mycriteria.GtIsDeleted!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Is_Deleted >" + mycriteria.GtIsDeleted;
            }
            if (mycriteria.NullIsDeleted)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Is_Deleted is null"; 
            }
            if (mycriteria.NotNullIsDeleted)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Is_Deleted is not null";
            }
            if (mycriteria.EqTimestamp!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Timestamp =" + mycriteria.EqTimestamp;
            }
            if (mycriteria.NeTimestamp!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Timestamp <>" + mycriteria.NeTimestamp;
            }
            if (mycriteria.LeTimestamp!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Timestamp <=" + mycriteria.LeTimestamp;
            }
            if (mycriteria.GeTimestamp!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Timestamp >=" + mycriteria.GeTimestamp;
            }
            if (mycriteria.LtTimestamp!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Timestamp <" + mycriteria.LtTimestamp;
            }
            if (mycriteria.GtTimestamp!=null)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += "Timestamp >" + mycriteria.GtTimestamp;
            }
            if (mycriteria.NullTimestamp)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Timestamp is null"; 
            }
            if (mycriteria.NotNullTimestamp)
            {
                if(where==""){ where += "where "; } else if(mycriteria.UseOr) {where += " or ";} else { where += " and "; }
                where += " Timestamp is not null";
            }
            
            return where;
        }
        
        #endregion
        
        
        
        
    }
}
