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 Microsoft.Extensions.Logging;
using Api.SignToken;
using DistributionApi.IRepositories;
using System.Security.Claims;
using Microsoft.Extensions.Configuration;

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

        public DistributionAppChartController(            
            ILogger<DistributionWorkOrderReportController> logger,
            IConfiguration configuration,
            ISys_User_AccountRepository sys_user_accountRepository,
            ISignTokenService signTokenService,
            IDistributionAppChartRepository distributionappchartRepository)
        {
            _logger = logger;
            _configuration = configuration;
            _sys_user_accountRepository = sys_user_accountRepository;
            _signTokenService = signTokenService;
            _distributionappchartRepository = distributionappchartRepository;
        }

        #region 配送工单统计
        /// <summary>
        /// 统计报表 - 工单，满意度，科室
        /// </summary>
        /// <param name="projectid"></param>
        /// <param name="stime"></param>
        /// <param name="etime"></param>
        /// <param name="datetype"></param>
        /// <returns></returns>
        [HttpGet("getworkordercount")]
        public IActionResult GetWorkOrderCount(string projectid, int datetype = -1)
        {
            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());

            string stime = string.Empty, etime = string.Empty;
            //获取当天、本周、本月、本年、累计时间
            if (datetype != -1)
            {
                GetStartEndTime(datetype, out stime, out etime);
            }

            var result = _distributionappchartRepository.GetWorkOrderCount(projectid, stime, etime, userinfo);

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

        #endregion

        #region 公示系统
        /// <summary>
        /// 各科室下单数量
        /// </summary>
        /// <param name="projectid"></param>
        /// <param name="stime"></param>
        /// <param name="etime"></param>
        /// <param name="datetype"></param>
        /// <returns></returns>
        [HttpGet("getdepartmentwocount")]
        public IActionResult GetDepartmentWOCount(string projectid, int datetype = -1)
        {
            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());

            string stime = string.Empty, etime = string.Empty;
            //获取当天、本周、本月、本年、累计时间
            if (datetype != -1)
            {
                GetStartEndTime(datetype, out stime, out etime);
            }

            var result = _distributionappchartRepository.GetDepartmentWOCount(projectid, stime, etime, userinfo);

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

        /// <summary>
        /// 配送物品前五
        /// </summary>
        /// <param name="projectid"></param>
        /// <param name="stime"></param>
        /// <param name="etime"></param>
        /// <param name="datetype"></param>
        /// <returns></returns>
        [HttpGet("getgoodswocount")]
        public IActionResult GetGoodsWOCount(string projectid, int datetype = -1)
        {
            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());

            string stime = string.Empty, etime = string.Empty;
            //获取当天、本周、本月、本年、累计时间
            if (datetype != -1)
            {
                GetStartEndTime(datetype, out stime, out etime);
            }

            var result = _distributionappchartRepository.GetGoodsWOCount(projectid, stime, etime, userinfo);

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

        #endregion


        /// <summary>
        /// 获取当天、本周、本月、本年、累计时间
        /// </summary>
        /// <param name="datetype"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        public void GetStartEndTime(int datetype, out string start, out string end)
        {
            start = string.Empty;
            end = string.Empty;

            var datenow = DateTime.Now;
            var datestart = datenow;
            var dateend = datenow;
            switch (datetype)
            {
                case 0://当天
                    break;
                case 1://本周
                    int n = (int)datenow.DayOfWeek == 0 ? 7 : (int)datenow.DayOfWeek;
                    datestart = datenow.AddDays(1 - n);//本周周一
                    dateend = datestart.AddDays(6);//本周周日
                    break;
                case 2://本月
                    datestart = datenow.AddDays(1 - datenow.Day);  //本月月初  
                    dateend = datestart.AddMonths(1).AddDays(-1);  //本月月末  
                    break;
                case 3://本年
                    datestart = new DateTime(datenow.Year, 1, 1);  //本年年初  
                    dateend = new DateTime(datenow.Year, 12, 31);  //本年年末  
                    break;
                case 4://全部
                    break;
                default:
                    break;
            }
            if (datetype != 4)
            {
                start = datestart.ToString("yyyy-MM-dd");
                end = dateend.ToString("yyyy-MM-dd");
            }
        }


    }
}