package com.YuyeTech.TPlat.controller;

import com.YuyeTech.TPlat.VO.FitnessVO;
import com.YuyeTech.TPlat.VO.ResultVO;
import com.YuyeTech.TPlat.enums.UserEnum;
import com.YuyeTech.TPlat.exception.UserException;
import com.YuyeTech.TPlat.form.FitnessForm;
import com.YuyeTech.TPlat.dataobject.FitnessProgram;
import com.YuyeTech.TPlat.service.FitnessProgramService;
import com.YuyeTech.TPlat.utils.PropertyUtil;
import com.YuyeTech.TPlat.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author:slambb
 * @date:2019/12/31
 */
@RestController
@RequestMapping("/fitness_program")
@Slf4j
public class FitnessProgramController {

    @Autowired
    private FitnessProgramService fitnessProgramService;

    @PostMapping("add")
    public ResultVO addFitnessInfo(@Valid FitnessForm form,
                                   @RequestParam(value = "userId") String userId) {

//        log.info("addFitnessInfo userId"+userId);
        FitnessProgram fitnessProgram = new FitnessProgram();
        try {
            fitnessProgram = fitnessProgramService.findFitnessProgramById(userId);


            if (fitnessProgram != null) {
                //获取数据库的卡路里数量
                Integer _fitnessCumulativeCalorie = fitnessProgram.getCumulativeCalorie();

                BeanUtils.copyProperties(form, fitnessProgram, PropertyUtil.getNullPropertyNames(form));

                if (fitnessProgram.getCumulativeCalorie() < _fitnessCumulativeCalorie)
                    fitnessProgram.setCumulativeCalorie(_fitnessCumulativeCalorie);

            } else {
                fitnessProgram = new FitnessProgram();
                form.setUserId(userId);
                BeanUtils.copyProperties(form, fitnessProgram);
            }
            //保存数据库，获取保存后的字段
            fitnessProgram = fitnessProgramService.addFitnessProgram(fitnessProgram);
        } catch (UserException e) {
            return ResultVOUtil.error(e.getCode(), e.getMessage());
        }

        //返回对应展示的信息
        FitnessVO fitnessVO = new FitnessVO();
        BeanUtils.copyProperties(fitnessProgram, fitnessVO);
        return ResultVOUtil.success(fitnessVO);
    }

    @GetMapping("/get")
    public ResultVO getUserInfo(@RequestParam(value = "userId", required = false) String userId) {
        //请求userId为空时候
        if (userId == null) {
            throw new UserException(UserEnum.USER_NOT_EXIST);
        }
        //查询对应数据
        FitnessProgram fitnessProgram = fitnessProgramService.findFitnessProgramById(userId);

        if (fitnessProgram == null) {
            return ResultVOUtil.error(UserEnum.FITNESS_NOT_DATA.getCode(), UserEnum.FITNESS_NOT_DATA.getMessage());
        }
        //返回对应展示的信息
        FitnessVO fitnessVO = new FitnessVO();
        BeanUtils.copyProperties(fitnessProgram, fitnessVO);
        return ResultVOUtil.success(fitnessVO);
    }
}
