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.entity.UnitEntity;
import com.huimv.management.entity.vo.DrinkingWaterVo;
import com.huimv.management.entity.vo.UnitVo;
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.UnitDao;
import com.huimv.management.service.UnitService;


@Service("unitService")
public class UnitServiceImpl extends ServiceImpl<UnitDao, UnitEntity> implements UnitService {

    @Autowired
    private UnitDao unitDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //构建条件查询wrapper
        QueryWrapper<DrinkingWaterVo> queryWrapper = new QueryWrapper<>();

        String formFarmId = (String) params.get("formFarmId");
        if (StringUtils.isNotBlank(formFarmId)) {
            queryWrapper.eq("unit.farm_id", Integer.parseInt(formFarmId));
        }

        //单元编号
        String keyword = (String) params.get("keyword");
        if (StringUtils.isNotBlank(keyword)) {

            queryWrapper.like("unit.number", keyword);
        }

        //栏期编号
        String periodId = (String) params.get("periodId");
        if (StringUtils.isNotBlank(periodId)) {
            queryWrapper.eq("unit.current_period_id", periodId);
        }

        //猪舍编号
        String pigstyId = (String) params.get("pigstyId");
        if (StringUtils.isNotBlank(pigstyId)) {
            queryWrapper.eq("unit.pigsty_id", pigstyId);
        }
        queryWrapper.apply("unit.deleted = 0  AND pigsty.deleted = 0");
        IPage<UnitVo> page = unitDao.list(new Query<>().getPage(params),queryWrapper);


        return new PageUtils(page);
    }

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

    @Override
    public List findAllByFarmId(Integer id) {
        LambdaQueryWrapper<UnitEntity> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(UnitEntity::getPigstyId, id);
        List<UnitEntity> farmEntities = unitDao.selectList(lambdaQuery);
        return farmEntities;
    }

}