using DocumentFormat.OpenXml.EMMA;
using DocumentFormat.OpenXml.Office2010.Excel;
using Infrastructure;
using Infrastructure.Helpers;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OpenAuth.App.BaseApp.Base;
using OpenAuth.App.BaseApp.Request;
using OpenAuth.App.BasicQueryService;
using OpenAuth.App.Interface;
using OpenAuth.App.Request;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OpenAuth.App.ServiceApp
{
    public class ReSubmitApp : SqlSugarBaseApp<Repository.Domain.ReSubmit, SugarDbContext>
    {
        private IConfiguration _configuration;
        ISqlSugarClient client;
        public ReSubmitApp(ISugarUnitOfWork<SugarDbContext> unitWork, ISimpleClient<Repository.Domain.ReSubmit> repository, IAuth auth, IConfiguration configuration, ISqlSugarClient client) : base(unitWork, repository, auth)
        {
            _configuration = configuration;
            this.client = client;
        }

        public async Task<Response<bool>> SaveReSubmitInfo(string originalcaseno, string remark, string subjectkey)
        {
            var authStrategyContext = _auth.GetCurrentUser();
            var user = authStrategyContext.User;
            var data = this.Repository.AsSugarClient().Queryable<ReSubmit>().Where(r => r.CaseNo == originalcaseno && r.SubjectKey == subjectkey).ToList();
            //查询一下案件是否通过
            await IsReSubmit(originalcaseno, subjectkey);
            if (data.Count > 0)
            {
                throw new Exception("案件已复提");
            }
            else
            {
                using (var uow = base.UnitWork.CreateContext())
                {
                    ReSubmit rs = new ReSubmit();
                    rs.Id = Guid.NewGuid().ToString();
                    rs.CaseNo = originalcaseno;
                    rs.UserId = user.Id;
                    rs.UserName = user.Name;
                    rs.ReSubmitTime = DateTime.Now;
                    rs.Remark = remark;
                    rs.SubjectKey = subjectkey;

                    ReSubmitHistory rsh = new ReSubmitHistory();
                    rsh.Id = Guid.NewGuid().ToString();
                    rsh.CaseNo = originalcaseno;
                    rsh.UserId = user.Id;
                    rsh.UserName = user.Name;
                    rsh.ReSubmitTime = DateTime.Now;
                    rsh.Remark = remark;
                    rsh.SubjectKey = subjectkey;

                    await uow.ReSubmit.InsertAsync(rs);
                    await uow.ReSubmitHistory.InsertAsync(rsh);
                    var flag = uow.Commit();

                    //更新航飞库数据
                    var param = new
                    {
                        CaseNo = originalcaseno,
                        Remark = remark
                    };
                    var url = _configuration.GetSection("AppSetting:ResubmitUrl").Value;
                    var apiUrl = url + "api/DroneCaseinfo/ReSubmitCaseInfo";
                    var urlflag = await HttpMethods.Post(apiUrl, param.ToJson());

                    var reslut = (JObject)JsonConvert.DeserializeObject(urlflag);
                    var flags = flag && (bool)reslut["result"];
                    return new Response<bool>
                    {
                        Result = flags,
                        Message = flags == true ? "success" : "error"
                    };
                }
            }
        }
        private async Task IsReSubmit(string originalcaseno, string subjectkey)
        {
            using (var uow = base.UnitWork.CreateContext())
            {
                switch (subjectkey)
                {
                    case "Subject_WFYD":
                        var drone1 = await uow.DroneCaseInfoSingle.AsQueryable().Where(c => c.original_case_no == originalcaseno).FirstAsync();
                        if (drone1 != null && !string.IsNullOrEmpty(drone1.case_no))
                        {
                            throw new Exception("案件已审核，无法复提");
                        }
                        break;
                    case "Subject_GDFLH":
                        var drone2 = await uow.DroneCaseInfoFLH.AsQueryable().Where(c => c.original_case_no == originalcaseno).FirstAsync();
                        if (drone2 != null && !string.IsNullOrEmpty(drone2.case_no))
                        {
                            throw new Exception("案件已审核，无法复提");
                        }
                        break;
                    //case "Subject_FFCK":
                    //    var drone3 = await uow.DroneCaseInfoFLH.AsQueryable().Where(c => c.original_case_no == originalcaseno).FirstAsync();
                    //    if (drone3 != null && !string.IsNullOrEmpty(drone3.case_no))
                    //    {
                    //        throw new Exception("案件已审核，无法复提");
                    //    }
                    //    break;
                    //case "Subject_XCSJ":
                    //    var drone4 = await uow.DroneCaseInfoFLH.AsQueryable().Where(c => c.original_case_no == originalcaseno).FirstAsync();
                    //    if (drone4 != null && !string.IsNullOrEmpty(drone4.case_no))
                    //    {
                    //        throw new Exception("案件已审核，无法复提");
                    //    }
                    //    break;
                    //case "Subject_ZDWT1":
                    //    var drone5 = await uow.DroneCaseInfoFLH.AsQueryable().Where(c => c.original_case_no == originalcaseno).FirstAsync();
                    //    if (drone5 != null && !string.IsNullOrEmpty(drone5.case_no))
                    //    {
                    //        throw new Exception("案件已审核，无法复提");
                    //    }
                    //    break;
                    //case "Subject_ZDWT2":
                    //    var drone6 = await uow.DroneCaseInfoFLH.AsQueryable().Where(c => c.original_case_no == originalcaseno).FirstAsync();
                    //    if (drone6 != null && !string.IsNullOrEmpty(drone6.case_no))
                    //    {
                    //        throw new Exception("案件已审核，无法复提");
                    //    }
                    //    break;
                    //case "Subject_STBHHXJG":
                    //    var drone7 = await uow.DroneCaseInfoFLH.AsQueryable().Where(c => c.original_case_no == originalcaseno).FirstAsync();
                    //    if (drone7 != null && !string.IsNullOrEmpty(drone7.case_no))
                    //    {
                    //        throw new Exception("案件已审核，无法复提");
                    //    }
                    //    break;
                    //case "Subject_STXF":
                    //    var drone8 = await uow.DroneCaseInfoFLH.AsQueryable().Where(c => c.original_case_no == originalcaseno).FirstAsync();
                    //    if (drone8 != null && !string.IsNullOrEmpty(drone8.case_no))
                    //    {
                    //        throw new Exception("案件已审核，无法复提");
                    //    }
                    //    break;
                    default:
                        break;
                }
            }
        }
        /// <summary>
        /// 查询实时表数据
        /// </summary>
        /// <returns></returns>
        public async Task<Response<List<ReSubmit>>> LoadReSubmitDatas()
        {
            var resubmitlist = await this.Repository.AsSugarClient().Queryable<ReSubmit>().ToListAsync();

            return new Response<List<ReSubmit>>
            {
                Result = resubmitlist
            };
        }
    }
}
