package com.huimv.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huimv.common.exception.RRException;
import com.huimv.management.entity.dto.WarningInfoDto;
import com.huimv.management.entity.vo.WarningInfoVo;
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.WarningInfoDao;
import com.huimv.management.entity.WarningInfoEntity;
import com.huimv.management.service.WarningInfoService;

/**
 * <p>
 * 报警信息ServiceImpl
 * </p>
 *
 * @author yinhao
 * @version 1.0
 * @date 2021/6/15 13:37
 */
@Service("warningInfoService")
public class WarningInfoServiceImpl extends ServiceImpl<WarningInfoDao, WarningInfoEntity> implements WarningInfoService {

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

        QueryWrapper<WarningInfoVo> queryWrapper = new QueryWrapper<>();

        //报警类型不能为空
        String warningType = (String) params.get("warningType");
        if (StringUtils.isBlank(warningType)) {
            throw new RRException("报警类型不能为空!");
        }
        //牧场id
        String farmId = (String) params.get("farmId");
        //猪舍id
        String pigstyId = (String) params.get("pigstyId");
        //猪耳标号
        String earTag = (String) params.get("earTag");
        //时间区间(前)
        String startTime = (String) params.get("startTime");
        //时间区间(后)
        String endTime = (String) params.get("endTime");

        queryWrapper.eq(StringUtils.isNotBlank(warningType),"warn.warning_type",Short.parseShort(warningType))
                .eq(StringUtils.isNotBlank(farmId),"warn.farm_id",Integer.parseInt(farmId))
                .eq(StringUtils.isNotBlank(pigstyId),"warn.pigsty_id",Integer.parseInt(pigstyId))
                .eq(StringUtils.isNotBlank(earTag),"warn.ear_tag",Integer.parseInt(earTag))
                .ge(StringUtils.isNotBlank(startTime),"warn.warning_time",startTime)
                .le(StringUtils.isNotBlank(endTime),"warn.warning_time",endTime)
                .apply("warn.verify_status = 0 AND farm.deleted = 0 AND pigsty.deleted = 0")
                .orderByDesc("warn.warning_time");

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

    }

    @Override
    public void verify(WarningInfoDto warningInfoDto) {

        LambdaUpdateWrapper<WarningInfoEntity> updateWrapper = Wrappers.lambdaUpdate();

        updateWrapper.set(WarningInfoEntity::getVerifyDescription,warningInfoDto.getVerifyDescription())
                .set(WarningInfoEntity::getVerifyStatus,false)
                .eq(WarningInfoEntity::getId,warningInfoDto.getId());

        update(updateWrapper);
    }
}