zqy
2024-11-26 8a3d7199725a52b555d46daae5e95cef914c9ab3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.ruoyi.service.impl;
 
 
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.domain.ZHealthInfo;
import com.ruoyi.domain.ZHealthInfo;
import com.ruoyi.domain.ZHealthInfo;
import com.ruoyi.domain.dto.ZHealthInfoDto;
import com.ruoyi.domain.dto.ZHealthInfoDto;
import com.ruoyi.domain.health.*;
import com.ruoyi.mapper.ZHealthInfoMapper;
import com.ruoyi.service.ZHealthInfoService;
import org.springframework.stereotype.Service;
 
/**
 * <p>
 * 身体状况疾病表 服务实现类
 * </p>
 *
 * @author ojq
 * @since 2023-03-14
 */
@Service
public class ZHealthInfoServiceImpl extends ServiceImpl<ZHealthInfoMapper, ZHealthInfo> implements ZHealthInfoService {
 
    @Override
    public AjaxResult selectData() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId = user.getUserId();
 
        LambdaQueryWrapper<ZHealthInfo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZHealthInfo::getUid,userId);
        ZHealthInfo zHealthInfo = getOne(lqw);
 
        //如果是第一次进来,之前没有这个用户的数据,抛异常让用户先插入数据
        if(zHealthInfo == null){
            throw new RuntimeException("第一次进来,请先填写好数据");
        }
 
        //封装要返回的数据
        ZHealthInfoDto returnData = new ZHealthInfoDto();
        BeanUtils.copyProperties(zHealthInfo,returnData);
 
//        returnData.setName(zHealthInfo.getName());
//        returnData.setSex(zHealthInfo.getSex());
//        returnData.setEducationLevel(zHealthInfo.getEducationLevel());
//        returnData.setWork(zHealthInfo.getWork());
//        returnData.setPhone(zHealthInfo.getPhone());
//        returnData.setEmail(zHealthInfo.getEmail());
//        returnData.setMedicine(zHealthInfo.getMedicine());
//        returnData.setBaseDisease(zHealthInfo.getBaseDisease());
 
        //将json数组转换成对象
        returnData.setSkinTypeClass(JSON.parseObject(zHealthInfo.getSkinType(), SkinType.class));
        returnData.setPsychologyTypeClass(JSON.parseObject(zHealthInfo.getPsychologyType(), PsychologyType.class));
        returnData.setAttitudeClass(JSON.parseObject(zHealthInfo.getAttitude(), AttitudeType.class));
        returnData.setNutritionClass(JSON.parseObject(zHealthInfo.getNutrition(), NutritionType.class));
 
        return AjaxResult.success(returnData);
 
    }
 
    @Override
    public AjaxResult saveOrUpdateData(ZHealthInfoDto zHealthInfoDto) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId = user.getUserId();
 
        LambdaQueryWrapper<ZHealthInfo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZHealthInfo::getUid,userId);
        ZHealthInfo getResult = getOne(lqw);
 
        ZHealthInfo zHealthInfo = new ZHealthInfo();
 
        BeanUtils.copyProperties(zHealthInfoDto,zHealthInfo);
        zHealthInfo.setUid(userId);
        zHealthInfo.setSkinType(JSON.toJSONString(zHealthInfoDto.getSkinTypeClass()));
        zHealthInfo.setPsychologyType(JSON.toJSONString(zHealthInfoDto.getPsychologyTypeClass()));
        zHealthInfo.setAttitude(JSON.toJSONString(zHealthInfoDto.getAttitudeClass()));
        zHealthInfo.setNutrition(JSON.toJSONString(zHealthInfoDto.getNutritionClass()));
 
 
        if(getResult == null){
            //如果是第一次进来,那么是新增数据
            save(zHealthInfo);
        }else {
            //如果不是第一次,那就是修改数据了
            zHealthInfo.setId(getResult.getId());
            updateById(zHealthInfo);
        }
 
        return AjaxResult.success();
    }
}