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.FarmDao;
import com.huimv.management.dao.PigstyDao;

import com.huimv.management.entity.*;
import com.huimv.management.service.FarmService;
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;
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;

@Service("farmService")
public class FarmServiceImpl extends ServiceImpl<FarmDao, FarmEntity> implements FarmService {

    @Autowired
    private FarmDao farmDao;

    @Autowired
    private PigstyDao pigstyDao;

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

        LambdaQueryWrapper<FarmEntity> lambdaQuery = Wrappers.lambdaQuery();
        String keywords = (String) params.get("keywords");
        //有参数
        if (StringUtils.isNotEmpty(keywords)) {
            lambdaQuery.apply("CONCAT(name,front_location ,manager,build_time) like CONCAT('%',{0},'%')",keywords);
        }
        //lambdaQuery.orderByDesc(FarmEntity::getBuildTime);

        IPage<FarmEntity> page = page(this.page(
                new Query<FarmEntity>().getPage(params)
        ), lambdaQuery);


        return new PageUtils(page);
    }

    @Override
    public List findAll() {
        LambdaQueryWrapper<FarmEntity> lambdaQuery = Wrappers.lambdaQuery();
        return farmDao.selectList(lambdaQuery);
    }

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

        return 1;
    }

    @Override
    public Boolean findByName(FarmEntity farm) {
        LambdaQueryWrapper<FarmEntity> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(FarmEntity::getName, farm.getName());
        FarmEntity farmEntity = farmDao.selectOne(lambdaQuery);
        if (farmEntity == null) {
            return true;
        }
        return false;

    }
    
   /* @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<FarmEntity> page = this.page(
                new Query<FarmEntity>().getPage(params),
                new QueryWrapper<FarmEntity>()
        );

        return new PageUtils(page);
    }*/

}