package com.huimv.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.common.utils.PageUtils;
import com.huimv.common.utils.Query;

import com.huimv.management.dao.EartagDateDao;
import com.huimv.management.entity.EartagDateEntity;
import com.huimv.management.service.EartagDateService;


@Service("eartagDateService")
public class EartagDateServiceImpl extends ServiceImpl<EartagDateDao, EartagDateEntity> implements EartagDateService {

    @Autowired
    private EartagDateDao eartagDateDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<EartagDateEntity> page = this.page(
                new Query<EartagDateEntity>().getPage(params),
                new QueryWrapper<EartagDateEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public Map queryPageBytimeAndEartag(Map<String, Object> params) {

        //从请求头中找参数  1.耳标ID  2.starttime  3.end_time
        //构建条件查询wrapper
        //耳标
         String eartag = (String) params.get("eartag");
        String starttime = (String) params.get("startTime");
        String endtime = (String) params.get("endTime");
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Map map1 = new HashMap();

        //格式化传入的时间
        Date startTime = null;
        Date endTime = null;
        try {
            startTime = fmt.parse(starttime);
            endTime = fmt.parse(endtime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

          LambdaQueryWrapper<EartagDateEntity> lambdaQuery = Wrappers.lambdaQuery();
          lambdaQuery.eq(EartagDateEntity::getEartagId,eartag);
           //过滤时间
          lambdaQuery.between(EartagDateEntity::getCreateTime,startTime, endTime);

          List<EartagDateEntity> eartagDateEntities = baseMapper.selectList(lambdaQuery);

        List<EartagDateEntity> list = new ArrayList();
        for (EartagDateEntity data : eartagDateEntities) {

            Date times = data.getCreateTime();
            if (startTime.before(times) && endTime.after(times)){
                list.add(data);
            }
        }
        List timeList = new ArrayList();
        List tempList = new ArrayList();
        List sportsList = new ArrayList();
        List electricList = new ArrayList();
        List environmenttempList = new ArrayList();
        List sleeptime = new ArrayList();

        for (EartagDateEntity data : list) {
            timeList.add(fmt.format(data.getCreateTime()));
            tempList.add(data.getEartagTemp());
            if (data.getBoxId() == null) {
                sportsList.add(0);
            } else {
                sportsList.add(data.getGapSport());
            }
            electricList.add(data.getEartagPower());
            sleeptime.add(data.getBackFirst());
            environmenttempList.add(data.getEnvironmentTemp());
        }
        map1.put("time",timeList);
        map1.put("temp",tempList);
        map1.put("sports",sportsList);
        map1.put("electric",electricList);
        map1.put("environmenttemp",environmenttempList);
        map1.put("sleeptime",sleeptime);

        return map1;
    }

    @Override
    public Map getTemperatureCurve(Map<String, Object> params) {
        String eartag = (String)params.get("eartag");
        String startTime = (String)params.get("startTime");
        String endTime = (String)params.get("endTime");

        LambdaQueryWrapper<EartagDateEntity> lambdaQuery = Wrappers.lambdaQuery();

        lambdaQuery.eq(StringUtils.isNotBlank(eartag),EartagDateEntity::getEartagId,eartag);
        lambdaQuery.le(StringUtils.isNotBlank(endTime),EartagDateEntity::getCreateTime,endTime);
        lambdaQuery.ge(StringUtils.isNotBlank(startTime),EartagDateEntity::getCreateTime,startTime);

        List<EartagDateEntity> eartagDateEntities = baseMapper.selectList(lambdaQuery);

        List timeList = new ArrayList();
        List dataList = new ArrayList();
        for (EartagDateEntity eartagDateEntity : eartagDateEntities) {
            timeList.add(eartagDateEntity.getCreateTime());
            dataList.add(eartagDateEntity.getEartagTemp());
        }

        Map map = new HashMap();
        map.put("timeList",timeList);
        map.put("dataList",dataList);
        return map;
    }
}

