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.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 org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

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

    @Autowired
    private FarmDao farmDao;

    @Autowired
    private PigstyDao pigstyDao;

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

        //构建条件查询wrapper
        QueryWrapper<DrinkingWaterVo> queryWrapper = new QueryWrapper<>();

        //异常状态
        String abnormalStatus = (String) params.get("abnormalStatus");
        if (StringUtils.isNotEmpty(abnormalStatus)) {
            queryWrapper.eq("water.abnormal_status", Boolean.parseBoolean(abnormalStatus));
        }

        //牧场名称
        String farmName = (String) params.get("farmName");
        if (farmName != null) {
            LambdaQueryWrapper<FarmEntity> query = Wrappers.<FarmEntity>lambdaQuery().like(FarmEntity::getName, farmName);
            List<Integer> idList = farmDao.selectIdList(query);
            if (CollUtil.isNotEmpty(idList)) {
                queryWrapper.in("water.farm_id", idList);
            }
        }

        //猪舍名称
        String pigstyName = (String) params.get("pigstyName");
        if (StringUtils.isNotEmpty(pigstyName)) {
            List<Integer> idList = pigstyDao.selectIdListByPigstyName(pigstyName);
            if (CollUtil.isNotEmpty(idList)) {
                queryWrapper.in("water.pigsty_id", idList);
            }
        }

        //开始时间
        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");

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


}