using Hncore.Infrastructure.Common;
using Hncore.Infrastructure.Extension;
using Hncore.Infrastructure.WebApi;
using Hncore.Pass.BaseInfo.Models;
using Hncore.Pass.BaseInfo.Request;
using Hncore.Pass.BaseInfo.Request.User;
using Hncore.Pass.BaseInfo.Service;
using Hncore.Pass.Vpn.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Hncore.Infrastructure.EntitiesExtension;
namespace Hncore.Pass.BaseInfo.Controllers
{
    [ApiVersion("1.0")]
    [Route("api/baseinfo/v{version:apiVersion}/chargeorder/[action]")]
    public class ChargeOrderController : HncoreControllerBase
    {
        private UserService _userService;
        private WxAppUserService m_WxAppUserService;
        private IConfiguration _Configuration;
        private UserScoreService m_UserScoreService;

        private UserChargeOrderService m_ChargeOrderService;
        public ChargeOrderController(UserService userService
            , UserScoreService _UserScoreService
            , UserChargeOrderService _ChargeOrderService
            , IConfiguration configuration)
        {
            _userService = userService;
            _Configuration = configuration;
            m_ChargeOrderService = _ChargeOrderService;
            m_UserScoreService = _UserScoreService;
        }

        public async Task<ApiResult> Page([FromQuery]QueryChargeOrdertRequest request)
        {
            Expression<Func<UserChargeOrderEntity, bool>> expr = m => m.OrderState == UOrderStatus.PayOk || m.OrderState == UOrderStatus.Complete;
            if (request.KeyWord.Has())
            {
                expr = expr.And(m =>
                  m.UserName.Contains(request.KeyWord)
                || m.OrderNo.Contains(request.KeyWord)
                || m.TradeNo.Contains(request.KeyWord));
            }
            if (request.BTime.HasValue && request.ETime.HasValue)
            {
                expr = expr.And(m => m.CreateTime >= request.BTime && m.CreateTime <= request.ETime);
            }
            var ret = await m_ChargeOrderService.PageDesc(request.PageIndex, request.PageSize, expr, true, m => m.Id);

            // ret.List.ForEach(
            //     m => {m.UserName = m.UserName.Substring(0,3) + "***";
            // });
            var data = ret.ToApiResult();
            return data;
        }

        [HttpGet]
        public async Task<IActionResult> ExportOrder([FromQuery]QueryChargeOrdertRequest request)
        {
            Expression<Func<UserChargeOrderEntity, bool>> expr = m => m.OrderState == UOrderStatus.PayOk || m.OrderState == UOrderStatus.Complete;
            if (request.KeyWord.Has())
            {
                expr = expr.And(m =>
                  m.UserName.Contains(request.KeyWord)
                || m.OrderNo.Contains(request.KeyWord)
                || m.TradeNo.Contains(request.KeyWord));
            }
            if (request.BTime.HasValue && request.ETime.HasValue)
            {
                expr = expr.And(m => m.CreateTime >= request.BTime && m.CreateTime <= request.ETime);
            }
            var ret = await m_ChargeOrderService.PageDesc(1, 10000, expr, true, m => m.Id);


            var data = new ExcelData<UserChargeOrderEntity>
            {
                SheetName = DateTime.Now.ToString("yyyy-MM-dd"),
                Data = ret.List
            };

            var title = new List<ExcelTitle>(){
                    new ExcelTitle { Property = "CreateTime", Title = "创建日期",Format=(val)=>((DateTime) val).ToString("yyyy-MM-dd hh:mm:ss") },
                    new ExcelTitle { Property = "OrderNo", Title = "订单号" },
                    new ExcelTitle { Property = "TradeNo", Title = "交易号" },
                    new ExcelTitle { Property = "UserName", Title = "用户" },                  
                    new ExcelTitle { Property = "OrderAmount", Title = "充值金额" },
                               
                };
            var fileBytes = ExcelHelper.ExportListToExcel(data, title);

            var fileName = $"{DateTime.Now.ToString("yyyyMMdd")}充值明细.xlsx";
            Response.Headers.Add("X-Suggested-Filename", fileName.UrlEncode());
            return File(fileBytes, "application/octet-stream", fileName);

        }
    }
}