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

namespace EquipmentApi.Controllers.reportmanage
{
    /// <summary>
    /// App 设备运维报表
    /// </summary>
    [Authorize]
    [ApiVersion("6.0")]
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class RunningRepairController : BaseController
    {
        private readonly ILogger<RunningRepairController> _logger;
        private readonly IConfiguration _configuration;
        private readonly ISys_User_AccountRepository _sys_user_accountRepository;
        private readonly IRunningRepairRepository _runningrepairRepository;
        private readonly ISignTokenService _signTokenService;
        private readonly IMapper _mapper;

        public RunningRepairController(
            ILogger<RunningRepairController> logger,
            IConfiguration configuration,
            ISys_User_AccountRepository sys_user_accountRepository,
            IRunningRepairRepository runningrepairRepository,
            ISignTokenService signTokenService, IMapper mapper
            )
        {
            _logger = logger;
            _configuration = configuration;
            _sys_user_accountRepository = sys_user_accountRepository;
            _runningrepairRepository = runningrepairRepository;
            _signTokenService = signTokenService;
            _mapper = mapper;
        }

        #region Pc统计
        /// <summary>
        /// 项目统计报表（列表、统计） - 报表
        /// </summary>
        /// <returns></returns>
        [HttpGet("getprojectlistandstatisticsbypage")]
        public IActionResult GetProjectListAndStatisticsByPage(string keyword, string projectid, string start, string end, int pageIndex = 1, int pageSize = 10)
        {
            //if (string.IsNullOrEmpty(start) && string.IsNullOrEmpty(end))
            //{
            //    var date = DateTime.Now;
            //    start = new DateTime(date.Year, date.Month, 1).ToString("yyyy-MM-dd");
            //    end = date.ToString("yyyy-MM-dd");
            //}

            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());

            int recordCount = 0;
            var result = _runningrepairRepository.GetProjectListAndStatisticsByPage(keyword, projectid, start,end, pageIndex, pageSize, out recordCount, userinfo);
            var obj = new
            {
                rows = result,
                total = recordCount,
            };
            return Success("获取成功", obj);
        }

        /// <summary>
        /// 项目工单来源统计报表（列表、统计） - 报表
        /// </summary>
        /// <returns></returns>
        [HttpGet("getprojectorderlistandstatisticsbypage")]
        public IActionResult GetProjectRepairOrderListAndStatisticsByPage(string keyword, string projectid, string start,string end, int pageIndex = 1, int pageSize = 10)
        {
            if (string.IsNullOrEmpty(start) && string.IsNullOrEmpty(end))
            {
                var date = DateTime.Now;
                start = new DateTime(date.Year, date.Month, 1).ToString("yyyy-MM-dd");
                end = date.ToString("yyyy-MM-dd");
            }

            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());

            int recordCount = 0;
            var result = _runningrepairRepository.GetProjectRepairOrderListAndStatisticsByPage(keyword, projectid, start, end, pageIndex, pageSize, out recordCount, userinfo);
            var obj = new
            {
                rows = result,
                total = recordCount,
            };
            return Success("获取成功", obj);
        }

        /// <summary>
        /// 项目报修统计报表（列表、统计） - 报表
        /// </summary>
        /// <returns></returns>
        [HttpGet("getprojectrepairlistandstatisticsbypage")]
        public IActionResult GetProjectRepairListAndStatisticsByPage(string keyword, string projectid, string start, string end, int pageIndex = 1, int pageSize = 10)
        {
            if (string.IsNullOrEmpty(start) && string.IsNullOrEmpty(end))
            {
                var date = DateTime.Now;
                start = new DateTime(date.Year, date.Month, 1).ToString("yyyy-MM-dd");
                end = date.ToString("yyyy-MM-dd");
            }

            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());

            int recordCount = 0;
            var result = _runningrepairRepository.GetProjectRepairListAndStatisticsByPage(keyword, projectid, start, end, pageIndex, pageSize, out recordCount, userinfo);
            var obj = new
            {
                rows = result,
                total = recordCount,
            };
            return Success("获取成功", obj);
        }

        /// <summary>
        /// 设备状态分析报表（列表、统计） - 报表
        /// </summary>
        /// <returns></returns>
        [HttpGet("getequipmentstatestatisticsbypage")]
        public IActionResult GetEquipmentStateStatisticsByPage(string keyword, string projectid, string start, string end, int pageIndex = 1, int pageSize = 10)
        {
            if (string.IsNullOrEmpty(start) && string.IsNullOrEmpty(end))
            {
                var date = DateTime.Now;
                start = new DateTime(date.Year, date.Month, 1).ToString("yyyy-MM-dd");
                end = date.ToString("yyyy-MM-dd");
            }

            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());

            int recordCount = 0;
            var result = _runningrepairRepository.GetEquipmentStateStatisticsByPage(keyword, projectid, start, end, pageIndex, pageSize, out recordCount, userinfo);
            var obj = new
            {
                rows = result,
                total = recordCount,
            };
            return Success("获取成功", obj);
        }

        /// <summary>
        /// 设备保养分析报表（列表、统计） - 报表
        /// </summary>
        /// <returns></returns>
        [HttpGet("getequipmentmaintainstatisticsbypage")]
        public IActionResult GetEquipmentMaintainStatisticsByPage(string keyword, string projectid, string start, string end, int pageIndex = 1, int pageSize = 10)
        {
            if (string.IsNullOrEmpty(start) && string.IsNullOrEmpty(end))
            {
                var date = DateTime.Now;
                start = new DateTime(date.Year, date.Month, 1).ToString("yyyy-MM-dd");
                end = date.ToString("yyyy-MM-dd");
            }

            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());

            int recordCount = 0;
            var result = _runningrepairRepository.GetEquipmentMaintainStatisticsByPage(keyword, projectid, start, end, pageIndex, pageSize, out recordCount, userinfo);
            var obj = new
            {
                rows = result,
                total = recordCount,
            };
            return Success("获取成功", obj);
        }

        /// <summary>
        /// 耗材统计报表（列表、统计） - 报表
        /// </summary>
        /// <returns></returns>
        [HttpGet("getequipmentmaterialstatisticsbypage")]
        public IActionResult GetEquipmentMaterialStatisticsByPage(string keyword, string projectid, string start, string end, int pageIndex = 1, int pageSize = 10)
        {
            return Success("获取成功", null);
        }

        /// <summary>
        /// 人员满意度统计报表（列表、统计） - 报表
        /// </summary>
        /// <returns></returns>
        [HttpGet("getuserevaluatestatisticsbypage")]
        public IActionResult GetUserEvaluateStatisticsByPage(string keyword, string projectid, string start, string end, int pageIndex = 1, int pageSize = 10)
        {
            if (string.IsNullOrEmpty(start) && string.IsNullOrEmpty(end))
            {
                var date = DateTime.Now;
                start = new DateTime(date.Year, date.Month, 1).ToString("yyyy-MM-dd");
                end = date.ToString("yyyy-MM-dd");
            }

            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());

            int recordCount = 0;
            var result = _runningrepairRepository.GetUserEvaluateStatisticsByPage(keyword, projectid, start, end, pageIndex, pageSize, out recordCount, userinfo);
            var obj = new
            {
                rows = result,
                total = recordCount,
            };
            return Success("获取成功", obj);
        }

        /// <summary>
        /// 人员维修及时率统计报表（列表、统计） - 报表
        /// </summary>
        /// <returns></returns>
        [HttpGet("getuserpairetimeratestatisticsbypage")]
        public IActionResult GetUseRpairetimerateStatisticsByPage(string keyword, string projectid, string start, string end, int pageIndex = 1, int pageSize = 10)
        {
            if (string.IsNullOrEmpty(start) && string.IsNullOrEmpty(end))
            {
                var date = DateTime.Now;
                start = new DateTime(date.Year, date.Month, 1).ToString("yyyy-MM-dd");
                end = date.ToString("yyyy-MM-dd");
            }

            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());

            int recordCount = 0;
            var result = _runningrepairRepository.GetUseRpairetimerateStatisticsByPage(keyword, projectid, start, end, pageIndex, pageSize, out recordCount, userinfo);
            var obj = new
            {
                rows = result,
                total = recordCount,
            };
            return Success("获取成功", obj);
        }

        /// <summary>
        /// 人员个人工作量统计报表（列表、统计） - 报表
        /// </summary>
        /// <returns></returns>
        [HttpGet("getuserworkstatisticsbypage")]
        public IActionResult GetUseWorkStatisticsByPage(string keyword, string projectid, string start, string end, int pageIndex = 1, int pageSize = 10)
        {
            if (string.IsNullOrEmpty(start) && string.IsNullOrEmpty(end))
            {
                var date = DateTime.Now;
                start = new DateTime(date.Year, date.Month, 1).ToString("yyyy-MM-dd");
                end = date.ToString("yyyy-MM-dd");
            }

            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());

            int recordCount = 0;
            var result = _runningrepairRepository.GetUseWorkStatisticsByPage(keyword, projectid, start, end, pageIndex, pageSize, out recordCount, userinfo);
            var obj = new
            {
                rows = result,
                total = recordCount,
            };
            return Success("获取成功", obj);
        }

        /// <summary>
        /// 人员个人工作量统计报表（列表、统计） - 报表
        /// </summary>
        /// <returns></returns>
        [HttpGet("getuserworkcount")]
        public IActionResult GetUseWorkCount(string keyword, string projectid, string start, string end)
        {
            if (string.IsNullOrEmpty(start) && string.IsNullOrEmpty(end))
            {
                var date = DateTime.Now;
                start = new DateTime(date.Year, date.Month, 1).ToString("yyyy-MM-dd");
                end = date.ToString("yyyy-MM-dd");
            }

            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 = _runningrepairRepository.GetUseWorkCount(keyword, projectid, start, end, userinfo);

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

        /// <summary>
        /// 人员个人接单量统计报表（列表、统计） - 报表
        /// </summary>
        /// <returns></returns>
        [HttpGet("getuseracceptcount")]
        public IActionResult GetUserAcceptCount(string keyword, string projectid, string start, string end, int type=1)
        {
            if (string.IsNullOrEmpty(start) && string.IsNullOrEmpty(end))
            {
                var date = DateTime.Now;
                start = new DateTime(date.Year, date.Month, 1).ToString("yyyy-MM-dd");
                end = date.ToString("yyyy-MM-dd");
            }

            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 = _runningrepairRepository.GetUserAcceptCount(keyword, projectid, type, start, end, userinfo);

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


        /// <summary>
        /// 设备保修/维修状态（列表、统计） - 报表 //
        /// </summary>
        /// <returns></returns>
        //[HttpGet("getrepairelistandstatisticsbypagepc")]
        //public IActionResult GetRepaireListAndStatisticsByPagePc(string systemid, int pageIndex = 1, int pageSize = 10)
        //{
        //    int recordCount = 0;
        //    var result = _runningrepairRepository.GetListAndStatisticsByPage(systemid, pageIndex, pageSize, out recordCount);
        //    var obj = new
        //    {
        //        rows = result,
        //        total = recordCount,
        //    };
        //    return Success("获取成功", obj);
        //}

        /// <summary>
        /// 满意度统计详情（列表、统计） - 报表//
        /// </summary>
        /// <returns></returns>
        //[HttpGet("getrepairevaluatelistandstatisticsbypagepc")]
        //public IActionResult GetRepairEvaluateListAndStatisticsByPagePc(int pageIndex = 1, int pageSize = 10)
        //{
        //    int recordCount = 0;
        //    var result = _runningrepairRepository.GetRepairEvaluateListAndStatisticsByPagePc(pageIndex, pageSize, out recordCount);
        //    var obj = new
        //    {
        //        rows = result,
        //        total = recordCount,
        //    };
        //    return Success("获取成功", obj);
        //}

        /// <summary>
        /// 设备保养（列表、统计） - 报表//
        /// </summary>
        /// <returns></returns>
        //[HttpGet("getmaintainlistandstatisticsbypagepc")]
        //public IActionResult GetMaintainListAndStatisticsByPagePc(string systemid, int pageIndex = 1, int pageSize = 10)
        //{
        //    int recordCount = 0;
        //    var result = _runningrepairRepository.GetMaintainListAndStatisticsByPagePc(systemid, pageIndex, pageSize, out recordCount);
        //    var obj = new
        //    {
        //        rows = result,
        //        total = recordCount,
        //    };
        //    return Success("获取成功", obj);
        //}

        /// <summary>
        /// 设备状态详情（列表、统计） - 报表//
        /// </summary>
        /// <returns></returns>
        //[HttpGet("getrepairstatelistandstatisticsbypagepc")]
        //public IActionResult GetRepairStateListAndStatisticsByPagePc(string systemid, int pageIndex = 1, int pageSize = 10)
        //{
        //    int recordCount = 0;
        //    var result = _runningrepairRepository.GetRepairStateListAndStatisticsByPagePc(systemid, pageIndex, pageSize, out recordCount);
        //    var obj = new
        //    {
        //        rows = result,
        //        total = recordCount,
        //    };
        //    return Success("获取成功", obj);
        //}

        #endregion

        #region App统计

        /// <summary>
        /// 维修工维修数量排名
        /// </summary>
        /// <returns></returns>
        [HttpGet("getuserrepaircountreport")]
        public IActionResult GetUserRepairCountReport(string projectid, int datetype = 0, int top = 0)
        {
            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 start = string.Empty; string end = string.Empty;
            GetStartEndTime(datetype, out start, out end);

            var result = _runningrepairRepository.GetUserRepairCountReport(projectid, start, end, top, userinfo);
            return Success("获取成功", result);
        }

        /// <summary>
        /// 设备运维饼图
        /// </summary>
        /// <returns></returns>
        [HttpGet("getequipmentroundreport")]
        public IActionResult GetEquipmentRoundReport(string projectid, int datetype = 0)
        {
            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 start = string.Empty; string end = string.Empty;
            GetStartEndTime(datetype, out start, out end);

            var obj = _runningrepairRepository.GetEquipmentRoundReport(projectid, start, end, userinfo);
            return Success("获取成功", obj);
        }

        /// <summary>
        /// 来源设备保修/维修状态（列表、统计） - 报表
        /// </summary>
        /// <returns></returns>
        [HttpGet("getsourcelistandstatisticsbypage")]
        public IActionResult GetSourceListAndStatisticsByPage(string projectid, string systemid, int datetype = 0, int pageIndex = 1, int pageSize = 10)
        {
            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 start = string.Empty; string end = string.Empty;
            GetStartEndTime(datetype, out start, out end);

            int recordCount = 0;
            var result = _runningrepairRepository.GetSourceListAndStatisticsByPage(projectid, start, end, systemid, pageIndex, pageSize, out recordCount, userinfo);
            var obj = new
            {
                rows = result,
                total = recordCount,
            };
            return Success("获取成功", obj);
        }

        /// <summary>
        /// 设备保修/维修状态（列表、统计） - 报表
        /// </summary>
        /// <returns></returns>
        [HttpGet("getlistandstatisticsbypage")]
        public IActionResult GetListAndStatisticsByPage(string projectid, string systemid, int datetype = 0, int pageIndex = 1, int pageSize = 10)
        {
            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 start = string.Empty; string end = string.Empty;
            GetStartEndTime(datetype, out start, out end);

            int recordCount = 0;
            var result = _runningrepairRepository.GetListAndStatisticsByPage(projectid, start, end, systemid, pageIndex, pageSize, out recordCount, userinfo);
            var obj = new
            {
                rows = result,
                total = recordCount,
            };
            return Success("获取成功", obj);
        }

        /// <summary>
        /// 设备保修/维修状态（列表、统计） - 报表
        /// </summary>
        /// <returns></returns>
        [HttpGet("getlistandstatisticsbypage2")]
        public IActionResult GetListAndStatisticsByPage2(string projectid, string systemid, int datetype = 0, int pageIndex = 1, int pageSize = 10)
        {
            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 start = string.Empty; string end = string.Empty;
            GetStartEndTime(datetype, out start, out end);

            int recordCount = 0;
            var result = _runningrepairRepository.GetListAndStatisticsByPage(projectid, start, end, systemid, pageIndex, pageSize, out recordCount, userinfo);
            var obj = new
            {
                rows = result,
                total = recordCount,
            };
            return Success("获取成功", obj);
        }

        /// <summary>
        /// 维修及时率统计详情（列表、统计） - 报表
        /// </summary>
        /// <returns></returns>
        [HttpGet("getrepairetimerateReportbyoage")]
        public IActionResult GetRepaireTimeRateReportByPage(string projectid, string systemid, int datetype = 0, int pageIndex = 1, int pageSize = 10)
        {
            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 start = string.Empty; string end = string.Empty;
            GetStartEndTime(datetype, out start, out end);

            int recordCount = 0;
            var result = _runningrepairRepository.GetRepaireTimeRateReportByPage(projectid, start, end, systemid, pageIndex, pageSize, out recordCount, userinfo);
            var obj = new
            {
                rows = result,
                total = recordCount,
            };
            return Success("获取成功", obj);
        }

        /// <summary>
        /// 满意度统计详情（列表、统计） - 报表
        /// </summary>
        /// <returns></returns>
        [HttpGet("getrepairevaluatelistandstatisticsbypage")]
        public IActionResult GetRepairEvaluateListAndStatisticsByPage(string projectid, int datetype = 0, int pageIndex = 1, int pageSize = 10)
        {
            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 start = string.Empty; string end = string.Empty;
            GetStartEndTime(datetype, out start, out end);

            int recordCount = 0;
            var result = _runningrepairRepository.GetRepairEvaluateListAndStatisticsByPage(projectid, start, end, pageIndex, pageSize, out recordCount, userinfo);
            var obj = new
            {
                rows = result,
                total = recordCount,
            };
            return Success("获取成功", obj);
        }

        /// <summary>
        /// 满意度统计详情（列表、统计） - 报表 - 项目主管
        /// </summary>
        /// <returns></returns>
        [HttpGet("getusersevaluatelistandstatisticsbypage")]
        public IActionResult GetUsersEvaluateListAndStatisticsByPage(string projectid, int datetype = 0, int pageIndex = 1, int pageSize = 10)
        {
            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 start = string.Empty; string end = string.Empty;
            GetStartEndTime(datetype, out start, out end);

            int recordCount = 0;
            var result = _runningrepairRepository.GetUsersEvaluateListAndStatisticsByPage(projectid, start, end, pageIndex, pageSize, out recordCount, userinfo);
            var obj = new
            {
                rows = result,
                total = recordCount,
            };
            return Success("获取成功", obj);
        }

        /// <summary>
        /// 耗材统计（列表、统计） - 报表 缺少耗材表？？？？？？？？？？？
        /// </summary>
        /// <returns></returns>
        [HttpGet("getmateriallistandstatisticsbypage")]
        public IActionResult GetMaterialListAndStatisticsByPage(string projectid, int datetype = 0, int pageIndex = 1, int pageSize = 10)
        {
            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 start = string.Empty; string end = string.Empty;
            GetStartEndTime(datetype, out start, out end);

            int recordCount = 0;
            var result = _runningrepairRepository.GetMaterialListAndStatisticsByPage(projectid, start, end, pageIndex, pageSize, out recordCount, userinfo);
            var obj = new
            {
                rows = result,
                total = recordCount,
            };
            return Success("获取成功", obj);
        }

        /// <summary>
        /// 设备保养（列表、统计） - 报表
        /// </summary>
        /// <returns></returns>
        [HttpGet("getmaintainlistandstatisticsbypage")]
        public IActionResult GetMaintainListAndStatisticsByPage(string projectid, string systemid, int datetype = 0, int pageIndex = 1, int pageSize = 10)
        {
            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 start = string.Empty; string end = string.Empty;
            GetStartEndTime(datetype, out start, out end);

            int recordCount = 0;
            var result = _runningrepairRepository.GetMaintainListAndStatisticsByPage(projectid, start, end, systemid, pageIndex, pageSize, out recordCount, userinfo);
            var obj = new
            {
                rows = result,
                total = recordCount,
            };
            return Success("获取成功", obj);
        }

        /// <summary>
        /// 设备状态详情（列表、统计） - 报表
        /// </summary>
        /// <returns></returns>
        [HttpGet("getrepairstatelistandstatisticsbypage")]
        public IActionResult GetRepairStateListAndStatisticsByPage(string projectid, string systemid, int datetype = 0, int pageIndex = 1, int pageSize = 10)
        {
            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 start = string.Empty; string end = string.Empty;
            GetStartEndTime(datetype, out start, out end);

            int recordCount = 0;
            var result = _runningrepairRepository.GetRepairStateListAndStatisticsByPage(projectid, start, end, systemid, pageIndex, pageSize, out recordCount, userinfo);
            var obj = new
            {
                rows = result,
                total = recordCount,
            };
            return Success("获取成功", obj);
        }

        /// <summary>
        /// 设备巡检（列表、统计） - 报表
        /// </summary>
        /// <returns></returns>
        [HttpGet("getrunninglistandstatisticsbypage")]
        public IActionResult GetRunningListAndStatisticsByPage(string projectid, string systemid, int datetype = 0, int pageIndex = 1, int pageSize = 10)
        {
            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 start = string.Empty; string end = string.Empty;
            GetStartEndTime(datetype, out start, out end);

            int recordCount = 0;
            var result = _runningrepairRepository.GetRunningListAndStatisticsByPage(projectid, start, end, systemid, pageIndex, pageSize, out recordCount, userinfo);
            var obj = new
            {
                rows = result,
                total = recordCount,
            };
            return Success("获取成功", obj);
        }
        #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") + " 00:00:00";
                end = dateend.ToString("yyyy-MM-dd") + " 23:59:59";
            }
        }

    }
}