package com.njcn.prepare.harmonic.service.mysql.Impl.line;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.prepare.harmonic.mapper.mysql.line.ThsOverRunLogMapper;
import com.njcn.prepare.harmonic.mapper.mysql.line.ThsSuperviseMapper;
import com.njcn.prepare.harmonic.pojo.param.SuperviseParam;
import com.njcn.prepare.harmonic.pojo.po.ThsOverRunLog;
import com.njcn.prepare.harmonic.pojo.po.ThsSupervise;
import com.njcn.prepare.harmonic.pojo.vo.SuperviceRunLogVo;
import com.njcn.prepare.harmonic.service.mysql.line.ThsOverRunLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 告警/预警监测点列表 服务实现类
 * </p>
 *
 * @author lxp
 * @since 2023-03-16
 */
@Service
@Slf4j
@DS("process")
public class ThsOverRunLogServiceImpl extends ServiceImpl<ThsOverRunLogMapper, ThsOverRunLog> implements ThsOverRunLogService {
    @Autowired
    private ThsOverRunLogMapper thsOverRunLogMapper;
    @Autowired
    private ThsSuperviseMapper thsSuperviseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOverRunLog(SuperviseParam superviseParam) {
        this.saveBatch(superviseParam.getOverRunLog());
    }

    @Override
    public SuperviceRunLogVo superviseDetail(String supIndex) {
        ThsSupervise thsSupervise = thsSuperviseMapper.selectOne(new LambdaQueryWrapper<ThsSupervise>().eq(ThsSupervise::getSupIndex, supIndex));
        List<ThsOverRunLog> thsOverRunLogs = thsOverRunLogMapper.selectList(new LambdaQueryWrapper<ThsOverRunLog>().eq(ThsOverRunLog::getSupIndex, supIndex));
        SuperviceRunLogVo superviceRunLogVo = new SuperviceRunLogVo();
        superviceRunLogVo.setThsSupervise(thsSupervise);
        superviceRunLogVo.setOverRunLog(thsOverRunLogs);
        return superviceRunLogVo;
    }

    @Override
    public HttpResult updateSupervise(SuperviseParam superviseParam) {
        List<ThsOverRunLog> overRunLog = superviseParam.getOverRunLog();
        if (CollectionUtil.isEmpty(overRunLog)) {
            HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, "监测点不能为空", null);
        }
        List<ThsOverRunLog> thsOverRunLogs = thsOverRunLogMapper.selectList(new LambdaQueryWrapper<ThsOverRunLog>().eq(ThsOverRunLog::getSupIndex, superviseParam.getSupIndex()));
        List<String> saveIds = superviseParam.getOverRunLog().stream().map(ThsOverRunLog::getId).collect(Collectors.toList());
        List<String> allIds = thsOverRunLogs.stream().map(ThsOverRunLog::getId).collect(Collectors.toList());
        allIds.removeAll(saveIds);
        thsOverRunLogMapper.delete(new LambdaQueryWrapper<ThsOverRunLog>().in(ThsOverRunLog::getId, allIds));
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, null);
    }
}
