using System;
using System.Collections.Generic;
using System.Common;
using System.Data;
using System.IRepositories.Call;
using System.Model.Call;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;

namespace System.Repositories.Call
{
    
    public class Call_CallOutboundRepository : BaseRepository<T_Call_CallOutbound>, ICall_CallOutboundRepository
    {

        public async Task<int> GetCallNumber(string DeptCode, int teamid, DateTime dtime)
        {
            string sql = "SELECT COUNT(1) FROM T_Call_CallOutbound where  TalkLongTime>0 AND BeginTime BETWEEN @stime AND @etime ";
            if (!string.IsNullOrEmpty(DeptCode))
            {
                sql += $" AND UserCode in (SELECT F_UserCode FROM `T_Sys_UserAccount` WHERE F_DeptId IN(SELECT F_DeptId FROM  T_Sys_Department WHERE F_DeptCode LIKE '{DeptCode}%'))";
                DeptCode = "|0|1|";
            }

            if (teamid > 0)
            {
                sql += $" AND UserCode in (SELECT F_UserCode FROM `T_Sys_UserAccount` WHERE F_DeptTeamId ={teamid})";
            }
            List<SugarParameter> sugarParameter = new List<SugarParameter>()
            {
                new SugarParameter("@stime",dtime),
                new SugarParameter("@etime",dtime.AddMonths(1)),
            };

            DataTable dt = await GetTableSugar(sql, sugarParameter);
            if (dt != null && dt.Rows.Count > 0)
            {
                return dt.Rows[0][0].ObjToInt();
            }
            return 0;
        }
        public async Task<int> GetTotleCallNumber(string DeptCode, int teamid, DateTime dtime)
        {
            string sql = "SELECT COUNT(1) FROM T_Call_CallOutbound where  BeginTime BETWEEN @stime AND @etime ";
            if (!string.IsNullOrEmpty(DeptCode))
            {
                sql += $" AND UserCode in (SELECT F_UserCode FROM `T_Sys_UserAccount` WHERE F_DeptId IN(SELECT F_DeptId FROM  T_Sys_Department WHERE F_DeptCode LIKE '{DeptCode}%'))";
                DeptCode = "|0|1|";
            }

            if (teamid > 0)
            {
                sql += $" AND UserCode in (SELECT F_UserCode FROM `T_Sys_UserAccount` WHERE F_DeptTeamId ={teamid})";
            }

            List<SugarParameter> sugarParameter = new List<SugarParameter>()
            {
                new SugarParameter("@stime",dtime),
                new SugarParameter("@etime",dtime.AddMonths(1)),
            };
            DataTable dt = await GetTableSugar(sql, sugarParameter);
            if (dt != null && dt.Rows.Count > 0)
            {
                return dt.Rows[0][0].ObjToInt();
            }
            return 0;
        }
    }
}
