using System;
using System.Collections.Generic;
using System.Text;
using System.IRepositories;
using System.Model;
using System.Common;
using System.Data;
using SqlSugar;
using System.Threading.Tasks;
using NLog.Web.LayoutRenderers;

namespace System.Repositories
{
    public class Bus_OrderRepository : BaseRepository<T_Bus_Order>, IBus_OrderRepository
    {
        /// <summary>
        /// 获取某月员工绩效  发单总数  发单总金额   0 未发出 1未签收2签收3改代收4拒收
        /// </summary>
        /// <param name="userid">某员工</param>
        /// <param name="dtStart">时间所在月</param>
        /// <returns></returns>
        public async Task<List<decimal>> SendTotleNumber(int userid,DateTime dtStart)
        {
            List<decimal> intarry = new List<decimal>() { -1, -0.00M };
            try
            {
                List<SugarParameter> sugarParameter = new List<SugarParameter>();
                sugarParameter.Add(new SugarParameter("@userid", userid));
                sugarParameter.Add(new SugarParameter("@startTime", dtStart));
                sugarParameter.Add(new SugarParameter("@endTime", dtStart.AddMonths(1)));
                DataTable dt = await GetTableSugar("SELECT COUNT(1),SUM(F_ShouldPrice) FROM T_Bus_Order WHERE  F_Belong= @userid AND F_State>=6 AND  F_AddTime BETWEEN @startTime AND @endTime", sugarParameter);
                if (dt != null && dt.Rows.Count > 0)
                {
                    intarry.Clear();
                    intarry.Add(dt.Rows[0][0].ObjToInt());
                    intarry.Add(dt.Rows[0][1].ObjToDecimal());
                }
                return intarry;
            }
            catch { return intarry; }
        }
        /// <summary>
        /// 获取某月员工绩效  签收  0 未发出 1未签收2签收3改代收4拒收
        /// </summary>
        /// <param name="userid">某员工</param>
        /// <param name="dtStart">时间所在月</param>
        /// <returns></returns>
        public async Task<List<decimal>> SignNumber(int userid, DateTime dtStart)
        {
            List<decimal> intarry = new List<decimal>() { -1, -0.00M };
            try
            {
                List<SugarParameter> sugarParameter = new List<SugarParameter>();
                sugarParameter.Add(new SugarParameter("@userid", userid));
                sugarParameter.Add(new SugarParameter("@startTime", dtStart));
                sugarParameter.Add(new SugarParameter("@endTime", dtStart.AddMonths(1)));
                DataTable dt = await GetTableSugar("SELECT COUNT(1),SUM(F_ShouldPrice) FROM T_Bus_Order WHERE  F_Belong= @userid AND F_State>=6 AND (F_Status = 2 OR F_Status = 3) AND  F_AddTime BETWEEN @startTime AND @endTime", sugarParameter);
                if (dt != null && dt.Rows.Count > 0)
                {
                    intarry.Clear();
                    intarry.Add(dt.Rows[0][0].ObjToInt());
                    intarry.Add(dt.Rows[0][1].ObjToDecimal());
                }
                return intarry;
            }
            catch { return intarry; }
        }

        /// <summary>
        /// 获取某月员工绩效  拒收   0 未发出 1未签收2签收3改代收4拒收
        /// </summary>
        /// <param name="userid">某员工</param>
        /// <param name="dtStart">时间所在月</param>
        /// <returns></returns>
        public async Task<List<decimal>> RefuseNumber(int userid, DateTime dtStart)
        {
            List<decimal> intarry = new List<decimal>() { -1, -0.00M };
            try
            {
                List<SugarParameter> sugarParameter = new List<SugarParameter>();
                sugarParameter.Add(new SugarParameter("@userid", userid));
                sugarParameter.Add(new SugarParameter("@startTime", dtStart));
                sugarParameter.Add(new SugarParameter("@endTime", dtStart.AddMonths(1)));
                DataTable dt = await GetTableSugar("SELECT COUNT(1),SUM(F_ShouldPrice) FROM T_Bus_Order WHERE  F_Belong= @userid AND F_State>=6 AND F_Status = 4 AND  F_AddTime BETWEEN @startTime AND @endTime", sugarParameter);
                if (dt != null && dt.Rows.Count > 0)
                {
                    intarry.Clear();
                    intarry.Add(dt.Rows[0][0].ObjToInt());
                    intarry.Add(dt.Rows[0][1].ObjToDecimal());
                }
                return intarry;
            }
            catch { return intarry; }
        }

        /// <summary>
        /// 获取某月员工绩效  录入错误上上月  有可能拒改签，有可能签改拒
        /// </summary>
        /// <param name="userid">某员工</param>
        /// <param name="dtStart">时间所在月（下一个月）</param>
        /// <returns></returns>
        public async Task<List<decimal>> ErrorNumber(int userid, DateTime dtStart)
        {
            List<decimal> intarry = new List<decimal>();
            List<decimal> intarry1 = new List<decimal>() { 0, 0 };
            List<decimal> intarry2 = new List<decimal>() { 0, 0 };
            try
            {
                List<SugarParameter> sugarParameter = new List<SugarParameter>();
                sugarParameter.Add(new SugarParameter("@userid", userid));
                sugarParameter.Add(new SugarParameter("@startTime", dtStart.AddMonths(-1)));
                sugarParameter.Add(new SugarParameter("@endTime", dtStart));
                DataTable dt = await GetTableSugar("SELECT COUNT(1),SUM(F_ShouldPrice) FROM T_Bus_Order WHERE  F_Belong= @userid AND F_State>=6 AND F_Tag LIKE '拒改签' AND F_Status > 1 AND  F_AddTime BETWEEN @startTime AND @endTime", sugarParameter);
                if (dt != null && dt.Rows.Count > 0)
                {
                    intarry1.Clear();
                    intarry1.Add(dt.Rows[0][0].ObjToInt());
                    intarry1.Add(dt.Rows[0][1].ObjToDecimal());
                }
                await GetTableSugar("SELECT COUNT(1),SUM(F_ShouldPrice) FROM T_Bus_Order WHERE  F_Belong= @userid AND F_State>=6 AND F_Tag LIKE '签改拒' AND F_Status > 1 AND  F_AddTime BETWEEN @startTime AND @endTime", sugarParameter);
                if (dt != null && dt.Rows.Count > 0)
                {
                    intarry2.Clear();
                    intarry2.Add(dt.Rows[0][0].ObjToInt());
                    intarry2.Add(dt.Rows[0][1].ObjToDecimal());
                }
                intarry.Add(intarry1[0]- intarry2[0]);
                intarry.Add(intarry1[1] - intarry2[1]);
                return intarry;
            }
            catch { return intarry; }
        }

        /// <summary>
        /// 获取某月员工绩效  单独统计的物品 特殊统计物品  F_ClassId 1鞋垫 2袜子
        /// </summary>
        /// <param name="userid">某员工</param>
        /// <param name="dtStart">时间所在月</param>
        /// <returns></returns>
        public async Task<decimal> AlongMoney(int userid, DateTime dtStart)
        {
            decimal intarry =  0.00M ;
            try
            {
                List<SugarParameter> sugarParameter = new List<SugarParameter>();
                sugarParameter.Add(new SugarParameter("@userid", userid));
                sugarParameter.Add(new SugarParameter("@startTime", dtStart));
                sugarParameter.Add(new SugarParameter("@endTime", dtStart.AddMonths(1)));
                DataTable dt = await GetTableSugar("SELECT SUM(F_TotlePrice) FROM `T_Bus_OrderDetail` WHERE F_OrderId IN(SELECT F_Id FROM T_Bus_Order WHERE  F_Belong= @userid AND F_State>=6 AND (F_Status = 2 OR F_Status = 3) AND  F_AddTime BETWEEN @startTime AND @endTime) AND `F_ProductId` IN (SELECT F_ProductId FROM `T_Bus_Product` WHERE F_ClassId IN  (1,2))", sugarParameter);
                if (dt != null && dt.Rows.Count > 0)
                {
                    intarry = dt.Rows[0][0].ObjToDecimal();
                }
                return intarry;
            }
            catch { return intarry; }
        }

    }
}
