using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using HomeService.Models;
using HomeService.Utility;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using HcUtility.Comm;
using HomeService.DBHelper;

namespace HomeService
{
    public class HomeDBHelper
    {
        #region Inquery DataList

        public static void Main() {
            string gid = "";
            string companyid = "";
            string templetid = "";
            string linkurl = "";
            string sql = "";

            List<MsTempletStructure> modInquire = new List<MsTempletStructure>();
            modInquire = GetSQL();
            //得到公司用户
            List<Employee> employee = new List<Employee>();
            employee = GetUserInfo();
            foreach (Employee _emp in employee)
            {
                foreach (MsTempletStructure _templet in modInquire)
                {
                    gid = _templet.GID;
                    companyid = _templet.COMPANYID;
                    templetid = _templet.TEMPLETID;
                    linkurl = _templet.LINKURL;
                    sql = _templet.SQL;

                    sql = sql.Replace("\" + userid + \"", _emp.GID);
                    sql = "select (" + sql + ") as result";

                    DataTable dt = SqlHelper.ExecuteSql(sql);

                    CacheHelper.Set(gid + "|" + companyid + "|" + templetid, dt, DateTime.Now.AddSeconds(120));
                }
            }
        }

        private static List<MsTempletStructure> GetSQL() 
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("select B.GID,B.COMPANYID,LINKURL,[SQL] from main_module as A ");
            sql.AppendLine("left join user_templet as B on A.USERTEMPLETID=B.TEMPLETID ");
            sql.AppendLine("where B.LINKURL != '' and B.LINKURL is not NULL and B.[SQL] != '' and B.[SQL] is not NULL ");
            return SetSQL(sql);
        }
        private static List<MsTempletStructure> SetSQL(StringBuilder sql) 
        {
            var list = new List<MsTempletStructure>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader reader = db.ExecuteReader(CommandType.Text, sql.ToString())) 
            {
                while (reader.Read()) 
                {
                    MsTempletStructure data = new MsTempletStructure();
                    #region Set DB data to Object
                    data.GID = Convert.ToString(reader["GID"]);
                    data.COMPANYID = Convert.ToString(reader["COMPANYID"]);
                    data.TEMPLETID = Convert.ToString(reader["TEMPLETID"]);
                    data.LINKURL = Convert.ToString(reader["LINKURL"]);
                    data.SQL = Convert.ToString(reader["SQL"]);
                    #endregion
                    list.Add(data);
                }
                reader.Close();
            }
            return list;
        }

        private static List<Employee> GetUserInfo() 
        {
            DataTable dt = new DataTable();
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("select GID from [user] ");
            return SetUserInfo(sql);
        }
        private static List<Employee> SetUserInfo(StringBuilder sql) 
        { 
            var list = new List<Employee>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader reader = db.ExecuteReader(CommandType.Text, sql.ToString())) 
            {
                while (reader.Read()) 
                {
                    Employee data = new Employee();
                    #region Set DB data to Object
                    data.GID = Convert.ToString(reader["GID"]);
                    data.USERNAME = Convert.ToString(reader["USERNAME"]);
                    data.CODENAME = Convert.ToString(reader["CODENAME"]);
                    data.PASSWORD = Convert.ToString(reader["PASSWORD"]);
                    #endregion
                    list.Add(data);
                }
                reader.Close();
            }
            return list;
        }

        #endregion
    }
}