using AutoMapper;
using OASystem.Domain;
using OASystem.Domain.Entities.Groups;
using OASystem.Domain.ViewModels.Financial;
using OASystem.Domain.ViewModels.Groups;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OASystem.Infrastructure.Repositories.Financial
{
    /// <summary>
    /// 财务 - 付款信息类
    /// 雷怡 2023.08.16 16:19
    /// </summary>
    public class CreditCardPaymentRepository : BaseRepository<Grp_CreditCardPayment, Grp_Fin_CreditCardPaymentView>
    {
        private readonly IMapper _mapper;

        public CreditCardPaymentRepository(SqlSugarClient sqlSugar, IMapper mapper)
            : base(sqlSugar)
        {
            _mapper = mapper;
        }

        /// <summary>
        /// 根据diid查询团组付款信息
        /// </summary>
        /// <param name="diid"></param>
        /// <param name="isPay">false 未支付 true 已支付</param>
        /// <returns></returns>
        public async Task<Result> GetGroupPaymentInfoByDiid(int diid,bool isPay)
        {
            Result result = new() { Code = -2 };

            int _isPay = 0;
            if (isPay) { _isPay = 1; }

            string sql = string.Format(@"Select * From Grp_CreditCardPayment Where IsDel=0 And Diid={0} And IsPay={1}", diid, _isPay);

            var groupReceivablesList = await _sqlSugar.SqlQueryable<Fin_ProceedsReceivedView>(sql).ToListAsync();

            result.Code = 0;
            result.Msg = "查询成功!";
            result.Data = groupReceivablesList;

            return result;

        }

        /// <summary>
        /// 根据diid 数组 查询团组付款信息
        /// </summary>
        /// <param name="diid"></param>
        /// <param name="isPay">false 未支付 true 已支付</param>
        /// <returns></returns>
        public async Task<Result> GetGroupPaymentInfoByDiids(int[] diids, bool isPay)
        {
            Result result = new() { Code = -2 };

            int _isPay = 0;
            if (isPay) { _isPay = 1; }

            var groupReceivablesList = await _sqlSugar.Queryable<Grp_CreditCardPayment>()
                .Where(ccp => ccp.IsDel == 0 && diids.Contains(ccp.DIId) && ccp.IsPay == _isPay) .ToListAsync();

            result.Code = 0;
            result.Msg = "查询成功!";
            result.Data = groupReceivablesList;

            return result;

        }

        /// <summary>
        /// 团组 其他款项/退款
        /// </summary>
        /// <param name="diid">团组Id</param>
        /// <param name="isPay"></param>
        /// <returns></returns>
        public async Task<Result> GetGroupRefundByDiid(int diid, bool isPay)
        {
            Result result = new() { Code = -2 };

            int _isPay = 0;
            if (isPay) { _isPay = 1; }

            string sql = string.Format(@"Select ccp.DIId,ccp.CreateUserId,ccp.AuditGMDate,sd1.name As PayType,ccp.OrbitalPrivateTransfer,ccp.PayDid,
                                                            ccp.payee,ccp.IsPay,gdp.CreateTime,gdp.PriceName,gdp.Price,sd.[Name] Currency,
                                                            ccp.PayMoney As Spread,ccp.DayRate,gdp.Remark From Fin_OtherPrice gdp 
                                        join Grp_CreditCardPayment ccp On ccp.Diid = gdp.Diid and ccp.Cid = gdp.Id 
                                        join Sys_SetData sd On sd.Id =gdp.CurrencyId 
                                        join Sys_SetData sd1 On sd1.id=ccp.PayDid 
                                        Where ccp.DIId={0} And gdp.IsDel = 0 and ccp.isDel=0 and ccp.CTable=285 and ccp.IsPay = {1} ", diid, _isPay);

            var groupRefundList = await _sqlSugar.SqlQueryable<GroupRefundView>(sql).ToListAsync();

            result.Code = 0;
            result.Msg = "查询成功!";
            result.Data = groupRefundList;

            return result;
        }

        /// <summary>
        /// 团组 根据diid 数组 查询 其他款项/退款
        /// </summary>
        /// <param name="diid">团组Id</param>
        /// <param name="isPay"></param>
        /// <returns></returns>
        public async Task<Result> GetGroupRefundByDiids(int[] diids, bool isPay)
        {
            Result result = new() { Code = -2 };

            int _isPay = 0;
            if (isPay) { _isPay = 1; }

            string _diidsStr = string.Empty;
            #region 处理Diid
            StringBuilder diidSb = new StringBuilder();
            for (int i = 0; i < diids.Length; i++)
            {
                if (i == diids.Length - 1)
                {
                    diidSb.Append(diids[i]);
                }
                else
                {
                    diidSb.Append(diids[i]).Append(",");
                }
            }
            _diidsStr = diidSb.ToString().Trim();
            #endregion

            string sql = string.Format(@"Select ccp.DIId,ccp.CreateUserId,ccp.AuditGMDate,sd1.name As PayType,ccp.OrbitalPrivateTransfer,ccp.PayDid,
                                                            ccp.payee,ccp.IsPay,gdp.CreateTime,gdp.PriceName,gdp.Price,sd.[Name] Currency,
                                                            ccp.PayMoney As Spread,ccp.DayRate,gdp.Remark From Fin_OtherPrice gdp 
                                        join Grp_CreditCardPayment ccp On ccp.Diid = gdp.Diid and ccp.Cid = gdp.Id 
                                        join Sys_SetData sd On sd.Id =gdp.CurrencyId 
                                        join Sys_SetData sd1 On sd1.id=ccp.PayDid 
                                        Where ccp.isDel=0 And gdp.IsDel = 0 And ccp.DIId In ({0}) And ccp.CTable=285 and ccp.IsPay = {1} ", _diidsStr, _isPay);

            var groupRefundList = await _sqlSugar.SqlQueryable<GroupRefundView>(sql).ToListAsync();

            result.Code = 0;
            result.Msg = "查询成功!";
            result.Data = groupRefundList;

            return result;
        }
    }
}