using CallCenter.Utility;
using CallCenterApi.Common;
using CallCenterApi.DB;
using CallCenterApi.Interface.Controllers.Base;
using CallCenterApi.Interface.Controllers.Sms;
using CallCenterApi.Interface.Models.Input;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers
{
    [Authority]
    public class UserAccountController : BaseController
    {
        private BLL.T_Sys_UserAccount sysUserAccountBll = new BLL.T_Sys_UserAccount();
        private BLL.T_Sys_Department departmentBLL = new BLL.T_Sys_Department();
        private BLL.T_Sys_DictionaryValue dicValueBLL = new BLL.T_Sys_DictionaryValue();

        //用户列表
        //[Authority]
        public ActionResult GetList(int isdc=0)
        {
            
            
            DataTable dt = new DataTable();
            string sql = " ";
            try
            {
                //用户管理中，加个筛选项根据角色筛选（筛选二级单位时，可以看到该二级单位下的所有人员）
                string roleId = RequestString.GetQueryString("roleId");
                string rolecode = RequestString.GetQueryString("rolecode");
                //用户管理中，加个筛选项根据角色筛选（筛选二级单位时，可以看到该二级单位下的所有人员）
                string Key = RequestString.GetQueryString("Key");
                int Id = RequestString.GetInt("Id", 0);
                int activation = RequestString.GetInt("activation", -1);
                string strpageindex = RequestString.GetQueryString("page");
                int pageindex = 1;
                string strpagesize = RequestString.GetQueryString("pagesize");
                int pagesize = 10;

                if (Id > 0)
                {
                    sql += " and F_DeptId=" + Id;
                }
                if (User.F_RoleCode == "GLY" || User.F_RoleCode == "YSZY" || User.F_RoleCode == "ZXLD" || User.F_RoleCode == "ZXHWY"|| User.F_RoleCode == "MTDD" || User.F_RoleCode == "DMTSH")
                {
                }
                else
                {
                    sql += "and F_DeptId in (select F_DeptId from  T_Sys_Department where F_State=0 and ( F_DeptId='" + User.F_DeptId + "'  or  F_PartentId='" + User.F_DeptId + "'))";

                }
                if (!string.IsNullOrWhiteSpace(Key))
                {
                   // sql = "";
                    string str = string.Format(" and (F_UserCode like '%{0}%' or F_UserName like '%{1}%')", Key, Key);
                    sql += str;
                }
                if (activation>-1)
                {
                    if (activation==0)
                    {
                        sql += "and F_LastActiveTime=F_CreateOn";
                    }
                    else
                    {
                        sql += "and F_LastActiveTime>F_CreateOn";
                    }
                }
                if (!string.IsNullOrWhiteSpace(roleId))
                {
                  //  sql = "";
                    string str = string.Format(" and (F_RoleId = '{0}' )", roleId);
                    sql += str;
                }
                if (!string.IsNullOrWhiteSpace(rolecode))
                {
                    //  sql = "";
                    string str = string.Format(" and F_RoleId in ({0})", rolecode);
                    sql += str;
                }

                if (strpageindex.Trim() != "")
                {
                    pageindex = Convert.ToInt32(strpageindex);
                }

                if (strpagesize.Trim() != "")
                {
                    pagesize = Convert.ToInt32(strpagesize);
                }
                if (isdc==1)
                {
                    var dtdc = DbHelperSQL.Query(" select  F_UserCode 工号, F_UserName 姓名,F_Company 单位,F_Department 科室,F_Post 职务, F_Mobile 手机号码,F_Telephone 办公电话 from T_Sys_UserAccount  WITH(NOLOCK) where 1=1 " + sql).Tables[0];

                    var msg = new NPOIHelper().ExportToExcel("单位信息列表", dtdc);
                    if (msg == "")
                    {
                        return Success("导出成功");
                    }
                    else
                    {
                        return Error("导出失败");
                    }
                }
                if (isdc ==2)
                {
                    var dtdc = DbHelperSQL.Query("select  F_UserCode 坐席工号, F_UserName 用户名称, F_Telephone 用户电话, F_Mobile 用户手机, (select F_RoleName   from T_Sys_RoleInfo where F_RoleId = a.F_RoleId  )  用户角色,(select F_DeptName from T_Sys_Department where F_DeptId = a.F_DeptId  )  用户部门,(case F_DeleteFlag when 0 then '正常' else   '禁用' end )  用户状态 ,(case  when a.F_CreateOn =a.F_LastActiveTime  then '未激活' when a.F_LastActiveTime > a.F_CreateOn then '已激活' else   '未激活' end)  是否激活 ,F_CreateOn 创建时间 from T_Sys_UserAccount a  WITH(NOLOCK) where  1=1 " + sql).Tables[0];

                    var msg = new NPOIHelper().ExportToExcel("用户列表", dtdc);
                    if (msg == "")
                    {
                        return Success("导出成功");
                    }
                    else
                    {
                        return Error("导出失败");
                    }
                }
                else
                {
                    int recordCount = 0;
                    dt = BLL.PagerBLL.GetListPager(
                                            "T_Sys_UserAccount",
                                            "F_UserId",
                                            "*",
                                            sql,
                                            "ORDER BY F_UserId desc",
                                            pagesize,
                                            pageindex,
                                            true,
                                            out recordCount);

                    List<Model.UserAccount> modelList = new BLL.UserAccount().DataTableToList(dt);
                    var obj = new
                    {
                        rows = modelList.Select(x => new
                        {
                            F_UserId = x.F_UserId,
                            F_UserCode = x.F_UserCode,
                            F_ExtensionNumber = x.F_ExtensionNumber,
                            F_UserName = x.F_UserName,
                            F_GroupName = x.F_GroupName,
                            F_DeptId = x.F_DeptId,
                            F_RoleId = x.F_RoleId,
                            F_GroupId = x.F_GroupId,
                            F_SeatFlag = x.F_SeatFlag,
                            F_SeatRight = x.F_SeatRight,
                            F_SeatLevel = x.F_SeatLevel,
                            F_SexFlag = x.F_SexFlag,
                            F_RemindFlag = x.F_RemindFlag,
                            F_Remark = x.F_Remark,
                            F_Telephone = x.F_Telephone,
                            F_Mobile = x.F_Mobile,
                            F_HomePhone = x.F_HomePhone,
                            F_Birthday = x.F_Birthday,
                            F_CreateOn = x.F_CreateOn,
                            F_CancelOn = x.F_CancelOn,
                            F_DeleteFlag = x.F_DeleteFlag,
                            F_APPOnFlag = x.F_APPOnFlag,
                            F_LastActiveTime = x.F_LastActiveTime,
                            F_See = x.F_See,
                            F_HJType = x.F_HJType,
                            F_PId = x.F_PId,
                            F_TmId = x.F_TmId,
                            F_RoleName = x.F_RoleName,
                            F_DeptName = x.F_DeptName,
                            F_WorkNumber = x.F_WorkNumber,
                            F_Company = x.F_Company,
                            F_Department = x.F_Department,
                            F_Post = x.F_Post,
                            F_IsSms=x.F_IsSms,
                            F_SeartGroupCode = x.F_SeartGroupCode
                        }),
                        total = recordCount
                    };

                    return Content(obj.ToJson());
                }
             
            }
            catch (Exception err)
            {
                return Error("错误：" + err.ToString());
            }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
        }


        //获取用户信息
        //[Authority]
        public ActionResult GetUser(int userId = 0, string userCode = "")
        {
            string sql = "";
            if (userId > 0)
            {
                sql += " and F_UserId=" + userId;
            }
            if (!string.IsNullOrWhiteSpace(userCode))
            {
                sql += " and F_UserCode='" + userCode+"'";
            }
            if (string.IsNullOrWhiteSpace(sql))
                return Error("获取失败");

            Model.T_Sys_UserAccount User = sysUserAccountBll.GetModelList(" 1=1 " + sql).FirstOrDefault();
            if (User == null) return Error("获取失败");
            var depModel = departmentBLL.GetModel(User.F_DeptId);
            var zxzModel = dicValueBLL.GetModel(User.F_GroupId ?? 0);
            return Success("获取成功", new
            {
                F_UserId = User.F_UserId,
                F_UserCode = User.F_UserCode,
                F_ExtensionNumber = User.F_ExtensionNumber,
                F_UserName = User.F_UserName,
                F_DeptId = User.F_DeptId,
                F_RoleId = User.F_RoleId,
                F_GroupId = User.F_GroupId,
                F_SeatFlag = User.F_SeatFlag,
                F_SeatRight = User.F_SeatRight,
                F_SeatLevel = User.F_SeatLevel,
                F_SexFlag = User.F_SexFlag,
                F_RemindFlag = User.F_RemindFlag,
                F_Remark = User.F_Remark,
                F_Telephone = User.F_Telephone,
                F_Mobile = User.F_Mobile,
                F_HomePhone = User.F_HomePhone,
                F_Birthday = (User.F_Birthday ?? DateTime.MinValue).ToString("yyyy-MM-dd"),
                F_CreateOn = User.F_CreateOn,
                F_CancelOn = User.F_CancelOn,
                F_DeleteFlag = User.F_DeleteFlag,
                F_APPOnFlag = User.F_APPOnFlag,
                F_LastActiveTime = User.F_LastActiveTime,
                F_See = User.F_See,
                F_HJType = User.F_HJType ?? 0,
                F_PId = User.F_PId,
                F_TmId = User.F_TmId,
                F_WorkNumber = User.F_WorkNumber,
                F_Name=  User.F_Name ,
                F_Company = User.F_Company ,
                F_Department = User.F_Department ,
                F_Post = User.F_Post ,
                F_IsSms = User.F_IsSms,
                //zxzname = zxzModel?.F_Name ?? "",
                depname = depModel?.F_DeptName ?? "",
                F_SeartGroupCode = User.F_SeartGroupCode,
                F_SeartGroupID = User.F_SeartGroupID 
            });

        }
        //[Authority]
        //添加用户信息
        public ActionResult AddUsers(UserAccountInput input)
        {
            Regex reg = new Regex(@"^[1-9]\d*$");
            if (!reg.IsMatch(input.Usercode.Trim()))
            {
                return Error("工号必须为正整数");
            }

            Model.T_Sys_UserAccount userAccountModel = new Model.T_Sys_UserAccount();
            userAccountModel.F_UserCode = input.Usercode.Trim();
            userAccountModel.F_UserName = input.Username.Trim();
            userAccountModel.F_Password = input.Password.Trim();
            //userAccountModel.F_ExtensionNumber = input.Extno.Trim();
            userAccountModel.F_WorkNumber = input.Usercode;
            userAccountModel.F_DeptId = input.DeptId;
            userAccountModel.F_RoleId = input.RoleId;
            userAccountModel.F_GroupId = input.GroupId;
            userAccountModel.F_SeatFlag = input.IsSeat;
            userAccountModel.F_SeatRight = input.SearRight;
            userAccountModel.F_SexFlag = input.Sex;
            userAccountModel.F_Remark = input.Remark;
            userAccountModel.F_Mobile = input.Mobile;
            userAccountModel.F_Telephone = input.Telphone;
            userAccountModel.F_Birthday = input.Birth;
            userAccountModel.F_CreateOn = DateTime.Now;
            userAccountModel.F_DeleteFlag = 0;
            userAccountModel.F_ModifytTime = DateTime.Now;
            userAccountModel.F_LastActiveTime = DateTime.Now;
            userAccountModel.F_IsSms = input.F_IsSms;
            //userAccountModel.F_HJType = input.HjType;

            var model = sysUserAccountBll.GetModel(input.Usercode);
            if (model != null)
                return Error("当前员工工号存在，请更换！");
            if (input.GroupId != 0)
            {
                userAccountModel.F_SeartGroupID = input.GroupId;
                Model.T_Sys_SeatGroup sm = new BLL.T_Sys_SeatGroup().GetModel(input.GroupId);
                if (sm != null)
                {
                    userAccountModel.F_SeartGroupCode  = sm.F_ZXZName;
                }
            }
            if (input.IsSeat)
            {
               // userAccountModel.F_WorkNumber = input.Usercode.Trim();
                if (input.ClassId.Value > 0)
                {
                    var cls = new BLL.T_Sys_Class().GetModel(input.ClassId.Value);
                    userAccountModel.F_ClassCode = (cls != null ? cls.F_Code : "");
                }

                SysConfigController sys = new SysConfigController();
                //话务人员限制权限判断
                if (sys.IsValidSeatPermission())
                {
                    if (sysUserAccountBll.Add(userAccountModel) > 0)
                        return Success("新增成功！");
                    else
                        return Error("新增失败！");
                }
                else
                {
                    return Error("话务人员数量已超！");
                }
            }
            if (sysUserAccountBll.Add(userAccountModel) > 0)
                return Success("新增成功！");
            else
                return Error("新增失败！");
           
        }
        //[Authority]
        //修改用户信息
        public ActionResult EditUsers(UserAccountInput input)
        {
            Regex reg = new Regex(@"^[1-9]\d*$");
            if (!reg.IsMatch(input.Usercode.Trim()))
            {
                return Error("工号必须为正整数");
            }

            BLL.T_Sys_UserAccount sysUserAccountBll = new BLL.T_Sys_UserAccount();
            Model.T_Sys_UserAccount userAccountModel = sysUserAccountBll.GetModelList("  F_UserId = " + input.UserId).FirstOrDefault();
            if (userAccountModel == null)
                return Error("获取用户失败");
            userAccountModel.F_UserCode = input.Usercode.Trim();
            userAccountModel.F_UserName = input.Username.Trim();
            //userAccountModel.F_Password = input.Password.Trim();
            //userAccountModel.F_ExtensionNumber = input.Extno.Trim();
            userAccountModel.F_WorkNumber = input.Usercode;
            userAccountModel.F_DeptId = input.DeptId;
            userAccountModel.F_RoleId = input.RoleId;
          //  userAccountModel.F_GroupId = input.GroupId;
            userAccountModel.F_SeatFlag = input.IsSeat;
            userAccountModel.F_SeatRight = input.SearRight;
            userAccountModel.F_SexFlag = input.Sex;
            userAccountModel.F_Remark = input.Remark;
            userAccountModel.F_Mobile = input.Mobile;
            userAccountModel.F_Telephone = input.Telphone;
            userAccountModel.F_Birthday = input.Birth;
            userAccountModel.F_IsSms = input.F_IsSms;
            userAccountModel.F_ModifytTime = DateTime.Now;
            //userAccountModel.F_DeleteFlag = 0;
            //userAccountModel.F_HJType = input.HjType;
            if (input.GroupId != 0)
            {
                userAccountModel.F_SeartGroupID = input.GroupId;
                Model.T_Sys_SeatGroup sm = new BLL.T_Sys_SeatGroup().GetModel(input.GroupId);
                if (sm != null)
                {
                    userAccountModel.F_SeartGroupCode = sm.F_ZXZName;
                }
            }
            if (input.IsSeat)
            {
                //userAccountModel.F_WorkNumber = input.Usercode.Trim();
                if (input.ClassId.Value > 0)
                {
                    var cls = new BLL.T_Sys_Class().GetModel(input.ClassId.Value);
                    userAccountModel.F_ClassCode = (cls != null ? cls.F_Code : "");
                }

                SysConfigController sys = new SysConfigController();
                //话务人员限制权限判断
                if (sys.IsValidSeatPermission())
                {
                    if (sysUserAccountBll.Update(userAccountModel))
                        return Success("编辑成功！");
                    else
                        return Error("编辑失败！");
                }
                else
                {
                    return Error("话务人员数量已超！");
                }
            }
            if (sysUserAccountBll.Update(userAccountModel))
                return Success("编辑成功！");
            else
                return Error("编辑失败！");
        }
        //[Authority]
        //删除/禁用/启用 用户
        public ActionResult DelUsers(string[] ids, int state = 0)
        {
            if (ids == null || ids.Length <= 0)
                return Error("请选择用户");
            var idStr = string.Join(",", ids);
            if (string.IsNullOrEmpty(idStr))
                return Error("请选择用户");

            SysConfigController sys = new SysConfigController();
            //话务人员限制权限判断
            if (state == 0) {
                if (sys.IsValidSeatPermission())
                {
                    if (sysUserAccountBll.DeleteList(idStr, state))
                        return Success("设置成功");
                    else
                        return Error("设置失败");
                }
                else
                {
                    return Error("话务人员数量已超！");
                }
            }
            if (sysUserAccountBll.DeleteList(idStr, state))
                return Success("设置成功");
            else
                return Error("设置失败");
        }

        //[Authority]
        //删除用户
        public ActionResult WLDelUsers(string[] ids)
        {
            if (ids == null || ids.Length <= 0)
                return Error("请选择用户");
            var idStr = string.Join(",", ids);
            if (string.IsNullOrEmpty(idStr))
                return Error("请选择用户");

            if (sysUserAccountBll.DeleteList(idStr))
                return Success("删除成功");
            else
                return Error("删除失败");
        }

        //获取坐席列表
        public ActionResult GetSeatList()
        {
            //List<Model.T_Sys_UserAccount> userSeartList = sysUserAccountBll.GetModelList(" f_seatflag=1 ");
            var drs = sysUserAccountBll.GetModelList ("f_seatflag=1");
            var userSeartList = from r in drs select new {
                F_UserId =r .F_UserId ,
                F_UserName = r.F_UserName,
                F_UserCode = r.F_UserCode
            };
           // var result = userSeartList.OrderBy(p => p.F_UserName.Substring(p.F_UserName.Length - 1)).ToList();

            return Success("列表加载成功", userSeartList);
        }
        //根据角色获取用户
        public ActionResult GetUsersList(string rolecode)
        {
            var sql = " 1=1 ";
            if (!string.IsNullOrWhiteSpace(rolecode.Trim()))
            {
                string F_RoleId = DbHelperSQL.GetSingle("select F_RoleId from T_Sys_RoleInfo with(nolock) where F_RoleCode='" + rolecode.Trim() + "'")?.ToString() ?? "0";

                sql += " and F_RoleId = " + F_RoleId;
            }

            //List<Model.T_Sys_UserAccount> userList = sysUserAccountBll.GetModelList(sql, " f_userid desc ");
            var userList = sysUserAccountBll.GetList(sql, " f_userid desc ").Tables[0];
            return Success("列表加载成功", userList);
        }

        //根据部门获取用户
        public ActionResult GetDeptUserList(int deptid = 0)
        {
            var DeptUserList = sysUserAccountBll.GetList("F_DeptId='" + deptid + "' ", " f_userid desc ");
            return Success("列表加载成功", DeptUserList);

        }

        //[Authority]
        //获取当前用户信息
        public ActionResult GetNowUser()
        {
            if (User == null)
                return Error("获取失败");
            Model.T_Sys_RoleInfo roleModel = new BLL.T_Sys_RoleInfo().GetModel(User.F_RoleId);
            var obj = new
            {
                user = User,
                role = roleModel
            };
            return Success("获取成功", obj);

        }
        //[Authority]
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="usercode"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public ActionResult ResetPwd(string usercode = "", string pwd = "")
        {
            if (string.IsNullOrWhiteSpace(pwd))
                return Error("请输入密码");
            var model = sysUserAccountBll.GetModel(usercode);
            if (model == null) return Error("此用户不存在");
            model.F_Password = pwd;
            if (sysUserAccountBll.Update(model))
                return Success("重置密码成功");
            else
                return Error("重置密码失败");

        }
        public class VerificationCode
        {
            public string mobile { set; get; }
            public string code { set; get; }
            public DateTime Time { set; get; }
        }
        public ActionResult SendCode(string mobile)
        {
            if (string .IsNullOrEmpty (mobile))
            {
                return Error("请输入将要发送的号码");
            }
            int codes = new Random().Next(1000, 9999);
           // string msg = codes.ToString ();
           // bool n = SMSController.AddSmS(0, msg, "你的验证码是"+ codes+".十分钟有效.", mobile, "168411", "");

            string mag = "[\"" + codes.ToString() + "\"]";
            string v = SmsNewController.AddSmS(0, "你的验证码是" + codes + ".十分钟有效.", mobile, "681240638956277760", mag,
          "");
            if (v=="")
            {
                    return Success("发送成功");
            }
            else
                return Error ("发送失败");

        }
        public ActionResult GetUserModify( )
        {
            if (string.IsNullOrEmpty(User.F_Mobile))
                return Error("请填写手机号");
            if (string.IsNullOrEmpty(User.F_Company) || string.IsNullOrEmpty(User.F_Department) || string.IsNullOrEmpty(User.F_Post))
                return Error("用户信息不完整");
            if (User.F_ModifytTime!=null )
            {
                if (((DateTime )User.F_ModifytTime).Month  != DateTime.Now.Month)
                {
                    if (DateTime.Now.Day >= 5)
                    {
                        return Error("请及时修改密码");
                    }
                }
            }
            return Success("成功");
        }
        public ActionResult UpdatePwd(string name , string company,string department,string post,
        string mobile,string phone, string code,   string usercode = "", string pwd = "", string oldPwd = ""
           )
        {
            //if (string.IsNullOrEmpty(code))
            //    return Error("请输入短信验证码");
            if (string.IsNullOrWhiteSpace(pwd))
                return Error("请输入密码");
            if (string.IsNullOrEmpty(company) || string.IsNullOrEmpty(department) || string.IsNullOrEmpty(post)|| string.IsNullOrEmpty(name))
                return Error("用户信息不完整");
            var model = sysUserAccountBll.GetModel(usercode);
            if (model == null) return Error("此用户不存在");
            if (pwd.Equals(oldPwd))
                return Error("新密码不能和原密码相同");
            if (!model.F_Password.Equals(oldPwd)) return Error("原密码不正确");

            if (string.IsNullOrEmpty(mobile))
                return Error("请输入手机号码");
            //string msgcount = "你的验证码是" + code + ".十分钟有效.";
            //var sms = new BLL.T_SMS_RecvSMS().GetModelList("Content='"+ msgcount+ "' and CallerNum='"+ mobile+ "'order by RecvTime desc");
            //if (sms != null&& sms.Count >0)
            //{
            //    var modelSms = sms.First();
            //    if ((DateTime.Now - modelSms.RecvTime ).Minutes > 10)
            //    {
            //        return Error("验证码已失效");
            //    }
            //}
            //else
            //    return Error("请获取验证码");

            model.F_Password = pwd;
            if (!string.IsNullOrEmpty(name))
                model.F_UserName  = name;
            if (!string.IsNullOrEmpty(company))
                model.F_Company  = company;
            if (!string.IsNullOrEmpty(department))
                model.F_Department  = department;
            if (!string.IsNullOrEmpty(post))
                model.F_Post  = post;
            if (!string.IsNullOrEmpty(mobile))
                model.F_Mobile  = mobile;
            if (!string.IsNullOrEmpty(phone))
                model.F_Telephone  = phone;
            model.F_ModifytTime = DateTime.Now;
            if (sysUserAccountBll.Update(model))
                return Success("修改成功");
            else
                return Error("修改失败");

        }

        #region App手机端 用户列表
        /// <summary>
        /// App手机端 用户列表
        /// </summary>
        /// <returns></returns>
        //[Authority]
        public ActionResult GetAppUserList()
        {
            DataTable dt = new DataTable();
            string sql = " 1=1 and F_RoleId=2 ";
            try
            {
                string Key = RequestString.GetQueryString("Key");
                if (!string.IsNullOrWhiteSpace(Key))
                {
                    string str = string.Format(" and (F_DeptId IN (SELECT F_DeptId FROM dbo.T_Sys_Department WHERE F_DeptName like '%{0}%') or F_UserName like '%{1}%' or F_Mobile like '%{2}%' or F_Telephone like '%{3}%' or F_HomePhone like '%{3}%')", Key, Key, Key, Key, Key);
                    sql += str;
                }
                dt = sysUserAccountBll.GetListNoCache(sql).Tables[0];

                List<Model.UserAccount> modelList = new BLL.UserAccount().DataTableToList(dt);
                return Success("获取成功",
                    modelList.Select(x => new
                    {
                        F_UserId = x.F_UserId,
                        F_UserCode = x.F_UserCode,
                        F_UserName = x.F_UserName,
                        F_DeptId = x.F_DeptId,
                        F_SexFlag = x.F_SexFlag,
                        F_Remark = x.F_Remark,
                        F_Telephone = x.F_Telephone,
                        F_Mobile = x.F_Mobile,
                        F_HomePhone = x.F_HomePhone,
                        F_Birthday = x.F_Birthday,
                        F_CreateOn = x.F_CreateOn,
                        F_APPOnFlag = x.F_APPOnFlag,
                        F_LastActiveTime = x.F_LastActiveTime,
                        F_RoleName = x.F_RoleName,
                        F_DeptName = x.F_DeptName
                    })
                    );

            }
            catch (Exception err)
            {
                return Error("错误：" + err.ToString());
            }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
        }
        #endregion
    }
}