using Furion.DistributedIDGenerator;
using MiniExcelLibs;
using Nirvana.Common;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using YBDevice.Entity;

namespace YBDevice.CreateDB
{
    /// <summary>
    /// 导入中医体质信息
    /// </summary>
    public class ImportZYTZ
    {
        /// <summary>
        /// 导入QA信息
        /// </summary>
        public async Task InsertQAAsync(SqlSugarClient Db)
        {
            if (!Db.DbMaintenance.IsAnyTable("YB_ChildBodyQA") || await Db.Queryable<YB_ChildBodyQA>().CountAsync() > 0)
            {
                return;
            }
            //获取项目主目录
            var path = Path.GetFullPath("../../..");
            string filepath = Path.Combine(path, "儿童体质评估表.xlsx");
            var query = await MiniExcel.QueryAsync(filepath);
            var insertlist = new List<YB_ChildBodyQA>();

            for (var i = 0; i < query.Count(); i++)
            {
                var item = query.ElementAt(i) as IDictionary<string, object>;
                //脾虚质
                if (i < 8)
                {
                    if (i > 1)
                    {
                        insertlist.Add(InsertQA(item, ChildBodyTypeEnum.PXZ));
                    }
                }
                else if (i < 18)
                {
                    if (i > 11)
                    {
                        insertlist.Add(InsertQA(item, ChildBodyTypeEnum.JZZ));
                    }
                }
                else if (i < 28)
                {
                    if (i > 21)
                    {
                        insertlist.Add(InsertQA(item, ChildBodyTypeEnum.RZZ));
                    }
                }
                else if (i < 38)
                {
                    if (i > 31)
                    {
                        insertlist.Add(InsertQA(item, ChildBodyTypeEnum.XHPWZ));
                    }
                }
                else if (i < 48)
                {
                    if (i > 41)
                    {
                        insertlist.Add(InsertQA(item, ChildBodyTypeEnum.SZZ));
                    }
                }
                else if (i < 58)
                {
                    if (i > 51)
                    {
                        insertlist.Add(InsertQA(item, ChildBodyTypeEnum.TBZ));
                    }
                }
                else if (i < 69)
                {
                    if (i > 61)
                    {
                        insertlist.Add(InsertQA(item, ChildBodyTypeEnum.SJWSZ));
                    }
                }
            }
            if (insertlist.Count > 0)
            {
                await Db.Insertable(insertlist).ExecuteCommandAsync();
            }
        }
        /// <summary>
        /// 增加中医体质结果
        /// </summary>
        /// <param name="Db"></param>
        /// <returns></returns>
        public async Task InsertBodyAsync(SqlSugarClient Db)
        {
            if (!Db.DbMaintenance.IsAnyTable("YB_ChildBodyDetail") || await Db.Queryable<YB_ChildBodyDetail>().CountAsync() > 0)
            {
                return;
            }
            //获取项目主目录
            var path = Path.GetFullPath("../../..");
            string filepath = Path.Combine(path, "儿童常见中医体质辨识与保健方案.xlsx");
            var query = await MiniExcel.QueryAsync(filepath);
            var insertlist = new List<YB_ChildBodyDetail>();

            for (var i = 0; i < query.Count(); i++)
            {
                var item = query.ElementAt(i) as IDictionary<string, object>;
                if (item == null)
                {
                    continue;
                }
                if (i == 2)
                {
                    insertlist.Add(InsertQADetail(item, ChildBodyTypeEnum.SJWSZ));
                }
                else if (i == 3)
                {
                    insertlist.Add(InsertQADetail(item, ChildBodyTypeEnum.PXZ));
                }
                else if (i == 4)
                {
                    insertlist.Add(InsertQADetail(item, ChildBodyTypeEnum.JZZ));
                }
                else if (i == 5)
                {
                    insertlist.Add(InsertQADetail(item, ChildBodyTypeEnum.RZZ));
                }
                else if (i == 6)
                {
                    insertlist.Add(InsertQADetail(item, ChildBodyTypeEnum.XHPWZ));
                }
                else if (i == 7)
                {
                    insertlist.Add(InsertQADetail(item, ChildBodyTypeEnum.SZZ));
                }
                else if (i == 8)
                {
                    insertlist.Add(InsertQADetail(item, ChildBodyTypeEnum.TBZ));
                }
            }
            if (insertlist.Count > 0)
            {
                await Db.Insertable(insertlist).ExecuteCommandAsync();
            }
        }
        /// <summary>
        /// 解析qa
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        private YB_ChildBodyQA InsertQA(IDictionary<string, object> item, ChildBodyTypeEnum type)
        {
            item.TryGetValue("A", out object content);
            item.TryGetValue("B", out object NoneValue);
            item.TryGetValue("C", out object LittleValue);
            item.TryGetValue("D", out object SomeValue);
            item.TryGetValue("E", out object OftenValue);
            item.TryGetValue("F", out object AlWaysValue);
            return new YB_ChildBodyQA
            {
                Id = IDGen.NextID(),
                Content = content.ToString(),
                NoneValue = NoneValue.ToInt(),
                LittleValue = LittleValue.ToInt(),
                SomeValue = SomeValue.ToInt(),
                OftenValue = OftenValue.ToInt(),
                AlWaysValue = AlWaysValue.ToInt(),
                CreateTime = DateTime.Now,
                Type = type
            };
        }
        /// <summary>
        /// 解析结果
        /// </summary>
        /// <param name="item"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private YB_ChildBodyDetail InsertQADetail(IDictionary<string, object> item, ChildBodyTypeEnum type)
        {
            item.TryGetValue("B", out object Define);
            item.TryGetValue("C", out object Proper);
            item.TryGetValue("D", out object Show);
            item.TryGetValue("E", out object Food);
            item.TryGetValue("F", out object Growth);
            item.TryGetValue("G", out object OutsideEnv);
            item.TryGetValue("H", out object Conver);
            item.TryGetValue("J", out object Advice);
            item.TryGetValue("I", out object Causes);
            item.TryGetValue("K", out object ZYAdvice);
            item.TryGetValue("L", out object CareAdivce);
            return new YB_ChildBodyDetail
            {
                Id = IDGen.NextID(),
                Define = Define.ToString(),
                Proper = Proper.ToString(),
                Show = Show.ToString(),
                Food = Food.ToString(),
                Growth = Growth.ToString(),
                OutsideEnv = OutsideEnv.ToString(),
                Conver = Conver.ToString(),
                Causes = Causes.ToString(),
                Advice = Advice.ToString(),
                ZYAdvice = ZYAdvice.ToString(),
                CareAdivce = CareAdivce.ToString(),
                CreateTime = DateTime.Now,
                Type = type
            };
        }
    }
}
