using Hncore.Infrastructure.WebApi;
using Hncore.Pass.Sells.Domain;
using Hncore.Pass.Sells.Request.RedeemCode;
using Hncore.Pass.Sells.Service;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Hncore.Infrastructure.Extension;
using Hncore.Infrastructure.EntitiesExtension;
using Microsoft.EntityFrameworkCore;
using Hncore.Infrastructure.Common;
using System.Collections.Generic;

namespace Hncore.Pass.Live.Controllers
{
    [ApiVersion("1.0")]
    [Route("api/sells/v{version:apiVersion}/taobao/[action]")]
    public class TaoBaoController : HncoreControllerBase
    {
        SellerTaoBaoService m_SellerTaoBaoService;

        public TaoBaoController(SellerTaoBaoService _SellerTaoBaoService)
        {
            m_SellerTaoBaoService = _SellerTaoBaoService;
        }


        [HttpGet]
        public async Task<ApiResult> Get([FromQuery] int id)
        {
            var ret = await m_SellerTaoBaoService.GetById(id);
            return Success(ret);
        }

        /// <summary>
        /// ��ѯ
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>

        [HttpGet]
        public async Task<ApiResult> Page([FromQuery]TaoBaoRequest request)
        {
            Expression<Func<TaoBaoOrderEntity, bool>> expr = m => 1 == 1;              

            if (request.KeyWord.Has())
            {
                expr = expr.And(m => m.Phone.Contains(request.KeyWord));
                expr = expr.Or(m => m.Tid.Contains(request.KeyWord));
                expr = expr.Or(m => m.BuyerNick.Contains(request.KeyWord));             
            }
            if (request.shop.Has())
            {
                expr = expr.And(m => m.SellerNick.Contains(request.shop));           
            }
            if (request.BTime.HasValue && request.ETime.HasValue)
            {
                expr = expr.And(m => m.Created >= request.BTime && m.Created <= request.ETime);
            }
            var ret = await m_SellerTaoBaoService.PageDesc(request.PageIndex, request.PageSize, expr,true,m=>m.Id);
            // ret.List.ForEach(
            //     m => {m.Phone = m.Phone.Substring(0,3) + "***";
            // });
            var data = ret.ToApiResult();
            return data;
        }

        [HttpGet]
        public async Task<IActionResult> Export([FromQuery]TaoBaoRequest request)
        {
            Expression<Func<TaoBaoOrderEntity, bool>> expr = m => 1 == 1;
            if (request.BTime.HasValue && request.ETime.HasValue)
            {
                expr = expr.And(m => m.CreateDate >= request.BTime && m.CreateDate <= request.ETime);
            }
            if (request.shop.Has())
            {
                expr = expr.And(m => m.SellerNick.Contains(request.shop));           
            }
            var ret = await m_SellerTaoBaoService.Query(expr).ToListAsync();


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

            var title = new List<ExcelTitle>(){
                    new ExcelTitle { Property = "CreateDate", Title = "支付时间" ,Format=(val)=>((DateTime) val).ToString("yyyy-MM-dd HH:mm:ss")},
                    new ExcelTitle { Property = "Tid", Title = "订单号" },
                    new ExcelTitle { Property = "SellerNick", Title = "产品" },
                    new ExcelTitle { Property = "BuyerNick", Title = "购买人" },
                    new ExcelTitle { Property = "Phone", Title = "点话" },
                    new ExcelTitle { Property = "Price", Title = "单价" },
                    new ExcelTitle { Property = "Num", Title = "数量" },
                    new ExcelTitle { Property = "TotalFee", Title = "订单金额" },                   
                    new ExcelTitle { Property = "Payment", 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);

        }

    }
}