package com.huimv.management.service.impl;

import com.huimv.management.entity.vo.TransferPeriodLogVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
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.TransferPeriodLogDao;
import com.huimv.management.entity.TransferPeriodLogEntity;
import com.huimv.management.service.TransferPeriodLogService;


@Service("transferPeriodLogService")
public class TransferPeriodLogServiceImpl extends ServiceImpl<TransferPeriodLogDao, TransferPeriodLogEntity> implements TransferPeriodLogService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<TransferPeriodLogVo> queryWrapper = new QueryWrapper<>();

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

        String earTag = (String) params.get("earTag");
        if (StringUtils.isNotBlank(earTag)) {
            queryWrapper.like("log.ear_tag",earTag);
        }

        String oldPigstyId = (String) params.get("oldPigstyId");
        if (StringUtils.isNotBlank(oldPigstyId)) {
            queryWrapper.eq("log.old_pigsty_id",Integer.parseInt(oldPigstyId));
        }

        String newPigstyId = (String) params.get("newPigstyId");
        if (StringUtils.isNotBlank(newPigstyId)) {
            queryWrapper.eq("log.new_pigsty_id",newPigstyId);
        }

        String oldUnitId = (String) params.get("oldUnitId");
        if (StringUtils.isNotBlank(oldUnitId)) {
            queryWrapper.eq("log.old_unit_id",Integer.parseInt(oldUnitId));
        }

        String newUnitId = (String) params.get("newUnitId");
        if (StringUtils.isNotBlank(oldUnitId)) {
            queryWrapper.eq("log.new_unit_id",Integer.parseInt(newUnitId));
        }

        queryWrapper.apply("pigsty1.deleted = 0 AND pigsty2.deleted = 0 AND unit1.deleted = 0 AND unit2.deleted = 0 AND period1.deleted = 0 AND period2.deleted = 0");
        queryWrapper.orderByDesc("log.gmt_create");

        IPage<TransferPeriodLogVo> page = baseMapper.page(new Query<>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

}