package com.zcxk.service.impl.alarm;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxk.common.StringUtils;
import com.zcxk.dao.alarm.DataAlarmManageMapper;
import com.zcxk.dto.alarm.DataAlarmDto;
import com.zcxk.dto.alarm.DataAlarmManageDto;
import com.zcxk.dto.alarm.DataAlarmQueryDto;
import com.zcxk.entity.alarm.DataAlarmManageEntity;
import com.zcxk.exception.ServiceException;
import com.zcxk.model.LoginUser;
import com.zcxk.service.DataAlarmManageService;
import com.zcxk.util.UserUtil;
import com.zcxk.vo.DataAlarmManageVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

/**
 * 数据报警管理
 * @author lihui
 * @version V1.0
 * @date 2021/5/31
 **/
@Service
@Log4j2
public class DataAlarmManageServiceImpl implements DataAlarmManageService {

    @Resource
    private DataAlarmManageMapper dataAlarmManageMapper;

    @Override
    public void saveDataAlarmManage(DataAlarmManageDto dto) {
        DataAlarmManageEntity entity = new DataAlarmManageEntity();
        BeanUtils.copyProperties(dto, entity);
        LoginUser currentUser = UserUtil.getCurrentUser();
        if (currentUser == null || StringUtils.isEmpty(currentUser.getTenantId())) {
            throw new ServiceException(500,"保存数据报警设置失败，用户数据为空");
        }
        entity.setTenantId(currentUser.getTenantId());
        entity.setCompanyOrgId(currentUser.getCompanyId());
        entity.setDeptOrgId(currentUser.getDepartmentId());
        entity.setStatus(1);
        entity.setDateCreate(new Date());
        entity.setDateUpdate(new Date());
        entity.setCreateBy(currentUser.getUsername());
        dataAlarmManageMapper.saveDataAlarmManage(entity);
    }

    @Override
    public DataAlarmManageEntity findAlarmManage(Long alarmId) {
        if (alarmId == null || alarmId == 0) {
            return null;
        }
        return dataAlarmManageMapper.findAlarmManage(alarmId);
    }

    @Override
    public Page<DataAlarmManageVo> 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());
        return dataAlarmManageMapper.findPage(new Page<>(dto.getPageNum(), dto.getPageSize()), queryDto);
    }

    @Override
    public void updateDataAlarmManage(DataAlarmManageDto dto) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        if (currentUser == null || StringUtils.isEmpty(currentUser.getTenantId())) {
            throw new ServiceException(500,"保存数据报警设置失败，用户数据为空");
        }
        DataAlarmManageEntity entity = new DataAlarmManageEntity();
        BeanUtils.copyProperties(dto, entity);
        entity.setUpdateBy(currentUser.getUsername());
        entity.setDateUpdate(new Date());
        entity.setId(dto.getAlarmId());
        dataAlarmManageMapper.updateDataAlarmManage(entity);
    }

    @Override
    public void delDataAlarmManage(List<Long> alarmIds) {
        if (CollectionUtil.isEmpty(alarmIds)){
            return;
        }
        dataAlarmManageMapper.delDataAlarmManage(alarmIds);
    }
}
