using RMYY_CallCenter_Api.Utility;
using CallCenterApi.Interface.Models;
using RMYY_CallCenter_Api.Controllers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using RMYY_CallCenter_Api.Model;
using RMYY_CallCenter_Api.Models;

namespace RMYY_CallCenter_Api.Controllers
{
    
    public class DictionaryController : BaseController
    {
        private RMYY_CallCenter_Api.Bll.T_Sys_DictionaryBase dictionaryBaseBLL = new RMYY_CallCenter_Api.Bll.T_Sys_DictionaryBase();
        private RMYY_CallCenter_Api.Bll.T_Sys_DictionaryValue dictionaryValueBLL = new RMYY_CallCenter_Api.Bll.T_Sys_DictionaryValue();
      

        #region 字典操作

        /// <summary>
        /// 获取字典列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetList(FilterDictionary filter)
        {
                var sql = " and F_State=1 ";
                var recordCount = 0;
                var dt = RMYY_CallCenter_Api.Bll.PagerBll.GetListPager(
                  "T_Sys_DictionaryBase",
                  "F_DictionaryFlag",
                  "*",
                  sql,
                  "ORDER BY F_Sort ",
                  filter.PageSize,
                  filter.PageIndex,
                  true,
                  out recordCount);
                List<T_Sys_DictionaryBase> modelList = dictionaryBaseBLL.DataTableToList(dt);
                var obj = new
                {
                    rows = modelList.Select(x => new
                    {
                        name = x.F_DictionaryName,
                        flag = x.F_DictionaryFlag,
                        sort = x.F_Sort
                    }),
                    total = recordCount
                };
                return  Content(obj.ToJson());
        }

        /// <summary>
        /// 获取字典
        /// </summary>
        /// <param name="dicFlag"></param>
        /// <returns></returns>
        public ActionResult GetDic(string dicFlag)
        {
                T_Sys_DictionaryBase baseModel = dictionaryBaseBLL.GetModel(dicFlag);
                if (baseModel != null)
                    return Success("加载字典成功", new
                    {
                        name = baseModel.F_DictionaryName,
                        flag = baseModel.F_DictionaryFlag,
                        desc=baseModel.F_Describe,
                        sort = baseModel.F_Sort
                    });
                else
                    return Error("加载字典失败");
        }
        //[Authority]
        /// <summary>
        /// 添加字典
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ActionResult AddDic(DictionaryBaseInput input)
        {
                T_Sys_DictionaryBase orderModel = new T_Sys_DictionaryBase();
                var model = dictionaryBaseBLL.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 (dictionaryBaseBLL.Add(orderModel))
                    return Success("字典添加成功");
                else
                    return Error("字典添加失败");
        }

        //[Authority]
        /// <summary>
        /// 编辑字典
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ActionResult EditDic(DictionaryBaseInput input)
        {
                T_Sys_DictionaryBase orderModel = dictionaryBaseBLL.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 (dictionaryBaseBLL.Update(orderModel))
                    return Success("字典添加成功");
                else
                    return Error("字典添加失败");
        }

        //[Authority]
        /// <summary>
        /// 删除字典
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelDic(string[] ids)
        {
                if (ids == null || ids.Length <= 0)
                    return Error("获取参数失败");
                StringBuilder sb = new StringBuilder();
                foreach (var item in ids)
                {
                    sb.Append("'" + item + "',");
                }
                if (dictionaryBaseBLL.DeleteList(sb.ToString().Trim(',')))
                return Success("删除成功");
                else
                return Error("删除失败");
        }
        #endregion

        #region 字典值操作

        //获取字典值列表
        public ActionResult GetDicValueList(FilterDictionary filter)
        {
            
                string sql = "";
                DataTable dt = new DataTable();
                if (!string.IsNullOrWhiteSpace(filter.Id))
                {
                    sql += " and F_DictionaryValueId= '" + 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 = RMYY_CallCenter_Api.Bll.PagerBll.GetListPager(
                    "T_Sys_DictionaryValue",
                    "F_DictionaryValueId",
                    "*",
                    sql,
                    "ORDER BY F_Sort ",
                    filter.PageSize,
                    filter.PageIndex,
                    true,
                    out recordCount);
                List<T_Sys_DictionaryValue> modelList = dictionaryValueBLL.DataTableToList(dt);

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

            return Content(obj.ToJson());

        }

        //获取字典值列表
        public ActionResult GetDicValueListByFlag(string flag)
        {
             DataTable dt = new DataTable();
             dt = dictionaryValueBLL.GetList(" F_DictionaryFlag='" + flag + "' and F_State=1 ").Tables[0];
            return Success("列表加载成功", dt);

        }        

        //加载字典值
        public ActionResult GetDicValue(int dicValueId = 0)
        {
                if (dicValueId <= 0)
                    return Error("字典值标识传输失败");
                T_Sys_DictionaryValue valueModel = dictionaryValueBLL.GetModel(dicValueId);
                if (valueModel != null)
                return Success("加载字典值成功", new
                    {
                        id = valueModel.F_DictionaryValueId,
                        dicflag = valueModel.F_DictionaryFlag,
                        name = valueModel.F_Name,
                        remark = valueModel.F_Describe,
                        sort = valueModel.F_Sort,
                    });
                else
                return Error("加载字典值失败");
        }

        //[Authority]
        //添加字典值
        public ActionResult AddDicValue(DictionaryValueInput input)
        {
                T_Sys_DictionaryValue orderModel = new 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;
              orderModel.F_TimeOut = input.TimeOut;

            if (dictionaryValueBLL.Add(orderModel) > 0)
                return Success("字典值添加成功");
                else
                return Error("字典值添加失败");
        }
        ////[Authority]
        //编辑字典值
        public ActionResult EditDicValue(DictionaryValueInput input)
        {
                if (input.DicVid <= 0)
                    return Error("字典值id获取失败");
                T_Sys_DictionaryValue orderModel = dictionaryValueBLL.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;
               orderModel.F_TimeOut = input.TimeOut;
                if (dictionaryValueBLL.Update(orderModel))
                return Success("字典值编辑成功");
                else
                return Error("字典值编辑失败");
        }
        //[Authority]
        //删除字典值
        public ActionResult DelDicValue(string[] ids)
        {
                if (ids == null || ids.Length <= 0)
                    return Error("获取参数失败");
                var idStr = string.Join(",", ids);
                if (dictionaryValueBLL.DeleteList(idStr))
                return Success("删除成功");
                else
                return Error("删除失败");
        }
        #endregion

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


    }
}