using Nirvana.Common;
using Nirvana.Common.ApiBase;
using Nirvana.Data;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YBDevice.Entity;

namespace YBDevice.Api.DBServices
{
    /// <summary>
    /// 家庭成员管理
    /// </summary>
    public partial class FamilyApp : BaseApp
    {
        /// <summary>
        /// 家庭成员列表
        /// </summary>
        /// <returns></returns>
        public async Task<ResultInfo> GetListAsync()
        {
            using (var dbClient = ReadDbContext.GetInstance())
            {
                var tempquery = dbClient.Queryable<YB_Family>()
                    .Where(x => x.UserId == authInfo.UserId && x.Status != -1);
                var result = await tempquery
                    .OrderBy(x => x.Createtime, OrderByType.Desc)
                    .ToListAsync();
                return new ResultInfo(ResultState.SUCCESS, "success", result);
            }
        }
        /// <summary>
        /// 家庭成员信息修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ResultInfo> SubmitAsync(FamilySubmitModel model)
        {
            if (!string.IsNullOrEmpty(model.name))
            {
                return new ResultInfo(ResultState.FAIL, "名称不可为空");
            }
            if (model.height <= 50)
            {
                return new ResultInfo(ResultState.FAIL, "身高不可小于50厘米");
            }
            if (model.birthday <= DateTime.Now.Date)
            {
                return new ResultInfo(ResultState.FAIL, "出生年月不可大于当前日期");
            }
            using (var dbClient = ReadDbContext.GetInstance())
            {
                int age = model.birthday.ToAge();
                if (model.id > 0)
                {
                    await dbClient.Updateable<YB_Family>().SetColumns(x => new YB_Family
                    {
                        Age = age,
                        Sex = model.sex,
                        Birthday = model.birthday,
                        Height = model.height,
                        Name = model.name,
                        Type = model.type
                    }).Where(x => x.Id == model.id).ExecuteCommandAsync();
                    return new ResultInfo(ResultState.SUCCESS, "资料更新成功");
                }
                else
                {
                    await dbClient.Insertable<YB_Family>(new YB_Family
                    {
                        Age = age,
                        Sex = model.sex,
                        Birthday = model.birthday,
                        Height = model.height,
                        Name = model.name,
                        Type = model.type,
                        Status = 1,
                        IsSelf = 0,
                        Createtime = DateTime.Now,
                        UserId = authInfo.UserId,
                        Weight = 0,
                        LastHeartTime = null
                    }).ExecuteCommandAsync();
                    return new ResultInfo(ResultState.SUCCESS, "成员添加成功");
                }
            }
        }
        /// <summary>
        /// 家庭成员删除
        /// </summary>
        /// <param name="id">家庭成员ID</param>
        /// <returns></returns>
        public async Task<ResultInfo> DeleteAsync(int id)
        {
            using (var dbClient = ReadDbContext.GetInstance())
            {
                if (!await dbClient.Queryable<YB_Family>().AnyAsync(x => x.Id == id))
                {
                    return new ResultInfo(ResultState.FAIL, "家庭成员未找到");
                }
                await dbClient.Updateable<YB_Family>().SetColumns(x => new YB_Family
                {
                    Status = -1
                }).Where(x => x.Id == id).ExecuteCommandAsync();
                return new ResultInfo(ResultState.SUCCESS, "删除成功");
            }
        }

        /// <summary>
        /// 家庭成员详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ResultInfo> DetailAsync(int id)
        {
            using (var dbClient = ReadDbContext.GetInstance())
            {
                var data = await dbClient.Queryable<YB_Family>().FirstAsync(x => x.Id == id);
                return new ResultInfo(ResultState.SUCCESS, "success", data);
            }
        }
        /// <summary>
        /// 设置目标体重
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ResultInfo> SetTargetAsync(YB_FamilyTarget model)
        {
            using (var dbClient = ReadDbContext.GetInstance())
            {
                if (await dbClient.Queryable<YB_FamilyTarget>().AnyAsync(x => x.familyid == model.familyid))
                {
                    await dbClient.Updateable<YB_FamilyTarget>().SetColumns(x => new YB_FamilyTarget
                    {
                        time = model.time,
                        weight = model.weight
                    }).Where(x => x.familyid == model.familyid).ExecuteCommandAsync();
                }
                else
                {
                    await dbClient.Insertable<YB_FamilyTarget>(model).ExecuteCommandAsync();
                }
                return new ResultInfo(ResultState.SUCCESS, "设置成功");
            }
        }
    }
}
