package com.xydl.cac.service.impl;

import com.xydl.cac.entity.IedDlConfig;
import com.xydl.cac.entity.IedDlRecord;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.repository.IedDlRecordRepository;
import com.xydl.cac.service.IedDlConfigService;
import com.xydl.cac.service.IedDlRecordService;
import com.xydl.cac.spectrogram.SpectrogramHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class IedDlRecordServiceImpl implements IedDlRecordService {

    @Resource
    IedDlRecordRepository repository;
    @Resource
    IedDlConfigService configService;
    @Resource
    SpectrogramHandler spectrogramHandler;

    @Override
    public Page<IedDlRecord> list(Integer configId, Integer devId, Date startTime, Date endTime, int pageNum, int pageSize) throws Exception {
        PageRequest request = PageRequest.of(pageNum - 1, pageSize);
        Specification<IedDlRecord> specification = (root, query, builder) -> {
            Predicate predicate = builder.conjunction();
            if (configId != null) {
                predicate.getExpressions().add(builder.equal(root.get("configId"), configId));
                query.orderBy(builder.desc(root.get("dTime")));
            } else if (devId != null) {
                predicate.getExpressions().add(builder.equal(root.get("devId"), devId));
                query.orderBy(builder.desc(root.get("dTime")));
            } else {
                query.orderBy(builder.desc(root.get("id")));
            }
            if (startTime != null) {
                predicate.getExpressions().add(builder.greaterThan(root.get("dTime"), startTime));
            }
            if (endTime != null) {
                predicate.getExpressions().add(builder.lessThanOrEqualTo(root.get("dTime"), endTime));
            }
            return predicate;
        };
        Page<IedDlRecord> result = repository.findAll(specification, request);
        if (!CollectionUtils.isEmpty(result.getContent())) {
            List<IedDlConfig> configList = configService.listAll();
            for (IedDlRecord item : result.getContent()) {
                for (IedDlConfig config : configList) {
                    if (item.getConfigId().intValue() == config.getId().intValue()) {
                        item.setConfig(config);
                        break;
                    }
                }
                spectrogramHandler.jsonToModel(item);
            }
        }
        return result;
    }

    @Override
    public void add(IedDlRecord item) {
        item.setId(null);
        repository.save(item);
    }

    @Override
    public boolean exist(IedDlRecord item) {
        List<IedDlRecord> list = repository.findByConfigIdAndFilename(item.getConfigId(), item.getFilename());
        if (CollectionUtils.isEmpty(list)) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public void update(IedDlRecord item) {
        repository.save(item);
    }

    @Override
    public void rebuildData(Integer id) throws BusinessException {
        Optional<IedDlRecord> optional = repository.findById(id);
        if (!optional.isPresent()) {
            throw new BusinessException("未找到该记录");
        }
        IedDlRecord record = optional.get();
        spectrogramHandler.processFile(record);
        repository.save(record);
    }

}
