using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Ropin.Inspection.Api.Common;
using Ropin.Inspection.Model;
using Ropin.Inspection.Model.SearchModel;
using Ropin.Inspection.Model.ViewModel;
using Ropin.Inspection.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Ropin.Inspection.Api.Controllers
{
    public class ReportController : BaseController
    {
        public ILogger<ReportController> _logger { get; }
        private readonly IReportService _service;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="service"></param>
        /// <param name="logger"></param>
        public ReportController(IReportService service, ILogger<ReportController> logger)
        {
            _service = service;
            _logger = logger;
        }

        /// <summary>
        /// 通过ID获取报表信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetReportDataAsync/{id}")]
        public async Task<ApiResult> GetReportDataAsync(Guid id)
        {
            if (Guid.Empty == id)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                object Report = await _service.GetReportDataByIdAsync(id);
                return new ApiResult<object>(Report);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }



        /// <summary>
        /// 条件获取报表记录,不包含报表数据,带分页
        /// </summary>
        /// <param name="searchModel"></param>
        /// <returns></returns>
        [HttpPost("GetWithoutReportData")]
        public async Task<ApiResult> GetWithoutReportData(ReportSearchModel searchModel)
        {
            if (searchModel == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                var reportList = await _service.GetWithoutReportData(searchModel);
                return new ApiResult<PagesModel<ReportViewModel>>(new PagesModel<ReportViewModel>(reportList?.ToList(), searchModel));
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 更新巡检记录
        /// </summary>
        /// <param name="id"></param>
        /// <param name="updateModel"></param>
        /// <returns></returns>
        [HttpPut("UpdateReportAsync/{id}")]
        public async Task<ApiResult> UpdateReportAsync(Guid id, ReportUpdateViewModel updateModel)
        {
            if (Guid.Empty == id|| updateModel == null)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _service.UpdateAsync(id, updateModel);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("DeleteReportAsync/{id}")]
        public async Task<ApiResult> DeleteReportAsync(Guid id)
        {
            if (Guid.Empty == id)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _service.DeleteAsync(id);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

    }
}