package com.huimv.beeboxs.service.impl;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.beeboxs.common.utils.Result;
import com.huimv.beeboxs.common.utils.ResultCode;
import com.huimv.beeboxs.entity.HiveBeehive;
import com.huimv.beeboxs.entity.dto.ExcelDto;
import com.huimv.beeboxs.mapper.HiveBeehiveMapper;
import com.huimv.beeboxs.service.IHiveBeehiveService;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-04-21
 */
@Service
public class HiveBeehiveServiceImpl extends ServiceImpl<HiveBeehiveMapper, HiveBeehive> implements IHiveBeehiveService {

    @Override
    public Result searchList(Map map) {

//        String.format("%.2f",maxWater);
        String startDate = (String)map.get("startDate");
        String endDate = (String)map.get("endDate");
        String hiveId = (String)map.get("hiveId");

        QueryWrapper<HiveBeehive> wrapper = new QueryWrapper<>();
        if (StringUtils.isBlank(hiveId)){
            return new Result(10001,"请选择蜂箱",false);
        }
        wrapper.eq("hive_id",hiveId);
//        wrapper.orderByDesc("create_date");

        wrapper.ge(StringUtils.isNotBlank(startDate),"create_date",startDate);
        wrapper.le(StringUtils.isNotBlank(endDate),"create_date",endDate);
        List<HiveBeehive> list = this.list(wrapper);
//        for (HiveBeehive hiveBeehive : list) {
//
//            Double humi = Double.parseDouble(hiveBeehive.getHumi());
//            double random = Math.random()*2;
//            if (humi >55.0){
//                hiveBeehive.setHumi(String.format("%.2f",47+random));
//            }
//
//        }
        return new Result(ResultCode.SUCCESS,list);
    }

    @Override
    public void listExcel(Map map, HttpServletResponse response)   {
        String startDate = (String)map.get("startDate");
        String endDate = (String)map.get("endDate");
        String hiveId = (String)map.get("hiveId");

        QueryWrapper<HiveBeehive> wrapper = new QueryWrapper<>();

        wrapper.eq("hive_id",hiveId);

        wrapper.ge(StringUtils.isNotBlank(startDate),"create_date",startDate);
        wrapper.le(StringUtils.isNotBlank(endDate),"create_date",endDate);
        List<HiveBeehive> list = this.list(wrapper);

        List<ExcelDto> excelDtos = new ArrayList<>();
        for (int  i = 1 ; i<=list.size();i++){
            HiveBeehive hiveBeehive = list.get(i - 1);
            ExcelDto excelDto = new ExcelDto();
            excelDto.setSerial(i);
            excelDto.setUploadTime(hiveBeehive.getCreateDate());
            excelDto.setHumi(hiveBeehive.getHumi());
            excelDto.setHumiOut(hiveBeehive.getHumiOut());
            excelDto.setTemp(hiveBeehive.getTemp());
            excelDto.setTempOut(hiveBeehive.getTempOut());
            excelDto.setOther1(hiveBeehive.getOther1());

            excelDtos.add(excelDto);
        }



        ExcelWriter writer = ExcelUtil.getWriter();
//        ExcelWriter writer = ExcelUtil.getWriter("d:/writeBeanTest.xlsx");
        writer.addHeaderAlias("serial", "序号");
        writer.addHeaderAlias("uploadTime", "时间");
        writer.addHeaderAlias("temp", "室内温度");
        writer.addHeaderAlias("humi", "室内湿度");
        writer.addHeaderAlias("tempOut", "室外温度");
        writer.addHeaderAlias("humiOut", "室外湿度");
        writer.addHeaderAlias("other1", "蜜蜂状态");
        writer.write(excelDtos,true);
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        writer.flush(out);
        writer.close();
//        return new Result(ResultCode.SUCCESS,excelDtos);
    }
}
