using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using XYY.Common.Standard;
using XYY.Core.Standard.Mvc;
using XYY.Model.Standard;
using XYY.Model.Standard.Finance;
using XYY.Service.Standard.Finance;

namespace XYY.Api.Finance.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CustomerAccountController : ApiControllerBase
    {
        ICustomerAccountService _customerAccountService;
        public CustomerAccountController(ICustomerAccountService customerAccountService)
        {
            _customerAccountService = customerAccountService;
        }

        #region 流水

        [HttpPost]
        public async Task<IActionResult> QueryCustomerIncome(QueryModel query)
        {
            var data = await _customerAccountService.QueryCustomerIncome(query);
            return Ok(data);
        }

        [HttpPost]
        public async Task<IActionResult> DownloadData(QueryModel query)
        { 
            var data = await _customerAccountService.OutCustomerIncome(query);
            return File(data, MimeMapping.MimeUtility.GetMimeMapping("1.xlsx"));
        }

        #endregion
        #region 账号管理
        /// <summary>
        /// 创建默认账号
        /// </summary>
        /// <param name="queryModel"></param>
        /// <returns></returns>
        public async Task<IActionResult> CreateDefault(AccountTypeEnum enumValue, int customerId)
        {
            if ((int)enumValue < 1) { return Error("请求数据异常，请重试"); }
            return Ok(await _customerAccountService.CreateDefault(enumValue, customerId));
        }
        public async Task<IActionResult> GetPager(QueryModel queryModel)
        {
            return Ok(await _customerAccountService.GetPager(queryModel));
        }

        public async Task<IActionResult> Update(Finance_Customer_Account account)
        {
            await _customerAccountService.Update(account);
            return Ok();
        }

        public async Task<IActionResult> GetLogs(int Id)
        {

            return Ok(await _customerAccountService.GetLogs(Id));
        }
        #endregion
        #region 授信相关

        /// <summary>
        /// 申请授信额度
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<IActionResult> SubmitCustomerAccountQuota(Finance_Customer_Account_ApplyRecord model)
        {
            var json = await _customerAccountService.AddAccountQuotaApply(model);
            return Ok(json.error ?? json.message);
        }

        //测试Api
        public async Task<IActionResult> DingTalkStartProcessEvent(string code)
        {
            string msg = await _customerAccountService.DingTalkStartProcessEvent(code);
            return Ok(msg);
        }
        public async Task<IActionResult> GetAccountQuotaPager(QueryModel queryModel)
        {
            return Ok(await _customerAccountService.GetAccountQuotaPager(queryModel));
        }
        public async Task<IActionResult> ApplySubmit(ApplySubmitRequest model)
        {
            if (model.applyStatu < 1) { return Error("数据请求异常，请重试"); }
            await _customerAccountService.ApplySubmit(model);
            return Ok();
        }
        /// <summary>
        /// 授信到期还原
        /// </summary>
        /// <param name="startTime">大于等于</param>
        /// <param name="endTime">小于</param>
        /// <returns></returns>
        public async Task<IActionResult> QuotaReduction(DateTime startTime, DateTime endTime)
        {
            ApiJsonModel json = await _customerAccountService.QuotaReduction(startTime, endTime);
            return Ok(json.error ?? json.message);
        }
        #endregion
    }
}
