using DS.Module.Core;
using DS.WMS.Core.Application.Dtos;
using DS.WMS.Core.Fee.Dtos;
using DS.WMS.Core.Invoice.Dto;
using DS.WMS.Core.Invoice.Dtos;
using DS.WMS.Core.Invoice.Entity;
using DS.WMS.Core.Invoice.Interface;
using Microsoft.AspNetCore.Mvc;

namespace DS.WMS.FeeApi.Controllers
{
    /// <summary>
    /// 自由开票API
    /// </summary>
    public class FreeInvoiceController : ApiController
    {
        readonly IFreeInvoiceService _service;

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="service"></param>
        public FreeInvoiceController(IFreeInvoiceService service)
        {
            _service = service;
        }

        /// <summary>
        /// 获取发票详情
        /// </summary>
        /// <param name="id">发票ID</param>
        /// <returns></returns>
        [HttpGet, Route("Get")]
        public async Task<DataResult<InvoiceDto>> GetAsync(long id)
        {
            return await _service.GetAsync(id);
        }

        /// <summary>
        /// 获取待开票的业务列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns>注意!!【费用范围】需通过 OtherQueryCondition 字段传入</returns>
        [HttpPost, Route("GetBizList")]
        public async Task<DataResult<List<BizInvoiceApplication>>> GetBizListAsync([FromBody] PageRequest<FeeRange?> request)
        {
            return await _service.GetBizListAsync(request);
        }

        /// <summary>
        /// 根据业务编号及类型获取费用记录
        /// </summary>
        /// <param name="items">业务ID与业务类型</param>
        /// <returns></returns>
        [HttpPost, Route("GetFees")]
        public async Task<DataResult<InvoiceApplicaitonBiz>> GetFeesAsync([FromBody] BizItem[] items)
        {
            return await _service.GetFeesAsync(items);
        }

        /// <summary>
        /// 提交发票开票
        /// </summary>
        /// <param name="request">请求参数</param>
        /// <returns></returns>
        [HttpPost, Route("Save")]
        public async Task<DataResult<Invoice>> SaveAsync(InvoiceRequest<Invoice> request)
        {
            if (!ModelState.IsValid)
                return DataResult<Invoice>.Failed(ModelState.GetErrorMessage(), MultiLanguageConst.IllegalRequest);

            return await _service.SaveAsync(request);
        }
    }
}