using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using PostCardHitsuji.DataClient.Model;


namespace  PostCardHitsuji.DataClient.DAO
{
    public class MappingLayoutDao : AbsDaoHelper<MappingLayoutContentModel>
    {
        private Dictionary<string, string> orderDictionary_ = new Dictionary<string, string>();
        public Dictionary<string, string> orderDictionary
        {
            get { return orderDictionary_; }
            set { orderDictionary_ = value; }
        }

        public object selectIdMaximum(object obj)
        {
            try
            {
                var essence = base.getDaoEssence();
                var model = base.inModel;

                StringBuilder sql = new StringBuilder();
                sql.AppendLine(" select ");
                sql.AppendLine("        coalesce(max(id), 0) id");
                sql.AppendLine("   from ");
                sql.AppendLine("        tblmappinglayout ");
                sql.AppendLine("  where ");
                sql.AppendLine("        id is not null ");

                essence.setSql(sql.ToString());

                var data = (DataSet)essence.postSelect(null);
                DataTable table = data.Tables[0];

                outModelList = new List<MappingLayoutContentModel>();
                foreach (DataRow row in table.Rows)
                {
                    MappingLayoutContentModel resultModel = new MappingLayoutContentModel();
                    outModelList.Add(resultModel);
                    resultModel.Id = base.getFieldAsString(row["id"]);
                }

                return outModelList;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return null;
            }
        }

        //insert
        public int insertSimple(object obj)
        {
            try
            {
                var essence = base.getDaoEssence();
                var model = inModel;

                StringBuilder sql = new StringBuilder();
                sql.AppendLine(" insert into tblperson ");
                sql.AppendLine("  ( ");
                sql.AppendLine("     enable, ");
                sql.AppendLine("     whois, ");
                sql.AppendLine("     groupid, ");
                sql.AppendLine("     addressid, ");
                sql.AppendLine("     addressdetail1, ");
                sql.AppendLine("     addressdetail2, ");
                sql.AppendLine("     name1, ");
                sql.AppendLine("     name2, ");
                sql.AppendLine("     name3, ");
                sql.AppendLine("     name4, ");
                sql.AppendLine("     namepost1, ");
                sql.AppendLine("     namepost2, ");
                sql.AppendLine("     namepost3, ");
                sql.AppendLine("     namepost4, ");
                sql.AppendLine("     tel, ");
                sql.AppendLine("     memo, ");
                sql.AppendLine("     reseiverlayoutid, ");
                sql.AppendLine("     senderid ");
                sql.AppendLine("  )values( ");
                sql.AppendLine("     ?, ");
                sql.AppendLine("     ?, ");
                sql.AppendLine("     ?, ");
                sql.AppendLine("     ?, ");
                sql.AppendLine("     ?, ");
                sql.AppendLine("     ?, ");
                sql.AppendLine("     ?, ");
                sql.AppendLine("     ?, ");
                sql.AppendLine("     ?, ");
                sql.AppendLine("     ?, ");
                sql.AppendLine("     ?, ");
                sql.AppendLine("     ?, ");
                sql.AppendLine("     ?, ");
                sql.AppendLine("     ?, ");
                sql.AppendLine("     ?, ");
                sql.AppendLine("     ?, ");
                sql.AppendLine("     ?, ");
                sql.AppendLine("     ? ");
                sql.AppendLine("  ); ");

                essence.setSql(sql.ToString());

                return essence.postUpdate(null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //update
        public int updateSimple(object obj)
        {
            try
            {
                var essence = base.getDaoEssence();
                var model = inModel;

                StringBuilder sql = new StringBuilder();
                sql.AppendLine(" update tblperson set ");
                sql.AppendLine("     enable    = ?, ");
                sql.AppendLine("     whois     = ?, ");
                sql.AppendLine("     groupid   = ?, ");
                sql.AppendLine("     addressid = ?, ");
                sql.AppendLine("     addressdetail1 = ?, ");
                sql.AppendLine("     addressdetail2 = ?, ");
                sql.AppendLine("     name1 = ?, ");
                sql.AppendLine("     name2 = ?, ");
                sql.AppendLine("     name3 = ?, ");
                sql.AppendLine("     name4 = ?, ");
                sql.AppendLine("     namepost1 = ?, ");
                sql.AppendLine("     namepost2 = ?, ");
                sql.AppendLine("     namepost3 = ?, ");
                sql.AppendLine("     namepost4 = ?, ");
                sql.AppendLine("     tel  = ?, ");
                sql.AppendLine("     memo = ?, ");
                sql.AppendLine("     reseiverlayoutid = ?, ");
                sql.AppendLine("     senderid = ? ");
                sql.AppendLine("  where ");
                sql.AppendLine("     id = ? ");

                essence.setSql(sql.ToString());

                return essence.postUpdate(null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
