package cn.iocoder.yudao.module.energy.service.price;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.energy.controller.admin.price.vo.EnergyStationPricePageReqVO;
import cn.iocoder.yudao.module.energy.controller.admin.price.vo.EnergyStationPriceSaveReqVO;
import cn.iocoder.yudao.module.energy.convert.price.EnergyStationPriceConvert;
import cn.iocoder.yudao.module.energy.dal.dataobject.price.EnergyStationPriceDO;
import cn.iocoder.yudao.module.energy.dal.mysql.price.EnergyStationPriceMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import java.time.LocalDate;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.energy.enums.ErrorCodeConstants.*;

@Service
@Validated
public class EnergyStationPriceServiceImpl implements EnergyStationPriceService {

    @Resource
    private EnergyStationPriceMapper stationPriceMapper;

    @Override
    public Long createPrice(EnergyStationPriceSaveReqVO createReqVO) {
        // Validate unique constraint: station + customer + effectiveDate
        validatePriceUnique(createReqVO.getStationId(), createReqVO.getCustomerId(), createReqVO.getEffectiveDate(), null);
        EnergyStationPriceDO price = EnergyStationPriceConvert.INSTANCE.convert(createReqVO);
        price.setStatus(0); // 生效中
        stationPriceMapper.insert(price);
        return price.getId();
    }

    @Override
    public void updatePrice(EnergyStationPriceSaveReqVO updateReqVO) {
        validatePriceExists(updateReqVO.getId());
        validatePriceUnique(updateReqVO.getStationId(), updateReqVO.getCustomerId(), updateReqVO.getEffectiveDate(), updateReqVO.getId());
        EnergyStationPriceDO updateObj = EnergyStationPriceConvert.INSTANCE.convert(updateReqVO);
        stationPriceMapper.updateById(updateObj);
    }

    @Override
    public void deletePrice(Long id) {
        validatePriceExists(id);
        stationPriceMapper.deleteById(id);
    }

    @Override
    public EnergyStationPriceDO getPrice(Long id) {
        return stationPriceMapper.selectById(id);
    }

    @Override
    public PageResult<EnergyStationPriceDO> getPricePage(EnergyStationPricePageReqVO pageReqVO) {
        return stationPriceMapper.selectPage(pageReqVO);
    }

    @Override
    public EnergyStationPriceDO getEffectivePrice(Long stationId, Long customerId, LocalDate date) {
        return stationPriceMapper.selectEffective(stationId, customerId, date);
    }

    private void validatePriceExists(Long id) {
        if (stationPriceMapper.selectById(id) == null) {
            throw exception(STATION_PRICE_NOT_EXISTS);
        }
    }

    private void validatePriceUnique(Long stationId, Long customerId, LocalDate effectiveDate, Long excludeId) {
        EnergyStationPriceDO existing = stationPriceMapper.selectEffective(stationId, customerId, effectiveDate);
        if (existing != null && !existing.getId().equals(excludeId)) {
            throw exception(STATION_PRICE_DUPLICATE);
        }
    }
}
