using PushApi.Act.Models.Trial;
using PushInfo.Core.BLL;
using PushPlatform.Common.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PushPlatform.Common.Util;
using MySql.Data.MySqlClient;
using PushPlatform.Common.Enumerate;

namespace PushApi.Act.BLL
{
    // 91trial核心推送接口类
    public class TrialBLL
    {
        public static APIResult<List<DataMessage>> VisitRemind(string proCfgId)
        {
            APIResult<List<DataMessage>> model = new APIResult<List<DataMessage>>();
            model.Result = 1;
            model.Data = new List<DataMessage>();
            try
            {
                MapProjectPushBLL mapPro = new MapProjectPushBLL();
                PushDataBLL pushData = new PushDataBLL();
                SelectItemDetailBLL selectBll = new SelectItemDetailBLL();
                var task = mapPro.Query_Sys_Task_By_Id(proCfgId);
                if (task != null)
                {
                    var result = selectBll.Query_By_Id(task.Db_ConnectId);
                    if (result == null)
                    {
                        return model;
                    }
                    string dbConnectStr = result.Value;

                    if (!string.IsNullOrEmpty(task.Push_User_Action) && !string.IsNullOrEmpty(task.Db_User_ConnectId))  //权限系统独立出来使用，用户取自其它数据库
                    {
                        var selectModel = selectBll.Query_By_Id(task.Db_User_ConnectId);
                        if (selectModel == null)
                        {
                            return model;
                        }
                        if (task.Push_User_ActionType == (int)PushActionType.SQL && task.Push_User_Action.IndexOf("@userId") > -1 && string.IsNullOrEmpty(task.User_Id))
                        {
                            MappushUserRoleBLL mapPushUser = new MappushUserRoleBLL();
                            var dataInfo = mapPushUser.QueryByProCfg(task.Project_Id, task.Id);
                            if (dataInfo.Count > 0)
                            {
                                if (dataInfo.FirstOrDefault().User_Id == "allUserId")
                                {
                                    task.User_Id = "allUserId";
                                }
                                else
                                {
                                    task.User_Id = string.Join(",", dataInfo.Select(x => x.User_Id));
                                }
                            }
                        }
                        DataTable table2 = pushData.GetTableValue(task.Push_User_ActionType, selectModel.Select_Type, selectModel.Value, task.Push_User_Action, task.Push_Target, task.Project_Id, task.User_Id, null);
                        if (table2.Rows.Count > 0)
                        {
                            List<DataMessage> needTo = table2.ToList<DataMessage>();
                            foreach (var item in needTo)
                            {
                                Dictionary<string, object> param = new Dictionary<string, object>();
                                param.Add("@ProjectId", task.Project_Id);
                                param.Add("@UserId", item.userId);
                                if (item.IsFilter != 1)
                                    param.Add("@creatUserId", DBNull.Value);
                                else
                                    param.Add("@creatUserId", item.userId);
                                MySqlParameter[] msp = {
                                };
                                if (param.Count > 0)
                                {
                                    List<MySqlParameter> linList = new List<MySqlParameter>();
                                    foreach (var pItem in param)
                                    {
                                        linList.Add(new MySqlParameter(pItem.Key, pItem.Value));
                                    }
                                    msp = linList.ToArray();
                                }
                                MySQLHelper mysqlHelper = new MySQLHelper(dbConnectStr);
                                DataTable crccount = mysqlHelper.ExecuteDataTable(CommandType.StoredProcedure, "Proc_QueryVisitListCount", msp);
                                if (crccount != null && crccount.Rows.Count > 0 && crccount.Rows[0]["VistiRemindCount"].ToString() != "0")
                                {
                                    item.dicValue = new Dictionary<string, string>();
                                    item.dicValue.Add("VistiRemindCount", crccount.Rows[0]["VistiRemindCount"].ToString());
                                    item.dicValue.Add("projectId", item.projectId);
                                    if (table2.Columns.Contains("projectName"))
                                    {
                                        item.dicValue.Add("projectName", table2.Rows[0]["projectName"].ToString());
                                    }
                                }
                            }
                            model.Data = needTo.Where(x => x.dicValue != null).ToList();
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                model.Result = 0;
                model.Msg = ex.Message;
                LogHelper.Error(ex);
            }
            
            return model;
        }
    }
}
