using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using ZXDT.CallCenter.Model;
using ZXDT.CallCenter.MVCWeb.Commons;
using ZXDT.CallCenter.MVCWeb.Models;
using ZXDT.Common;
using ZXDT.DBUtility;

namespace ZXDT.CallCenter.MVCWeb.Controllers
{
    public class SystemManageController : BaseController
    {

        #region 部门管理

        BLL.T_Sys_Department deptBLL = new BLL.T_Sys_Department();
        BLL.T_SYS_Time timeBLL = new BLL.T_SYS_Time();
        BLL.T_Sys_UserAccount userbll = new BLL.T_Sys_UserAccount();
        

        /// <summary>
        /// 部门列表
        /// </summary>
        /// <returns></returns>
        public ActionResult DepartmentList()
        {
            WorkOrderMyModel model = new WorkOrderMyModel();

            return View(model);
        }
        ///通过部门父级节点获取数据
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public string GetDepartmentJsonModel(int parentId)
        {
            AddAction("t_sys_department", parentId.ToMyString(), "获取父级树结构", "");
            return Newtonsoft.Json.JsonConvert.SerializeObject(deptBLL.GetDepartmentJsonModel(parentId));
        }

        /// <summary>
        /// 部门编辑
        /// </summary>
        /// <param name="editType">类型1、新增 2、修改</param>
        /// /// <returns></returns>
        public ActionResult DepartmentEdit(int deptId, int editType)
        {
            AddAction("t_sys_department", deptId.ToMyString(), "打开部门编辑页面", "");
            Model.T_Sys_Department viewModel = new Model.T_Sys_Department();


            //当前对象实体
            if (editType == 1)
            {
                viewModel.F_ParentId = deptId;
                viewModel.F_Sort = 1;
            }
            else
            {
                Model.T_Sys_Department deptModel = deptBLL.GetModel(deptId);
                viewModel = deptModel;
            }

            return View(viewModel);
        }

        /// <summary>
        /// 保存编辑
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public bool SaveDeptData(T_Sys_Department deptModel)
        {


            if (deptModel.F_DeptId > 0)
            {
                AddAction("t_sys_department", deptModel.F_DeptId.ToMyString(), "更新部门", deptModel.F_DeptName);

                return deptBLL.Update(deptModel);
            }
            else
            {
                int id = deptBLL.Add(deptModel);
                AddAction("t_sys_department", id.ToMyString(), "添加部门", deptModel.F_DeptName);

                return  id> 0;
            }
        }
        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get)]
        public bool DeleteDeptData(int deptId)
        {
            AddAction("t_sys_department", deptId.ToMyString(), "删除部门");
            return deptBLL.Delete(deptId);

        }
        /// <summary>
        /// 获取部门数据
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <returns></returns>
        [ActionName("DeptDate")]
        [HttpGet]
        public string DeptDate(DateTime? NowDateTime, int page, int limit, int? parentId)
        {

            //数据结果集
            ResponseData dataModel = new ResponseData();
            string sql = "";

            if (parentId != null)
            {
                sql += " and F_ParentId=" + parentId;
            }

            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
                "T_Sys_Department",
                "F_DeptId",
                "*",
                sql,
                "ORDER BY F_Sort asc ",
                limit,
                page,
                true,
                out recordCount);


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;
            AddAction("t_sys_department", parentId.ToMyString(), "获取部门列表", "");
            return JsonConvert.SerializeObject(dataModel);
        }

        #endregion

        #region 角色管理

        BLL.T_Sys_RoleInfo roleBLL = new BLL.T_Sys_RoleInfo();
        public ActionResult RoleList()
        {
            WorkOrderMyModel model = new WorkOrderMyModel();
            return View(model);
        }

        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <returns></returns>
        [ActionName("RoleDate")]
        [HttpGet]
        public string RoleDate(DateTime? NowDateTime, int page, int limit)
        {
            AddAction("t_sys_roleinfo", "page", "查询角色列表",page.ToMyString()+"-"+ limit.ToMyString());
            //数据结果集
            ResponseData dataModel = new ResponseData();

            string sql = "";

            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
                "T_Sys_RoleInfo",
                "F_RoleId",
                "*",
                sql,
                "ORDER BY F_RoleId",
                limit,
                page,
                true,
                out recordCount);


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }



        /// <summary>
        /// 编辑角色
        /// </summary>

        /// <returns></returns>
        public ActionResult RoleEdit(int? roleId)
        {
           
            Model.T_Sys_RoleInfo viewModel = new Model.T_Sys_RoleInfo();

            if (roleId != null && roleId > 0)
            {
                viewModel = roleBLL.GetModel(int.Parse(roleId.ToString()));
                AddAction("t_sys_roleinfo", roleId.ToMyString(), "打开角色编辑页", "角色名称："+ viewModel.F_RoleName, "敏感信息");
            }
            return View(viewModel);
        }

        /// <summary>
        /// 保存编辑
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public bool SaveRoleEdit(T_Sys_RoleInfo roleInfoModel)
        {
            roleInfoModel.F_RoleCode = roleInfoModel.F_RoleCode.ToMyString();

            if (roleInfoModel.F_RoleId > 0)
            {
                AddAction("t_sys_roleinfo", roleInfoModel.F_RoleId.ToMyString(), "更新角色", roleInfoModel.F_RoleName);
                AddAction("t_sys_roleinfo", roleInfoModel.F_RoleId.ToMyString(), "更新角色", "数据：" + JsonConvert.SerializeObject(roleInfoModel), "敏感信息");
                return roleBLL.Update(roleInfoModel);
            }
            else
            {
                int id = roleBLL.Add(roleInfoModel);
                AddAction("t_sys_roleinfo", id.ToMyString(), "添加角色", roleInfoModel.F_RoleName);
                AddAction("t_sys_roleinfo", id.ToMyString(), "添加角色", "数据："  +JsonConvert.SerializeObject(roleInfoModel), "敏感信息");
                return id > 0;
            }
        }




        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get)]
        public bool DeleteRoleData(string roleId)
        {
            AddAction("t_sys_roleinfo", roleId.ToMyString(), "删除角色");
            AddAction("t_sys_roleinfo", roleId.ToMyString(), "删除角色", "数据：" + roleId.ToMyString(), "敏感信息");
            return roleBLL.DeleteList(roleId);
        }
        #endregion

        #region 获取菜单树
        public class DicSeaarchModel
        {
            public string RootCode
            {
                get;
                set;
            }
        }
        /// <summary>
        /// 初始化字典结构
        /// </summary>
        /// <param name="rootCode"></param>
        /// <returns></returns>
        public ActionResult MenuTree(string rootCode)
        {
            DicSeaarchModel model = new DicSeaarchModel();
            model.RootCode = rootCode;
            return View(model);
        }
        /// <summary>
        /// 获取字典数据
        /// </summary>
        /// <param name="rootCode"></param>
        /// <returns></returns>
        public string GetMenuTreeData(int roleId)
        {
            try
            {
                AddAction("T_Sys_ModuleFunctions".ToLower(), roleId.ToMyString(), "获取角色字典", "角色id"+ roleId.ToMyString());

                return "[" + Newtonsoft.Json.JsonConvert.SerializeObject(roleBLL.GetMenuTree(roleId)).Replace("Checked", "checked") + "]";

            } 
            catch (Exception ex)
            {

                return "";
            }
        }
        /// <summary>
        ///授权
        /// </summary>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get)]
        public bool AcceptList(int roleId, string MenuIdStr)
        {
            bool accRersult = false;

            AddAction("T_Sys_ModuleFunctions", roleId.ToMyString(), "授权", "角色id" + roleId.ToMyString()+"功能菜单"+ MenuIdStr);
           if (roleBLL.AddRolesMenuList(roleId, MenuIdStr))
            {
                AddAction("T_Sys_ModuleFunctions", roleId.ToMyString(), "授权角色功能", string.Format("给角色{0}授权菜单功能{1}", roleId, MenuIdStr), "敏感信息");
                accRersult = true;
            }

            return accRersult;

        }
        #endregion

        #region 业务类型管理

        BLL.T_Wo_WorkOrderType dicTittleBLL = new BLL.T_Wo_WorkOrderType();

        public ActionResult BusinessTypeList()
        {
            WorkOrderMyModel model = new WorkOrderMyModel();

            return View(model);
        }
        ///通过dic编码获取dic值
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public string GetDicTittleJson(int parentId)
        {
            AddAction("t_wo_workordertype".ToLower(), parentId.ToMyString(), "获取业务类型");
            return Newtonsoft.Json.JsonConvert.SerializeObject(dicTittleBLL.GetDicTittleJsonModel(parentId));
        }
        /// <summary>
        /// 获取业务类型数据
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <returns></returns>
        [ActionName("OrderTypeDate")]
        [HttpGet]
        public string OrderTypeDate(DateTime? NowDateTime, int page, int limit, int? parentId)
        {
            AddAction("t_wo_workordertype".ToLower(),"page", "获取业务类型列表", page.ToMyString() + "-" + limit.ToMyString());
            //数据结果集
            ResponseData dataModel = new ResponseData();
            string sql = "";

            if (parentId != null && parentId > 0)
            {
                sql += " and F_ParentId=" + parentId;
            }

            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
                "T_Wo_WorkOrderType",
                "F_WorkOrderTypeId",
                "*",
                sql,
                "ORDER BY F_Sort asc ",
                limit,
                page,
                true,
                out recordCount);


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }
        /// <summary>
        /// 编辑业务类型
        /// </summary>
        /// <param name="F_WorkOrderTypeId">当前选中id</param>
        /// <param name="editType">类型1、新增 2、修改</param>
        /// <returns></returns>
        public ActionResult BussinessTypeEdit(int F_WorkOrderTypeId, int editType)
        {
            AddAction("t_wo_workordertype".ToLower(), F_WorkOrderTypeId.ToMyString(), "打开业务类型编辑页");
            Model.T_Wo_WorkOrderType viewModel = new Model.T_Wo_WorkOrderType();
            //当前对象实体
            Model.T_Wo_WorkOrderType orderTypeModel = dicTittleBLL.GetModel(F_WorkOrderTypeId);

            if (editType == 1)
            {
                viewModel.F_ParentId = orderTypeModel.F_WorkOrderTypeId;
                viewModel.F_ParentName = orderTypeModel.F_ParentName;
                viewModel.F_Sort = 1;
            }
            else
            {
                viewModel = orderTypeModel;
            }


            return View(viewModel);
        }



        /// <summary>
        /// 保存编辑
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public bool SaveBussiTypeData(T_Wo_WorkOrderType WorkOrderTypeModel)
        {

            ////当前用户信息
            //workOrderBaseModel.F_USERID = F_UserID;//id
            //workOrderBaseModel.F_LINKMAN = F_UserCode;//工号
            //workOrderBaseModel.F_REPAIRMANNAME = "12";//姓名

            if (WorkOrderTypeModel.F_WorkOrderTypeId > 0)
            {
                AddAction("t_wo_workordertype", WorkOrderTypeModel.F_WorkOrderTypeId.ToMyString(), "更新工单分类",WorkOrderTypeModel.F_Name);
                return dicTittleBLL.Update(WorkOrderTypeModel) > 0;
            }
            else
            {
                int id = dicTittleBLL.Add(WorkOrderTypeModel);
                AddAction("t_wo_workordertype", id.ToMyString(), "添加工单分类", WorkOrderTypeModel.F_Name);
                return id > 0;
            }
        }


        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get)]
        public bool DeleteBussiTypeData(int WorkOrderTypeId)
        {
            AddAction("T_Sys_ModuleFunctions", WorkOrderTypeId.ToMyString(), "删除工单分类","", "敏感信息");
            AddAction("t_wo_workordertype", WorkOrderTypeId.ToMyString(), "删除工单分类");
            return dicTittleBLL.Delete(WorkOrderTypeId);
        }
        #endregion

        #region 字典管理

        BLL.T_Sys_DictionaryValue dicValueBLL = new BLL.T_Sys_DictionaryValue();
        BLL.T_Sys_DictionaryBase dicBaseBLL = new BLL.T_Sys_DictionaryBase();
        /// <summary>
        /// 字典列表
        /// </summary>
        /// <returns></returns>
        public ActionResult DictionaryList()
        {
            WorkOrderMyModel model = new WorkOrderMyModel();

            return View(model);
        }
        ///通过字典获取数据
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public string GetDictionaryJsonModel(int parentId)
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(dicBaseBLL.GetDictionaryJsonModel());
        }
        /// <summary>
        /// 获取字典值数据
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <returns></returns>
        [ActionName("DictionaryDate")]
        [HttpGet]
        public string DictionaryDate(DateTime? NowDateTime, int page, int limit, string dictionaryFlag)
        {
            AddAction("t_sys_dictionarybase".ToLower(),"page", "获取字典列表", page+"-"+ limit);
            //数据结果集
            ResponseData dataModel = new ResponseData();
            string sql = "";

            if (!string.IsNullOrEmpty(dictionaryFlag) && dictionaryFlag != "0")
            {
                sql += " and F_DictionaryFlag='" + dictionaryFlag + "'";
            }

            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();

            if (string.IsNullOrEmpty(dictionaryFlag) || dictionaryFlag != "0")
            {
                dt = BLL.PagerBLL.GetListPager(
                    "T_Sys_DictionaryValue",
                    "F_DictionaryValueId",
                    "*",
                    sql,
                    "ORDER BY F_Sort asc ",
                    limit,
                    page,
                    true,
                    out recordCount);
            }
            else
            {
                dt = BLL.PagerBLL.GetListPager(
              "T_Sys_DictionaryBase",
              "F_DictionaryName",
              "*,F_DictionaryName as F_Name",
              sql,
              "ORDER BY F_Sort asc ",
              limit,
              page,
              true,
              out recordCount);
            }


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }


        /// <summary>
        /// 获取字典值数据
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <returns></returns>
        [ActionName("DictionaryDateOut")]
        [HttpGet]
        public string DictionaryDateOut(DateTime? NowDateTime, int page, int limit, string dictionaryFlag)
        {
            AddAction("t_sys_dictionarybase".ToLower(), "page", "获取字典列表", page + "-" + limit);
            //数据结果集
            ResponseData dataModel = new ResponseData();
            string sql = "and (F_DictionaryFlag='ZDXX' OR F_DictionaryFlag='ZDXX2')";

            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();


            dt = BLL.PagerBLL.GetListPager(
                "T_Sys_DictionaryValue",
                "F_DictionaryValueId",
                "*",
                sql,
                "ORDER BY F_Sort asc ",
                limit,
                page,
                true,
                out recordCount);



            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }



        /// <summary>
        /// 字典项编辑
        /// </summary>
        /// <param name="editType">类型1、新增 2、修改</param>
        /// /// <returns></returns>
        public ActionResult DictionaryBaseEdit(string dicCode, int editType)
        {
            AddAction("t_sys_dictionarybase".ToLower(), dicCode, "打开字典编辑页");
            Model.T_Sys_DictionaryBase viewModel = new Model.T_Sys_DictionaryBase();
            //当前对象实体
            if (editType == 1)
            {
                viewModel.F_Sort = 1;
            }
            else
            {
                Model.T_Sys_DictionaryBase dicBaseModel = dicBaseBLL.GetModel(dicCode);
                viewModel = dicBaseModel;
            }

            return View(viewModel);
        }
        /// <summary>
        /// 保存字典项编辑
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public bool SaveDictionaryBaseData(T_Sys_DictionaryBase dicBaseModel)
        {
            try
            {
                if (!string.IsNullOrEmpty(dicBaseModel.F_DictionaryFlagType))
                {
                    AddAction("t_sys_dictionarybase", dicBaseModel.F_DictionaryFlagType, "更新字典项");
                    return dicBaseBLL.Update(dicBaseModel);
                }
                else
                {
                    AddAction("t_sys_dictionarybase", dicBaseModel.F_DictionaryFlagType, "添加字典项");
                    return dicBaseBLL.Add(dicBaseModel);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Default.WriteError("字典操作失败",ex);
                return false;
            }
        }

        /// <summary>
        /// 删除字典项
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get)]
        public bool DeleteBaseData(string baseCode)
        {
            AddAction("t_sys_dictionarybase", baseCode,"删除字典项");
            return dicBaseBLL.Delete(baseCode);
        }

        /// <summary>
        /// 字典值编辑
        /// </summary>
        /// <param name="editType">类型1、新增 2、修改</param>
        /// /// <returns></returns>
        public ActionResult DictionaryValueEdit(int? valueId, string parentCode, int editType)
        {
            AddAction("t_sys_dictionarybase".ToLower(), valueId.ToMyString(), "打开字典值编辑页");
            Model.T_Sys_DictionaryValue viewModel = new Model.T_Sys_DictionaryValue();
            //当前对象实体
            if (editType == 1)
            {
                viewModel.F_Sort = 1;
                viewModel.F_DictionaryFlag = parentCode;
            }
            else
            {
                Model.T_Sys_DictionaryValue dicValueModel = dicValueBLL.GetModel(int.Parse(valueId.ToString()));
                viewModel = dicValueModel;
            }

            return View(viewModel);
        }
        /// <summary>
        /// 保存字典值
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public bool SaveDictionaryValueData(T_Sys_DictionaryValue dicBaseModel)
        {
            if (dicBaseModel.F_DictionaryValueId > 0)
            {
                AddAction("t_sys_dictionaryvalue", dicBaseModel.F_DictionaryValueId.ToMyString(), "更新字典值");
                return dicValueBLL.Update(dicBaseModel);
            }
            else
            {
                
                int id = dicValueBLL.Add(dicBaseModel);
                AddAction("t_sys_dictionaryvalue", id.ToMyString(), "添加字典值");
                return id > 0;
            }
        }

        /// <summary>
        /// 删除字典值
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get)]
        public bool DeleteValueData(int id)
        {
            AddAction("t_sys_dictionaryvalue", id.ToMyString(), "删除字典值");
            return dicValueBLL.Delete(id);

        }
        #endregion

        #region 菜单管理

        BLL.T_Sys_ModuleFunctions menuBLL = new BLL.T_Sys_ModuleFunctions();
        /// <summary>
        /// 部门列表
        /// </summary>
        /// <returns></returns>
        public ActionResult MenuList()
        {
            WorkOrderMyModel model = new WorkOrderMyModel();

            return View(model);
        }
        ///通过部门父级节点获取数据
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public string GetMenuJsonModel(int parentId)
        {
            AddAction("t_sys_modulefunctions", parentId.ToMyString(), "获取父级下菜单","父级id:"+ parentId);
            return Newtonsoft.Json.JsonConvert.SerializeObject(menuBLL.GetMenuJsonModel(parentId));
        }
        /// <summary>
        /// 获取部门数据
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <returns></returns>
        [ActionName("MenuDate")]
        [HttpGet]
        public string MenuDate(DateTime? NowDateTime, int page, int limit, int? parentId)
        {
            AddAction("t_sys_modulefunctions", "page", "获取菜单列表",page+"-"+limit);
            //数据结果集
            ResponseData dataModel = new ResponseData();
            string sql = "";

            if (parentId != null)
            {
                sql += " and F_ParentId=" + parentId;
            }

            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
                "T_Sys_ModuleFunctions",
                "F_FunctionId",
                "* ,(CASE F_StateFlag WHEN 1 THEN '启用' WHEN 0 THEN '禁用' ELSE NULL END ) as F_StateFlagName",
                sql,
                "ORDER BY F_Sort asc ",
                limit,
                page,
                true,
                out recordCount);


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }

        /// <summary>
        /// 功能编辑
        /// </summary>
        /// <param name="editType">类型1、新增 2、修改</param>
        /// /// <returns></returns>
        public ActionResult MenuEdit(int menuId, int editType)
        {

            Model.T_Sys_ModuleFunctions viewModel = new Model.T_Sys_ModuleFunctions();

            AddAction("t_sys_modulefunctions", menuId.ToMyString(), "打开功能编辑页", "菜单编号"+ menuId);
            //当前对象实体
            if (editType == 1)
            {
                viewModel.F_ParentId = menuId;
                viewModel.F_Sort = 1;
            }
            else
            {
                Model.T_Sys_ModuleFunctions deptModel = menuBLL.GetModel(menuId);
                viewModel = deptModel;
            }

            return View(viewModel);
        }
        /// <summary>
        /// 保存编辑
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public bool SaveMenuData(T_Sys_ModuleFunctions menuModel)
        {

            if (menuModel.F_FunctionId > 0)
            {
                AddAction("t_sys_modulefunctions", menuModel.F_FunctionId.ToMyString(), "更新菜单", menuModel.F_Name);
                AddAction("t_sys_modulefunctions", menuModel.F_FunctionId.ToMyString(), "更新菜单", "数据："+JsonConvert.SerializeObject(menuModel), "主要设置");
                return menuBLL.Update(menuModel);
            }
            else
            {
              int id = menuBLL.Add(menuModel);
              AddAction("t_sys_modulefunctions", id.ToMyString(), "添加菜单", menuModel.F_Name);
              AddAction("t_sys_modulefunctions", menuModel.F_FunctionId.ToMyString(), "添加菜单", "数据：" + JsonConvert.SerializeObject(menuModel), "主要设置");

              return id > 0;
            }
        }


        /// <summary>
        /// 删除功能
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get)]
        public bool DeleteMenuData(int menuId)
        {
            Model.T_Sys_ModuleFunctions deptModel = menuBLL.GetModel(menuId);
            AddAction("t_sys_modulefunctions", deptModel.F_FunctionId.ToMyString(), "删除菜单", "数据：" + JsonConvert.SerializeObject(deptModel), "主要设置");
            AddAction("t_sys_modulefunctions", menuId.ToMyString(), "删除菜单");
            return menuBLL.Delete(menuId);

        }
        #endregion

        #region 用户管理

        BLL.T_Sys_UserAccount userBLL = new BLL.T_Sys_UserAccount();
        /// <summary>
        /// 部门列表
        /// </summary>
        /// <returns></returns>
        public ActionResult UserList()
        {
            WorkOrderMyModel model = new WorkOrderMyModel();

            return View(model);
        }


        /// <summary>
        /// 用户编辑
        /// </summary>
        /// <param name="editType">类型1、新增 2、修改</param>
        /// /// <returns></returns>
        public ActionResult UserEdit(int? userId, int? deptId, int editType)
        {
            AddAction("t_sys_useraccount", userId.ToMyString(), "打开用户编辑页", "用户编号/部门" + userId+"-"+ deptId);

            Model.T_Sys_UserAccount viewModel = new Model.T_Sys_UserAccount();

            //当前对象实体
            if (editType == 1)
            {
                viewModel.F_DeptId = int.Parse(deptId.ToString());
                AddAction("t_sys_useraccount", deptId.ToMyString(), "打开用户添加页", "", "敏感信息");

            }
            else
            {
                Model.T_Sys_UserAccount userModel = userBLL.GetModel(int.Parse(userId.ToString()));
                viewModel = userModel;
                AddAction("t_sys_useraccount", userId.ToMyString(), "打开用户编辑页", "数据：" + JsonConvert.SerializeObject(userModel), "敏感信息");
            }
            //获取部门列表
            viewModel.DepartmentList = deptBLL.GetModelList("F_ParentId=0");
            //获取角色列表
            viewModel.RoleInfoList = roleBLL.GetModelList("");
            return View(viewModel);
        }

        /// <summary>
        /// 保存编辑
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public bool SaveUserData(T_Sys_UserAccount userModel)
        {
            BLL.T_Sys_RoleInfo bll = new BLL.T_Sys_RoleInfo();

            BLL.T_Sys_UserAccount blluser = new BLL.T_Sys_UserAccount();

            if (userModel.F_UserId > 0)
            {
                Model.T_Sys_UserAccount oldusermodel = blluser.GetModel(userModel.F_UserId);

                Model.T_Sys_RoleInfo role2 = bll.GetModel(F_RoleID); //当前登录人的角色

                Model.T_Sys_RoleInfo role = bll.GetModel(oldusermodel.F_RoleId);//要修改账户的角色
                                                                                //要修改de账号 角色代码，登录人不是系统管理员，返回false
                if (role.F_RoleCode == "XTGLY" || role.F_RoleCode == "YWGLY")
                {
                  
                    if (role2.F_RoleCode != "XTGLY")
                    {
                        return false;
                    }

                }
                Model.T_Sys_RoleInfo rolenew = bll.GetModel(userModel.F_RoleId);//要修改账户的改了之后的角色
                if (rolenew.F_RoleCode == "XTGLY" || rolenew.F_RoleCode == "YWGLY")
                {                   
                    if (role2.F_RoleCode != "XTGLY")
                    {
                        return false;
                    }
                }
            }

            userModel.F_WorkNumber = userModel.F_UserCode;
            if (userModel.F_UserId > 0)
            {
                T_Sys_UserAccount oldUserModel = userBLL.GetModel(userModel.F_UserId);

                oldUserModel.F_WorkNumber = userModel.F_WorkNumber;
                oldUserModel.F_UserName = userModel.F_UserName; 
                oldUserModel.F_UserCode = userModel.F_UserCode;
                oldUserModel.F_DeptId = userModel.F_DeptId;
                oldUserModel.F_RoleId = userModel.F_RoleId;
                oldUserModel.F_SexFlag = userModel.F_SexFlag;
                oldUserModel.F_Telephone = userModel.F_Telephone;
                oldUserModel.F_SeatFlag = userModel.F_SeatFlag;
                oldUserModel.F_SeatRight = userModel.F_SeatRight;
                oldUserModel.F_Password = userModel.F_Password;
                oldUserModel.F_Remark = userModel.F_Remark;
                oldUserModel.F_ExtensionNumber = userModel.F_ExtensionNumber.ToMyString();
                AddAction("t_sys_useraccount", userModel.F_UserId.ToMyString(), "更新用户", userModel.F_UserName);
                AddAction("t_sys_useraccount", userModel.F_UserId.ToMyString(), "更新用户", "数据：" + JsonConvert.SerializeObject(oldUserModel), "敏感信息");

                return userBLL.Update(oldUserModel);
            }
            else
            {
                Model.T_Sys_RoleInfo role2 = bll.GetModel(F_RoleID); //当前登录人的角色
                Model.T_Sys_RoleInfo rolenew = bll.GetModel(userModel.F_RoleId);//要添加账户的的角色
                if (rolenew.F_RoleCode == "XTGLY" || rolenew.F_RoleCode == "YWGLY")
                {
                    if (role2.F_RoleCode != "XTGLY")
                    {
                        return false;
                    }
                }



                userModel.F_HJType = 0;
                userModel.F_GroupId = 1;

                if (userModel.F_SeatFlag)//&&false)
                {
                    #region 调用接口插入用户信息
                    try
                    {
                        AddAction("t_sys_useraccount", userModel.F_UserId.ToMyString(), "跨站插入用户数据", "rexian.zzmetro.com用户编号" + userModel.F_UserCode);
                        AddAction("t_sys_useraccount", userModel.F_UserId.ToMyString(), "跨站插入用户数据", "数据：" + JsonConvert.SerializeObject(userModel), "敏感信息");

                        StringBuilder returnStr = new StringBuilder();
                        string wxAddUrl = "http://rexian.zzmetro.com/api/customer/insert";
                        UserInfo model = new UserInfo();
                        model.name = userModel.F_UserCode;

                        object userInfo = JObject.Parse(JsonConvert.SerializeObject(model));
                        string addWxResult = HttpHelper.HttpPost(wxAddUrl, userInfo);
                        ResponseResult ResultModel = JsonConvert.DeserializeObject<ResponseResult>(addWxResult);
                        userModel.F_PId = ResultModel.data;
                    }
                    catch (Exception ex)
                    {

                    }
                    #endregion
                }


                
                int userid = userBLL.Add(userModel);
                AddAction("t_sys_useraccount", userid.ToMyString(), "新增用户", userModel.F_UserName);
                AddAction("t_sys_useraccount", userModel.F_UserId.ToMyString(), "新增用户", "数据：" + JsonConvert.SerializeObject(userModel), "敏感信息");

                return userid > 0;
            }
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get)]
        public bool DeleteUserData(int userId)
        {
            T_Sys_UserAccount oldUserModel = userBLL.GetModel(userId);

            if (F_RoleID != 17 &&(oldUserModel.F_RoleId==17 || oldUserModel.F_RoleId == 57))
            {
                return false;            
            }
            AddAction("t_sys_useraccount", userId.ToMyString(), "删除用户");
            AddAction("t_sys_useraccount", userId.ToMyString(), "删除用户", "数据：" + JsonConvert.SerializeObject(oldUserModel), "敏感信息");

        
            return userBLL.Delete(userId);
        }
        /// <summary>
        /// 获取用户数据
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <returns></returns>
        [ActionName("UserDate")]
        [HttpGet]
        public string UserDate(DateTime? NowDateTime, int page, int limit, int? deptId)
        {
            AddAction("t_sys_useraccount", "page", "获取用户列表", "部门编号" + deptId.ToMyString());

            //数据结果集
            ResponseData dataModel = new ResponseData();
            string sql = "";

            if (deptId != null && deptId > 0)
            {
                sql += " and F_DeptId=" + deptId;
            }

            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
                "T_Sys_UserAccount",
                "F_UserId",
                "*"
                + ",(select  top 1 t.F_DeptName from T_Sys_Department t where t.F_DeptId=T_Sys_UserAccount.F_DeptId) as F_DeptName  "
                 + ",(select  top 1 t.F_RoleName from T_Sys_RoleInfo t where t.F_RoleId=T_Sys_UserAccount.F_RoleId) as F_RoleName  "
                  + ",(CASE F_SeatFlag WHEN 1 THEN '使用' WHEN 0 THEN '不使用' ELSE NULL END ) as F_SeatFlagName"
                    + ",(CASE F_SeatRight WHEN 1 THEN '班长坐席' WHEN 0 THEN '普通坐席' ELSE NULL END ) as F_SeatRightName"
                ,
                sql,
                "ORDER BY F_UserCode asc ",
                limit,
                page,
                true,
                out recordCount);


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;
            return JsonConvert.SerializeObject(dataModel);
        }

        #endregion

        #region 修改密码
        /// <summary>
        ///用户修改个人密码
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult UserPwdEdit()
        {
            AddAction("t_sys_useraccount", F_UserID.ToMyString(), "打开修改密码页");
            Model.T_Sys_UserAccount model = new Model.T_Sys_UserAccount();

            if (F_UserID > 0)
            {
                model = userBLL.GetModel(F_UserID);

            }

            return View(model);
        }

        /// </summary>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CheckPwd(string pwd)
        {
            checkpwd.Chkrslt chkrslt = checkpwd.PasswordStrength(pwd);

            return Json(new
            {
                code = chkrslt.RSL ? 0 : 1,
                msg = chkrslt.MSG
            });
        }

        /// <summary>
        ///保存修改密码
        /// </summary>
        [AcceptVerbs(HttpVerbs.Post)]
        public string UpdateUserPwd(Model.T_Sys_UserAccount model)
        {
            //checkpwd.Chkrslt chkrslt = checkpwd.PasswordStrength(model.password);
            //if(!chkrslt.RSL)
            //{
            //    return chkrslt.MSG;
            //}
            string AddRersult = "false";
            if (model.F_UserId > 0)
            {

                Model.T_Sys_UserAccount newModel = userBLL.GetModel(model.F_UserId);
                if (newModel != null && newModel.F_Password == model.oldPwd)
                {
                    newModel.F_Password = model.password;
                    if (userBLL.Update(newModel))
                    {
                        AddAction("t_sys_useraccount", model.F_UserId.ToMyString(), "修改密码");
                        AddAction("t_sys_useraccount", model.F_UserId.ToMyString(), "修改密码", "数据：" + JsonConvert.SerializeObject(newModel), "敏感信息");
                        AddRersult = "True";
                    }
                }
                else
                {
                    AddRersult = "旧密码不正确！";
                }
            }

            return AddRersult;


        }
        #endregion

        #region 基本信息

        /// <summary>
        /// 用户编辑
        /// </summary>
        /// <param name="editType">类型1、新增 2、修改</param>
        /// /// <returns></returns>
        public ActionResult UserView()
        {

            Model.T_Sys_UserAccount userModel = userBLL.GetModel(F_UserID);
            AddAction("t_sys_useraccount", F_UserID.ToMyString(), "打开用户编辑页", "用户："+userModel.F_UserName);
            if (string.IsNullOrEmpty(userModel.F_HomePhone))
            {
                userModel.F_HomePhone = "/Content/images/face.jpg";

            }
            else
            {
                if (!System.IO.File.Exists(Server.MapPath(userModel.F_HomePhone)))
                {
                    userModel.F_HomePhone = "/Content/images/face.jpg";
                }
            }
            return View(userModel);
        }
        #endregion

        #region 图片上传


        /// <summary>
        /// 图片上传
        /// </summary>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UploadFile()
        {
            try
            {
                if (Request.Files.Count > 0)
                {
                    string uppath = string.Empty;
                    string savepath = string.Empty;
                    HttpPostedFileBase imgFile = Request.Files[0];
                    if (imgFile != null)
                    {
                        //创建图片新的名称
                        string nameImg = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                        //获得上传图片的路径
                        string strPath = imgFile.FileName;
                        //获得上传图片的类型(后缀名)
                        string type = strPath.Substring(strPath.LastIndexOf(".") + 1).ToLower();
                        //拼写数据库保存的相对路径字符串
                        savepath = "/Content/HeadImg/images/";
                        //拼写上传图片的路径
                        uppath = Server.MapPath("~/Content/HeadImg/images/");
                        if (!Directory.Exists(Path.GetDirectoryName(uppath)))
                        {
                            Directory.CreateDirectory(Path.GetDirectoryName(uppath));
                        }
                        uppath += nameImg + "." + type;
                        savepath += nameImg + "." + type;
                        //上传图片
                        imgFile.SaveAs(uppath); //原图片路径
                    }

                    Model.T_Sys_UserAccount userModel = userBLL.GetModel(F_UserID);
                    userModel.F_HomePhone = savepath;
                    if (userBLL.Update(userModel))
                    {

                        return Json(new
                        {
                            code = 0,
                            src = savepath,
                            msg = "上传成功"
                        });
                    }
                    else
                    {
                        return Json(new
                        {
                            code = 1,
                            src = "",
                            msg = "上传出错 请检查图片名称或图片内容"
                        });
                    }
                }
                else
                {
                    return Json(new
                    {
                        code = 1,
                        src = "",
                        msg = "上传出错 请检查图片名称或图片内容"
                    });
                }
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    code = 1,
                    src = "",
                    msg = "上传出错: " + ex.Message
                });
            }


        }
        #endregion

        #region 公告管理

        /// <summary>
        /// 公告管理
        /// </summary>
        /// <returns></returns>
        public ActionResult noticeManage()
        {
            Model.T_Msg_NoticeInfo noticeModel = new Model.T_Msg_NoticeInfo(); 
            return View(noticeModel);
        }
        public ActionResult test()
        {
            Model.T_Msg_NoticeInfo noticeModel = new Model.T_Msg_NoticeInfo();
            return View(noticeModel);
        }
        /// <summary>
        /// 获取公告数据
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <returns></returns>
        [ActionName("NoticeListDate")]
        [HttpGet]
        public string NoticeListDate(DateTime? NowDateTime, int page, int limit, int? parentId)
        {
            AddAction("t_msg_noticeinfo", parentId.ToMyString(), "获取公告列表", page+"-"+ limit);
            //数据结果集
            ResponseData dataModel = new ResponseData();
            string sql = "";

            if (parentId != null)
            {
                sql += " and F_ParentId=" + parentId;
            }

            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Msg_NoticeInfo> pageModel = new Model.PageData<Model.T_Msg_NoticeInfo>();
            dt = BLL.PagerBLL.GetListPager(
                "T_Msg_NoticeInfo",
                "F_NoticeId",
                "*,CONVERT(varchar,F_CreateOn, 120 ) as F_CreateOnNew,isnull( CONVERT(varchar,F_ModifyDate, 120 ) ,CONVERT(varchar,F_CreateOn, 120 ) )as F_ModifyDateNew ",
                sql,
                "ORDER BY isnull( CONVERT(varchar,F_ModifyDate, 120 ) ,CONVERT(varchar,F_CreateOn, 120 ) ) desc ",
                limit,
                page,
                true,
                out recordCount);


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        } 
        
        BLL.T_Msg_NoticeInfo noticeBLL = new BLL.T_Msg_NoticeInfo();
        BLL.T_Msg_NoticeUsers noticeuserBLL = new BLL.T_Msg_NoticeUsers(); 
        BLL.T_Sys_UserAccount usersBLL = new BLL.T_Sys_UserAccount();
        /// <summary>
        /// 编辑公告
        /// </summary>
        /// <param name="editType">类型1、新增 2、修改</param>
        /// /// <returns></returns>
        public ActionResult NoticeEdit(int? noticeId, int editType)
        {
            AddAction("t_msg_noticeinfo", noticeId.ToMyString(), "打开公告编辑页");
            Model.T_Msg_NoticeInfo viewModel = new Model.T_Msg_NoticeInfo();

            //当前对象实体
            if (editType == 1)
            {
                viewModel.F_NoticeId = int.Parse(noticeId.ToString());
                //viewModel.UseList = userBLL.GetModelList("");
            }
            else
            {
                Model.T_Msg_NoticeInfo userModel = noticeBLL.GetModel(int.Parse(noticeId.ToString())); 
                viewModel = userModel;
                if (!string.IsNullOrEmpty(userModel.F_ReceiveInfo))
                {
                    int deviceid = userModel.F_DeviceId == null ? 0 : (int)userModel.F_DeviceId;
                    viewModel.UseList = userBLL.GetModelList("F_UserId=" + deviceid);
                    viewModel.DepartmentList = deptBLL.GetModelList("F_DeptId=" + userModel.F_ReceiveInfo);
                }
            }
            //获取部门列表
           
            return View(viewModel);
        }
        /// <summary>
        /// 查看公告
        /// </summary>
        /// <param name="editType">类型1、新增 2、修改</param>
        /// /// <returns></returns>
        public ActionResult NoticeView(int? noticeId)
        {
            AddAction("t_msg_noticeinfo", noticeId.ToMyString(), "查看公告详情");
            Model.T_Msg_NoticeInfo viewModel = new Model.T_Msg_NoticeInfo();
                viewModel.F_NoticeId = int.Parse(noticeId.ToString());
                viewModel.UseList = userBLL.GetModelList("");
          
                Model.T_Msg_NoticeInfo userModel = noticeBLL.GetModel(int.Parse(noticeId.ToString()));
                viewModel = userModel;
                viewModel.UseList = userBLL.GetModelList("F_DeptId=" + userModel.F_ReceiveInfo);
           
            //获取部门列表
            viewModel.DepartmentList = deptBLL.GetModelList("F_ParentId=0");
            return View(viewModel);
        }
        /// <summary>
        /// 保存编辑公告
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public bool SaveNoticeData(T_Msg_NoticeInfo noticeinfoModel)
        {
            bool result = false;
            DataTable dt = new DataTable();
            DataTable dtuser = new DataTable();
            int noticeid = 0;
            Model.T_Msg_NoticeUsers noticeuserModel = new Model.T_Msg_NoticeUsers();
            DateTime? readdate =null;
            try
            {
                //获取当前用户
                noticeinfoModel.F_CreateBy = F_UserID;
                noticeinfoModel.F_CreateName = HttpUtility.UrlDecode(F_UserName); 
                if (noticeinfoModel.F_NoticeId > 0)
                {
                    noticeid = noticeinfoModel.F_NoticeId;
                    Model.T_Msg_NoticeInfo oldnoticeinfoModel = noticeBLL.GetModel(int.Parse(noticeid.ToString())); ;
                    
                        dt = new ZXDT.CallCenter.BLL.T_Msg_NoticeInfo().GetList("1=1 and F_NoticeId= " + noticeid).Tables[0];
                        if (dt.Rows.Count > 0)
                        {
                            dtuser = new ZXDT.CallCenter.BLL.T_Sys_UserAccount().GetList("1=1 and F_DeptId= " + Convert.ToInt32(dt.Rows[0]["F_ReceiveInfo"].ToString())).Tables[0];
                            foreach (DataRow dr in dtuser.Rows)
                            {
                                noticeuserModel.F_UserId = Convert.ToInt32(dr["F_UserId"].ToString());
                                result = noticeuserBLL.Delete(noticeid, noticeuserModel.F_UserId);
                            }
                        }
                    readdate =Convert.ToDateTime( oldnoticeinfoModel.F_CreateOn);
                    oldnoticeinfoModel.F_Code = noticeinfoModel.F_Code;
                    oldnoticeinfoModel.F_Title = noticeinfoModel.F_Title;
                    oldnoticeinfoModel.F_Resume = noticeinfoModel.F_Resume;
                    oldnoticeinfoModel.F_Content = noticeinfoModel.F_Content;
                    oldnoticeinfoModel.F_ReceiveInfo = noticeinfoModel.F_ReceiveInfo;
                    oldnoticeinfoModel.F_DeviceId = noticeinfoModel.F_DeviceId;
                    oldnoticeinfoModel.F_State = 1;
                    oldnoticeinfoModel.F_ModifyDate = DateTime.Now;
                    oldnoticeinfoModel.F_ModifyBy = F_UserID;
                    result = noticeBLL.Update(oldnoticeinfoModel);
                    AddAction("t_msg_noticeinfo", oldnoticeinfoModel.F_NoticeId.ToMyString(), "修改公告");
                }
                else
                {
                    noticeid = 0;
                    noticeinfoModel.F_DeviceId = noticeinfoModel.F_UserId;
                    noticeinfoModel.F_State = 1;
                    noticeinfoModel.F_CreateOn = DateTime.Now;
                    noticeinfoModel.F_ModifyDate = DateTime.Now;
                    readdate = noticeinfoModel.F_CreateOn;
                    
                    noticeid = noticeBLL.Add(noticeinfoModel);
                    if (noticeid > 0)
                    {
                        result = true;
                    }         
                     
                    AddAction("t_msg_noticeinfo", noticeid.ToMyString(), "添加公告");
                } 
                //指定方式发送公告
                if (noticeinfoModel.F_DeviceId > 0)
                {//指定人员发公告 
                    noticeuserModel.F_NoticeId = noticeid;
                    noticeuserModel.F_UserId = Convert.ToInt32(noticeinfoModel.F_UserId);
                    noticeuserModel.F_UserName = noticeinfoModel.F_UserName;
                    //if (readdate.ToString() != "")
                    //{ noticeuserModel.F_ReadDate = readdate; }
                    //else
                    //{
                    //    noticeuserModel.F_ReadDate = noticeinfoModel.F_CreateOn;
                    //}
                    noticeuserModel.F_State = 0;
                    noticeuserModel.F_Type = 0;
                    result = noticeuserBLL.Add(noticeuserModel);
                }
                else
                {//指定部门发公告 
                    dt = new ZXDT.CallCenter.BLL.T_Sys_UserAccount().GetList("1=1 and F_DeptId= " + noticeinfoModel.F_ReceiveInfo).Tables[0];
                    if (dt.Rows.Count > 0)
                    {
                        noticeuserModel.F_NoticeId = noticeid;
                        //if (readdate.ToString()!="")
                        //{ noticeuserModel.F_ReadDate = readdate; }
                        //else { 
                        //noticeuserModel.F_ReadDate = noticeinfoModel.F_CreateOn;
                        //}
                        foreach (DataRow dr in dt.Rows)
                        {
                            noticeuserModel.F_UserId = Convert.ToInt32(dr["F_UserId"].ToString());
                            noticeuserModel.F_UserName = dr["F_UserName"].ToString(); 
                            noticeuserModel.F_State = 0;
                            noticeuserModel.F_Type = 0;
                            result = noticeuserBLL.Add(noticeuserModel);
                        }

                    }

                }
            }
            catch (Exception) { }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
                return result;
        }
        /// <summary>
        /// 删除公告
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get)]
        public bool DeleteNoticeData(int noticeId)
        {
            bool res = false;
            DataTable dt = new DataTable();
            DataTable dtuser = new DataTable();
            Model.T_Msg_NoticeUsers noticeuserModel = new Model.T_Msg_NoticeUsers();
            try
            {
                dt = noticeBLL.GetList("1=1 and F_NoticeId= " + noticeId).Tables[0];
                if (dt.Rows.Count > 0)
                { 
                    res = noticeBLL.Delete(noticeId);
                    AddAction("t_msg_noticeinfo", noticeId.ToMyString(), "删除公告");
                    //指定部门发公告 
                    dtuser = new ZXDT.CallCenter.BLL.T_Sys_UserAccount().GetList("1=1 and F_DeptId= " + Convert.ToInt32( dt.Rows[0]["F_ReceiveInfo"].ToString()) ).Tables[0];
                    foreach (DataRow dr in dtuser.Rows)
                    {
                        noticeuserModel.F_UserId = Convert.ToInt32(dr["F_UserId"].ToString());
                        noticeuserBLL.Delete(noticeId, noticeuserModel.F_UserId);
                    }
                }
            }
            catch (Exception) { }
            finally {
                dt.Clear();
                dt.Dispose();
                dtuser.Clear();
                dtuser.Dispose();
            }
            return res;
        }

        /// <summary>
        /// 未接根据recordid更新回访信息
        /// </summary>
        /// <param name="SetCallRecordHfInfo"></param>
        /// <returns></returns>
        [ActionName("SetNoticeState")]
        [HttpGet]
        public string SetNoticeState(string id, string timeno)
        {
            string res = ""; 
            try
            {
                Model.T_Msg_NoticeInfo model = new Model.T_Msg_NoticeInfo();
                model.F_State =1; 
                model.F_NoticeId = Convert.ToInt32(id);

                bool bl = new BLL.T_Msg_NoticeInfo().UpdateNoticeState(model);
                if (bl)
                {
                    res = "success";
                }
            }
            catch
            { } 
            return res;
        }
        [ActionName("CookieData")]
        [HttpGet]
        public string CookieData() {
            return F_UserID.ToString();
        }
        #endregion



        /// <summary>
        /// 工作时间管理
        /// </summary>
        /// <returns></returns>
        public ActionResult workTimeManage()
        {
            Model.T_SYS_Time timeModel = new Model.T_SYS_Time();
            return View(timeModel);
        }


        /// <summary>
        /// 获取工作时间数据
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <returns></returns>
        [ActionName("workTimeListData")]
        [HttpGet]
        public string workTimeListData()
        {
           
            //数据结果集
            ResponseData dataModel = new ResponseData();
            string sql = " select top 1 * from T_SYS_Time";
            DataTable dt= DbHelperSQL.Query(sql).Tables[0];    
          

            dataModel.code = 0;
            dataModel.count = 1;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }

        public ActionResult workTimeEdit(int  workTimeId )
        {
           
            Model.T_SYS_Time  viewModel = new Model.T_SYS_Time();        
           
            
            
             //   Model.T_SYS_Time userModel = timeBLL.GetModel(workTimeId);
              
            
            //获取部门列表

            return View(viewModel);
        }
     
        [AcceptVerbs(HttpVerbs.Post)]
        public bool SaveWorkTimeData(T_SYS_Time systimeModel)
        {
            bool result = false;
            DataTable dt = new DataTable();
            DataTable dtuser = new DataTable();
        
            Model.T_Msg_NoticeUsers noticeuserModel = new Model.T_Msg_NoticeUsers();
            try
            {
           
                Model.T_SYS_Time oldsystimeModel = timeBLL.GetModel();
                oldsystimeModel.F_WorkEndTimes = systimeModel.F_WorkEndTimes;
                oldsystimeModel.F_WorkStartTimes = systimeModel.F_WorkStartTimes;
                result = timeBLL.Update(oldsystimeModel);
                AddAction("t_sys_time", "1",  "修改工作时间", "", F_UserCode + "在" + DateTime.Now.ToString() + "修改工作时间");
            }
            catch (Exception) { }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
            return result;
        }


        public string GetUnreadData()
        {
            DataTable dt = new ZXDT.CallCenter.BLL.T_Msg_NoticeUsers().GetList("1=1 and F_ReadDate is null and  F_UserId= " + F_UserID + "order by F_ReadDate desc").Tables[0];

            DataTable dtinfo = new DataTable();
            if (dt.Rows.Count > 0)
            {

                List<int> noticelist = (from r in dt.AsEnumerable() select r.Field<int>("F_NoticeId")).ToList<int>();
               dtinfo = new ZXDT.CallCenter.BLL.T_Msg_NoticeInfo().GetList("1=1 and F_State=1 and  F_NoticeId in (  '" + string.Join("','", noticelist) + "')  order by  ISNULL(F_ModifyDate,F_CreateOn) desc").Tables[0];
            }
                return JsonConvert.SerializeObject(dtinfo);
        }

        public bool readNoticeData(int noticeid)
        {
            BLL.T_Msg_NoticeUsers noticeUsers = new BLL.T_Msg_NoticeUsers();
            Model.T_Msg_NoticeUsers model= noticeUsers.GetModel(noticeid,F_UserID);
            model.F_ReadDate = DateTime.Now;
            noticeUsers.Update(model);
            return true;
        }




    }

    #region 接口基本信息
    /// <summary>
    /// 用户信息
    /// </summary>
    public class UserInfo
    {
        public int id
        {
            get;
            set;
        }
        public string name
        {
            get;
            set;
        }
    }
    /// <summary>
    /// 返回结果信息
    /// </summary>
    public class ResponseResult
    {
        public int code
        {
            get;
            set;

        }
        public int data
        {
            get;
            set;
        }
    }

    /// <summary>
    /// 返回结果信息
    /// </summary>
    public class WXResponseResult
    {
        public int customer
        {
            get;
            set;

        }
        public int service
        {
            get;
            set;
        }

        public int total
        {
            get;
            set;
        }
    }
    #endregion
}
