package com.huimv.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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 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) {

        LambdaQueryWrapper<PigstyEntity> lambdaQuery = Wrappers.lambdaQuery();
        String keywords = (String) params.get("keywords");
        //没有参数
        if (keywords == null || keywords.equals("")){
            IPage<PigstyEntity> page = this.page(
                    new Query<PigstyEntity>().getPage(params),
                    new QueryWrapper<PigstyEntity>()
            );
            return new PageUtils(page);
        }

        lambdaQuery.like(PigstyEntity::getNumber,keywords);
        IPage<PigstyEntity> page = page(this.page(
                new Query<PigstyEntity>().getPage(params)
        ), lambdaQuery);


        return new PageUtils(page);
    }

    @Override
    public List findAll() {
        LambdaQueryWrapper<PigstyEntity> lambdaQuery = Wrappers.lambdaQuery();
        List<PigstyEntity> pastureEntities = pigstyDao.selectList(lambdaQuery);
        return pastureEntities;
    }


    @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 PageUtils queryPage(Map<String, Object> params) {


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

        return new PageUtils(page);
    }*/

}