package com.huimv.management.service.impl;

import com.huimv.common.exception.RRException;
import com.huimv.management.entity.vo.DeviceVo;
import org.apache.commons.lang3.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.DeviceDao;
import com.huimv.management.entity.DeviceEntity;
import com.huimv.management.service.DeviceService;

/**
 * 慧牧环控设备
 *
 * @author yinhao
 * @version ${version}
 * @date 2021-07-05 10:42:14
 */
@Service("deviceService")
public class DeviceServiceImpl extends ServiceImpl<DeviceDao, DeviceEntity> implements DeviceService {

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

        QueryWrapper<DeviceVo> queryWrapper = new QueryWrapper<>();
        String farmId = (String) params.get("formFarmId");
        if (StringUtils.isBlank(farmId)) {
            throw new RRException("请先指定牧场id再进行查询!");
        }
        queryWrapper.eq("device.farm_id", Integer.parseInt(farmId));

        String keyword = (String) params.get("keyword");
        if (StringUtils.isNotBlank(keyword)) {
            queryWrapper.like("device.device_id", keyword);
        }

        String pigstyId = (String) params.get("pigstyId");
        if (StringUtils.isNotBlank(pigstyId)) {
            queryWrapper.eq("device.pigsty_id", pigstyId);
        }

        queryWrapper.apply("device.deleted = 0");
        queryWrapper.orderByDesc("device.gmt_modified");

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

}