using System;
using System.Collections.Generic;

using IRaCIS.Api.Filter;
using IRaCIS.Application.Interfaces;
using IRaCIS.Application.ViewModels;
using IRaCIS.Core.Application.Contracts.RequestAndResponse;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace IRaCIS.Api.Controllers.Pay
{
    /// <summary>
    /// Financial 支撑信息---职称付费价格
    /// </summary>
    [Route("rankPrice")]
    [ApiController, Authorize, ApiExplorerSettings(GroupName = "Financial")]
    public class RankPriceController : ControllerBase
    {
        private readonly IRankPriceService _rankPriceService;
        private readonly ICalculateService _calculateService;
        private readonly IReviewerPayInfoService _reviewerPayInfoService;
        public RankPriceController(IRankPriceService rankPriceService,
            ICalculateService calculateService, IReviewerPayInfoService reviewerPayInfoService)
        {
            _rankPriceService = rankPriceService;
            _calculateService = calculateService;
            _reviewerPayInfoService = reviewerPayInfoService;
        }

        /// <summary>
        /// 添加或更新 职称单价[AUTH]
        /// </summary>

        [LogFilter]
        [HttpPost, Route("addOrUpdateRankPrice")]
        public IResponseOutput AddOrUpdateRankPrice(RankPriceCommand addOrUpdateModel)
        {
            if (addOrUpdateModel.Id != Guid.Empty&& addOrUpdateModel.Id !=null)
            {
                var needCalReviewerIds = _reviewerPayInfoService.GetReviewerIdByRankId(Guid.Parse(addOrUpdateModel.Id.ToString()));
                var calcList = _calculateService.GetNeedCalculateReviewerList(Guid.Empty, string.Empty);

                foreach (var item in calcList)
                {
                    if (item != null && needCalReviewerIds.Contains(item.DoctorId))
                    {
                        _calculateService.CalculateMonthlyPayment(new CalculateDoctorAndMonthDTO()
                        {
                            NeedCalculateReviewers = new List<Guid>()
                    {
                        item.DoctorId
                    },
                            CalculateMonth = DateTime.Parse(item.YearMonth)
                        }, User.FindFirst("id").Value);
                    }
                }
            }
            var userId = Guid.Parse(User.FindFirst("id").Value);
            return _rankPriceService.AddOrUpdateRankPrice(addOrUpdateModel, userId);
        }

        /// <summary>
        /// 获取职称单价列表
        /// </summary>

        [HttpPost, Route("getRankPriceList")]
        public IResponseOutput<PageOutput<RankPriceDTO>> GetRankPriceList(RankPriceQueryDTO queryParam)
        {
            return ResponseOutput.Ok(_rankPriceService.GetRankPriceList(queryParam));
        }

        /// <summary>
        /// 删除职称单价记录
        /// </summary>

        [HttpDelete, Route("deleteRankPrice/{id:guid}")]
        public IResponseOutput DeleteRankPrice(Guid id)
        {
            return _rankPriceService.DeleteRankPrice(id);
        }

        /// <summary>
        /// 获取职称字典数据
        /// </summary>

        [HttpGet, Route("getRankDic")]
        public IResponseOutput<List<RankDic>> GetRankDic()
        {
            return ResponseOutput.Ok(_rankPriceService.GetRankDic());
        }
    }
}