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

namespace CallCenterApi.Interface.Controllers
{
    
    public class DictionaryController : BaseController
    {
        private BLL.T_Sys_DictionaryBase dictionaryBaseBLL = new BLL.T_Sys_DictionaryBase();
        private BLL.T_Sys_DictionaryValue dictionaryValueBLL = new BLL.T_Sys_DictionaryValue();

        #region 字典操作

        /// <summary>
        /// 获取字典列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetList(FilterDictionary filter)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                var sql = " and F_State=1 ";
                var recordCount = 0;
                var dt = BLL.PagerBLL.GetListPager(
                  "T_Sys_DictionaryBase",
                  "F_DictionaryFlag",
                  "*",
                  sql,
                  "ORDER BY F_Sort ",
                  filter.PageSize,
                  filter.PageIndex,
                  true,
                  out recordCount);
                List<Model.T_Sys_DictionaryBase> modelList = new BLL.T_Sys_DictionaryBase().DataTableToList(dt);
                var obj = new
                {
                    rows = modelList.Select(x => new
                    {
                        name = x.F_DictionaryName,
                        flag = x.F_DictionaryFlag,
                        sort = x.F_Sort
                    }),
                    total = recordCount
                };
                res = Content(obj.ToJson());
            }
            return res;
        }

        /// <summary>
        /// 获取字典
        /// </summary>
        /// <param name="dicFlag"></param>
        /// <returns></returns>
        public ActionResult GetDic(string dicFlag)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                Model.T_Sys_DictionaryBase baseModel = dictionaryBaseBLL.GetModel(dicFlag);
                if (baseModel != null)
                    res = Success("加载字典成功", new
                    {
                        name = baseModel.F_DictionaryName,
                        flag = baseModel.F_DictionaryFlag,
                        sort = baseModel.F_Sort
                    });
                else
                    res = Error("加载字典失败");
            }
            return res;
        }
        //[Authority]
        /// <summary>
        /// 添加字典
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ActionResult AddDic(DictionaryBaseInput input)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                Model.T_Sys_DictionaryBase orderModel = new Model.T_Sys_DictionaryBase();
                BLL.T_Sys_DictionaryBase orderBll = new BLL.T_Sys_DictionaryBase();
                var model = orderBll.GetModel(input.DicFlag.Trim());
                if (model != null)
                    return Error("字典标志已存在");
                orderModel.F_DictionaryFlag = input.DicFlag;
                orderModel.F_DictionaryName = input.Name;
                orderModel.F_Describe = input.Remark;
                orderModel.F_State = true;
                orderModel.F_Sort = input.Sort;

                if (orderBll.Add(orderModel))
                    res = Success("字典添加成功");
                else
                    res = Error("字典添加失败");
            }
            return res;
        }

        //[Authority]
        /// <summary>
        /// 编辑字典
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ActionResult EditDic(DictionaryBaseInput input)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                BLL.T_Sys_DictionaryBase orderBll = new BLL.T_Sys_DictionaryBase();
                Model.T_Sys_DictionaryBase orderModel = orderBll.GetModel(input.DicFlag.Trim());
                if (orderModel == null)
                    return Error("字典标志不要修改");   //字典对象不存在
                orderModel.F_DictionaryFlag = input.DicFlag;
                orderModel.F_DictionaryName = input.Name;
                orderModel.F_Describe = input.Remark;
                orderModel.F_Sort = input.Sort;
                if (orderBll.Update(orderModel))
                    res = Success("字典添加成功");
                else
                    res = Error("字典添加失败");
            }
            return res;
        }

        //[Authority]
        /// <summary>
        /// 删除字典
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelDic(string[] ids)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                if (ids == null || ids.Length <= 0)
                    return Error("获取参数失败");
                StringBuilder sb = new StringBuilder();
                foreach (var item in ids)
                {
                    sb.Append("'" + item + "',");
                }
                if (new BLL.T_Sys_DictionaryBase().DeleteList(sb.ToString().Trim(',')))
                    res = Success("删除成功");
                else
                    res = Error("删除失败");
            }
            return res;
        }
        #endregion

        #region 字典值操作

        //获取字典值列表
        public ActionResult GetDicValueList(FilterDictionary filter)
        {
             ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                string sql = "";
                DataTable dt = new DataTable();



                if (!string.IsNullOrWhiteSpace(filter.Id))
                {
                    sql += " and F_DictionaryFlag= '" + filter.Id + "' ";
                }

                if (!string.IsNullOrWhiteSpace(filter.Flag))
                {
                    sql += " and F_DictionaryFlag like '%" + filter.Flag + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(filter.Name))
                {
                    sql += " and F_Name like '%" + filter.Name + "%' ";
                }

                int recordCount = 0;
                dt = BLL.PagerBLL.GetListPager(
                    "T_Sys_DictionaryValue",
                    "F_DictionaryValueId",
                    "*",
                    sql,
                    "ORDER BY F_Sort ",
                    filter.PageSize,
                    filter.PageIndex,
                    true,
                    out recordCount);
                List<Model.T_Sys_DictionaryValue> modelList = new BLL.T_Sys_DictionaryValue().DataTableToList(dt);

                var obj = new
                {
                    rows = dt,
                    total = recordCount
                };

                res = Content(obj.ToJson());
            }
            return res;
        }

        //获取字典值列表
        public ActionResult GetDicValueListByFlag(string flag)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {

                DataTable dt = new DataTable();

                dt = new BLL.T_Sys_DictionaryValue().GetList(" F_DictionaryFlag='" + flag + "' and F_State=1 ").Tables[0];

                res = Success("列表加载成功", dt);
            }
            return res;
        }        

        //加载字典值
        public ActionResult GetDicValue(int dicValueId = 0)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                if (dicValueId <= 0)
                    return Error("字典值标识传输失败");
                Model.T_Sys_DictionaryValue valueModel = dictionaryValueBLL.GetModel(dicValueId);
                if (valueModel != null)
                    res = Success("加载字典值成功", new
                    {
                        id = valueModel.F_DictionaryValueId,
                        dicflag = valueModel.F_DictionaryFlag,
                        name = valueModel.F_Name,
                        remark = valueModel.F_Describe,
                        sort = valueModel.F_Sort,
                    });
                else
                    res = Error("加载字典值失败");
            }
            return res;
        }

        //[Authority]
        //添加字典值
        public ActionResult AddDicValue(DictionaryValueInput input)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                Model.T_Sys_DictionaryValue orderModel = new Model.T_Sys_DictionaryValue();
                BLL.T_Sys_DictionaryValue orderBll = new BLL.T_Sys_DictionaryValue();

                orderModel.F_Name = input.DicvName;
                orderModel.F_Describe = input.DicDes;
                orderModel.F_State = true;
                orderModel.F_ValueCode = "";
                orderModel.F_DictionaryFlag = input.DicFlag;
                orderModel.F_Sort = input.Sort;


                if (orderBll.Add(orderModel) > 0)
                    res = Success("字典值添加成功");
                else
                    res = Error("字典值添加失败");
            }
            return res;
        }
        ////[Authority]
        //编辑字典值
        public ActionResult EditDicValue(DictionaryValueInput input)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                if (input.DicVid <= 0)
                    return Error("字典值id获取失败");
                BLL.T_Sys_DictionaryValue orderBll = new BLL.T_Sys_DictionaryValue();
                Model.T_Sys_DictionaryValue orderModel = orderBll.GetModel(input.DicVid);
                if (orderModel == null)
                    return Error("字典值对象获取失败");
                orderModel.F_Name = input.DicvName;
                orderModel.F_Describe = input.DicDes;
                orderModel.F_ValueCode = "";
                orderModel.F_DictionaryFlag = input.DicFlag;
                orderModel.F_Sort = input.Sort;
                if (orderBll.Update(orderModel))
                    res = Success("字典值编辑成功");
                else
                    res = Error("字典值编辑失败");
            }
            return res;
        }
        //[Authority]
        //删除字典值
        public ActionResult DelDicValue(string[] ids)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                if (ids == null || ids.Length <= 0)
                    return Error("获取参数失败");
                var idStr = string.Join(",", ids);
                if (new BLL.T_Sys_DictionaryValue().DeleteList(idStr))
                    res = Success("删除成功");
                else
                    res = Error("删除失败");
            }
            return res;
        }
        #endregion

        #region 微信端字典
        /// <summary>
        /// 获取字典值列表 - 微信端使用
        /// </summary>
        /// <param name="flag"></param>
        /// <returns></returns>
        public ActionResult GetDicValueListByFlagWx(string flag)
        {
            DataTable dt = new DataTable();
            dt = new BLL.T_Sys_DictionaryValue().GetList(" F_DictionaryFlag='" + flag + "' and F_State=1 ").Tables[0];
            return Success("列表加载成功", dt);
        }
        #endregion


    }
}