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.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.common.exception.RRException;
import com.huimv.common.utils.PageUtils;
import com.huimv.common.utils.Query;
import com.huimv.management.dao.DrinkingWaterDao;
import com.huimv.management.dao.FarmDao;
import com.huimv.management.dao.IndoorEnvironmentDao;
import com.huimv.management.dao.PigstyDao;
import com.huimv.management.entity.DrinkingWaterEntity;
import com.huimv.management.entity.FarmEntity;
import com.huimv.management.entity.IndoorEnvironmentEntity;
import com.huimv.management.entity.vo.DrinkingWaterVo;
import com.huimv.management.entity.vo.IndoorEnvironmentVo;
import com.huimv.management.service.DrinkingWaterService;
import com.huimv.management.service.IndoorEnvironmentService;
import com.huimv.management.util.ExportUtil;
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 org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 饮用水指标ServiceImpl
 * </p>
 *
 * @author yinhao
 * @version 1.0
 * @since 2021/6/11 16:46
 */
@Service("drinkingWaterService")
public class DrinkingWaterServiceImpl extends ServiceImpl<DrinkingWaterDao, DrinkingWaterEntity> implements DrinkingWaterService {

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

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

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

        //准备数据
        QueryWrapper<DrinkingWaterVo> queryWrapper = buildQueryWrapper(params);
        List<DrinkingWaterVo> list = baseMapper.page(queryWrapper);
        //构建auto-poi mv对象,导出数据
        return ExportUtil.buildModelAndView("饮用水指标", DrinkingWaterVo.class, "饮用水指标列表数据", list);
    }

    private QueryWrapper<DrinkingWaterVo> buildQueryWrapper(Map<String, Object> params) {
        //构建条件查询wrapper
        QueryWrapper<DrinkingWaterVo> queryWrapper = new QueryWrapper<>();

        //异常状态
        String abnormalStatus = (String) params.get("abnormalStatus");
        if (StringUtils.isEmpty(abnormalStatus)) {
            throw new RRException("请选择正常/异常状态!");
        }
        queryWrapper.eq("water.abnormal_status", Boolean.parseBoolean(abnormalStatus));

        //牧场名称
        String farmId = (String) params.get("formFarmId");
        if (StringUtils.isBlank(farmId)) {
            throw new RRException("请先选择要查询的牧场!");
        }
        queryWrapper.eq("water.farm_id", Integer.parseInt(farmId));


        //猪舍id
        String pigstyId = (String) params.get("pigstyId");
        if (StringUtils.isNotBlank(pigstyId)) {
            int val = Integer.parseInt(pigstyId);
            queryWrapper.eq("water.pigsty_id", val);
        }

        //开始时间
        String startTime = (String) params.get("startTime");
        if (StringUtils.isNotEmpty(startTime)) {
            queryWrapper.ge("water.collect_time", startTime);
        }

        //结束时间
        String endTime = (String) params.get("endTime");
        if (StringUtils.isNotEmpty(endTime)) {
            queryWrapper.le("water.collect_time", startTime);
        }

        //排除逻辑删除
        queryWrapper.apply(" water.deleted = 0 AND farm.deleted = 0 AND pigsty.deleted = 0");
        queryWrapper.orderByDesc("water.collect_time");
        return queryWrapper;
    }
}