namespace SafeCampus.Application.Services.Business.PersonInfoService;

public class PersonInfoService:DbRepository<PersonInfo>, IPersonInfoService
{
    public async Task<bool> Add(PersonInfoDto input)
    {
        var model = input.Adapt<PersonInfo>();
        var oldModel = await GetFirstAsync(x => x.PersonId == input.PersonId);
        if (oldModel != null)
        {
            throw Oops.Oh("该人员id已存在");
        }
        await InsertAsync(model);
        return true;
    }

    public async Task<bool> Delete(string id)
    {
        var model = await GetFirstAsync(x => x.PersonId == id);
        if (model == null)
        {
            throw Oops.Oh("信息不存在");
        }
        await DeleteAsync(model);
        return true; 
    }

    public async Task<PersonInfoDto> GetInfo(string id)
    {
        var model = await Context.Queryable<PersonInfo>()
            .Includes(x => x.DormitoryInfoItem)
            .Includes(x => x.PersonSetInfoItem,x=>x.MajorInfoItem,x=>x.DepartmentInfoItem)
            .Includes(x => x.PersonFacesList)
            .FirstAsync(x => x.PersonId == id);
        if (model == null)
        {
            return null;
        }
        return model.Adapt<PersonInfoDto>();
    }

    public async Task<bool> Update(PersonInfoDto input)
    {
       
        var model = await GetFirstAsync(p => p.PersonId == input.PersonId);
        if (model == null)
        {
            throw Oops.Oh("信息不存在");
        }
        var res = input.Adapt(model);
        await UpdateAsync(res);
        return true;
    }

    public async Task<bool> UpdateSet(List<string> personId, string personSetId)
    {
        var result = await Context.Updateable<PersonInfo>()
            .SetColumns(it => it.PersonSetId == personSetId)//SetColumns是可以叠加的 写2个就2个字段赋值
            .Where(it => personId.Contains(it.PersonId))
            .ExecuteCommandAsync();
        return true;
    }

    public async Task<SqlSugarPagedList<PersonInfoDto>> GetPageList(PersonInfoSearch search)
    {
        var query = Context.Queryable<PersonInfo>()
            .Includes(x => x.PersonSetInfoItem, x => x.MajorInfoItem, x => x.DepartmentInfoItem)
            .Includes(x=>x.PersonFacesList)
            .Includes(x=>x.DormitoryInfoItem)
            .WhereIF(!string.IsNullOrEmpty(search.PersonSetId), x => x.PersonSetId == search.PersonSetId)
            .WhereIF(!string.IsNullOrEmpty(search.PersonName), x => x.Name.Contains(search.PersonName))
            .WhereIF(!string.IsNullOrEmpty(search.Phone), x => x.Phone.Contains(search.Phone));

        var list = await query.OrderByDescending(x => x.CreateTime)
            .ToPagedListAsyncMapster<PersonInfo, PersonInfoDto>(search.PageNum, search.PageSize);
        return list;
    }

    public async Task<int> GetCount(string sex)
    {
        var query = await Context.Queryable<PersonInfo>()
            .WhereIF(!string.IsNullOrEmpty(sex),x => x.Gender == sex)
            .CountAsync();
        return query;
    }

    public async Task<dynamic> GetAge()
    {
        var query = await Context.Queryable<PersonInfo>().GroupBy(x => x.Age)
            .Select(x => new { Label = x.Age, Value = SqlFunc.AggregateCount(x.Age) }).ToListAsync();
        return new {Label=query.Select(x=>x.Label),Value=query.Select(x=>x.Value)};
    }
}