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 com.huimv.management.util.GetUserUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
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.WarningInfoDao;
import com.huimv.management.entity.WarningInfoEntity;
import com.huimv.management.service.WarningInfoService;

import javax.servlet.http.HttpServletRequest;

/**
 * <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 = buildQueryWrapper(params);
        IPage<WarningInfoVo> page = baseMapper.page(new Query<>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public void verify(WarningInfoDto warningInfoDto, HttpServletRequest request) {

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

        String username = null;
        try {
            username = GetUserUtil.getUser(request).getUsername();
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            throw new RRException("获取用户信息异常!");
        }

        updateWrapper.set(WarningInfoEntity::getVerifyDescription, warningInfoDto.getVerifyDescription())
                .set(WarningInfoEntity::getVerifyStatus, true)
                .set(WarningInfoEntity::getVerifyUser,username)
                .eq(WarningInfoEntity::getId, warningInfoDto.getId());
        update(updateWrapper);
    }

    @Override
    public List<WarningInfoVo> queryByCondition(Map<String, Object> params) {
        QueryWrapper<WarningInfoVo> queryWrapper = buildQueryWrapper(params);
        return baseMapper.page(queryWrapper);
    }


    private QueryWrapper<WarningInfoVo> buildQueryWrapper(Map<String, Object> params) {
        QueryWrapper<WarningInfoVo> queryWrapper = new QueryWrapper<>();

        String belongTo = (String) params.get("belongTo");
        if (StringUtils.isNotBlank(belongTo)) {
            queryWrapper.eq("warn.belong_to", Short.parseShort(belongTo));
        }
        //报警类型
        String warningType = (String) params.get("warningType");
        if (StringUtils.isNotBlank(warningType)) {
            queryWrapper.like("warn.warning_type", warningType);
        }
        //牧场id
        String farmId = (String) params.get("formFarmId");
        if (StringUtils.isNotBlank(farmId)) {
            queryWrapper.eq("warn.farm_id", Integer.parseInt(farmId));
        }
        //猪舍id
        String pigstyId = (String) params.get("pigstyId");
        if (StringUtils.isNotBlank(pigstyId)) {
            queryWrapper.eq("warn.pigsty_id", Integer.parseInt(pigstyId));
        }
        String unitId = (String) params.get("unitId");
        if (StringUtils.isNotBlank(unitId)) {
            queryWrapper.eq("warn.unit_id", Integer.parseInt(unitId));
        }
        //猪耳标号
        String earTag = (String) params.get("earTag");
        if (StringUtils.isNotBlank(earTag)) {
            queryWrapper.like("warn.ear_tag", earTag);
        }
        String verifyStatus = (String) params.get("verifyStatus");
        if (StringUtils.isNotBlank(verifyStatus)) {
            queryWrapper.eq("warn.verify_status", Boolean.parseBoolean(verifyStatus));
        }
        //时间区间(前)
        String startTime = (String) params.get("startTime");
        //时间区间(后)
        String endTime = (String) params.get("endTime");

        queryWrapper
                .ge(StringUtils.isNotBlank(startTime), "warn.warning_time", startTime)
                .le(StringUtils.isNotBlank(endTime), "warn.warning_time", endTime)
                .apply("farm.deleted = 0 AND IFNULL(pigsty.deleted,0) = 0 AND IFNULL(unit.deleted,0) = 0")
                .orderByDesc("warn.warning_time");
        return queryWrapper;
    }


}