279 lines
10 KiB
C#
279 lines
10 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// 家庭成员管理
|
|
/// </summary>
|
|
public class FamilyService : BaseService, IFamilyService, ITransient
|
|
{
|
|
private readonly ISqlSugarRepository<YB_Family> repository;
|
|
private readonly SqlSugarClient dbClient;
|
|
public FamilyService(ISqlSugarRepository<YB_Family> sqlSugarRepository)
|
|
|
|
{
|
|
repository = sqlSugarRepository;
|
|
dbClient = repository.Context;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 家庭成员列表
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<ResultInfo> GetListAsync()
|
|
{
|
|
var tempquery = dbClient.Queryable<YB_Family>()
|
|
.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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 家庭成员信息修改
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public async Task<ResultInfo> 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<YB_Family>().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<YB_Family>().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<YB_FamilyData>().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, "成员添加成功");
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 家庭成员删除
|
|
/// </summary>
|
|
/// <param name="id">家庭成员ID</param>
|
|
/// <returns></returns>
|
|
public async Task<ResultInfo> DeleteAsync(int id)
|
|
{
|
|
if (!await dbClient.Queryable<YB_Family>().AnyAsync(x => x.Id == id))
|
|
{
|
|
return new ResultInfo(ResultState.FAIL, "家庭成员未找到");
|
|
}
|
|
if (await dbClient.Queryable<YB_Family>().AnyAsync(x => x.Id == id && x.IsSelf == 1))
|
|
{
|
|
return new ResultInfo(ResultState.FAIL, "不能够删除自己");
|
|
}
|
|
await dbClient.Updateable<YB_Family>().SetColumns(x => new YB_Family
|
|
{
|
|
Status = StatusType.Delete
|
|
}).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)
|
|
{
|
|
var data = await dbClient.Queryable<YB_Family>().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()
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 家庭成员详情
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<YB_Family> InfoAsync(int id)
|
|
{
|
|
var data = await dbClient.Queryable<YB_Family>()
|
|
.Where(x => x.Id == id)
|
|
.Select(x=>new YB_Family
|
|
{
|
|
Type=x.Type
|
|
})
|
|
.FirstAsync();
|
|
return data;
|
|
}
|
|
/// <summary>
|
|
/// 设置目标体重
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public async Task<ResultInfo> SetTargetAsync(YB_FamilyTarget model)
|
|
{
|
|
if (model.time.Date < DateTime.Now.Date)
|
|
{
|
|
return new ResultInfo(ResultState.SUCCESS, "目标日期设置错误");
|
|
}
|
|
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(model).ExecuteCommandAsync();
|
|
}
|
|
return new ResultInfo(ResultState.SUCCESS, "设置成功");
|
|
}
|
|
/// <summary>
|
|
/// 家庭成员模式切换
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
/// <returns></returns>
|
|
public async Task<ResultInfo> SetTypeAsync(FamilyTypeSetC2SDto data)
|
|
{
|
|
if (!await dbClient.Queryable<YB_Family>().AnyAsync(x => x.Id == data.FamilyId))
|
|
{
|
|
return new ResultInfo(ResultState.FAIL, "家庭成员未找到");
|
|
}
|
|
await dbClient.Updateable<YB_Family>().SetColumns(x => new YB_Family
|
|
{
|
|
Type = data.Type
|
|
}).Where(x => x.Id == data.FamilyId).ExecuteCommandAsync();
|
|
return new ResultInfo(ResultState.SUCCESS, "成员类型修改成功");
|
|
}
|
|
/// <summary>
|
|
/// 计算成员的类型
|
|
/// </summary>
|
|
/// <param name="brithday">出生年月</param>
|
|
/// <returns></returns>
|
|
public FamilyType GetType(DateTime brithday)
|
|
{
|
|
//获取年龄
|
|
var age = brithday.ToAge();
|
|
return age > 16 ? FamilyType.Adult :(age<=3 ?FamilyType.Baby: FamilyType.Children);
|
|
}
|
|
}
|
|
}
|