using Hncore.Infrastructure.Common;
using Hncore.Infrastructure.EntitiesExtension;
using Hncore.Infrastructure.Extension;
using Hncore.Infrastructure.WebApi;
using Hncore.Pass.Vpn.Domain;
using Hncore.Pass.Vpn.Request.Product;
using Hncore.Pass.Vpn.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Hncore.Pass.Vpn.Controllers
{
    [ApiVersion("1.0")]
    [Route("api/course/v{version:apiVersion}/productroute/[action]")]
    public class ProductRouteController : HncoreControllerBase
    {
        private ProductRouteService m_ProductRouteService;

        private ProductService m_ProductService;

        public ProductRouteController(ProductRouteService _ProductRouteService, ProductService _ProductService)
        {
            m_ProductRouteService = _ProductRouteService;
            m_ProductService = _ProductService;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> Post([FromBody]ProductRouteEntity request)
        {
            var product = await m_ProductService.GetById(request.ProductId);
            request.ProductName = product?.Name;
            await m_ProductRouteService.Add(request);
            return Success();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> Put([FromBody]ProductRouteEntity request)
        {
            var product = await m_ProductService.GetById(request.ProductId);
            request.ProductName = product?.Name;
            await m_ProductRouteService.Update(request);
            return Success();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> Delete([FromQuery]int id)
        {
            var flag = await m_ProductRouteService.DeleteById(id);
            if (flag)
                return Success();
            else
                return Error("删除失败");
        }


        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> Get([FromQuery]int id)
        {
            var data = await m_ProductRouteService.GetById(id);
            return Success(data);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> Page([FromQuery]RoutePageRequest request)
        {
            Expression<Func<ProductRouteEntity, bool>> expr = m => 1 == 1;
            if (request.KeyWord.Has())
            {
                expr = expr.And(m => m.Province.Contains(request.KeyWord) || m.Name.Contains(request.KeyWord) || m.City.Contains(request.KeyWord) || m.ServerUrl.Contains(request.KeyWord));
            }
            if (request.ProductId.HasValue&& request.ProductId>0)
            {
                expr = expr.And(m => m.ProductId==request.ProductId);
            }
            var ret = await m_ProductRouteService.PageAsc(request.PageIndex, request.PageSize, expr, true,m=>m.Sort);
            var data = ret.ToApiResult();
            return data;
        }

        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost, AllowAnonymous]
        public async Task<ApiResult> Import(IFormFile file)
        {
            // var file = this.Request.Form.Files.First();
            var stream = file.OpenReadStream();// this.Request.Body;
            //stream.Position = 0;
            var data = ExcelHelper.ReadFromStream(stream);

            var products = await m_ProductService.GetAll();

            var list = new List<ProductRouteEntity>();

            var dataRows = data.Skip(1);
            foreach (var row in dataRows)
            {
                var model = new ProductRouteEntity();
                model.ProductName = row[0];
                model.Province = row[1];
                model.Name = row[2];
                model.ServerUrl = row[3];
                model.Status = row[4];
                model.LineType = row[5];
                model.BandWidth = row[6];
                model.IpRemark = row[7];
                model.ProductId = products.FirstOrDefault(m => m.Name == model.ProductName)?.Id;
            }
            if (list.Count > 1)
            {
                await m_ProductRouteService.Adds(list);
            }
            return Success();
        }
    }
}
