using System;
using System.Linq.Expressions;
using System.Collections.Generic;
using DapperORM;
using DapperORM.Common.Models;
using PushInfo.Core.Models.Tables;
using PushInfo.Core.IDAL;
using System.Linq;

namespace PushInfo.Core.DAL.MySql
{
    public class MapProjectPushDAL : BaseRepository<Map_Project_Push>, IMapProjectPushDAL
    {
        public ResultModel<Map_Project_Push> GetModel(Expression<Func<Map_Project_Push, bool>> where = null, Expression<Func<Map_Project_Push, object>> select = null, IDictionary<string, bool> sort = null)
        {
            return GetAsync(where, select, sort).Result;
        }
        public Map_Project_Push GetModel(string Id)
        {
            string sql = @"select t1.Id,t1.Name_Des,t1.Push_Id,t1.PushSys_ProId,t1.Project_Id,
			                      t1.Exec_Time,t1.Exec_Status,t1.Push_Target,t1.Push_Style,t1.Push_TagId,t1.Push_Title,case when t1.Push_Body is null then t2.Push_Body else t1.Push_Body end as Push_Body,
			                      t1.Push_Type,t1.Push_Cycle,t1.Push_Month,t1.Push_Which,t2.Push_Action,t2.Push_ActionType,
		                          t2.Push_IosLink,t2.Push_AndLink,t2.Push_WebLink,t2.Push_WechatLink,t2.Db_ConnectId,
		                          t1.Wx_MsmodelId,t1.Is_Deleted,t1.Trigger_Key,t2.Push_User_Action,t2.Push_User_ActionType,t2.Db_User_ConnectId
                            from map_project_push t1 join sys_push t2 on t1.Push_Id = t2.Id and t1.ID = @Id and t1.Is_Deleted = 0 and t2.Is_Deleted = 0 ";
            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("@Id", Id);
            var result = QueryAsync<Map_Project_Push>(sql, param).Result;
            if(result.Data.Count() >0 && result.IsSuccess())
            {
                return result.Data.FirstOrDefault();
            }
            else
            {
                return null;
            }
        }

        public Map_Project_Push GetModel(string sysPushId, string projectId)
        {
            string sql = @"select t1.Id,t1.Name_Des,t1.Push_Id,t1.PushSys_ProId,t1.Project_Id,
			                      t1.Exec_Time,t1.Exec_Status,t1.Push_Target,t1.Push_Style,t1.Push_TagId,t1.Push_Title,case when t1.Push_Body is null then t2.Push_Body else t1.Push_Body end as Push_Body,
			                      t1.Push_Type,t1.Push_Cycle,t1.Push_Month,t1.Push_Which,t2.Push_Action,t2.Push_ActionType,
		                          t2.Push_IosLink,t2.Push_AndLink,t2.Push_WebLink,t2.Push_WechatLink,t2.Db_ConnectId,
		                          t1.Wx_MsmodelId,t1.Is_Deleted,t1.Trigger_Key,t2.Push_User_Action,t2.Push_User_ActionType,t2.Db_User_ConnectId
                            from map_project_push t1 join sys_push t2 on t1.Push_Id = t2.Id  and t2.ID = @sysPushId and t1.Project_Id = @projectId and t1.Is_Deleted = 0 and t2.Is_Deleted = 0";
            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("@sysPushId", sysPushId);
            param.Add("@projectId", projectId);
            var result = QueryAsync<Map_Project_Push>(sql, param).Result;
            if (result.Data.Count() > 0 && result.IsSuccess())
            {
                return result.Data.FirstOrDefault();
            }
            else
            {
                return null;
            }
        }

        public ResultModel<IEnumerable<Map_Project_Push>> GetList(Expression<Func<Map_Project_Push, bool>> where = null, Expression<Func<Map_Project_Push, object>> select = null, IDictionary<string, bool> sort = null)
        {
            return FetchAsync(where, select, sort).Result;
        }
        /// <summary>
        /// 查询数据集合
        /// </summary>
        /// <param name="sqlStr">查询sql语句</param>
        /// <returns></returns>
        public ResultModel<IEnumerable<Map_Project_Push>> GetList(string sqlStr, Dictionary<string, object> param)
        {
            return QueryAsync<Map_Project_Push>(sqlStr, param).Result;
        }

        public ResultModel Update(Map_Project_Push model, Expression<Func<Map_Project_Push, object>> updateExp, Expression<Func<Map_Project_Push, bool>> whereExp)
        {
            if(updateExp == null)
            {
                return UpdateAllAsync<Map_Project_Push>(model, whereExp).Result;
            }
            else
            {
                return UpdatePartailAsync<Map_Project_Push>(model, updateExp, whereExp).Result;
            }
        }
    }
}
