package com.huimv.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huimv.management.dao.YearPigBaseDao;
import com.huimv.management.entity.PigstyEntity;
import com.huimv.management.entity.PigstyEntity;
import com.huimv.management.entity.YearPigBaseEntity;
import com.huimv.management.entity.vo.CountOut;
import com.huimv.management.entity.vo.DrinkingWaterVo;
import com.huimv.management.entity.vo.PigStyVo;
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.PigstyDao;
import com.huimv.management.entity.PigstyEntity;
import com.huimv.management.service.PigstyService;


@Service("pigstyService")
public class PigstyServiceImpl extends ServiceImpl<PigstyDao, PigstyEntity> implements PigstyService {

    @Autowired
    private PigstyDao pigstyDao;

    @Autowired
    private YearPigBaseDao pigBaseDao;

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

        QueryWrapper<PigstyEntity> wrapper = new QueryWrapper<>();
        String farmId = (String) params.get("formFarmId");
        wrapper.eq(StringUtils.isNotBlank(farmId),"pigsty.farm_id",Integer.parseInt(farmId));

        String keyword = (String) params.get("keyword");
        if (StringUtils.isNotBlank(keyword)) {
            wrapper.like("pigsty.number",keyword);
        }

        String feederId = (String) params.get("feederId");

        if (StringUtils.isNotBlank(feederId)) {
            wrapper.like("pigsty.feeder_id",feederId);
        }


        //排除逻辑删除
        wrapper.apply("  employee.deleted = 0 AND pigsty.deleted = 0");
        IPage<PigStyVo> page = pigstyDao.page(new Query<>().getPage(params), wrapper);


        return new PageUtils(page);
    }

    @Override
    public List findAll(Integer formFarmId) {
        LambdaQueryWrapper<PigstyEntity> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(PigstyEntity::getFarmId,formFarmId);
        List<PigstyEntity> farmEntities = pigstyDao.selectList(lambdaQuery);
        return farmEntities;
    }


    @Override
    public Integer delete(Integer[] ids) {
        LambdaQueryWrapper<YearPigBaseEntity> lambdaQuery = Wrappers.lambdaQuery();
        for (Integer id : ids) {
            lambdaQuery.eq(YearPigBaseEntity::getPigstyId,id);
            List<YearPigBaseEntity> yearPigBaseEntities = pigBaseDao.selectList(lambdaQuery);
            if (yearPigBaseEntities != null && yearPigBaseEntities.size() >0){
                return 2;
            }
        }

       return 1;
    }

    @Override
    public List findAllByFarmId(Integer id) {
        LambdaQueryWrapper<PigstyEntity> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(PigstyEntity::getFarmId,id);
        return  pigstyDao.selectList(lambdaQuery);
    }

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


        IPage<PigstyEntity> page = this.page(
                new Query<PigstyEntity>().getPage(params),
                new QueryWrapper<PigstyEntity>()
        );

        return new PageUtils(page);
    }*/

}