package com.YuyeTech.TPlat.service.impl;

import com.YuyeTech.TPlat.enums.UserEnum;
import com.YuyeTech.TPlat.exception.UserException;
import com.YuyeTech.TPlat.dataobject.WeightInfo;
import com.YuyeTech.TPlat.repository.WeightInfoRepository;
import com.YuyeTech.TPlat.service.WeightInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:slambb
 * @date:2019/12/31
 */
@Service
public class WeightInfoServiceImpl implements WeightInfoService {
    @Autowired
    private WeightInfoRepository weightInfoRepository;

    @Override
    public WeightInfo findWeightInfoById(String userId) {
        WeightInfo mainInfo = weightInfoRepository.findById(userId).orElse(null);
        if (mainInfo == null) {
            throw new UserException(UserEnum.USER_NOT_EXIST.getCode(), UserEnum.USER_NOT_EXIST.getMessage());
        }
        return null;
    }

    @Override
    public WeightInfo addWeightInfo(WeightInfo weightInfo) {
        return weightInfoRepository.save(weightInfo);
    }

    @Override
    public Map findAll(String userId) {
        List<Object> doubleList = new ArrayList<Object>();
        List<Object> timestampList = new ArrayList<Object>();
        weightInfoRepository.findByUserId(userId).stream().forEach(
                e -> {
                    doubleList.add(e.getWeight());
                    timestampList.add(e.getCreateTime());
                }
        );
//        List<Map>  map= weightInfoRepository.findByUserId(userId).stream().map(
//                e->{
//                    Map map1 = new HashMap();
//                    map1.put("weightList",e.getWeight());
//                    map1.put("timestamp",e.getCreateTime());
//                    return map1;
//                }
//        ).collect(Collectors.toList());
        Map map = new HashMap();
        map.put("weightList", doubleList);
        map.put("timestamp", timestampList);
        return map;
    }


    @Override
    public Map findWeightInfoByPage(String userId,Pageable pageable) {
        List<Object> doubleList = new ArrayList<>();
        List<Object> timestampList = new ArrayList<>();
        weightInfoRepository.findByUserId(userId, pageable).stream().forEach(
                e -> {
                    doubleList.add(e.getWeight());
                    timestampList.add(e.getCreateTime());
                }
        );
        Map map = new HashMap();
        map.put("weightList", doubleList);
        map.put("timestamp", timestampList);
        return map;
    }
}
