using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using PushService.Act.BLL;
using PushPlatform.Common.Model;
using PushService.Act.Model;
using PushPlatform.Common.Util;
using PushInfo.Act.Models.QueryResults;
using PushService.Act.Model.QueryResults;
using PushInfo.Act.BLL;
using PushInfo.Act.Models.Tables;
using PushPlatform.Common.Enumerate;
using DapperORM.Common.Models;
using PushService.Act.Model.Request;

namespace PushPfService
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码、svc 和配置文件中的类名“Service1”。
    // 注意: 为了启动 WCF 测试客户端以测试此服务，请在解决方案资源管理器中选择 Service1.svc 或 Service1.svc.cs，然后开始调试。
    public class PushPfService : IPushPfService
    {
        private PushLogBLL pLogBll = new PushLogBLL();
        private PushCfgBLL pCfgBll = new PushCfgBLL();

        #region 获取程序员配置的模板列表
        public ResultEntity<ListModel<SysPushSimple>> QuerySysPushSelect(string likeName)
        {
            ResultEntity<ListModel<SysPushSimple>> response = new ResultEntity<ListModel<SysPushSimple>>();
            try
            {
                response.Result = 1;
                response.Data = pCfgBll.GetSysPushSelect(likeName);
            }
            catch (Exception ex)
            {
                response.Result = 0;
                response.Msg = "服务器异常，请联系负责人。";
                LogHelper.Error(ex);
            }
            return response;
        }
        #endregion

        #region 获取某个模板的详细信息
        public ResultEntity<PushConfigResult> GetSysPushModel(string Id)
        {
            ResultEntity<PushConfigResult> response = new ResultEntity<PushConfigResult>();
            try
            {
                if (string.IsNullOrEmpty(Id))
                {
                    response.Data = null;
                    response.Result = 0;
                    response.Msg = "缺少必要参数";
                    return response;
                }
                PushConfigBLL bll = new PushConfigBLL();
                PushConfigResult pushConfig = bll.GetPushTempletById(Id);
                if (pushConfig == null)
                {
                    response.Data = null;
                    response.Result = 0;
                    response.Msg = "查询结果为空，请联系管理员。";
                    return response;
                }
                response.Result = 1;
                response.Data = pushConfig;
            }
            catch (Exception ex)
            {
                response.Result = 0;
                response.Msg = "服务器异常，请联系负责人。";
                LogHelper.Error(ex);
            }
            return response;
        }
        #endregion

        #region 获取下拉控件的数据
        public ResultEntity<List<SelectItemDetail>> QuerySelectItem()
        {
            ResultEntity<List<SelectItemDetail>> response = new ResultEntity<List<SelectItemDetail>>();
            try
            {
                PushConfigBLL bll = new PushConfigBLL();
                List<PushInfo.Act.Models.QueryResults.SelectItemDetail> list = bll.GetCycleDetailList().ToList();
                list.Sort((x, y) => {
                    int intx = 0;
                    int inty = 0;
                    if (int.TryParse(x.Value, out intx) && int.TryParse(y.Value, out inty))
                        return intx - inty;
                    else
                        return 0;
                });
                response.Data = list;
                response.Result = 1;
            }
            catch (Exception ex)
            {
                response.Result = 0;
                response.Msg = "服务器异常，请联系负责人。";
                LogHelper.Error(ex);
            }
            return response;
        }
        #endregion

        #region 获取当前用户配置推送列表
        public ResultEntity<ListModel<ProjectCfgPush>> QueryProCfgPushList(string projectId, int? execStatus, string likeName,string likeBody, int pageIndex, int pageSize)
        {
            ResultEntity<ListModel<ProjectCfgPush>> response = new ResultEntity<ListModel<ProjectCfgPush>>();
            try
            {
                if (string.IsNullOrEmpty(projectId))
                {
                    response.Data = null;
                    response.Result = 0;
                    response.Msg = "缺少必要参数";
                    return response;
                }
                response.Result = 1;
                response.Data = pCfgBll.GetProCfgPushList(projectId, execStatus, likeName, likeBody, pageIndex, pageSize);
            }
            catch (Exception ex)
            {
                response.Result = 0;
                response.Msg = "服务器异常，请联系负责人。";
                LogHelper.Error(ex);
            }
            return response;
        }
        #endregion

        #region 获取当前用户某个配置详细信息
        public ResultEntity<PushConfigResult> GetProCfgPushMo(string Id)
        {
            ResultEntity<PushConfigResult> response = new ResultEntity<PushConfigResult>();
            try
            {
                if (string.IsNullOrEmpty(Id))
                {
                    response.Data = null;
                    response.Result = 0;
                    response.Msg = "缺少必要参数";
                    return response;
                }
                PushConfigBLL bll = new PushConfigBLL();
                PushConfigResult pushConfig = bll.GetConfigById(Id);
                if(pushConfig == null)
                {
                    response.Data = null;
                    response.Result = 0;
                    response.Msg = "查询结果为空，请联系管理员。";
                    return response;
                }
                response.Result = 1;
                response.Data = pushConfig;
            }
            catch (Exception ex)
            {
                response.Result = 0;
                response.Msg = "服务器异常，请联系负责人。";
                LogHelper.Error(ex);
            }
            return response;
        }
        #endregion

        #region 保存当前用户某个配置详细信息提交
        public ResultEntity<string> proCfgSaveConfig(PushConfigResult pushConfig, bool isNew)
        {
            ResultEntity<string> response = new ResultEntity<string>();
            try
            {
                if (pushConfig == null)
                {
                    response.Data = null;
                    response.Result = 0;
                    response.Msg = "缺少必要参数";
                    return response;
                }
                ResultModel result = new ResultModel();
                PushConfigBLL bll = new PushConfigBLL();
                Map_Project_Push mapProjectPush = new Map_Project_Push();
                ObjectHelper.CopyProperties(pushConfig, mapProjectPush);
                if (isNew)
                {
                    mapProjectPush.Id = Guid.NewGuid().ToString();
                    mapProjectPush.Exec_Status = (int)ExecuteStatus.新建未安排触发器;
                    mapProjectPush.Create_Time = DateTime.Now;
                    result = bll.SaveMapProjectPush(mapProjectPush);
                }
                else
                {
                    mapProjectPush.Update_Time = DateTime.Now;
                    mapProjectPush.Exec_Status = (int)ExecuteStatus.修改后未安排触发器;
                    result = bll.UpdateMapProjectPush(mapProjectPush);
                }
                response.Result = result.Result;
                response.Msg = result.Msg;
            }
            catch (Exception ex)
            {
                response.Result = 0;
                response.Msg = "服务器异常，请联系负责人。";
                LogHelper.Error(ex);
            }
            return response;
        }
        #endregion

        #region 获取推送的日志
        /// <summary>
        /// 获取推送的日志
        /// </summary>
        /// <param name="userId">推送对象的Id</param>
        /// <param name="pushStyle">推送类型APP = 1,短信 = 2,微信 = 3,socket = 4,站内推送 = 5</param>
        /// <param name="pageIndex">第几页</param>
        /// <param name="pageSize">分页大小</param>
        /// <returns></returns>
        public ResultEntity<ListModel<PushMsg>> QueryPushMsgList(string userId, List<int> pushStyle,int?haveRead, int pageIndex = 1, int pageSize = 10)
        {
            ResultEntity<ListModel<PushMsg>> response = new ResultEntity<ListModel<PushMsg>>();
            try
            {
                if (string.IsNullOrEmpty(userId))
                {
                    response.Data = null;
                    response.Result = 0;
                    response.Msg = "缺少必要参数";
                    return response;
                }
                if (pushStyle != null && pushStyle.Count() > 10)
                {
                    response.Data = null;
                    response.Result = 0;
                    response.Msg = "pushStyle参数传输异常！";
                    return response;
                }
                response.Result = 1;
                response.Data = pLogBll.GetPushMsgList(userId, pushStyle, pageIndex, pageSize, haveRead,"","",null);
            }
            catch (Exception ex)
            {
                response.Result = 0;
                response.Msg = "服务器异常，请联系负责人。";
                LogHelper.Error(ex);
            }
            return response;
        }
        #endregion
        public ResultEntity<string> SendPushOfProCfg(string proCfgId, string userId)
        {
            ResultEntity<string> response = new ResultEntity<string>();
            try
            {
                if (string.IsNullOrEmpty(proCfgId))
                {
                    response.Data = null;
                    response.Result = 0;
                    response.Msg = "缺少必要参数";
                    return response;
                }
                var result = OutProcessTaskBLL.SendPushOfProCfg(proCfgId, userId);
                response.Result = result.Result;
                response.Msg = result.Msg;
            }
            catch (Exception ex)
            {
                response.Result = 0;
                response.Msg = "服务器异常，请联系负责人。";
                LogHelper.Error(ex);
            }
            return response;
        }

        public ResultEntity<string> SendPushOfExternal(PushValueModel valModel)
        {
            ResultEntity<string> response = new ResultEntity<string>();
            try
            {
                if (string.IsNullOrEmpty(valModel.Push_Style) || (string.IsNullOrEmpty(valModel.Push_Body) && string.IsNullOrEmpty(valModel.Wx_MsmTemp)) || string.IsNullOrEmpty(valModel.ProCfg_Id) || string.IsNullOrEmpty(valModel.User_Id))
                {
                    response.Data = null;
                    response.Result = 0;
                    response.Msg = "缺少必要参数";
                    return response;
                }
                var result = OutProcessTaskBLL.SendPushOfExternal(valModel);
                response.Result = result.Result;
                response.Msg = result.Msg;
            }
            catch (Exception ex)
            {
                response.Result = 0;
                response.Msg = "服务器异常，请联系负责人。";
                LogHelper.Error(ex);
            }
            return response;
        }
    }
}
