package com.zksy.drainage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.base.domain.EquipmentBase;
import com.zksy.base.domain.EquipmentTest;
import com.zksy.base.mapper.EquipmentBaseMapper;
import com.zksy.base.mapper.EquipmentTestMapper;
import com.zksy.common.exception.ServiceException;
import com.zksy.drainage.service.DrainageEquipmentTestService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DrainageEquipmentTestServiceImpl extends ServiceImpl<EquipmentTestMapper, EquipmentTest>
        implements DrainageEquipmentTestService {

    @Autowired
    private EquipmentBaseMapper equipmentBaseMapper;

    @Override
    public Page<EquipmentTest> findByPage(long pageNum, long pageSize,
                                          String equipmentId, String testType, String testResult) {
        Page<EquipmentTest> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<EquipmentTest> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(equipmentId)) {
            wrapper.eq(EquipmentTest::getEquipmentId, equipmentId);
        }
        if (StringUtils.isNotBlank(testType)) {
            wrapper.like(EquipmentTest::getTestType, testType);
        }
        if (StringUtils.isNotBlank(testResult)) {
            wrapper.like(EquipmentTest::getTestResult, testResult);
        }
        wrapper.orderByDesc(EquipmentTest::getTestTime);
        return this.page(page, wrapper);
    }

    @Override
    @Transactional
    public boolean saveWithCheck(EquipmentTest entity) {
        String equipmentId = entity.getEquipmentId();
        if (StringUtils.isBlank(equipmentId)) {
            throw new ServiceException("设备ID不能为空");
        }
        if (equipmentBaseMapper.selectById(equipmentId) == null) {
            throw new ServiceException("关联的设备不存在：" + equipmentId);
        }
        entity.setCreateTime(LocalDateTime.now());
        return this.save(entity);
    }

    @Override
    @Transactional
    public boolean saveBatchWithCheck(List<EquipmentTest> entityList) {
        Set<String> equipmentIds = entityList.stream()
                .map(EquipmentTest::getEquipmentId)
                .collect(Collectors.toSet());
        List<String> existIds = equipmentBaseMapper.selectBatchIds(equipmentIds).stream()
                .map(EquipmentBase::getEquipmentId)
                .collect(Collectors.toList());
        Set<String> invalidIds = equipmentIds.stream()
                .filter(id -> !existIds.contains(id))
                .collect(Collectors.toSet());
        if (!invalidIds.isEmpty()) {
            throw new ServiceException("存在无效的设备ID：" + invalidIds);
        }
        LocalDateTime now = LocalDateTime.now();
        entityList.forEach(e -> e.setCreateTime(now));
        return this.saveBatch(entityList);
    }

    @Override
    @Transactional
    public boolean updateWithCheck(EquipmentTest entity) {
        String testId = entity.getTestId();
        if (this.getById(testId) == null) {
            throw new ServiceException("测试记录不存在：" + testId);
        }
        return this.updateById(entity);
    }

    @Override
    @Transactional
    public boolean removeWithCheck(String id) {
        if (this.getById(id) == null) {
            throw new ServiceException("测试记录不存在：" + id);
        }
        return this.removeById(id);
    }

    @Override
    @Transactional
    public boolean removeBatchWithCheck(List<String> ids) {
        return this.removeByIds(ids);
    }
}
