using DapperORM;
using PushInfo.Act.IDAL;
using PushInfo.Act.Models.Tables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DapperORM.Common.Models;

namespace PushInfo.Act.DAL.MySql
{
    internal class MapProjectPushDAL : BaseRepository<Map_Project_Push>, IMapProjectPushDAL
    {
        public ResultModel DeleteMapProjectPushById(string id)
        {
            return LogicalDeleteAsync<Map_Project_Push>(x => x.Id == id).Result;
        }

        public ResultModel<Map_Project_Push> GetPushById(string id)
        {
            return GetAsync<Map_Project_Push>(x => x.Id == id).Result;
        }

        public ResultModel<IEnumerable<Map_Project_Push>> GetPushListByProjectId(string projId)
        {
            return FetchAsync<Map_Project_Push>(x => x.Project_Id == projId).Result;
        }

        public ResultModel GetCountByPushId(string pushId)
        {
            return GetCountAsync<Map_Project_Push>(x => x.Push_Id == pushId).Result;
        }

        public ResultModel SaveMapProjectPush(Map_Project_Push mapProjectPush)
        {
            return InsertAsync<Map_Project_Push>(mapProjectPush).Result;
        }

        public ResultModel UpdateMapProjectPush(Map_Project_Push mapProjectPush)
        {
            return UpdatePartailAsync<Map_Project_Push>(mapProjectPush, x => new { x.Name_Des, x.Push_Id, x.Push_Cycle, x.Push_Month, x.Push_Which,x.Push_Target,x.Push_TagId, x.Push_Title,x.Push_Body, x.Push_Style, x.Push_Type, x.Update_User, x.Update_Time, x.Exec_Time, x.Exec_Status,x.Wx_MsmodelId },x=>x.Id == mapProjectPush.Id).Result;
        }

        public ResultModel UpdateMapProPushState(Map_Project_Push mapProjectPush)
        {
            return UpdatePartailAsync<Map_Project_Push>(mapProjectPush, x => new {x.Exec_Status }, x => x.Id == mapProjectPush.Id).Result;
        }

        public PageListModel<Map_Project_Push> GetPushListByPage(int pageIndex, string projId,string pushProId)
        {
            string selectSql = @"select Id,Project_Id,Name_Des,Exec_Time,Exec_Status,Push_Style,Push_Type,Create_Time from map_project_push where is_deleted = 0 and project_id = @project_id and PushSys_ProId=@pushProId";
            string totalSql = @"select count(*) from map_project_push where is_deleted = 0 and project_id = @project_id and PushSys_ProId=@pushProId";
            Dictionary<string, object> condition = new Dictionary<string, object> { { "@project_id", projId }, { "@pushProId", pushProId } };
            
            return PagedFetchAsync<Map_Project_Push>(selectSql, totalSql, condition, pageIndex).Result;
        }
    }
}
