using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization;
using MS.Common.IDCode;
using MS.Component.Jwt.UserClaim;
using MS.DbContexts;
using MS.Entities.AccountingService_Models;
using MS.Entities.AuthService_Models;
using MS.Entities.Models;
using MS.Models;
using MS.UnitOfWork;
using MS.WebCore.Core;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MS.Services.PosTrxService
{
    public class MiniProgramPaymentCancelService : BaseService, IMiniProgramPaymentCancelService
    {
        public MiniProgramPaymentCancelService(IUnitOfWork<MSDbContext> unitOfWork, IMapper mapper, IdWorker idWorker, IClaimsAccessor claimsAccessor, IStringLocalizer localizer, IUnitOfWork<AccountingContext> AccountingContext, IUnitOfWork<SQLiteContext> SQLiteContext, IUnitOfWork<AuthContext> AuthContext) : base(unitOfWork, mapper, idWorker, claimsAccessor, localizer, AccountingContext, SQLiteContext, AuthContext)
        {
        }
        public async Task<ExecuteResult> CancelADealAsync(string startTime)
        {

            DateTime dateTime = string.IsNullOrEmpty(startTime) ? DateTime.Now.AddHours(-1) : DateTime.Parse(startTime);
            DateTime enddateTime = string.IsNullOrEmpty(startTime) ? DateTime.Now.AddDays(-1) : DateTime.Parse(startTime).AddDays(-2);
            var query = _unitOfWork.GetRepository<PosTrxes>().GetAll().Include(a => a.PosTrxLineItems).ThenInclude(a => a.PosItem)
.Include(a => a.PosTrxMops)
.ThenInclude(a => a.PosMop).Where(a => a.CreatedDateTime >= enddateTime && a.CreatedDateTime <= dateTime
&& a.TransactionStatus == 0
&& a.PosTrxLineItems.Any(b => b.PosItem.BarCode == "8888") //&& a.Id == guid_
).ToList();

            //            string sql = string.Format(@"
            //select a.* from ""PosTrxes"" a left join ""PosTrxLineItems""  b on a.""Id""  = b.""PosTrxId"" 
            //left join ""PosItems"" c on b.""PosItemId""  = c.""Id"" 
            //where a.""CreatedDateTime""  >=  '{0}' and a.""CreatedDateTime""  <= '{1}'
            //and a.""TransactionStatus""  = 0
            //and c.""ItemId""  = '8888'", enddateTime, dateTime);
            //            var results = await _unitOfWork.DbContext.posTrxes  "ResultCode"	"ResultMessage"
            //                .FromSqlRaw(sql).Include(a => a.PosTrxLineItems).ThenInclude(e => e.PosItem).ToListAsync();
            var mop = _unitOfWork.GetRepository<PosMops>().GetAll().Where(a => a.PaymentId == 1).FirstOrDefault();
            foreach (var item in query)
            {
                PosTrxMops posTrxMops = new PosTrxMops();
                posTrxMops.Id = Guid.NewGuid();
                posTrxMops.LineNum = 0;
                posTrxMops.Paid = 0;
                posTrxMops.ResultCode = "0";
                posTrxMops.ResultMessage = "小程序撤销支付";
                posTrxMops.BillNumber = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                posTrxMops.PayBack = 0;
                posTrxMops.PosTrxId = item.Id;
                posTrxMops.PosMopId = mop.Id;
                item.TransactionType = 12;
                _unitOfWork.GetRepository<PosTrxMops>().Insert(posTrxMops);
                _unitOfWork.GetRepository<PosTrxes>().Update(item);
              
            }
            await _unitOfWork.SaveChangesAsync();//提交
            return null;
        }
    }
}
