using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Ropin.Inspection.Api.Common;
using Ropin.Inspection.Api.Controllers;
using Ropin.Inspection.Model;
using Ropin.Inspection.Model.Entities;
using Ropin.Inspection.Model.ViewModel;
using Ropin.Inspection.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Ropin.Inspection.Api
{

    public class TmtnDevOperateRecordController : BaseController
    {
        public ILogger<TmtnDevOperateRecordController> _logger { get; }
        private readonly ITmtnDevOperateRecordService _TmtnDevOperateRecordService;
        
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="TmtnDevOperateRecordService"></param>
        /// <param name="logger"></param>
        public TmtnDevOperateRecordController(ITmtnDevOperateRecordService TmtnDevOperateRecordService, ILogger<TmtnDevOperateRecordController> logger)
        {
            _TmtnDevOperateRecordService = TmtnDevOperateRecordService;
            _logger = logger;
            
        }

        /// <summary>
        /// 取运维工单统计
        /// </summary>
        /// <param name="storeCode"></param>
        /// <returns></returns>
        [HttpGet("GetDevOpsStatisticsAsync/{storeCode}")]
        public async Task<ApiResult> GetDevOpsStatisticsAsync(string storeCode)
        {
            if (string.IsNullOrEmpty(storeCode))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                var content = await _TmtnDevOperateRecordService.GetDevOpsStatisticsAsync(storeCode);
                return new ApiResult<RepairStatistics>(content);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
        /// <summary>
        /// 维保次数统计,大屏
        /// </summary>
        /// <param name="storeCode"></param>
        /// <returns></returns>
        [HttpGet("GetDevOpsStatisticsFullScreenAsync/{storeCode}")]
        [AllowAnonymous]
        public async Task<IEnumerable<FullScreenRecordItem>> GetDevOpsStatisticsFullScreenAsync(string storeCode)
        {
            if (string.IsNullOrEmpty(storeCode))
            {
                return new FullScreenRecordItem[0];
            }
            try
            {
                var content = await _TmtnDevOperateRecordService.GetDevOpsStatisticsFullScreenAsync(storeCode);
                return content;
            }
            catch
            {
                return new FullScreenRecordItem[0];
            }
        }

        /// <summary>
        /// 通过id获取设备操作记录信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetDevOperateRecordAsync/{id}")]
        public async Task<ApiResult> GetDevOperateRecordAsync(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                var content = await _TmtnDevOperateRecordService.GetConditionAsync(new TmtnDevOperateRecordSearchModel { C_ID = id });
                return new ApiResult<TmtnDevOperateRecordViewModel>(content.FirstOrDefault());
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 获取所有设备操作记录
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetDevOperateRecordsAsync")]
        public async Task<ApiResult> GetDevOperateRecordsAsync()
        {
            try
            {
                var contentList = await _TmtnDevOperateRecordService.GetAllAsync();
                return new ApiResult<IEnumerable<TmtnDevOperateRecordViewModel>>(contentList);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 通过设备操作记录名称条件查询
        /// </summary>
        /// <param name="searchModel"></param>
        /// <returns></returns>
        [HttpPost("GetDevOperateRecordsByAsync")]
        public async Task<ApiResult> GetDevOperateRecordsByAsync(TmtnDevOperateRecordSearchModel searchModel)
        {
            if (searchModel == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            searchModel.IsPagination = false;
            try
            {
                var contentList = await _TmtnDevOperateRecordService.GetConditionAsync(searchModel);
                return new ApiResult<PagesModel<TmtnDevOperateRecordViewModel>>(new PagesModel<TmtnDevOperateRecordViewModel>(contentList, searchModel));
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 创建设备操作记录
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        [HttpPost("CreateDevOperateRecordAsync")]
        public async Task<ApiResult> CreateDevOperateRecordAsync(TmtnDevOperateRecordViewModel content)
        {
            if (content == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                await _TmtnDevOperateRecordService.CreateOneAsync(content);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

        /// <summary>
        /// 删除设备操作记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("DeleteDevOperateRecordAsync/{id}")]
        public async Task<ApiResult> DeleteDevOperateRecordAsync(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _TmtnDevOperateRecordService.DeleteAsync(id);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }
        /// <summary>
        /// 更新设备操作记录
        /// </summary>
        /// <param name="id"></param>
        /// <param name="updateModel"></param>
        /// <returns></returns>
        [HttpPut("UpdateDevOperateRecordAsync/{id}")]
        public async Task<ApiResult> UpdateDevOperateRecordAsync(string id, TmtnDevOperateRecordUpdateModel updateModel)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _TmtnDevOperateRecordService.UpdateAsync(id, updateModel);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

        /// <summary>
        /// 通过维保id获取维保列表
        /// </summary>
        /// <param name="devId"></param>
        /// <param name="repairId"></param>
        /// <returns></returns>
        [HttpGet("GetDevOpsRecordItemAsync/{devId}/{repairId}")]
        public async Task<ApiResult> GetRepairOrderItemViewAsync(string devId, string repairId)
        {
            if (string.IsNullOrEmpty(devId))
                return new ApiResult(ReturnCode.GeneralError);
            if (string.IsNullOrEmpty(repairId))
                return new ApiResult(ReturnCode.GeneralError);
            try
            {
                var content = await _TmtnDevOperateRecordService.GetDevOpsRecordItemAsync(devId, repairId);
                return new ApiResult<TmtnDevOpsRecordItemDetailViewMode>(content);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }

        }

    }
}