using CallCenter.Utility;
using CallCenterApi.Interface.Controllers.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers.Client
{
    [Authority]
    public class ArticleController : BaseController
    {
        BLL.T_Client_Article bll = new BLL.T_Client_Article();

        /// <summary>
        /// 获取所有库列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAllList()
        {
            DataTable dt = new DataTable();
            string F_TypeId = HttpUtility.UrlDecode(RequestString.GetQueryString("typeId"));
            string F_Title = HttpUtility.UrlDecode(RequestString.GetQueryString("title"));
            string strstarttime = HttpUtility.UrlDecode(RequestString.GetQueryString("strstarttime"));
            string strendtime = HttpUtility.UrlDecode(RequestString.GetQueryString("strendtime"));
            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;

            string sql = " and F_IsDelete=0 ";
            if (F_TypeId.Trim() != "")
            {
                sql += " and F_TypeId like '%" + F_TypeId + "%'";
            }
            if (F_Title.Trim() != "")
            {
                sql += " and F_Title like '%" + F_Title + "%'";
            }
            if (strstarttime.Trim() != "" && strstarttime != "undefined")
            {
                sql += " and datediff(day,F_CreateTime,'" + strstarttime + "')<=0 ";
            }
            if (strendtime.Trim() != "" && strendtime != "undefined")
            {
                sql += " and datediff(day,F_CreateTime,'" + strendtime + "')>=0   ";
            }
            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }
            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                    "T_Client_Article",
                    "F_Id",
                    "*",
                    sql,
                    "ORDER BY F_Id desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);
            var obj = new
            {
                rows = dt,
                total = recordCount
            };
            return Content(obj.ToJson());
        }

        /// <summary>
        /// 获取一条详细信息 Id
        /// </summary>
        /// <param name="infoid"></param>
        /// <returns></returns>
        public ActionResult GetInfo(string infoid)
        {
            if (infoid != null && infoid.Trim() != "")
            {
                Model.T_Client_Article model = bll.GetModel(int.Parse(infoid.Trim()));
                if (model != null)
                {
                    return Success("获取成功", model);
                }
                else
                {
                    return Error("获取失败");
                };
            }
            else
            {
                return Error("获取参数失败");
            }
        }

        /// <summary>
        /// 获取一条详细信息 title
        /// </summary>
        /// <param name="title"></param>
        /// <returns></returns>
        public ActionResult GetInfoByTitle(string title)
        {
            if (title != null && title.Trim() != "")
            {
                Model.T_Client_Article model = bll.GetModelByTitle(title.Trim());
                return Success("获取成功", model);
                //if (model != null)
                //{
                //    return Success("获取成功", model);
                //}
                //else
                //{
                //    return Error("获取失败");
                //}
            }
            else
            {
                return Error("获取参数失败");
            }
        }

        /// <summary>
        /// 保存信息
        /// </summary>
        /// <returns></returns>
        public ActionResult AddInfo()
        {
            int id = RequestString.GetInt("id", 0);
            int F_TypeId = RequestString.GetInt("typeId", 0);
            string F_Title = RequestString.GetFormString("title");
            string F_Content = RequestString.GetFormString("content");
            int UserId = RequestString.GetInt("userId", 0);
            string UserCode = RequestString.GetFormString("userCode");
            int F_IsDelete = RequestString.GetInt("isDelete", 0);
            int F_Order = RequestString.GetInt("order", 0);

            Model.T_Client_Article model = new Model.T_Client_Article();
            if (id == 0)
            {
                model.F_TypeId = F_TypeId;
                model.F_Title = F_Title;
                model.F_Content = F_Content;
                model.UserId = UserId;
                model.UserCode = UserCode;
                model.F_CreateTime = DateTime.Now;
                //model.F_UpdateTime = F_UpdateTime;
                //model.F_DeleteTime = F_DeleteTime;
                model.F_IsDelete = F_IsDelete;
                model.F_Order = F_Order;
                int n = bll.Add(model);
                if (n > 0)
                {
                    return Success("添加成功", n);
                }
                else
                {
                    return Error("添加失败");
                }
            }
            else
            {
                model = bll.GetModel(id);
                if (model != null)
                {
                    model.F_Id = id;
                    model.F_TypeId = F_TypeId;
                    model.F_Title = F_Title;
                    model.F_Content = F_Content;
                    model.UserId = UserId;
                    model.UserCode = UserCode;
                    model.F_CreateTime = model.F_CreateTime;
                    model.F_UpdateTime = DateTime.Now;
                    //model.F_DeleteTime = F_DeleteTime;
                    model.F_IsDelete = F_IsDelete;
                    model.F_Order = F_Order;
                    if (bll.Update(model))
                    {
                        return Success("修改成功");
                    }
                    else
                    {
                        return Error("修改失败");
                    }
                }
                return Error("信息不存在");
            }
        }        


    }
}