package com.huimv.management.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huimv.management.dao.FarmDao;
import com.huimv.management.entity.FarmEntity;
import com.huimv.management.entity.OutdoorEnvironmentEntity;
import com.huimv.management.entity.vo.IndoorEnvironmentVo;
import com.huimv.management.entity.vo.OutdoorEnvironmentVo;
import org.apache.commons.lang3.StringUtils;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
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.OutdoorEnvironmentDao;
import com.huimv.management.entity.OutdoorEnvironmentEntity;
import com.huimv.management.service.OutdoorEnvironmentService;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 *  室外环境ServiceImpl
 * </p>
 *
 * @author yinhao
 * @version 1.0
 * @date 2021/6/11 17:45
 */
@Service("outdoorEnvironmentService")
public class OutdoorEnvironmentServiceImpl extends ServiceImpl<OutdoorEnvironmentDao, OutdoorEnvironmentEntity> implements OutdoorEnvironmentService {


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<OutdoorEnvironmentVo> queryWrapper = buildQueryWrapper(params);

        //mapper文件sql编写并返回分页对象
        IPage<OutdoorEnvironmentVo> page = baseMapper.page(new Query<>().getPage(params),queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public ModelAndView exportXls(Map<String, Object> params) {
        QueryWrapper<OutdoorEnvironmentVo> queryWrapper = buildQueryWrapper(params);
        List<OutdoorEnvironmentVo> list = baseMapper.page(queryWrapper);

        //构建auto-poi mv对象,导出数据
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME,"室外环境");
        mv.addObject(NormalExcelConstants.CLASS, OutdoorEnvironmentVo.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("室外环境列表数据", "导出信息"));
        mv.addObject(NormalExcelConstants.DATA_LIST, list);

        return mv;
    }

    private QueryWrapper<OutdoorEnvironmentVo> buildQueryWrapper(Map<String, Object> params) {
        //构建条件wrapper
        QueryWrapper<OutdoorEnvironmentVo> queryWrapper = new QueryWrapper<>();
        //异常状态
        String abnormalStatus = (String) params.get("abnormalStatus");
        if (StringUtils.isNotEmpty(abnormalStatus)) {
            queryWrapper.eq("env.abnormal_status", Boolean.parseBoolean(abnormalStatus));
        }
        String farmId = (String) params.get("formFarmId");
        if (StringUtils.isNotBlank(farmId)) {
            queryWrapper.eq("env.farm_id",Integer.parseInt(farmId));
        }
        //时间区间(前)
        String startTime = (String) params.get("startTime");
        if (StringUtils.isNotEmpty(startTime)) {
            queryWrapper.ge("env.collect_time", startTime);
        }
        //时间区间(后)
        String endTime = (String) params.get("endTime");
        if (StringUtils.isNotEmpty(endTime)) {
            queryWrapper.le("env.collect_time", endTime);
        }
        //排除逻辑删除
        queryWrapper.apply(" env.deleted = 0 AND farm.deleted = 0");
        //根据采集时间倒序查询
        queryWrapper.orderByDesc("env.collect_time");
        return queryWrapper;
    }
}