using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using MadRunFabric.Common;
using Microsoft.AspNetCore.Authorization;
using Api.SignToken;
using Microsoft.Extensions.Logging;
using DistributionApi.IRepositories;
using System.Security.Claims;
using Microsoft.Extensions.Configuration;

namespace DistributionApi.Controllers.reportmanage
{
    /// <summary>
    /// 配送报表统计
    /// </summary>
    [Authorize]
    [ApiVersion("6.0")]
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class DistributionWorkOrderReportController : BaseController
    {
        private readonly ILogger<DistributionWorkOrderReportController> _logger;
        private readonly IConfiguration _configuration;
        private readonly ISys_User_AccountRepository _sys_user_accountRepository;
        private readonly ISignTokenService _signTokenService;
        private readonly IDistributionWorkOrderReportRepository _distributionworkorderreportRepository;

        public DistributionWorkOrderReportController(
            ILogger<DistributionWorkOrderReportController> logger,
            IConfiguration configuration,
            ISys_User_AccountRepository sys_user_accountRepository,
            ISignTokenService signTokenService,
            IDistributionWorkOrderReportRepository distributionworkorderreportRepository)
        {
            _logger = logger;
            _configuration = configuration;
            _sys_user_accountRepository = sys_user_accountRepository;
            _signTokenService = signTokenService;
            _distributionworkorderreportRepository = distributionworkorderreportRepository;
        }

        #region 物品种类
        /// <summary>
        /// 物品种类运送量 - 总体统计报表
        /// </summary>
        /// <param name="projectid">项目id</param>
        /// <param name="departmentname">科室</param>
        /// <param name="usercode">用户code</param>
        /// <param name="stime">开始时间</param>
        /// <param name="etime">结束时间</param>
        /// <returns></returns> 
        [HttpGet("getwocountforgoods")]
        public IActionResult GetGoodsCount(string projectid, string departmentname, string usercode, string stime, string etime)
        {
            string nowusercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            //var userstr = _signTokenService.GetUserInfoAsync(nowusercode).Result;
            //var userinfo = userstr.ToJObject()["data"].ToString().ToObject<UserInfoModel>();
            
            var userinfos = _sys_user_accountRepository.GetUserInfo(nowusercode);
            var userinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());

            var result = _distributionworkorderreportRepository.GetWOCountForGoods(projectid, departmentname, usercode, stime, etime, userinfo);

            return Success("获取成功", result);
        }

        /// <summary>
        /// 物品运送量 - 明细统计报表
        /// </summary>
        /// <param name="projectid">项目id</param>
        /// <param name="departmentname">科室</param>
        /// <param name="usercode">用户code</param>
        /// <param name="stime">开始时间</param>
        /// <param name="etime">结束时间</param>
        /// <returns></returns> 
        [HttpGet("getwocountforgoodsdetail")]
        public IActionResult GetGoodsDetailCount(string projectid, string departmentname, string usercode, string stime, string etime)
        {
            string nowusercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            //var userstr = _signTokenService.GetUserInfoAsync(nowusercode).Result;
            //var userinfo = userstr.ToJObject()["data"].ToString().ToObject<UserInfoModel>();
            
            var userinfos = _sys_user_accountRepository.GetUserInfo(nowusercode);
            var userinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());

            var result = _distributionworkorderreportRepository.GetWOCountForGoodsDetail(projectid, departmentname, usercode, stime, etime, userinfo);

            return Success("获取成功", result);
        }

        #endregion

        #region 配送工单

        /// <summary>
        /// 医务科室运送报单量 - 统计报表
        /// </summary>
        /// <param name="projectid">项目id</param>
        /// <param name="usercode">用户code</param>
        /// <param name="stime">开始时间</param>
        /// <param name="etime">结束时间</param>
        /// <returns></returns> 
        [HttpGet("getwocountfordepartment")]
        public IActionResult GetWOForDepartmentCount(string projectid, string usercode, string stime, string etime)
        {
            string nowusercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            //var userstr = _signTokenService.GetUserInfoAsync(nowusercode).Result;
            //var userinfo = userstr.ToJObject()["data"].ToString().ToObject<UserInfoModel>();
            
            var userinfos = _sys_user_accountRepository.GetUserInfo(nowusercode);
            var userinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());

            var result = _distributionworkorderreportRepository.GetWOCountForDepartment(projectid, usercode, stime, etime, userinfo);

            return Success("获取成功", result);
        }

        /// <summary>
        /// 员工运送工作量 - 统计报表
        /// </summary>
        /// <param name="projectid">项目id</param>
        /// <param name="usercode">用户code</param>
        /// <param name="stime">开始时间</param>
        /// <param name="etime">结束时间</param>
        /// <returns></returns> 
        [HttpGet("getwocountbyuser")]
        public IActionResult GetWOForUserCount(string projectid, string usercode, string stime, string etime)
        {
            string nowusercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            //string rolecode = User.Claims.FirstOrDefault(c => c.Type == "RoleCode").Value;
            //var userstr = _signTokenService.GetUserInfoAsync(nowusercode).Result;
            //var userinfo = userstr.ToJObject()["data"].ToString().ToObject<UserInfoModel>();
            
            var userinfos = _sys_user_accountRepository.GetUserInfo(nowusercode);
            var userinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());

            var result = _distributionworkorderreportRepository.GetWOCountForUser(projectid, usercode, stime, etime, userinfo);

            return Success("获取成功", result);
        }

        #endregion

        #region 工单评价
        /// <summary>
        /// 员工满意度评价 - 统计报表
        /// </summary>
        /// <param name="projectid">项目id</param>
        /// <param name="usercode">用户code</param>
        /// <param name="stime">开始时间</param>
        /// <param name="etime">结束时间</param>
        /// <returns></returns> 
        [HttpGet("getwoevaluatecountforuser")]
        public IActionResult GetWOEvaluateForUserCount(string projectid, string usercode, string stime, string etime)
        {
            string nowusercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            //string rolecode = User.Claims.FirstOrDefault(c => c.Type == "RoleCode").Value;
            //var userstr = _signTokenService.GetUserInfoAsync(nowusercode).Result;
            //var userinfo = userstr.ToJObject()["data"].ToString().ToObject<UserInfoModel>();
            
            var userinfos = _sys_user_accountRepository.GetUserInfo(nowusercode);
            var userinfo = JsonHelper.JsonToEntity<UserAccountInfoModel>(userinfos.ToJson());

            var result = _distributionworkorderreportRepository.GetWOEvaluateCountForUser(projectid, usercode, stime, etime, userinfo);

            return Success("获取成功", result);
        }

        #endregion

    }
}