import cn.iocoder.yudao.hospital.dal.dataobject.family_friends.HFamilyInfoIncremental;
import cn.iocoder.yudao.hospital.dal.dataobject.user.FoodAllergyIntoleranceIncremental;
import cn.iocoder.yudao.hospital.dal.dataobject.user.HBodyBasicInfo;
import cn.iocoder.yudao.hospital.dal.dataobject.user.HDailyActivities;
import cn.iocoder.yudao.hospital.service.family_friends.HFamilyInfoIncrementalService;
import cn.iocoder.yudao.hospital.service.user.FoodAllergyIntoleranceIncrementalService;
import cn.iocoder.yudao.hospital.service.user.HBodyBasicInfoService;
import cn.iocoder.yudao.hospital.service.user.HDailyActivitiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.lang.reflect.InvocationTargetException;
@Service
public class UserBasicInfoServiceImpl implements UserBasicInfoService {
//活動興趣和愛好
@Autowired
private HDailyActivitiesService hDailyActivitiesService;
//身高和體重
@Autowired
private HBodyBasicInfoService bodyBasicInfoService;
//家庭資訊
@Autowired
private HFamilyInfoIncrementalService hFamilyInfoIncrementalService;
//食品過敏史
@Autowired
private FoodAllergyIntoleranceIncrementalService foodAllergyService;
/**
* 新增使用者的基本資訊
*
* @param userBasicInfo 使用者基本資訊
* @return
*/
@Override
public int insert(UserBasicInfo userBasicInfo) {
int result = 0;
//子實體類
HBodyBasicInfo hBodyBasicInfo = new HBodyBasicInfo();
//主類 屬性賦值到對應的 子實體類的屬性
setSubEntityProperties(userBasicInfo, hBodyBasicInfo);
if (hasNonCommonFields(hBodyBasicInfo)) {//判斷子實體類,賦值是否成功
//子實體類,新增一條記錄
result += bodyBasicInfoService.insertHBodyBasicInfo(hBodyBasicInfo);
}
HDailyActivities hDailyActivities = new HDailyActivities();
setSubEntityProperties(userBasicInfo, hDailyActivities);
if (hasNonCommonFields(hDailyActivities)) {
result += hDailyActivitiesService.insertHDailyActivities(hDailyActivities);
}
HFamilyInfoIncremental hFamilyInfoIncremental = new HFamilyInfoIncremental();
setSubEntityProperties(userBasicInfo, hFamilyInfoIncremental);
if (hasNonCommonFields(hFamilyInfoIncremental)) {
result += hFamilyInfoIncrementalService.insertHFamilyInfoIncremental(hFamilyInfoIncremental);
}
FoodAllergyIntoleranceIncremental allergyIntoleranceIncremental = new FoodAllergyIntoleranceIncremental();
setSubEntityProperties(userBasicInfo, allergyIntoleranceIncremental);
if (hasNonCommonFields(allergyIntoleranceIncremental)) {
String str = foodAllergyService.createFoodAllergyIntoleranceIncremental(allergyIntoleranceIncremental);
result += (str == null || str.equals("")) ? 0 : 1;
}
return result;
}
/**
* 查詢使用者的基本資訊
*
* @param userId
* @return
*/
@Override
public UserBasicInfo selectByUserId(Long userId) {
UserBasicInfo userBasicInfo = new UserBasicInfo();
//查詢子類資訊
HBodyBasicInfo hBodyBasicInfo = bodyBasicInfoService.selectHBodyBasicInfoByUserId(userId);
//將子類查詢到的資訊,賦值給 主類
userBasicInfo.setHBodyBasicInfo(hBodyBasicInfo);
HDailyActivities hDailyActivities = hDailyActivitiesService.selectHDailyActivitiesByUserId(userId);
userBasicInfo.setHDailyActivities(hDailyActivities);
HFamilyInfoIncremental hFamilyInfoIncremental = hFamilyInfoIncrementalService.selectHFamilyInfoIncrementalByUserId(userId);
userBasicInfo.setHFamilyInfoIncremental(hFamilyInfoIncremental);
FoodAllergyIntoleranceIncremental foodAllergyIntoleranceIncremental = foodAllergyService.selectFoodAllergyByUserId(userId);
userBasicInfo.setHFoodAllergyIntoleranceIncremental(foodAllergyIntoleranceIncremental);
return userBasicInfo;
}
/**
* 將主類的欄位分發給子類
*
* @param user 主類
* @param subEntity 子類
*/
public void setSubEntityProperties(UserBasicInfo user, Object subEntity) {
//獲取子實體類的所有屬性
Field[] fields = subEntity.getClass().getDeclaredFields();
for (Field field : fields) {
//獲取屬性的名稱
String fieldName = field.getName();
Object fieldValue = null;
try {
//獲取fieldName對應在user類的get方法
Method method = user.getClass().getMethod("get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1));
fieldValue = method.invoke(user);
} catch (NoSuchMethodException e) {
//user類沒有對應的get方法,可忽略
} catch (InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
if (fieldValue != null) {
field.setAccessible(true);
try {
//給subEntity子類屬性賦值
field.set(subEntity, fieldValue);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
}
/**
* 判斷子類中,除了5個共通欄位其他欄位是否有值
*
* @param subEntity 待檢測的子類
* @return 任意一個欄位有值,則返回true,否則返回false
*/
public Boolean hasNonCommonFields(Object subEntity) {
Field[] fields = subEntity.getClass().getDeclaredFields();
for (Field field : fields) {
// 排除共同欄位
if ("id".equals(field.getName()) ||
"userId".equals(field.getName()) ||
"recordDate".equals(field.getName()) ||
"createTime".equals(field.getName()) ||
"updateTime".equals(field.getName())) {
continue;
}
field.setAccessible(true);
try {
Object value = field.get(subEntity);
//檢查子類屬性值,任意屬性值不為空,即返回true
if (value != null && !"".equals(value)) {
return true;
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
return false;
}
}