package com.YuyeTech.HeartRate.game.userData.service;

import cn.hutool.core.date.DateUtil;
import com.YuyeTech.HeartRate.common.service.*;
import com.YuyeTech.HeartRate.game.userData.pojo.UserData;
import com.YuyeTech.HeartRate.game.userData.vo.UserDataSimpleVo;
import com.YuyeTech.HeartRate.game.userData.vo.UserDataVo;
import com.YuyeTech.HeartRate.game.userData.repository.UserDataRepository;
import com.YuyeTech.HeartRate.utils.CopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Service
@Transactional
public class UserDataServiceImpl extends CommonServiceImpl<UserDataVo, UserData, String> implements UserDataService{

    @PersistenceContext
    private EntityManager em;
    @Autowired
    private UserDataRepository userDataRepository;


    @Override
    public UserDataSimpleVo findSimpleByUserId(String userId) {

        Optional<UserData> optionalE = userDataRepository.findByUserId(userId);
        if (!optionalE.isPresent()) {
            return null;
        }
        return CopyUtil.copy(optionalE.get(), UserDataSimpleVo.class);
    }


    @Override
    public UserDataVo findByUserId(String userId) {
        Optional<UserData> optionalE = userDataRepository.findByUserId(userId);
        if (!optionalE.isPresent()) {
            return null;
        }
        return CopyUtil.copy(optionalE.get(), UserDataVo.class);
    }


    /**
     * 获取hitCount，事务结束自动update到数据库
     * @param userId
     * @return
     */
    @Override
    public Map getHitCount(String userId) {
        Map map = new HashMap();
        boolean isReset = false;
        UserData userData = userDataRepository.findByUserId(userId).orElse(null);
        if (userData == null) {
            userData = new UserData();
            userData.setUserId(userId);
            userData.setHitCount(0);
            userData.setWeight(0d);
            userData.setCreateTime(DateUtil.date());
            userData.setUpdateTime(DateUtil.date());
            super.save(CopyUtil.copy(userData,UserDataVo.class));
        }
        if(!DateUtil.isSameDay(userData.getUpdateTime(),DateUtil.date())){
            //假如不是同一天，重置
            userData.setHitCount(0);
            userData.setUpdateTime(DateUtil.date());
            super.save(CopyUtil.copy(userData,UserDataVo.class));
            isReset = true;
        }
        map.put("hitCount",userData.getHitCount());
        map.put("isReset",isReset);
        return   map;
    }

    @Override
    public Map uploadHitCount(String userId, Integer hitCount) {
        Map map = new HashMap();
        boolean isReset = false;
        Optional<UserData> optionalE = userDataRepository.findByUserId(userId);
        if (!optionalE.isPresent()) {
            throw new RuntimeException("不存在userData");
        }
        UserData userData = optionalE.get();
        userData.setHitCount(hitCount);
        userData.setUpdateTime(DateUtil.date());
        super.save(CopyUtil.copy(userData,UserDataVo.class));
        map.put("hitCount",userData.getHitCount());
        map.put("isReset",isReset);
        return   map;

    }
}
