using CallCenter.Utility;
using CallCenterApi.Common;
using CallCenterApi.DB;
using CallCenterApi.Interface.Controllers.Base;
using CallCenterApi.Interface.Controllers.workorder;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Transactions;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers
{
    public class WebController : BaseController
    {
        private BLL.T_Web_Link linkBLL = new BLL.T_Web_Link();
        private BLL.T_Web_News newsBLL = new BLL.T_Web_News();
        private BLL.T_Web_SurveyResult resBLL = new BLL.T_Web_SurveyResult();
        private BLL.T_Web_WorkRelease fbBLL = new BLL.T_Web_WorkRelease();

        #region 链接地址
        /// <summary>
        /// 获取链接列表
        /// </summary>
        /// <returns></returns>
        [Authority]
        public ActionResult GetLinkList()
        {
            string name = RequestString.GetQueryString("name");
            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;

            var sql = " and 1=1 ";
            if (!string.IsNullOrEmpty(name))
            {
                sql += " and LinkName like '%" + name + "%'";
            }
            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }

            var recordCount = 0;
            var dt = BLL.PagerBLL.GetListPager(
              "T_Web_Link",
              "Id",
              "*",
              sql,
              "ORDER BY Id ",
              pagesize,
              pageindex,
              true,
              out recordCount);
            var obj = new
            {
                rows = dt,
                total = recordCount
            };
            return Content(obj.ToJson());

        }

        /// <summary>
        /// 获取链接
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        [Authority]
        public ActionResult GetLink()
        {
            int id = RequestString.GetInt("id", 0);

            Model.T_Web_Link baseModel = linkBLL.GetModel(id);
            if (baseModel != null)
                return Success("加载成功", baseModel);
            else
                return Error("加载失败");

        }

        /// <summary>
        /// 添加/编辑链接
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Authority]
        public ActionResult AddLink()
        {
            int id = RequestString.GetInt("id", 0);
            string name = RequestString.GetFormString("name");
            string url = RequestString.GetFormString("url");
            Model.T_Web_Link orderModel = new Model.T_Web_Link();
            if (id == 0)
            {
                var list = linkBLL.GetModelList("LinkName='" + name + "' ");
                if (list.Count > 0)
                {
                    return Error("此链接名称已经存在");
                }
                else
                {
                    orderModel.LinkName = name;
                    orderModel.LinkUrl = url;
                    if (linkBLL.Add(orderModel) > 0)
                        return Success("链接添加成功");
                    else
                        return Error("链接添加失败");
                }
            }
            else
            {
                orderModel = linkBLL.GetModel(id);
                if (orderModel != null)
                {
                    var list = linkBLL.GetModelList("LinkName='" + name + "' and Id!='" + id + "' ");
                    if (list.Count > 0)
                    {
                        return Error("此链接名称已经存在");
                    }
                    else
                    {
                        orderModel.LinkName = name;
                        orderModel.LinkUrl = url;

                        if (linkBLL.Update(orderModel))
                            return Success("链接修改成功");
                        else
                            return Error("链接修改失败");
                    }
                }
                else
                {
                    return Success("链接修改成功");
                }
            }

        }

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

        }
        #endregion

        #region 新闻

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        [Authority]
        public ActionResult UploadImage()
        {
            string path = string.Empty;
            //HttpPostedFile _upfile = RequestString.GetFile("upFile");
            string dataurl = HttpUtility.UrlDecode(RequestString.GetFormString("dataurl"));
            if (!string.IsNullOrEmpty(dataurl))
            {
                path = "/Upload/News/" + DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("dd") + "/";
                ImageUpload iu = new ImageUpload();
                iu.SavePath = path;
                iu.DataUrl = dataurl;
                iu.Upload64();

                var configfj = new BLL.T_Sys_SystemConfig().GetModelList(" F_ParamCode='FileUrlPath' ").FirstOrDefault();

                int n = iu.Error;
                if (n == 0)
                {
                    var obj = new
                    {
                        ym = configfj != null ? configfj.F_ParamValue : "",
                        imgurl = path + iu.OutFileName,
                        smallimgurl = path + iu.OutThumbFileName
                    };

                    return Success("成功", obj);
                }
                else
                {
                    string msg = string.Empty;
                    switch (n)
                    {
                        case 1: msg = "请选择要上传的文件"; break;
                        case 2: msg = "上传的文件类型不支持"; break;
                        case 3: msg = "上传的文件过大"; break;
                        case 4: msg = "未知错误"; break;
                    }
                    return Error(msg);
                }
            }
            else
            {
                return Error("请选择要上传的文件");
            }

        }

        /// <summary>
        /// 获取新闻列表
        /// </summary>
        /// <returns></returns>
        [Authority]
        public ActionResult GetNewsList()
        {
            int type = RequestString.GetInt("type", 0);
            string name = RequestString.GetQueryString("name");
            int iszd = RequestString.GetInt("iszd", -1);
            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;

            var sql = " and 1=1 ";
            if (type != 0)
            {
                sql += " and ClassId = '" + type + "'";
            }
            if (!string.IsNullOrEmpty(name))
            {
                sql += " and bt like '%" + name + "%'";
            }
            if (iszd != -1)
            {
                sql += " and isnull(iszd,0) = '" + iszd + "'";
            }
            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }

            var recordCount = 0;
            var dt = BLL.PagerBLL.GetListPager(
              "T_Web_News",
              "id",
              "*",
              sql,
              "ORDER BY ordernum asc,sj desc ",
              pagesize,
              pageindex,
              true,
              out recordCount);

            var configfj = new BLL.T_Sys_SystemConfig().GetModelList(" F_ParamCode='FileUrlPath' ").FirstOrDefault();

            dt.Columns.Add("UserCode", typeof(string));
            foreach (DataRow dr in dt.Rows)
            {
                string id = (dr["zxbh"] == null ? "" : dr["zxbh"].ToString());
                if (id != "")
                {
                    var u = new BLL.T_Sys_UserAccount().GetModel(Int32.Parse(id));
                    if (u != null)
                    {
                        dr["UserCode"] = u.F_UserCode;
                    }
                }
                string url = (dr["img"] == null ? "" : dr["img"].ToString());
                string smallurl = (dr["urlname"] == null ? "" : dr["urlname"].ToString());
                if (!string.IsNullOrEmpty(url) && configfj != null)
                {
                    dr["img"] = configfj.F_ParamValue + url;
                }
                if (!string.IsNullOrEmpty(smallurl) && configfj != null)
                {
                    dr["urlname"] = configfj.F_ParamValue + smallurl;
                }
            }

            var obj = new
            {
                rows = dt,
                total = recordCount
            };
            return Content(obj.ToJson());

        }

        /// <summary>
        /// 获取新闻
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        [Authority]
        public ActionResult GetNews()
        {
            int id = RequestString.GetInt("id", 0);

            Model.T_Web_News baseModel = newsBLL.GetModel(id);
            if (baseModel != null)
            {
                string ym = "";
                var configfj = new BLL.T_Sys_SystemConfig().GetModelList(" F_ParamCode='FileUrlPath' ").FirstOrDefault();
                if (configfj != null)
                {
                    ym = configfj.F_ParamValue;
                }

                var obj = new
                {
                    bt = baseModel.bt,
                    ClassId = baseModel.ClassId,
                    id = baseModel.id,
                    img = baseModel.img,
                    iszd = baseModel.iszd,
                    nr = baseModel.nr,
                    ordernum = baseModel.ordernum,
                    sj = baseModel.sj,
                    urlname = baseModel.urlname,
                    zxbh = baseModel.zxbh,
                    ym = ym,
                    source = baseModel.source
                };
                return Success("加载成功", obj);
            }
            else
            {
                return Error("加载失败");
            }

        }

        /// <summary>
        /// 添加/编辑新闻
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Authority]
        public ActionResult AddNews()
        {
            int userId = Utils.StrToInt(User.UserData["F_UserID"], 0);
            Model.T_Sys_UserAccount userModel = new BLL.T_Sys_UserAccount().GetModel(userId);
            int id = RequestString.GetInt("id", 0);
            int type = RequestString.GetInt("type", 0);
            int sort = RequestString.GetInt("sort", 0);
            int iszd = RequestString.GetInt("iszd", 0);
            string title = RequestString.GetFormString("title");
            string cont = WebHelper.UrlDecode(RequestString.GetFormString("cont"));
            string img = RequestString.GetFormString("img");
            string smallimg = RequestString.GetFormString("smallimg");
            string source = RequestString.GetFormString("source");
            Model.T_Web_News orderModel = new Model.T_Web_News();
            if (id == 0)
            {
                orderModel.ClassId = type;
                orderModel.bt = title;
                orderModel.img = img;
                orderModel.urlname = smallimg;
                orderModel.nr = cont;
                orderModel.ordernum = sort;
                orderModel.iszd = iszd;
                orderModel.zxbh = userModel.F_UserId;
                orderModel.source = source;
                if (newsBLL.Add(orderModel) > 0)
                {
                    if (iszd > 0)
                        //CacheHelper.Remove($"ImgNewsList{type}1");
                        RedisHelper.KeyDelete($"ImgNewsList{type}1");
                    else
                        //CacheHelper.Remove($"NewsList{type}");
                        RedisHelper.KeyDelete($"NewsList{type}");
                    return Success("新闻添加成功");
                }
                else
                    return Error("新闻添加失败");

            }
            else
            {
                orderModel = newsBLL.GetModel(id);
                if (orderModel != null)
                {
                    orderModel.ClassId = type;
                    orderModel.bt = title;
                    orderModel.img = img;
                    orderModel.urlname = smallimg;
                    orderModel.nr = cont;
                    orderModel.ordernum = sort;
                    orderModel.iszd = iszd;
                    orderModel.source = source;
                    if (newsBLL.Update(orderModel))
                    {
                        if (iszd > 0)
                            //CacheHelper.Remove($"ImgNewsList{type}1");
                            RedisHelper.KeyDelete($"ImgNewsList{type}1");
                        else
                            //CacheHelper.Remove($"NewsList{type}");
                            RedisHelper.KeyDelete($"NewsList{type}");
                        //CacheHelper.Remove($"WebNews{orderModel.id}");
                        RedisHelper.KeyDelete($"WebNews{orderModel.id}");
                        return Success("新闻修改成功");
                    }
                    else
                        return Error("新闻修改失败");

                }
                else
                {
                    return Error("新闻修改失败");
                }
            }
        }

        /// <summary>
        /// 删除新闻
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [Authority]
        public ActionResult DelNews(string[] ids)
        {
            if (ids == null || ids.Length <= 0)
                return Error("获取参数失败");
            StringBuilder sb = new StringBuilder();
            foreach (var item in ids)
            {
                sb.Append("'" + item + "',");
            }
            if (newsBLL.DeleteList(sb.ToString().Trim(',')))
            {
                foreach (var id in ids)
                {
                    //CacheHelper.Remove($"WebNews{id}");
                    RedisHelper.KeyDelete($"WebNews{id}");
                }

                //System.Web.Caching.Cache cache = HttpRuntime.Cache;
                //IDictionaryEnumerator cacheEnum = cache.GetEnumerator();
                //List<string> keys = new List<string>();
                //while (cacheEnum.MoveNext())
                //{
                //    keys.Add(cacheEnum.Key.ToString());
                //}

                //var query = keys.Where(x => x.Contains("ImgNewsList") || x.Contains("NewsList")).ToList();
                //foreach (var key in query)
                //{
                //    CacheHelper.Remove(key);
                //}
                RedisHelper.KeysDelete(RedisHelper.GetKeyList("ImgNewsList"));
                RedisHelper.KeysDelete(RedisHelper.GetKeyList("NewsList"));
                return Success("删除成功");
            }
            else
                return Error("删除失败");

        }
        #endregion

        #region 发布
        /// <summary>
        /// 获取发布工单列表
        /// </summary>
        /// <returns></returns>
        [Authority]
        public ActionResult GetReleaseList()
        {
            string title = RequestString.GetQueryString("title");
            string cont = RequestString.GetQueryString("cont");
            string workorderid = RequestString.GetQueryString("workorderid");
            string starttime = HttpUtility.UrlDecode(RequestString.GetQueryString("starttime"));
            string endtime = HttpUtility.UrlDecode(RequestString.GetQueryString("endtime"));
            int efftstate = RequestString.GetInt("efftstate", 0);
            string starttime1 = HttpUtility.UrlDecode(RequestString.GetQueryString("starttime1"));
            string endtime1 = HttpUtility.UrlDecode(RequestString.GetQueryString("endtime1"));
            int state = RequestString.GetInt("state", 0);

            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;

            var sql = " and 1=1 ";
            if (!string.IsNullOrEmpty(title))
            {
                sql += " and F_Title like '%" + title + "%' ";
            }
            if (!string.IsNullOrEmpty(cont))
            {
                sql += " and F_Content like '%" + cont + "%' ";
            }
            if (!string.IsNullOrEmpty(workorderid))
            {
                sql += " and F_WorkOrderId like '%" + workorderid + "%' ";
            }
            if (!string.IsNullOrEmpty(starttime))
            {
                sql += " and datediff(d,F_AcceptTime,'" + starttime + "')<=0 ";
            }
            if (!string.IsNullOrEmpty(endtime))
            {
                sql += " and datediff(d,F_AcceptTime,'" + endtime + "')>=0 ";
            }
            if (efftstate != 0)
            {
                sql += " and F_EffectiveState=" + efftstate;
                if (efftstate == 2)
                {
                    if (!string.IsNullOrEmpty(starttime1))
                    {
                        sql += " and datediff(d,F_EffectiveTime,'" + starttime1 + "')<=0 ";
                    }
                    if (!string.IsNullOrEmpty(endtime1))
                    {
                        sql += " and datediff(d,F_EffectiveTime,'" + endtime1 + "'>=0 ";
                    }
                }
            }
            if (state != 0)
            {
                sql += " and F_ReleaseState=" + state;
            }
            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }

            var recordCount = 0;
            var dt = BLL.PagerBLL.GetListPager(
              "T_Web_WorkRelease",
              "F_WorkOrderId",
              "*,dbo.GetDeptName(F_Department) as F_DeptName,dbo.GetDictionaryName(F_WorkSort) as F_Value",
              sql,
              "order by F_WorkOrderId desc",
              pagesize,
              pageindex,
              true,
              out recordCount);
            var obj = new
            {
                rows = dt,
                total = recordCount
            };
            return Content(obj.ToJson());

        }

        /// <summary>
        /// 获取发布工单
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        [Authority]
        public ActionResult GetRelease()
        {
            string workorderid = RequestString.GetQueryString("workorderid");

            Model.T_Web_WorkRelease baseModel = fbBLL.GetModel(workorderid);
            if (baseModel != null)
                return Success("加载成功", baseModel);
            else
                return Error("加载失败");

        }

        /// <summary>
        /// 发布
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Authority]
        public ActionResult EditRelease()
        {
            string workorderid = RequestString.GetFormString("workorderid");
            int state = RequestString.GetInt("state", 0);
            int smalltype = RequestString.GetInt("smalltype", 0);
            int deptid = RequestString.GetInt("deptid", 0);
            string acceptime = RequestString.GetFormString("acceptime");
            string backtime = RequestString.GetFormString("backtime");
            string result = RequestString.GetFormString("result");
            string fbtime = RequestString.GetFormString("fbtime");
            string opinion = RequestString.GetFormString("opinion");

            Model.T_Web_WorkRelease orderModel = new Model.T_Web_WorkRelease();
            orderModel = fbBLL.GetModel(workorderid);

            if (orderModel != null)
            {
                orderModel.F_ReleaseState = state;
                if (!string.IsNullOrEmpty(fbtime))
                {
                    orderModel.F_ReleaseTime = DateTime.Parse(fbtime);
                }
                orderModel.F_Department = deptid;
                orderModel.F_WorkSort = smalltype;
                if (!string.IsNullOrEmpty(acceptime))
                {
                    orderModel.F_AcceptTime = DateTime.Parse(acceptime);
                }
                if (!string.IsNullOrEmpty(backtime))
                {
                    orderModel.F_BackDate = DateTime.Parse(backtime);
                }
                orderModel.F_Result = result;
                orderModel.F_Opinion = opinion;

                if (fbBLL.Update(orderModel))
                    return Success("成功");
                else
                    return Error("失败");

            }
            else
            {
                return Success("成功");
            }

        }

        /// <summary>
        /// 更新状态
        /// </summary>
        /// <returns></returns>
        [Authority]
        public ActionResult UpdateReleaseState()
        {
            string workid = RequestString.GetFormString("workid");
            int ste = RequestString.GetInt("state", 0);

            var model = new BLL.T_Web_WorkRelease().GetModel(workid);

            if (model != null && ste != 0)
            {
                model.F_ReleaseState = ste;
                if (new BLL.T_Web_WorkRelease().Update(model))
                {
                    return Success("成功");
                }
                else
                {
                    return Error("操作失败");
                }
            }
            else
            {
                return Error("操作失败");
            }
        }

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

        }
        #endregion

        #region 官网

        /// <summary>
        /// 获取最新新闻列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetTopImgNewsList()
        {
            int type = RequestString.GetInt("type", 0);
            int num = RequestString.GetInt("num", 6);
            //var dtCache = CacheHelper.Get<DataTable>($"ImgNewsList{type}1");
            var dtCache = RedisHelper.StringGet($"TopImgNewsList_{type}_{num}");
            if (dtCache != null)
            {
                return Success("成功", dtCache.ToString().ToObject<DataTable>());
            }
            var sql = " select top " + num + " * from T_Web_News where iszd=1 ";
            Dictionary<string, string> paras = new Dictionary<string, string>();
            if (type != 0)
            {
                sql += " and ClassId = @ClassId";
                paras.Add("@ClassId", type.ToString());
            }

            sql += " order by ordernum asc,sj desc";

            var dt = DbHelperSQL.Query(sql, paras).Tables[0];

            var configfj = new BLL.T_Sys_SystemConfig().GetModelList(" F_ParamCode='FileUrlPath' ").FirstOrDefault();

            foreach (DataRow dr in dt.Rows)
            {
                string url = (dr["img"] == null ? "" : dr["img"].ToString());
                string smallurl = (dr["urlname"] == null ? "" : dr["urlname"].ToString());
                if (!string.IsNullOrEmpty(url) && configfj != null)
                {
                    dr["img"] = configfj.F_ParamValue + url;
                }
                if (!string.IsNullOrEmpty(smallurl) && configfj != null)
                {
                    dr["urlname"] = configfj.F_ParamValue + smallurl;
                }
            }
            //// key=ImgNewsList+type+iszd
            //CacheHelper.Insert($"ImgNewsList{type}1", dt, 10);
            RedisHelper.StringSet($"TopImgNewsList_{type}_{num}", dt.ToJson(), new TimeSpan(0, 10, 0));
            return Success("成功", dt);
        }

        /// <summary>
        /// 获取最新新闻列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetTopNewsList()
        {
            int type = RequestString.GetInt("type", 0);
            int num = RequestString.GetInt("num", 8);
            //var dtCache = CacheHelper.Get<DataTable>($"NewsList{type}");
            var dtCache = RedisHelper.StringGet($"TopNewsList_{type}_{num}");
            if (dtCache != null)
            {
                return Success("成功", dtCache.ToString().ToObject<DataTable>());
            }
            var sql = " select top " + num + " * from T_Web_News where 1=1 ";
            Dictionary<string, string> paras = new Dictionary<string, string>();
            if (type != 0)
            {
                sql += " and ClassId = @ClassId";
                paras.Add("@ClassId", type.ToString());
            }

            sql += " order by ordernum asc,sj desc";

            var dt = DbHelperSQL.Query(sql, paras).Tables[0];

            var configfj = new BLL.T_Sys_SystemConfig().GetModelList(" F_ParamCode='FileUrlPath' ").FirstOrDefault();

            foreach (DataRow dr in dt.Rows)
            {
                string url = (dr["img"] == null ? "" : dr["img"].ToString());
                string smallurl = (dr["urlname"] == null ? "" : dr["urlname"].ToString());
                if (!string.IsNullOrEmpty(url) && configfj != null)
                {
                    dr["img"] = configfj.F_ParamValue + url;
                }
                if (!string.IsNullOrEmpty(smallurl) && configfj != null)
                {
                    dr["urlname"] = configfj.F_ParamValue + smallurl;
                }
            }
            //// key=NewsList+type
            //CacheHelper.Insert($"NewsList{type}", dt, 10);
            RedisHelper.StringSet($"TopNewsList_{type}_{num}",dt.ToJson(), new TimeSpan(0, 10, 0));
            return Success("成功", dt);
        }

        /// <summary>
        /// 获取新闻
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public ActionResult GetWebNews()
        {

            int id = RequestString.GetInt("id", 0);

            //var newsCache = CacheHelper.Get<Model.T_Web_News>($"WebNews{id}");
            var newsCache = RedisHelper.StringGet($"WebNews{id}");
            if (newsCache != null)
            {
                return Success("成功", newsCache.ToString().ToObject<Model.T_Web_News>());
            }
            var configfj = new BLL.T_Sys_SystemConfig().GetModelList(" F_ParamCode='FileUrlPath' ").FirstOrDefault();
            var configfjedit = new BLL.T_Sys_SystemConfig().GetModelList(" F_ParamCode='EditFilePath' ").FirstOrDefault();
            Model.T_Web_News baseModel = newsBLL.GetModel(id);
            if (baseModel != null)
            {
                if (!string.IsNullOrEmpty(baseModel.img) && configfj != null)
                {
                    baseModel.img = configfj.F_ParamValue + baseModel.img;
                }
                if (!string.IsNullOrEmpty(baseModel.urlname) && configfj != null)
                {
                    baseModel.urlname = configfj.F_ParamValue + baseModel.urlname;
                }
                if (!string.IsNullOrEmpty(baseModel.nr) && configfjedit != null)
                {
                    Uri uri = new Uri(configfjedit.F_ParamValue);

                    baseModel.nr = baseModel.nr.Replace("\"/js/kindeditor", "\"" + uri.Scheme + "://" + uri.Authority + "/js/kindeditor");
                }
                //// key=action+id
                //CacheHelper.Insert($"WebNews{id}", baseModel, 10);
                RedisHelper.StringSet($"WebNews{id}",baseModel.ToJson(), new TimeSpan(0, 10, 0));
                return Success("加载成功", baseModel);
            }
            else
            {
                return Error("加载失败");
            }
        }

        /// <summary>
        /// 获取新闻列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetWebNewsList()
        {
            int type = RequestString.GetInt("type", 0);
            string name = RequestString.GetQueryString("name");
            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;
            if (strpageindex.Trim() != "")
                pageindex = Convert.ToInt32(strpageindex);
            if (strpagesize.Trim() != "")
                pagesize = Convert.ToInt32(strpagesize);

            var dtCache = RedisHelper.StringGet($"WebNewsList_{type}_{name}_{pageindex}_{pagesize}");
            if (dtCache != null)
            {
                return Content(dtCache.ToString());
            }

            var sql = " and 1=1 ";
            if (type != 0)
            {
                sql += " and ClassId = '" + type + "'";
            }
            if (!string.IsNullOrEmpty(name))
            {
                sql += " and bt like '%" + name + "%'";
            }

            var recordCount = 0;
            var dt = BLL.PagerBLL.GetListPager(
              "T_Web_News",
              "id",
              "*",
              sql,
              "ORDER BY ordernum asc,sj desc",
              pagesize,
              pageindex,
              true,
              out recordCount);

            var configfj = new BLL.T_Sys_SystemConfig().GetModelList(" F_ParamCode='FileUrlPath' ").FirstOrDefault();

            dt.Columns.Add("UserCode", typeof(string));
            foreach (DataRow dr in dt.Rows)
            {
                string id = (dr["zxbh"] == null ? "" : dr["zxbh"].ToString());
                if (id != "")
                {
                    var u = new BLL.T_Sys_UserAccount().GetModel(Int32.Parse(id));
                    if (u != null)
                    {
                        dr["UserCode"] = u.F_UserCode;
                    }
                }
                string url = (dr["img"] == null ? "" : dr["img"].ToString());
                string smallurl = (dr["urlname"] == null ? "" : dr["urlname"].ToString());
                if (!string.IsNullOrEmpty(url) && configfj != null)
                {
                    dr["img"] = configfj.F_ParamValue + url;
                }
                if (!string.IsNullOrEmpty(smallurl) && configfj != null)
                {
                    dr["urlname"] = configfj.F_ParamValue + smallurl;
                }
            }

            var obj = new
            {
                state = ResultTypes.success.ToString(),
                rows = dt,
                total = recordCount
            };

            RedisHelper.StringSet($"WebNewsList_{type}_{name}_{pageindex}_{pagesize}", obj.ToJson(), new TimeSpan(0, 10, 0));

            return Content(obj.ToJson());
        }

        /// <summary>
        /// 获取新闻列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAllWebNews()
        {

            int type = RequestString.GetInt("type", 0);
            string name = RequestString.GetQueryString("name");

            var dtCache = RedisHelper.StringGet($"AllWebNews_{type}_{name}");
            if (dtCache != null)
            {
                return Success("成功", dtCache.ToString().ToObject<DataTable>());
            }

            var sql = " 1=1 ";
            if (type != 0)
            {
                sql += " and ClassId = '" + type + "'";
            }
            if (!string.IsNullOrEmpty(name))
            {
                sql += " and bt like '%" + name + "%'";
            }
            var dt = new BLL.T_Web_News().GetList(0, sql, " ordernum asc,sj desc ").Tables[0];

            var configfj = new BLL.T_Sys_SystemConfig().GetModelList(" F_ParamCode='FileUrlPath' ").FirstOrDefault();

            dt.Columns.Add("UserCode", typeof(string));
            foreach (DataRow dr in dt.Rows)
            {
                string id = (dr["zxbh"] == null ? "" : dr["zxbh"].ToString());
                if (id != "")
                {
                    var u = new BLL.T_Sys_UserAccount().GetModel(Int32.Parse(id));
                    if (u != null)
                    {
                        dr["UserCode"] = u.F_UserCode;
                    }
                }
                string url = (dr["img"] == null ? "" : dr["img"].ToString());
                string smallurl = (dr["urlname"] == null ? "" : dr["urlname"].ToString());
                if (!string.IsNullOrEmpty(url) && configfj != null)
                {
                    dr["img"] = configfj.F_ParamValue + url;
                }
                if (!string.IsNullOrEmpty(smallurl) && configfj != null)
                {
                    dr["urlname"] = configfj.F_ParamValue + smallurl;
                }
            }
            RedisHelper.StringSet($"AllWebNews_{type}_{name}", dt.ToJson(), new TimeSpan(0, 10, 0));
            return Success("成功",dt);
        }

        /// <summary>
        /// 获取最新发布列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetTopReleaseList()
        {
            int num = RequestString.GetInt("num", 8);

            var dtCache = RedisHelper.StringGet($"TopReleaseList_{num}");
            if (dtCache != null)
            {
                return Success("成功", dtCache.ToString().ToObject<DataTable>());
            }

            var sql = " select top " + num + " *,dbo.GetDictionaryName(F_InfoType) as TypeName,dbo.GetDictionaryName(F_InfoSource) as SourceName from T_Bus_WorkOrder where isnull(F_IsRelease,0) = 1 and F_IsDelete=0 ";

            sql += " order by F_CreateTime desc";

            var dt = DbHelperSQL.Query(sql).Tables[0];
            RedisHelper.StringSet($"TopReleaseList_{num}", dt.ToJson(), new TimeSpan(0, 10, 0));
            return Success("成功", dt);
        }

        /// <summary>
        /// 获取发布工单列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetWebReleaseList()
        {
            string title = RequestString.GetQueryString("title");
            string cont = RequestString.GetQueryString("cont");
            string workorderid = RequestString.GetQueryString("workorderid");
            string starttime = HttpUtility.UrlDecode(RequestString.GetQueryString("starttime"));
            string endtime = HttpUtility.UrlDecode(RequestString.GetQueryString("endtime"));
            int type = RequestString.GetInt("type", 0);
            int deptid = RequestString.GetInt("deptid", 0);
            int keyid = RequestString.GetInt("keyid", 0);

            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;
            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }

            var dtCache = RedisHelper.StringGet($"WebReleaseList_{title}_{cont}_{workorderid}_{starttime}_{endtime}_{type}_{deptid}_{keyid}_{pageindex}_{pagesize}");
            if (dtCache != null)
            {
                return Content(dtCache.ToString());
            }

            var sql = " and isnull(F_IsRelease,0) = 1 and F_IsDelete=0 ";
            if (!string.IsNullOrEmpty(title))
            {
                sql += " and F_Title like '%" + title + "%' ";
            }
            if (!string.IsNullOrEmpty(cont))
            {
                sql += " and F_Content like '%" + cont + "%' ";
            }
            if (!string.IsNullOrEmpty(workorderid))
            {
                sql += " and F_WorkOrderId like '%" + workorderid + "%' ";
            }
            if (!string.IsNullOrEmpty(starttime))
            {
                sql += " and datediff(d,F_CreateTime,'" + starttime + "')<=0 ";
            }
            if (!string.IsNullOrEmpty(endtime))
            {
                sql += " and datediff(d,F_CreateTime,'" + endtime + "')>=0 ";
            }
            if (type != 0)
            {
                sql += " and F_InfoType = '" + type + "' ";
            }
            if (deptid != 0)
            {
                sql += " and F_Department=" + deptid;
            }
            if (keyid != 0)
            {
                sql += " and ','+F_Key+',' like '%," + keyid + ",%' ";
            }
            if (deptid != 0)
            {
                string sqlwhere = "select F_WorkOrderID from T_Bus_AssignedInfo where ','+F_MainDeptId+',' like '%," + deptid + ",%' and F_IsSure in (0,1) and F_State=1 and F_IsDelete=0 ";
                //string sqlwhere = "select F_WorkOrderID from T_Bus_AssignedInfo where F_MainDeptId = '" + deptid + "' and F_IsSure in (0,1) and F_State=1 and F_IsDelete=0 ";
                sql += " and F_WorkOrderID in(" + sqlwhere + ")";
            }

            var recordCount = 0;
            string cols = "*,dbo.GetUserName(F_CreateUser) as UserName,dbo.GetDictionaryName(F_InfoType) as TypeName,dbo.GetOverState(F_WorkOrderId) as OverState,"
                + "dbo.GetDictionaryName(F_InfoSource) as SourceName,dbo.GetWorkOrderAssign(F_WorkOrderId) as DeptName ,dbo.GetLimitTime(F_WorkOrderId) as LimitTime";

            var dt = BLL.PagerBLL.GetListPager(
                "T_Bus_WorkOrder",
                "F_WorkOrderId",
                cols,
                sql,
                "ORDER BY F_CreateTime DESC",
                pagesize,
                pageindex,
                true,
                out recordCount);

            var obj = new
            {
                state = ResultTypes.success.ToString(),
                rows = dt,
                total = recordCount
            };
            RedisHelper.StringSet($"WebReleaseList_{title}_{cont}_{workorderid}_{starttime}_{endtime}_{type}_{deptid}_{keyid}_{pageindex}_{pagesize}", obj.ToJson(), new TimeSpan(0, 10, 0));
            return Content(obj.ToJson());
        }

        /// <summary>
        /// 获取发布工单详情
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public ActionResult GetWebRelease()
        {
            string workorderid = RequestString.GetQueryString("workorderid");

            var dtCache = RedisHelper.StringGet($"WebRelease{workorderid}");
            if (dtCache != null)
            {
                return Success("成功", dtCache.ToString().ToJObject());
            }

            Model.T_Web_WorkRelease baseModel = fbBLL.GetModel(workorderid);
            if (baseModel != null)
            {
                string where = " F_WorkOrderId=@F_WorkOrderId";
                Dictionary<string, string> paras = new Dictionary<string, string>();
                paras.Add("@F_WorkOrderId", workorderid);
                DbHelperSQL.ExecuteSql("update T_Web_WorkRelease set F_ViewCount=F_ViewCount+1 where " + where, paras);
                var obj = new
                {
                    data = baseModel,
                    deptname = DbHelperSQL.GetSingle(" select dbo.GetDeptName(" + baseModel.F_Department + ")"),
                    classname = DbHelperSQL.GetSingle(" select dbo.GetDictionaryName(" + baseModel.F_WorkSort + ")")
                };
                RedisHelper.StringSet($"WebRelease{workorderid}", obj.ToJson(), new TimeSpan(0, 10, 0));
                return Success("加载成功", obj);
            }
            else
            {
                return Success("加载失败");
            }

        }

        /// <summary>
        /// 获取链接列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetWebLinkList()
        {
            string name = RequestString.GetQueryString("name");

            var dtCache = RedisHelper.StringGet($"WebLinkList{name}");
            if (dtCache != null)
            {
                return Success("成功", dtCache.ToString().ToObject<DataTable>());
            }

            string where = " select * from T_Web_Link where 1=1 ";
            Dictionary<string, string> paras = new Dictionary<string, string>();
            if (!string.IsNullOrEmpty(name))
            {
                where += " and LinkName like @LinkName";
                paras.Add("@LinkName", "%" + name + "%");
            }

            var dt = DbHelperSQL.Query(where, paras).Tables[0];

            RedisHelper.StringSet($"WebLinkList{name}", dt.ToJson(), new TimeSpan(0, 10, 0));
            return Success("列表加载成功", dt);
        }

        /// <summary>
        /// 获取工单类型列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetWebTypeList()
        {
            var dtCache = RedisHelper.StringGet($"WebTypeList");
            if (dtCache != null)
            {
                return Success("成功", dtCache.ToString().ToObject<DataTable>());
            }


            string sql = "select * from T_Sys_DictionaryValue where F_ItemId=2 and F_State=0 ";
            var dt = DbHelperSQL.Query(sql).Tables[0];

            RedisHelper.StringSet($"WebTypeList", dt.ToJson(), new TimeSpan(0, 10, 0));
            return Success("列表加载成功", dt);
        }

        /// <summary>
        /// 获取事发地域列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetWebAreaList()
        {
            var dtCache = RedisHelper.StringGet($"WebAreaList");
            if (dtCache != null)
            {
                return Success("成功", dtCache.ToString().ToObject<DataTable>());
            }

            string sql = "select * from T_Sys_Area where isnull(F_PrentId,0)=0 and F_State=0 ";
            var dt = DbHelperSQL.Query(sql).Tables[0];

            RedisHelper.StringSet($"WebAreaList", dt.ToJson(), new TimeSpan(0, 10, 0));

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

        /// <summary>
        /// 获取事发地域列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult GetWebAreaListById(int id = 0)
        {
            var dtCache = RedisHelper.StringGet($"WebAreaListById{id}");
            if (dtCache != null)
            {
                return Success("成功", dtCache.ToString().ToObject<DataTable>());
            }

            Dictionary<string, string> paras = new Dictionary<string, string>();
            paras.Add("@Id", id.ToString());
            DataTable dt = DbHelperSQL.Query(" select * from T_Sys_Area where isnull(F_PrentId,0)=@id and F_State=0 ", paras).Tables[0];

            RedisHelper.StringSet($"WebAreaListById{id}", dt.ToJson(), new TimeSpan(0, 10, 0));
            return Success("列表加载成功", dt);
        }

        /// <summary>
        /// 获取工单类型列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetWebKeyList()
        {
            var dtCache = RedisHelper.StringGet($"WebKeyList");
            if (dtCache != null)
            {
                return Success("成功", dtCache.ToString().ToObject<DataTable>());
            }

            string sql = "select * from T_Sys_DictionaryValue where F_ItemId=3 and F_State=0 ";
            var dt = DbHelperSQL.Query(sql).Tables[0];

            RedisHelper.StringSet($"WebKeyList", dt.ToJson(), new TimeSpan(0, 10, 0));
            return Success("列表加载成功", dt);
        }

        /// <summary>
        /// 获取部门列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetWebDeptList()
        {
            var dtCache = RedisHelper.StringGet($"WebDeptList");
            if (dtCache != null)
            {
                return Success("成功", dtCache.ToString().ToObject<DataTable>());
            }

            DataTable dt = new DataTable();
            dt = new BLL.T_Sys_Department().GetList(0, "F_State=0", " F_Sort").Tables[0];
            RedisHelper.StringSet($"WebDeptList", dt.ToJson(), new TimeSpan(0, 10, 0));
            return Success("加载成功", dt);
        }

        /// <summary>
        /// 上传附件
        /// </summary>
        /// <returns></returns>
        public ActionResult UploadFile()
        {

            #region no use
            //string path = string.Empty;
            //HttpPostedFile _upfile = RequestString.GetFile("upFile");
            //if (_upfile != null)
            //{
            //    //byte[] buffer = new Byte[(int)_upfile.InputStream.Length]; //声明文件长度的二进制类型
            //    //_upfile.InputStream.Read(buffer, 0, buffer.Length); //将文件转成二进制
            //    string name = string.Empty;
            //    FileUp fu = new FileUp();
            //    var model = new BLL.T_Sys_SystemConfig().GetModelList(" F_ParamCode='FilePath' ").FirstOrDefault();
            //    if (model != null)
            //    {
            //        name = fu.Upload(_upfile, model.F_ParamValue);
            //        return Success("成功", name);
            //    }
            //    else
            //    {
            //        return Error("上传失败");
            //    }

            //}
            //else
            //{
            //    return Error("请选择要上传的文件");
            //} 
            #endregion

            #region 多个上传
            HttpFileCollection files = RequestString.GetFiles();
            if (files.Count > 0)
            {
                List<Model.T_Sys_Accessories> acs = new List<Model.T_Sys_Accessories>();
                string path = "/Upload/Files/" + DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("dd") + "/";
                for (int i = 0; i < files.Count; i++)
                {
                    HttpPostedFile file = files[i];
                    FileUp fu = new FileUp();
                    string fileExtension = Configs.GetValue("fileExtension");
                    if (!fileExtension.Contains((System.IO.Path.GetExtension(file.FileName).ToLower())))
                    {
                        return Error("附件格式不正确");
                    }
                    string name = fu.Upload(file, path);

                    Model.T_Sys_Accessories model_T_Sys_Accessories = new Model.T_Sys_Accessories();
                    model_T_Sys_Accessories.F_AddTime = DateTime.Now;//上传时间
                    model_T_Sys_Accessories.F_FileName = name;//附件名称
                    model_T_Sys_Accessories.F_FileType = System.IO.Path.GetExtension(file.FileName);//附件类型
                    model_T_Sys_Accessories.F_FileUrl = path + name;//附件地址
                    model_T_Sys_Accessories.F_Size = file.ContentLength;
                    //model_T_Sys_Accessories.F_UserCode = userModel.F_UserCode;//上传人  
                    int id = new BLL.T_Sys_Accessories().Add(model_T_Sys_Accessories);
                    model_T_Sys_Accessories.F_FileId = id;

                    acs.Add(model_T_Sys_Accessories);
                }

                return Success("成功", acs);
            }
            else
            {
                return Error("请选择要上传的文件");
            }
            #endregion

        }


        /// <summary>
        /// 上传base64图片
        /// </summary>
        /// <returns></returns>
        public ActionResult Upload64()
        {
            //string dataurl = HttpUtility.UrlDecode(RequestString.GetFormString("dataurl"));
            string dataurl = RequestString.GetFormString("dataurl");
            string filename = RequestString.GetFormString("filename");
            if (!string.IsNullOrEmpty(dataurl))
            {
                string path = "/Upload/APP/" + DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("dd") + "/";

                ImageUpload iu = new ImageUpload();
                iu.SavePath = path;
                iu.DataUrl = dataurl;
                if (!string.IsNullOrEmpty(filename))
                {
                    iu.SaveType = 1;
                    iu.InFileName = filename;
                }
                iu.Upload64();

                int n = iu.Error;
                if (n == 0)
                {
                    path = path + iu.OutFileName;

                    Model.T_Sys_Accessories model_T_Sys_Accessories = new Model.T_Sys_Accessories();
                    model_T_Sys_Accessories.F_AddTime = DateTime.Now;//上传时间
                    model_T_Sys_Accessories.F_FileName = iu.OutFileName;//附件名称
                    model_T_Sys_Accessories.F_FileType = ".jpg";//附件类型
                    model_T_Sys_Accessories.F_FileUrl = path;//附件地址
                    model_T_Sys_Accessories.F_Size = iu.FileSize;
                    //model_T_Sys_Accessories.F_UserCode = userModel.F_UserCode;//上传人  
                    int id = new BLL.T_Sys_Accessories().Add(model_T_Sys_Accessories);
                    model_T_Sys_Accessories.F_FileId = id;

                    return Success("成功", model_T_Sys_Accessories);
                }
                else
                {
                    string msg = string.Empty;
                    switch (n)
                    {
                        case 1: msg = "请选择要上传的文件"; break;
                        case 2: msg = "上传的文件类型不支持"; break;
                        case 3: msg = "上传的文件过大"; break;
                        case 4: msg = "未知错误"; break;
                    }
                    return Error(msg);
                }
            }
            else
            {
                return Error("请选择要上传的文件");
            }
        }

        /// <summary>
        /// 添加工单信息(网上受理)
        /// </summary>
        /// <returns></returns>
        public ActionResult AddWebWorkOrder()
        {

            int source = RequestString.GetInt("source", 4);

            string cusname = RequestString.GetFormString("cusname");
            string cussex = RequestString.GetFormString("cussex");
            string cusphone = RequestString.GetFormString("cusphone");
            string cusaddress = RequestString.GetFormString("cusaddress");
            string email = RequestString.GetFormString("email");
            string zipcode = RequestString.GetFormString("zipcode");
            string conname = RequestString.GetFormString("conname");
            string conphone = RequestString.GetFormString("conphone");

            string title = RequestString.GetFormString("title");
            string content = RequestString.GetFormString("content");
            string files = RequestString.GetFormString("files");
            int sourcearea = RequestString.GetInt("sourcearea", 0);
            string sourceaddress = RequestString.GetFormString("sourceaddress");

            string keys = RequestString.GetFormString("keys");
            string splituser = RequestString.GetFormString("splituser");

            int type = RequestString.GetInt("type", 0);
            int bigtype = RequestString.GetInt("bigtype", 0);
            int smalltype = RequestString.GetInt("smalltype", 0);

            int isprotect = RequestString.GetInt("isprotect", 0);
            int level = RequestString.GetInt("level", 0);

            string workid = string.Empty;
            string pwd = string.Empty; ;
            using (TransactionScope trans = new TransactionScope())
            {
                #region 保存工单信息
                WorkOrderController wo = new WorkOrderController();
                string workorderid = wo.AddWorkOrderBySource(source, cusname, cussex, cusphone, cusaddress, email, zipcode, conname, conphone, title,
                    content, sourcearea, sourceaddress, keys, splituser, type, bigtype, smalltype, isprotect, level, files);
                #endregion

                if (source == 4)
                {
                    #region 保存密码
                    BLL.T_Web_WorkAccountSearch WorkAccount = new BLL.T_Web_WorkAccountSearch();
                    Model.T_Web_WorkAccountSearch WorkAccountInfo = new Model.T_Web_WorkAccountSearch();
                    WorkAccountInfo.F_WorkOrderId = workorderid;

                    pwd = Utils.Number(4);
                    WorkAccountInfo.F_SearchPassword = pwd;
                    WorkAccountInfo.F_AddTime = DateTime.Now;
                    WorkAccount.Add(WorkAccountInfo);
                    #endregion
                }
                workid = workorderid;
                trans.Complete();
            }
            var obj = new
            {
                workorderid = workid,
                password = pwd
            };
            return Success("成功", obj);

        }

        /// <summary>
        /// 检查密码
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public ActionResult CheckWebPassword()
        {

            string workorderid = RequestString.GetQueryString("workorderid");
            string password = RequestString.GetQueryString("password");

            string sql = " select * from T_Web_WorkAccountSearch where F_WorkOrderId=@F_WorkOrderId and F_SearchPassword=@F_SearchPassword";
            Dictionary<string, string> paras = new Dictionary<string, string>();
            paras.Add("@F_WorkOrderId", workorderid);
            paras.Add("@F_SearchPassword", password);

            var dt = DbHelperSQL.Query(sql, paras).Tables[0];
            if (dt.Rows.Count > 0)
            {
                string sqlwork = "select top 1 *,dbo.GetUserName(F_CreateUser) as UserName,dbo.GetDictionaryName(F_InfoType) as TypeName1,"
                    //+ "dbo.GetDictionaryName(F_InfoConBigType) as TypeName2,dbo.GetDictionaryName(F_InfoConSmallType) as TypeName3,"
                    + "dbo.GetWorkOrderAssign(F_WorkOrderId) as DeptName,dbo.GetDictionaryName(F_InfoSource) as SourceName ,"
                    + "dbo.GetAreaName(F_SourceArea) as AreaName "
                    + " from T_Bus_WorkOrder where F_WorkOrderId =@F_WorkOrderId ";
                Dictionary<string, string> paraswork = new Dictionary<string, string>();
                paraswork.Add("@F_WorkOrderId", workorderid);
                var dtwork = DbHelperSQL.Query(sqlwork, paraswork).Tables[0];
                if (dtwork.Rows.Count > 0)
                {
                    dtwork.Columns.Add("StateName", typeof(string));
                    if (dtwork.Rows[0]["F_IsEnabled"] != null)
                    {
                        if (dtwork.Rows[0]["F_IsDelete"].ToString() == "1" && dtwork.Rows[0]["F_IsEnabled"].ToString() == "0")
                        {
                            dtwork.Rows[0]["StateName"] = "受理中";
                        }
                        else if (dtwork.Rows[0]["F_WorkState"].ToString() != "9")
                        {
                            dtwork.Rows[0]["StateName"] = "办理中";
                        }
                        else if (dtwork.Rows[0]["F_WorkState"].ToString() == "9")
                        {
                            dtwork.Rows[0]["StateName"] = "已结案";
                        }
                    }
                }

                return Success("加载成功", dtwork);
            }
            else
            {
                return Error("受理编号或密码错误");
            }
        }

        /// <summary>
        /// 获取来源受理数
        /// </summary>
        /// <returns></returns>
        public ActionResult GetWebSourceCount()
        {
            var dtCache = RedisHelper.StringGet($"WebSourceCount");
            if (dtCache != null)
            {
                return Success("成功", dtCache.ToString().ToJObject());
            }

            string sql = " select F_Value Source,(select COUNT(1) from T_Bus_WorkOrder where F_IsDelete=0 "
                + " and  F_InfoSource=F_ValueId) Count from  dbo.T_Sys_DictionaryValue where F_ItemId=1 and F_State=0 ";
            DataTable dt = DbHelperSQL.Query(sql).Tables[0];

            string sqlday = " select F_Value Source,(select COUNT(1) from T_Bus_WorkOrder where datediff(day,F_CreateTime,getdate())=0 and F_IsDelete=0 "
                + " and  F_InfoSource=F_ValueId) Count from  dbo.T_Sys_DictionaryValue where F_ItemId=1 and F_State=0 ";
            DataTable dtday = DbHelperSQL.Query(sqlday).Tables[0];

            var obj = new
            {
                total = dt,
                day = dtday,
                dayaccept = DbHelperSQL.GetSingle("select COUNT(1) from T_Bus_WorkOrder where datediff(day,F_CreateTime,getdate())=0 and F_IsDelete=0").ToString(),
                dayassign= DbHelperSQL.GetSingle("select COUNT(1) from T_Bus_WorkOrder where datediff(day,F_CreateTime,getdate())=0 and F_IsDelete=0 and isnull(F_IsResult,0)=0 and F_WorkState>1").ToString()
            };
            RedisHelper.StringSet($"WebSourceCount", obj.ToJson(), new TimeSpan(0, 10, 0));
            return Success("加载成功", obj);
        }

        /// <summary>
        /// 获取类型比例
        /// </summary>
        /// <returns></returns>
        public ActionResult GetWebTypePercent()
        {
            var dtCache = RedisHelper.StringGet($"WebTypePercent");
            if (dtCache != null)
            {
                return Success("成功", dtCache.ToString().ToObject<object>());
            }

            var typelist = new BLL.T_Sys_DictionaryValue().GetModelList(" F_ItemId=2 ");
            string sql = " select count(1) from dbo.T_Bus_WorkOrder where F_IsDelete = 0 ";
            var tl = Int32.Parse(DbHelperSQL.GetSingle(sql).ToString());
            var obj = typelist.Select(p =>
            {
                string sqltype = " select count(1) from dbo.T_Bus_WorkOrder where F_IsDelete = 0 and F_InfoType=" + p.F_ValueId;
                var cl = Int32.Parse(DbHelperSQL.GetSingle(sqltype).ToString());
                return new
                {
                    typename = p.F_Value,
                    percent = tl > 0 ? Math.Round(((double)cl * 100 / tl), 2) : 0
                };
            });
            RedisHelper.StringSet($"WebTypePercent", obj.ToJson(), new TimeSpan(0, 10, 0));
            return Success("加载成功", obj);
        }

        /// <summary>
        /// 获取类型比例
        /// </summary>
        /// <returns></returns>
        public ActionResult GetWebKeyCount(int num = 7,int isscqt = 0)
        {
            var dtCache = RedisHelper.StringGet($"WebKeyCount_{num}_{isscqt}");
            if (dtCache != null)
            {
                return Success("成功", dtCache.ToString().ToObject<object>());
            }

            string strwhere = " F_ItemId=3 ";
            if (isscqt > 0)
            {
                strwhere += " and F_Value != '其他' ";
            }
            var typelist = new BLL.T_Sys_DictionaryValue().GetModelList(strwhere);

            var obj = typelist.Select(p =>
            {
                string sqltype = " select count(1) from dbo.T_Bus_WorkOrder where F_IsDelete = 0 and ','+F_Key+',' like '%," + p.F_ValueId + ",%'";
                var cl = Int32.Parse(DbHelperSQL.GetSingle(sqltype).ToString());
                return new
                {
                    keyname = p.F_Value,
                    count = cl
                };
            }).OrderByDescending(p => p.count).Take(num);

            RedisHelper.StringSet($"WebKeyCount_{num}_{isscqt}", obj.ToJson(), new TimeSpan(0, 10, 0));
            return Success("加载成功", obj);
        }

        #endregion

    }
}