package cn.wise.sc.energy.power.plant.business.service.impl;

import cn.wise.sc.energy.power.plant.business.domain.DeviceInfo;
import cn.wise.sc.energy.power.plant.business.domain.EventInfo;
import cn.wise.sc.energy.power.plant.business.domain.vo.EntityVo;
import cn.wise.sc.energy.power.plant.business.repository.DeviceInfoRepository;
import cn.wise.sc.energy.power.plant.business.repository.EventInfoRepository;
import cn.wise.sc.energy.power.plant.business.service.IEventInfoService;
import lombok.Data;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @description: 事件记录服务层实现类
 * @author: qh
 * @create: 2020-09-03 10:22
 **/
@Service
public class EventInfoServiceImpl extends BaseServiceImpl<Long, EventInfo> implements IEventInfoService {

    final
    DeviceInfoRepository deviceInfoRepository;
    final
    EventInfoRepository eventInfoRepository;

    public EventInfoServiceImpl(EventInfoRepository eventInfoRepository,
                                DeviceInfoRepository deviceInfoRepository) {
        this.eventInfoRepository = eventInfoRepository;
        this.deviceInfoRepository = deviceInfoRepository;
    }

    @Override
    public List<AlertCount> countAlert() {

        List<Object[]> objects = eventInfoRepository.countAlert();
        List<AlertCount> rts = new ArrayList<>();

        for (Object[] objs : objects) {
            //countAlert()查询2个字段 count()、alertState
            if (objs.length != 2) {
                continue;
            }
            Long count = (long) objs[0];
            Integer state = (int) objs[1];
            String deviceId = (String) objs[2];
            Optional<DeviceInfo> byId = deviceInfoRepository.findById(deviceId);
            if (byId.isPresent()) {
                continue;
            }
            String deviceName = byId.get().getDeviceName();
            AlertCount alertCount;
            if (state == 2) {
                alertCount = new AlertCount("preAlert", deviceName, count);
            } else if (state == 3) {
                alertCount = new AlertCount("tallAlert", deviceName, count);
            } else {
                alertCount = new AlertCount("tooTallAlert", deviceName, count);
            }
            rts.add(alertCount);
        }

        return rts;
    }

    @Override
    public List<EntityVo> listByState(Integer state) {

        if (state < 2 || state > 4) {
            return null;
        }
        List<EventInfo> list = eventInfoRepository.findAll((Specification<EventInfo>) (root, query, criteriaBuilder) -> {

            Path<Integer> alertStat = root.get("alertStat");
            return criteriaBuilder.equal(alertStat, state);
        });

        List<EntityVo> rts = new ArrayList<>(list.size());
        for (EventInfo eventInfo : list) {
            EntityVo entityVo = eventInfo.toVo();
            rts.add(entityVo);
        }
        return rts;
    }

    @Override
    public List<EventInfo> getList(List<String> kKSCodes) {


       return eventInfoRepository.findAll((Specification<EventInfo>) (root, query, criteriaBuilder) -> {
            CriteriaBuilder.In<String> in = criteriaBuilder.in(root.get("kKSCode"));
            if (kKSCodes.size() != 0) {
                for (String kKSCode : kKSCodes) {
                    in.value(kKSCode);
                }
            }
            return in;
        });
    }

    @Data
    public static class AlertCount {
        public AlertCount(String alertLevel, String deviceName, Long count) {
            this.alertLevel = alertLevel;
            this.deviceName = deviceName;
            this.count = count;
        }

        /**
         * 报警级别
         */
        private String alertLevel;

        /**
         * 对应设备名字
         */
        private String deviceName;

        /**
         * 对应报警次数
         */
        private Long count;
    }
}
