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

namespace RMYY_CallCenter_Api.Controllers
{
    public class CustomFiledController : BaseController
    {
        /// <summary>
        /// 添加自定义显示列
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddFiled(string TableId,string Fileds,string FiledNames,int id=0)
        {

            Model.T_Sys_CustomFiled dModel = new Model.T_Sys_CustomFiled();
            if (id == 0)
            {
                var list = new Bll.T_Sys_CustomFiled().GetModelList(" F_CreateUser='" + User .F_UserCode  + "' and F_TableId='"+ TableId + "' ");
                if (list!=null&&list.Count > 0)
                {
                    dModel = list.FirstOrDefault();
                    dModel.F_TableId = TableId;
                    dModel.F_Fileds = Fileds;
                    dModel.F_FiledNames = FiledNames;
                    dModel.F_CreateUser = User.F_UserCode;
                    dModel.F_CreateTime = DateTime.Now;
                    if (new Bll.T_Sys_CustomFiled().Update(dModel))
                        return Success("修改成功");
                    else
                        return Error("修改失败");
                }
                else
                {
                    dModel.F_TableId = TableId;
                    dModel.F_Fileds = Fileds;
                    dModel.F_FiledNames = FiledNames;
                    dModel.F_CreateUser = User.F_UserCode;
                    dModel.F_CreateTime = DateTime.Now;
                    int n = new Bll.T_Sys_CustomFiled().Add(dModel);
                    if (n > 0)
                        return Success("添加成功", n);
                    else
                        return Error("添加失败");
                }
            }
            else
            {
                dModel = new Bll.T_Sys_CustomFiled().GetModel(id);
                if (dModel != null)
                {
                    dModel.F_TableId = TableId;
                    dModel.F_Fileds = Fileds;
                    dModel.F_FiledNames = FiledNames;
                    dModel.F_CreateUser = User.F_UserCode;
                    dModel.F_CreateTime = DateTime.Now;
                    if (new Bll.T_Sys_CustomFiled().Update(dModel))
                        return Success("修改成功");
                    else
                        return Error("修改失败");
                }
                else
                {
                    return Error("修改失败");
                }
            }
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult Del(string TableId)
        {
            if (string .IsNullOrEmpty ( TableId) )
            {
                return Error("请选择要删除的自定义列");
            }
            var modellist = new Bll.T_Sys_CustomFiled().GetModelList("F_CreateUser='" + User.F_UserCode + "' and F_TableId='" + TableId + "'");
            if (modellist!=null&& modellist.Count >0)
            {
                var n = new Bll.T_Sys_CustomFiled().DeleteList(string.Join(",", modellist.Select(x => x.F_Id)));
                if (n)
                    return Success("删除成功");
            }
            else
            {
                return Error("该自定义列不存在");
            }
         
            return Error("删除失败");
        }


        /// <summary>
        /// 获取自定义列
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult GetTab(string TableId)
        {
            if (string.IsNullOrEmpty(TableId))
            {
                return Error("请选择自定义列");
            }
            var modellist = new Bll.T_Sys_CustomFiled().GetModelList("F_CreateUser='" + User.F_UserCode + "' and F_TableId='" + TableId + "'");
            if (modellist != null && modellist.Count > 0)
            {
              
                var obj = new
                {
                    F_Id = modellist.FirstOrDefault().F_Id ,
                    F_TableId =  modellist.FirstOrDefault().F_TableId,
                    F_Fileds = string.IsNullOrEmpty(modellist.FirstOrDefault().F_Fileds)?null:modellist.FirstOrDefault().F_Fileds.Split (','),
                    F_CreateUser = modellist.FirstOrDefault().F_CreateUser ,
                    F_CreateTime = modellist.FirstOrDefault().F_CreateTime ,
                    F_FiledNames = modellist.FirstOrDefault().F_FiledNames ,
                };


                return Content(obj.ToJson());
            }
            else
            {
                var obj = new
                {
                    F_Id = 0,
                    F_TableId ="",
                    F_Fileds= string.IsNullOrEmpty("") ? null : "",
                    F_CreateUser ="",
                    F_CreateTime = DateTime .Now ,
                    F_FiledNames = "",
                };
                return Content(obj.ToJson());
            }

           
        }

        /// <summary>
        /// 导出公共方法
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="TableId"></param>
        /// <param name="dt"></param>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public bool ExportToExcel(string Name,string TableId, DataTable dt,string usercode)
        {
            var modellist = new Bll.T_Sys_CustomFiled().GetModelList("F_CreateUser='" + usercode + "' and F_TableId='" + TableId + "'");

            if (modellist != null && modellist.Count > 0) { }
            else
            {
                var npoi = new NPOIHelper().ExportToExcel(Name, dt);
                if (npoi == "")
                    return true;
                else
                    return false;

            }
            if(modellist!=null && modellist.Count > 0)
            {
              
                DataTable newTable = dt.DefaultView.ToTable(false, modellist.FirstOrDefault().F_Fileds.Split(','));
                var npoi = new NPOIHelper().ExportToExcel(Name, newTable);
                if (npoi == "")
                    return true;
                else
                    return false;
            }
            else
            {
                return false;
            }
        }

    }
}