using AutoMapper;
using NPOI.Util;
using OASystem.Domain;
using OASystem.Domain.Dtos.Groups;
using OASystem.Domain.Dtos.Task;
using OASystem.Domain.Entities.Groups;
using OASystem.Domain.ViewModels.Groups;
using OASystem.Domain.ViewModels.SmallFun;
using OASystem.Infrastructure.Tools;
using System.Reflection;
using System.Text.Json.Serialization;
namespace OASystem.Infrastructure.Repositories.Groups
{
    /// 
    /// 签证流程步骤仓储
    /// 
    public class VisaProcessRepository : BaseRepository
    {
        private readonly IMapper _mapper;
        public VisaProcessRepository(SqlSugarClient sqlSugar, IMapper mapper)
            : base(sqlSugar)
        {
            _mapper = mapper;
        }
        /// 
        /// 创建签证流程步骤
        /// 
        /// 
        /// 
        /// 
        public async Task Create(int createUderId,int groupId)
        {
            //团组有效验证
            var groupIsValid = await _sqlSugar.Queryable()
                .Where(g => g.Id == groupId && g.IsDel == 0)
                .AnyAsync();
            if (!groupIsValid)
            {
                return new Result(400, "团组无效,无法创建签证流程步骤。");
            }
            var existingSteps = await _sqlSugar.Queryable()
                .Where(s => s.GroupId == groupId && s.IsDel == 0)
                .AnyAsync();
            if (existingSteps)
            {
                return new Result(400, "该团组的签证流程步骤已存在,无法重复创建。");
            }
            var steps = Grp_VisaProcessSteps.StepsInit(groupId, createUderId);
            var add = await _sqlSugar.Insertable(steps).ExecuteCommandAsync();
            if (add < 1) return new Result(400, "签证流程步骤创建失败。");
            // 记录创建日志
            foreach (var step in steps)
            {
                await LogOperationAsync(null, step, "Create", createUderId);
            }
            return new Result(200,"Success");
        }
        /// 
        /// 签证流程 Info
        /// 
        /// 
        /// 
        /// 
        public async Task Info(VisaProcessInfoByGroupIdDto dto)
        {
            //团组有效验证
            //var groupIsValid = await _sqlSugar.Queryable()
            //    .Where(g => g.Id == dto.GroupId && g.IsDel == 0)
            //    .AnyAsync();
            //if (!groupIsValid)
            //{
            //    return new Result(400, "团组无效,无法查询签证流程步骤。");
            //}
            var query = await _sqlSugar.Queryable()
                .Where(s => s.GroupId == dto.GroupId && s.IsDel == 0)
                .OrderBy(s => s.Step)
                .ToListAsync();
            var infos = query.Select(s => new
            {
                s.Id,
                s.GroupId,
                s.Step,
                s.DataType,
                s.TypedValue,
                //s.StoreVal,
                s.IsCompleted,
                s.AttachUrl,
                s.TypedFileNameValue,
                s.Remark
            }).ToList();
            string msg = "Success";
            //如果不存在,则返回默认数据
            if (infos == null || infos.Count < 1)
            {
                infos = Grp_VisaProcessSteps.StepsInit(dto.GroupId, 208)
                    .Select(s => new {
                        s.Id,
                        s.GroupId,
                        s.Step,
                        s.DataType,
                        s.TypedValue,
                        //s.StoreVal,
                        s.IsCompleted,
                        s.AttachUrl,
                        s.TypedFileNameValue,
                        s.Remark
                    }).ToList();
                msg = "签证流程步骤信息不存在,展示默认数据。";
            }
            //数据按照类型处理
            var datas = new List