using Dapper;
using Microsoft.Extensions.Logging;
using Performance.DtoModels;
using Performance.EntityModels;
using Performance.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Performance.Services.ExtractExcelService
{
    public class DictionaryService : IAutoInjection
    {
        private readonly ILogger<DictionaryService> logger;
        private readonly QueryService queryService;
        private readonly LogManageService logService;
        private readonly PerforPeremployeeRepository peremployeeRepository;
        private readonly PerforHospitalconfigRepository hospitalconfigRepository;
        private readonly PerforExtypeRepository extypeRepository;
        private readonly PerforExscriptRepository exscriptRepository;

        public DictionaryService(
            ILogger<DictionaryService> logger,
            QueryService queryService,
            LogManageService logService,
            PerforPeremployeeRepository peremployeeRepository,
            PerforHospitalconfigRepository hospitalconfigRepository,
            PerforExtypeRepository extypeRepository,
            PerforExscriptRepository exscriptRepository
            )
        {
            this.logger = logger;
            this.queryService = queryService;
            this.logService = logService;
            this.peremployeeRepository = peremployeeRepository;
            this.hospitalconfigRepository = hospitalconfigRepository;
            this.extypeRepository = extypeRepository;
            this.exscriptRepository = exscriptRepository;
        }

        public void Handler(int hospitalId, per_allot allot, string groupName, bool isSingle)
        {
            try
            {
                var configs = hospitalconfigRepository.GetEntities(t => t.HospitalId == hospitalId);
                if (configs == null || !configs.Any()) throw new Exception("医院未配置绩效抽取信息");

                var types = extypeRepository.GetEntities(t => t.HospitalId == hospitalId && new int[] { 2 }.Contains(t.Source));
                if (types == null || !types.Any()) return;

                logService.ReturnTheLog(allot.ID, groupName, 2, "提取数据", $"提取人员信息数据", isSingle: isSingle);
                var scripts = exscriptRepository.GetEntities(t => types.Select(s => s.Id).Contains(t.TypeId) && t.IsEnable == 1);
                if (scripts != null && scripts.Any())
                {
                    if (types.Any(t => t.Source == (int)SheetType.Employee))    //人员字典抽取配置
                    {
                        var typeIds = types.Where(t => t.Source == (int)SheetType.Employee).Select(t => t.Id);
                        var script = scripts.FirstOrDefault(t => typeIds.Contains(t.TypeId));
                        if (script == null) return;
                        Employee(allot, configs.FirstOrDefault(t => t.Id == script.ConfigId), script.ExecScript);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.LogError("获取数据时发生异常");
                throw ex;
            }
        }

        private void Employee(per_allot allot, sys_hospitalconfig config, string sql)
        {
            try
            {
                if (config == null || string.IsNullOrEmpty(sql)) return;

                var data = queryService.QueryData<per_employee>(config, allot, sql);
                if (data == null || !data.Any()) return;

                var employees = peremployeeRepository.GetEntities(t => t.AllotId == allot.ID);
                if (employees != null && employees.Any())
                {
                    peremployeeRepository.RemoveRange(employees.ToArray());
                }

                data.ToList().ForEach(t =>
                {
                    t.AllotId = allot.ID;
                    t.HospitalId = allot.HospitalId;
                });
                peremployeeRepository.AddRange(data.ToArray());
            }
            catch (Exception ex)
            {
                logger.LogError("获取人员名单时发生异常：" + ex.ToString());
            }
        }

        private void Department()
        {
        }
    }
}
