package com.huimv.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huimv.management.entity.YearPigBaseEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
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;
import com.huimv.management.dao.YearPigBaseDao;
import com.huimv.management.service.YearPigBaseService;




@Service("yearPigBaseService")
public class YearPigBaseServiceImpl extends ServiceImpl<YearPigBaseDao, YearPigBaseEntity> implements YearPigBaseService {

//    @Override
//    public PageUtils queryPage(Map<String, Object> params) {
//
//        LambdaQueryWrapper<YearPigBaseEntity> lambdaQuery = Wrappers.lambdaQuery();
//        String keywords = (String) params.get("keywords");
//        //没有参数
//        if (keywords == null || keywords.equals("")){
//            IPage<YearPigBaseEntity> page = this.page(
//                    new Query<YearPigBaseEntity>().getPage(params),
//                    new QueryWrapper<YearPigBaseEntity>()
//            );
//            return new PageUtils(page);
//        }
//
//        lambdaQuery.like(YearPigBaseEntity::getBreed,keywords);
//        IPage<YearPigBaseEntity> page = page(this.page(
//                new Query<YearPigBaseEntity>().getPage(params)
//        ), lambdaQuery);
//
//
//        return new PageUtils(page);
//    }


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

        LambdaQueryWrapper<YearPigBaseEntity> lambdaQuery = Wrappers.lambdaQuery();

        //耳标
        String eartag = (String) params.get("eartag");
        if (StringUtils.isNotEmpty(eartag)) {
            lambdaQuery.like(YearPigBaseEntity::getEartag,eartag);
        }

        //性别
        String sex = (String) params.get("sex");
        if (StringUtils.isNotEmpty(sex)) {
           lambdaQuery.eq(YearPigBaseEntity::getSex,sex);
        }

        //品种
        String breed = (String) params.get("breed");
        if (StringUtils.isNotEmpty(breed)) {
            lambdaQuery.eq(YearPigBaseEntity::getBreed,breed);
        }

        //健康状态
        String healthStatus = (String) params.get("healthStatus");
        if (StringUtils.isNotEmpty(healthStatus)) {
            lambdaQuery.eq(YearPigBaseEntity::getHealthStatus,healthStatus);
        }

        //养殖状态
        String outFenceStatus = (String) params.get("outFenceStatus");
        if (StringUtils.isNotEmpty(outFenceStatus)) {
            lambdaQuery.eq(YearPigBaseEntity::getOutFenceStatus,outFenceStatus);
        }

        //栏期
        String periodId = (String) params.get("periodId");
        if (StringUtils.isNotEmpty(periodId)) {
            lambdaQuery.eq(YearPigBaseEntity::getPeriodId,periodId);
        }

        //栋舍
        String pigstyId = (String) params.get("pigstyId");
        if (StringUtils.isNotEmpty(pigstyId)) {
            lambdaQuery.eq(YearPigBaseEntity::getPigstyId,pigstyId);
        }
        //单元
        String unitId = (String) params.get("unitId");
        if (StringUtils.isNotEmpty(unitId)) {
            lambdaQuery.eq(YearPigBaseEntity::getUnitId,unitId);
        }


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


        return new PageUtils(page);
    }


    @Override
    public void batchLairage(String[] eartags,Integer periodId) {
        for (String eartag : eartags) {
            UpdateWrapper<YearPigBaseEntity> updateWrapper = new UpdateWrapper<YearPigBaseEntity>();
            updateWrapper.eq("eartag",eartag).set("period_id",periodId);
            update(updateWrapper);
        }
    }

}