package com.zcxk.charge.web.service.impl.alarm;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxk.charge.core.dao.alarm.DataAlarmAnalysisMapper;
import com.zcxk.charge.api.dto.alarm.DataAlarmDto;
import com.zcxk.charge.api.dto.alarm.DataAlarmQueryDto;
import com.zcxk.charge.web.service.DataAlarmAnalysisService;
import com.zcxk.charge.api.vo.DataAlarmAnalysisVo;
import com.zcxk.core.oauth2.pojo.LoginUser;
import com.zcxk.core.oauth2.util.UserUtil;
import com.zcxk.core.utils.DateUtil;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 数据报警数据分析
 *
 * @author lihui
 * @version V1.0
 * @date 2021/5/31
 **/
@Service
@Log4j2
public class DataAlarmAnalysisServiceImpl implements DataAlarmAnalysisService {

    @Resource
    private DataAlarmAnalysisMapper dataAlarmAnalysisMapper;

    @Override
    public Page<DataAlarmAnalysisVo> findPage(DataAlarmDto dto) {
        DataAlarmQueryDto queryDto = new DataAlarmQueryDto();
        BeanUtils.copyProperties(dto, queryDto);
        LoginUser loginUser = UserUtil.getCurrentUser();
        queryDto.setTenantId(loginUser.getTenantId());
        queryDto.setProgramItems(loginUser.getProgramItemList());
        queryDto.setUserType(loginUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        queryDto.setPermissonType(loginUser.getPermissonType());
        queryDto.setBeginTime(StringUtils.isEmpty(dto.getBeginTime()) ? DateUtil.formatDate(new Date(),"yyyy-MM-dd") + " 00:00:00" : dto.getBeginTime());
        queryDto.setEndTime(StringUtils.isEmpty(dto.getEndTime()) ? DateUtil.formatDate(new Date(),"yyyy-MM-dd") + " 23:59:59" : dto.getEndTime());
        return dataAlarmAnalysisMapper.findPage(new Page<>(dto.getPageNum(), dto.getPageSize()), queryDto);
    }
}
