using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Befri.Query;
using Befri.Core;
using Befri.Utility;
using Befri.Web.Mvc;
using Befri.Web.Mvc.Extensions;

using Befri.Goldhoo.Models;
using Befri.Goldhoo.Services;
using Befri.Goldhoo.MvcUI.Controllers;

namespace Befri.Goldhoo.MvcUI.Areas.Basedata.Controllers
{ 
    /// <summary>
    /// 名    称:【<%= Request.Table.Text %>】管理控制器类
    /// 主要功能:
    /// 开发人员: 
    /// 开发日期: <%= DateTime.Today.ToString("yyyy-MM") %>
    /// </summary>
    [HandleException("defaultPolicy")]
    public class <%= Table.AliasName %>Controller : BaseController
    {
        readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private I<%= Table.AliasName %>Service _<%= Table.aliasName %>Service;

        public <%= Table.AliasName %>Controller(I<%= Table.AliasName %>Service service)
        {
            this._<%= Table.aliasName %>Service = service;
        }
 
        /// <summary>
        /// 打开【<%= Table.Text %>】列表记录页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
           return View("<%= Table.AliasName %>");
        }
        
        /// <summary>
        /// 打开【<%= Table.Text %>】新增、修改页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit<%= Table.AliasName %>()
        {
           return View("<%= Table.AliasName %>Edit");
        }
        
        /// <summary>
        /// 获取【<%= Table.Text %>】列表记录
        /// </summary>
        /// <returns></returns>
        public ActionResult List<%= Table.AliasName %>(string searchText, int page, int rows)
        {
            try
            {
                SqlPageParameter param = new SqlPageParameter("[<%= Table.SchemaName %>].[<%= Table.TableName %>]", page, rows, OrderExpression.Asc("<%= KeyParamsValueText %>"));
                param.AddWhereExpr(SegmentExpression.SqlSegment(" 1 = 1 "));
                if (!string.IsNullOrEmpty(searchText))
                {
                     param.AddWhereExpr(LogicExpression.And())
                        .AddWhereExpr(SegmentExpression.SqlSegment(" ( "))
                        .AddWhereExpr(SimpleExpression.Like("<%= Table.AliasName %>Number", DbType.String, "%" + searchText + "%"))
                        .AddWhereExpr(LogicExpression.Or())
                        .AddWhereExpr(SimpleExpression.Like("<%= Table.AliasName %>Name", DbType.String, "%" + searchText + "%"))
                        .AddWhereExpr(SegmentExpression.SqlSegment(" ) "));
                }
                DataSet dataSet = this._<%= Table.aliasName %>Service.GetPageData(param); 
                int total = (int)dataSet.Tables[0].Rows[0][0];
                var result = DataTableMapper.ToList<<%= Table.AliasName %>>(dataSet.Tables[1]);
                return this.Ajax(total, result);
            }
            catch (Exception ex)
            {
                logger.Error("ERROR", ex);
                return this.AjaxException(this.ControllerContext, ex);
            }
        }
        
        /// <summary>
        /// 获取【<%= Table.Text %>】记录
        /// </summary>
        /// <returns></returns>
        public ActionResult Load<%= Table.AliasName %>(<%= KeyParamsText %>)
        {
            try
            {            	
              	<%= Table.AliasName %> <%= Table.aliasName %> = this._<%= Table.aliasName %>Service.FindObject<<%= Table.AliasName %>>("[<%= Table.SchemaName %>].[<%= Table.TableName %>]", "<%= Table.AliasName %>Id", <%= KeyParamsValueText %>);
                
                return this.Json(<%= Table.aliasName %>, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
            	logger.Error("ERROR", ex);
                return this.AjaxException(this.ControllerContext, ex);
            }
        }
        
		/// <summary>
        /// 创建【<%= Table.Text %>】记录
        /// </summary>
        /// <returns></returns>
        public ActionResult Create<%= Table.AliasName %>(<%= Table.AliasName %> <%= Table.aliasName %>)
        {
            try
            {
            	<%= Table.aliasName %>.CreatedBy = CurrentEmpName; 
                this._<%= Table.aliasName %>Service.Create<%= Table.AliasName %>(<%= Table.aliasName %>);
                
                 <%= Table.AliasName %> result = this._<%= Table.aliasName %>Service.FindObject<<%= Table.AliasName %>>("[<%= Table.SchemaName %>].[<%= Table.TableName %>]", "<%= Table.AliasName %>Id", <%= Table.aliasName %>.<%= KeyParamsValueText %>);
                
                return this.Ajax(result);                
            }
            catch (Exception ex)
            {
            	logger.Error("ERROR", ex);
                return this.AjaxException(this.ControllerContext, ex);
            }
        }

		/// <summary>
        /// 更新【<%= Table.Text %>】记录
        /// </summary>
        /// <returns></returns>
        public ActionResult Update<%= Table.AliasName %>(<%= Table.AliasName %> <%= Table.aliasName %>)
        {
            try
            {
            	<%= Table.aliasName %>.ModifiedBy = CurrentEmpName; 
                this._<%= Table.aliasName %>Service.Update<%= Table.AliasName %>(<%= Table.aliasName %>); 
                
                <%= Table.AliasName %> result = this._<%= Table.aliasName %>Service.FindObject<<%= Table.AliasName %>>("[<%= Table.SchemaName %>].[<%= Table.TableName %>]", "<%= Table.AliasName %>Id", <%= Table.aliasName %>.<%= KeyParamsValueText %>);
                return this.Ajax(result);
            }
            catch (Exception ex)
            {
            	logger.Error("ERROR", ex);
                return this.AjaxException(this.ControllerContext, ex);
            }            
        }

		/// <summary>
        /// 删除【<%= Table.Text %>】记录
        /// </summary>
        /// <returns></returns>
        public ActionResult Delete<%= Table.AliasName %>(<%= KeyParamsText %>)
        {
            try
            {
                this._<%= Table.aliasName %>Service.Delete<%= Table.AliasName %>(<%= KeyParamsValueText %>);
                
            	return this.Ajax();
            }
            catch (Exception ex)
            {
            	logger.Error("ERROR", ex);
                return this.AjaxException(this.ControllerContext, ex);
            }
        }
        
	}
}