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 ConfigurationApi.IRepositories;
using MadRunFabric.Model;
using System.Security.Claims;
using System.ComponentModel;
using MadRunFabric.Common.Helpers;

namespace ConfigurationApi.Controllers
{
    /// <summary>
    /// 工单类型 - by usercode
    /// </summary>
    [Authorize]
    [ApiVersion("6.0")]
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class WorkOrderTypeController : BaseController
    {
        private readonly ILogger<WorkOrderTypeController> _logger;
        private readonly IWo_Repair_BaseRepository _wo_repair_baserepository;
        private readonly IDistri_WorkorderRepository _distri_workorderrepository;
        public WorkOrderTypeController(
            ILogger<WorkOrderTypeController> logger,
            IWo_Repair_BaseRepository wo_repair_baserepository,
            IDistri_WorkorderRepository distri_workorderrepository
            )
        {
            _logger = logger;
            _wo_repair_baserepository = wo_repair_baserepository;
            _distri_workorderrepository = distri_workorderrepository;
        }
        /// <summary>
        /// 获取工单类型 - by usercode - 微信端
        /// 1、维修表，如果此用户有工单，返回“维修”；
        /// 2、配送表，如果此用户有工单，返回“配送”；
        /// </summary>
        /// <returns></returns>
        [HttpGet("getwotype")]
        public async Task<IActionResult> GetWorkOrderTypeAsync()
        {
            string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            string username = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;
            //初始化工单类型
            var listwotype = new List<listWoType>();
            var wotypeArr = new List<int>();
            //维修
            var list_Repair = (await _wo_repair_baserepository.Get(x => x.isdelete == 0)).ToList();
            list_Repair = list_Repair.Where(x => x.touser.Contains(usercode) || x.dealuser.Contains(usercode) || x.createuser == usercode).ToList();
            //配送 - 微信端科室用，只展示随机工单
            var list_Distri = (await _distri_workorderrepository.Get(x => x.wosorts == (int)EnumDistributionWoSorts.random && x.isdelete == 0)).ToList();
            list_Distri = list_Distri.Where(x => x.touser.Contains(usercode) || x.dealby.Contains(usercode) || x.createtby == usercode).ToList();
            
            //维修
            if (list_Repair != null && list_Repair.Count > 0)
            {
                int wotype = (int)EnumWoType.repair;
                wotypeArr.Add(wotype);
            }
            //配送
            if (list_Distri != null && list_Distri.Count > 0)
            {
                int wotype = (int)EnumWoType.distribution;
                wotypeArr.Add(wotype);
            }

            foreach (var item in wotypeArr)
            {
                var wotypename = EnumberHelper.EnumToList<EnumWoType>().FirstOrDefault(x => x.EnumValue == item).Desction;
                var model = new listWoType();
                model.wotype = item;
                model.wotypename = wotypename;
                listwotype.Add(model);
            }
            return Success("获取成功！", listwotype);
        }


        /// <summary>
        /// 工单类型：1报修，2保养，3巡检，4配送
        /// </summary>
        public enum EnumWoType
        {
            [Description("报修")]
            repair = 1,
            [Description("保养")]
            maintain = 2,
            [Description("巡检")]
            inspection = 3,
            [Description("配送")]
            distribution = 4
        }

        /// <summary>
        /// 工单类型：1报修，2保养，3巡检，4配送
        /// </summary>
        public class listWoType
        {
            /// <summary>
            /// 工单类型
            /// </summary>
            public int wotype { get; set; }
            /// <summary>
            /// 工单类型名称
            /// </summary>
            public string wotypename { get; set; }
        }

    }
}