package com.huimv.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huimv.common.utils.PageUtils;
import com.huimv.common.utils.Query;
import com.huimv.management.dao.CameraInfoDao;
import com.huimv.management.entity.CameraInfoEntity;
import com.huimv.management.entity.PastureAreaEntity;
import com.huimv.management.entity.WaterTargetEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.huimv.management.dao.PastureAreaDao;
import com.huimv.management.service.PastureAreaService;


@Service("pastureAreaService")
public class PastureAreaServiceImpl extends ServiceImpl<PastureAreaDao, PastureAreaEntity> implements PastureAreaService {
    @Autowired
    private PastureAreaDao pastureAreaDao;

    @Autowired
    private CameraInfoDao cameraInfoDao;

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

        //根据牧场查询
        String farmId = (String) params.get("formFarmId");

        QueryWrapper<PastureAreaEntity> wrapper = new QueryWrapper<PastureAreaEntity>();
        wrapper.eq(StringUtils.isNotBlank(farmId), "farm_id", farmId);
        IPage<PastureAreaEntity> page = this.page(
                new Query<PastureAreaEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);

    }

    @Override
    public List findAll(Integer formFarmId) {
        QueryWrapper<PastureAreaEntity> wrapper = new QueryWrapper<PastureAreaEntity>();

        wrapper.eq("farm_id", formFarmId);
        List<PastureAreaEntity> pastureAreaEntities = pastureAreaDao.selectList(wrapper);

        return pastureAreaEntities;
    }

    @Override
    public Boolean findCamerInfoByAreaId(Integer[] ids) {
        LambdaQueryWrapper<CameraInfoEntity> wrapper = Wrappers.lambdaQuery();
        for (Integer id : ids) {
            wrapper.eq(CameraInfoEntity::getAreaId, id);
            if (cameraInfoDao.selectCount(wrapper) > 0){
                return true;
            }

        }
        return false;
    }
}

