using Infrastructure;
using Infrastructure.Helpers;
using OpenAuth.App.BaseApp.Base;
using OpenAuth.App.Interface;
using OpenAuth.App.ServiceApp.Achievement.Request;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;
using SqlSugar;

namespace OpenAuth.App.ServiceApp.Achievement;

public class InsTifApp : SqlSugarBaseApp<InsTif, SugarDbContext>
{
    public InsTifApp(ISugarUnitOfWork<SugarDbContext> unitWork, ISimpleClient<InsTif> repository,
        IAuth auth) : base(unitWork, repository, auth)
    {
    }

    public async Task<PageInfo<List<InsTif>>> Load(InsTifQuery req)
    {
        RefAsync<int> total = 0;
        var result = await Repository.AsQueryable()
            .ToPageListAsync(req.page, req.limit, total);
        return new PageInfo<List<InsTif>>
        {
            Items = result,
            Total = total
        };
    }

    public async Task<InsTif> GetTif(string id)
    {
        return await Repository.GetByIdAsync(id);
    }

    public async Task<Response<bool>> Update()
    {
        var tifDir = ConfigHelper.GetConfigRoot().GetSection("Insight:TifDir").Value;
        var files = Directory.GetFiles(tifDir, "*.tif", SearchOption.AllDirectories);
        foreach (var tifFile in files)
        {
            var tifName = Path.GetFileName(tifFile);
            var tifDate = new DirectoryInfo(tifFile).Parent?.Name;
            if (string.IsNullOrEmpty(tifDate))
            {
                throw new Exception("获取不到日期目录");
            }

            var areaName = new DirectoryInfo(tifFile).Parent?.Parent?.Name;
            var currentTime = DateTime.Now;
            var tifRecord = new InsTif
            {
                Id = Guid.NewGuid().ToString(),
                TifName = tifName,
                TifPath = tifFile,
                Remark = "备注",
                TifDate = DateTime.ParseExact(tifDate, "yyyy年MM月", null),
                CreateTime = currentTime,
                UpdateTime = currentTime,
                AreaName = areaName,
                PlotName = "地块",
                AreaId = "areaid",
                AreaNum = Convert.ToDecimal(0)
            };
            await Repository.InsertAsync(tifRecord);
        }

        return new Response<bool>
        {
            Result = true
        };
    }

    public async Task<Response<bool>> Insert(IEnumerable<InsTifInsert> req)
    {
        var tifList = req.MapToList<InsTif>();
        var removeList = new List<InsTif>();
        foreach (var tif in tifList)
        {
            if (await Repository.CountAsync(a => a.TifPath == tif.TifPath) > 0)
            {
                removeList.Add(tif);
            }

            tif.Id = Guid.NewGuid().ToString();
            tif.CreateTime = DateTime.Now;
            tif.UpdateTime = DateTime.Now;
        }
        tifList.RemoveAll(a => removeList.Contains(a));
        await Repository.InsertRangeAsync(tifList);
        return new Response<bool>
        {
            Result = true
        };
    }
}