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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zcxk.charge.api.dto.AlarmDetailsDto;
import com.zcxk.charge.api.dto.AlarmDetailsHistoryDto;
import com.zcxk.charge.api.dto.AlarmDetailsRealTimeDto;
import com.zcxk.charge.api.vo.AlarmDetailsEntityVo;
import com.zcxk.charge.api.vo.AlarmDetailsHistoryVo;
import com.zcxk.charge.api.vo.AlarmDetailsRealTimeVo;
import com.zcxk.charge.core.dao.AlarmDetailMapper;
import com.zcxk.charge.core.entity.AlarmDetailsEntity;
import com.zcxk.charge.web.client.OperationManagerClient;
import com.zcxk.charge.web.client.UserCenterClient;
import com.zcxk.charge.web.service.AlarmDetailsService;
import com.zcxk.core.common.pojo.Message;
import com.zcxk.core.oauth2.pojo.LoginUser;
import com.zcxk.core.oauth2.util.UserUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

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

/**
 * @description
 * @author wangli
 * @data 2020-11-17 15:48
 */
@Service
@Log4j2
public class AlarmDetailsServiceImpl implements AlarmDetailsService {

    @Resource
    private AlarmDetailMapper alarmDetailMapper;

    @Resource(name = "messageRabbitTemplate")
    private AmqpTemplate messageRabbitTemplate;

    @Value("${receive.exchange.name}")
    private  String receiveExchangeName;

    @Value("${dispath.routing.key}")
    private  String dispathRoutingKey;


    private void send(Message message){
        log.debug("消息发送 exchange={}  routingkey={} 用户id={}",receiveExchangeName,dispathRoutingKey,message.getUserId());
        messageRabbitTemplate.convertAndSend(receiveExchangeName,dispathRoutingKey, JSONObject.toJSONString(message));
    }

    @Override
    public AlarmDetailsEntityVo selectById(Integer id) {
        return alarmDetailMapper.selectById(id);
    }

    @Override
    public List<AlarmDetailsEntityVo> selectByDeviceId(Long sceneId,Integer deviceId) {
        return alarmDetailMapper.selectByDeviceId(sceneId,deviceId);
    }
    @Override
    public List<AlarmDetailsEntityVo> selectByDeviceAttributeId(Long sceneId,Integer deviceId,Integer attributeId) {
        return alarmDetailMapper.selectByDeviceAttributeId(sceneId, deviceId,attributeId);
    }

    @Override
    public List<AlarmDetailsEntityVo> selectBySceneId(Integer sceneId) {
        return alarmDetailMapper.selectBySceneId(sceneId);
    }

    @Override
    public List<AlarmDetailsRealTimeVo> selectRealTimeByIds(List<Integer> ids) {
        return alarmDetailMapper.selectRealTimeByIds(ids);
    }
    @Override
    public List<AlarmDetailsHistoryVo> selectHistoryByIds(List<Integer> ids) {
        return alarmDetailMapper.selectHistoryByIds(ids);
    }
    @Override
    public Integer insert(AlarmDetailsEntity alarmDetailsEntity) {
        return alarmDetailMapper.insert(alarmDetailsEntity);
    }

    @Override
    public Integer batchInsert(List<AlarmDetailsEntity> alarmDetails) {
        return alarmDetailMapper.batchInsert(alarmDetails);
    }

    @Override
    public Integer deleteById(Integer id) {
        return alarmDetailMapper.deleteById(id);
    }

    @Override
    public Integer batchDelete(List<Integer> ids) {

        return alarmDetailMapper.batchDelete(ids);
    }

    @Override
    public Integer suspendedById(Integer id , Integer opState) {
        LoginUser loginUser = UserUtil.getCurrentUser();
        return alarmDetailMapper.suspendedById(id,opState,new Date(),loginUser.getUsername());
    }

    @Override
    public Integer update(AlarmDetailsEntity alarmDetailsEntity) {
        return alarmDetailMapper.update(alarmDetailsEntity);
    }

    @Override
    public List<AlarmDetailsEntityVo> selectList(AlarmDetailsDto alarmDetailsDto) {
        LoginUser loginUser = UserUtil.getCurrentUser();
        alarmDetailsDto.setTenantId(loginUser.getTenantId());
        alarmDetailsDto.setProgramItems(loginUser.getProgramItemList());
        alarmDetailsDto.setUserType(loginUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        alarmDetailsDto.setPermissonType(loginUser.getPermissonType());
        return alarmDetailMapper.selectList(alarmDetailsDto);
    }

    @Override
    public IPage<AlarmDetailsEntityVo> selectPage(IPage<AlarmDetailsDto> page, AlarmDetailsDto alarmDetailsDto) {
        LoginUser loginUser = UserUtil.getCurrentUser();
        alarmDetailsDto.setTenantId(loginUser.getTenantId());
        alarmDetailsDto.setProgramItems(loginUser.getProgramItemList());
        alarmDetailsDto.setUserType(loginUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        alarmDetailsDto.setPermissonType(loginUser.getPermissonType());
        return alarmDetailMapper.selectPage(page,alarmDetailsDto);
    }

    @Override
    public List<AlarmDetailsRealTimeVo> selectRealTimeList(AlarmDetailsDto alarmDetailsDto) {
        LoginUser loginUser = UserUtil.getCurrentUser();
        alarmDetailsDto.setTenantId(loginUser.getTenantId());
        alarmDetailsDto.setProgramItems(loginUser.getProgramItemList());
        alarmDetailsDto.setUserType(loginUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        alarmDetailsDto.setPermissonType(loginUser.getPermissonType());
        return alarmDetailMapper.selectRealTimeList(alarmDetailsDto);
    }

    @Override
    public IPage<AlarmDetailsRealTimeVo> selectRealTimePage(IPage<AlarmDetailsRealTimeDto> page, AlarmDetailsDto alarmDetailsDto) {
        LoginUser loginUser = UserUtil.getCurrentUser();
        alarmDetailsDto.setTenantId(loginUser.getTenantId());
        alarmDetailsDto.setProgramItems(loginUser.getProgramItemList());
        alarmDetailsDto.setUserType(loginUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        alarmDetailsDto.setPermissonType(loginUser.getPermissonType());
        return alarmDetailMapper.selectRealTimePage(page,alarmDetailsDto);
    }

    @Override
    public List<AlarmDetailsHistoryVo> selectHistoryList(AlarmDetailsDto alarmDetailsDto) {
        LoginUser loginUser = UserUtil.getCurrentUser();
        alarmDetailsDto.setTenantId(loginUser.getTenantId());
        alarmDetailsDto.setProgramItems(loginUser.getProgramItemList());
        alarmDetailsDto.setUserType(loginUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        alarmDetailsDto.setPermissonType(loginUser.getPermissonType());
        return alarmDetailMapper.selectHistoryList(alarmDetailsDto);
    }

    @Override
    public IPage<AlarmDetailsHistoryVo> selectHistoryPage(IPage<AlarmDetailsHistoryDto> page, AlarmDetailsDto alarmDetailsDto) {
        LoginUser loginUser = UserUtil.getCurrentUser();
        alarmDetailsDto.setTenantId(loginUser.getTenantId());
        alarmDetailsDto.setProgramItems(loginUser.getProgramItemList());
        alarmDetailsDto.setUserType(loginUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        alarmDetailsDto.setPermissonType(loginUser.getPermissonType());
        return alarmDetailMapper.selectHistoryPage(page,alarmDetailsDto);
    }

}
