using Furion.DependencyInjection; using Nirvana.Common; using Nirvana.Common.ApiBase; using SqlSugar; using System; using System.Threading.Tasks; using YBDevice.CommonService; using YBDevice.Entity; using YBDevice.NApi.Application.FamilyInfo; namespace YBDevice.NApi.Application.UserInfo { /// /// 家庭成员管理 /// public class FamilyService : BaseService, IFamilyService, ITransient { private readonly ISqlSugarRepository repository; private readonly SqlSugarClient dbClient; public FamilyService(ISqlSugarRepository sqlSugarRepository) { repository = sqlSugarRepository; dbClient = repository.Context; } /// /// 家庭成员列表 /// /// public async Task GetListAsync() { var tempquery = dbClient.Queryable() .Where(x => x.UserId == authInfo.UserId && x.Status != StatusType.Delete); var result = await tempquery .OrderBy(x => x.Createtime, OrderByType.Desc) .Select(x => new FamilyListModel { Id = x.Id, Birthday = x.Birthday, HeadImg = x.HeadImg, Height = x.Height, IsSelf = x.IsSelf, LastHeartTime = SqlFunc.ToString(x.LastHeartTime), Name = x.Name, Sex = x.Sex, Type = x.Type, Weight = x.Weight, Age = x.Age }) .Mapper((it, cache) => { it.mAge = it.Birthday.TomAge(); if (!string.IsNullOrEmpty(it.HeadImg)) { it.HeadImg = it.HeadImg.ToLower().StartsWith("http") ? it.HeadImg : $"{APICDNURL}{it.HeadImg}"; } else { it.HeadImg = DefaultService.HeadImg(it.Sex, it.Type); } if (!string.IsNullOrEmpty(it.LastHeartTime)) { it.LastHeartTime = it.LastHeartTime.ToYearDateTime(); } else { it.LastHeartTime = "-"; } }) .ToListAsync(); return new ResultInfo(ResultState.SUCCESS, "success", result); } /// /// 家庭成员信息修改 /// /// /// public async Task SubmitAsync(FamilySubmitModel model) { int age = model.birthday.ToAge(); model.headimg = model.headimg.ToStr(); FamilyType type = GetType(model.birthday); if (model.id > 0) { if (!string.IsNullOrEmpty(model.headimg)) { await dbClient.Updateable().SetColumns(x => new YB_Family { Age = age, Sex = model.sex, Birthday = model.birthday, Height = model.height, Name = model.name, Type = type, HeadImg = model.headimg }).Where(x => x.Id == model.id).ExecuteCommandAsync(); } else { await dbClient.Updateable().SetColumns(x => new YB_Family { Age = age, Sex = model.sex, Birthday = model.birthday, Height = model.height, Name = model.name, Type = type }).Where(x => x.Id == model.id).ExecuteCommandAsync(); } return new ResultInfo(ResultState.SUCCESS, "资料更新成功"); } else { int familyid = await dbClient.Insertable(new YB_Family { Age = age, Sex = model.sex, Birthday = model.birthday, Height = model.height, Name = model.name, Type = type, Status = StatusType.Enabled, IsSelf = 0, Createtime = DateTime.Now, UserId = authInfo.UserId, Weight = 0, LastHeartTime = null, HeadImg = model.headimg }).ExecuteReturnIdentityAsync(); //增加一条家庭成员数据记录 if (!await dbClient.Queryable().AnyAsync(x => x.FamilyId == familyid)) { await dbClient.Insertable(new YB_FamilyData { FamilyId = familyid, AdultHeight = 0, CreateTime = DateTime.Now, DadHeight = 0, HalfYearHeight = 0, MomHeight = 0, YearHeight = 0 }).ExecuteCommandAsync(); } return new ResultInfo(ResultState.SUCCESS, "成员添加成功"); } } /// /// 家庭成员删除 /// /// 家庭成员ID /// public async Task DeleteAsync(int id) { if (!await dbClient.Queryable().AnyAsync(x => x.Id == id)) { return new ResultInfo(ResultState.FAIL, "家庭成员未找到"); } if (await dbClient.Queryable().AnyAsync(x => x.Id == id && x.IsSelf == 1)) { return new ResultInfo(ResultState.FAIL, "不能够删除自己"); } await dbClient.Updateable().SetColumns(x => new YB_Family { Status = StatusType.Delete }).Where(x => x.Id == id).ExecuteCommandAsync(); return new ResultInfo(ResultState.SUCCESS, "删除成功"); } /// /// 家庭成员详情 /// /// /// public async Task DetailAsync(int id) { var data = await dbClient.Queryable().FirstAsync(x => x.Id == id); if (data != null) { if (string.IsNullOrEmpty(data.HeadImg)) { data.HeadImg = DefaultService.HeadImg(data.Sex, data.Type); } else { if (!string.IsNullOrEmpty(data.HeadImg) && !data.HeadImg.ToLower().StartsWith("http")) { data.HeadImg = $"{APICDNURL}{data.HeadImg}"; } } } return new ResultInfo(ResultState.SUCCESS, "success", new { Id = data.Id, Age = data.Age, Birthday = data.Birthday.ToString("yyyy-MM-dd"), HeadImg = data.HeadImg, Height = data.Height, IsSelf = data.IsSelf, Name = data.Name, Sex = data.Sex, Type = data.Type, MAge = data.Birthday.TomAge() }); } /// /// 家庭成员详情 /// /// /// public async Task InfoAsync(int id) { var data = await dbClient.Queryable() .Where(x => x.Id == id) .Select(x=>new YB_Family { Type=x.Type }) .FirstAsync(); return data; } /// /// 设置目标体重 /// /// /// public async Task SetTargetAsync(YB_FamilyTarget model) { if (model.time.Date < DateTime.Now.Date) { return new ResultInfo(ResultState.SUCCESS, "目标日期设置错误"); } if (await dbClient.Queryable().AnyAsync(x => x.familyid == model.familyid)) { await dbClient.Updateable().SetColumns(x => new YB_FamilyTarget { time = model.time, weight = model.weight }).Where(x => x.familyid == model.familyid).ExecuteCommandAsync(); } else { await dbClient.Insertable(model).ExecuteCommandAsync(); } return new ResultInfo(ResultState.SUCCESS, "设置成功"); } /// /// 家庭成员模式切换 /// /// /// public async Task SetTypeAsync(FamilyTypeSetC2SDto data) { if (!await dbClient.Queryable().AnyAsync(x => x.Id == data.FamilyId)) { return new ResultInfo(ResultState.FAIL, "家庭成员未找到"); } await dbClient.Updateable().SetColumns(x => new YB_Family { Type = data.Type }).Where(x => x.Id == data.FamilyId).ExecuteCommandAsync(); return new ResultInfo(ResultState.SUCCESS, "成员类型修改成功"); } /// /// 计算成员的类型 /// /// 出生年月 /// public FamilyType GetType(DateTime brithday) { //获取年龄 var age = brithday.ToAge(); return age > 16 ? FamilyType.Adult :(age<=3 ?FamilyType.Baby: FamilyType.Children); } } }