package com.usky.vpp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usky.vpp.domain.VppDrEvent;
import com.usky.vpp.domain.VppDrExecution;
import com.usky.vpp.domain.VppDrParticipation;
import com.usky.vpp.mapper.VppDrExecutionMapper;
import com.usky.vpp.mapper.VppDrParticipationMapper;
import com.usky.vpp.service.VppDrExecutionBootstrapService;
import com.usky.vpp.util.VppAuditHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class VppDrExecutionBootstrapServiceImpl implements VppDrExecutionBootstrapService {

    private static final Logger log = LoggerFactory.getLogger(VppDrExecutionBootstrapServiceImpl.class);

    private static final int PARTICIPATE_STATUS_ACCEPT = 1;
    private static final int EXECUTE_STATUS_RUNNING = 1;

    @Autowired
    private VppDrParticipationMapper participationMapper;
    @Autowired
    private VppDrExecutionMapper executionMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void ensureExecutions(VppDrEvent event) {
        if (event == null || event.getId() == null) {
            return;
        }
        long existing = executionMapper.selectCount(new LambdaQueryWrapper<VppDrExecution>()
                .eq(VppDrExecution::getEventId, event.getId())
                .eq(VppDrExecution::getDeleteFlag, VppAuditHelper.NOT_DELETED));
        if (existing > 0) {
            return;
        }

        List<VppDrParticipation> participations = participationMapper.selectList(
                new LambdaQueryWrapper<VppDrParticipation>()
                        .eq(VppDrParticipation::getEventId, event.getId())
                        .eq(VppDrParticipation::getParticipateStatus, PARTICIPATE_STATUS_ACCEPT)
                        .eq(VppDrParticipation::getDeleteFlag, VppAuditHelper.NOT_DELETED));
        LocalDateTime now = LocalDateTime.now();
        int created = 0;
        for (VppDrParticipation participation : participations) {
            BigDecimal target = participation.getClearedCapacityKw();
            if (target == null || target.compareTo(BigDecimal.ZERO) <= 0) {
                target = participation.getDeclaredCapacityKw();
            }
            if (target == null || target.compareTo(BigDecimal.ZERO) <= 0 || participation.getResourceId() == null) {
                continue;
            }
            VppDrExecution execution = new VppDrExecution();
            execution.setEventId(event.getId());
            execution.setResourceId(participation.getResourceId());
            execution.setTargetKw(target);
            execution.setActualKw(BigDecimal.ZERO);
            execution.setExecuteStatus(EXECUTE_STATUS_RUNNING);
            execution.setStartedAt(now);
            VppAuditHelper.fillCreate(execution);
            executionMapper.insert(execution);
            created++;
        }
        if (created > 0) {
            log.info("已为事件 {} 初始化 {} 条执行明细", event.getEventId(), created);
        }
    }
}
