package cn.iocoder.yudao.module.asset.service.station;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.asset.controller.admin.station.vo.HydrogenStationPageReqVO;
import cn.iocoder.yudao.module.asset.controller.admin.station.vo.HydrogenStationSaveReqVO;
import cn.iocoder.yudao.module.asset.dal.dataobject.station.HydrogenStationDO;
import cn.iocoder.yudao.module.asset.dal.mysql.station.HydrogenStationMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import jakarta.annotation.Resource;
import java.util.List;

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

/**
 * 加氢站 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class HydrogenStationServiceImpl implements HydrogenStationService {

    @Resource
    private HydrogenStationMapper hydrogenStationMapper;

    @Override
    public Long createHydrogenStation(HydrogenStationSaveReqVO createReqVO) {
        // 业务校验
        validateHydrogenStationBusiness(createReqVO);
        
        HydrogenStationDO hydrogenStation = BeanUtils.toBean(createReqVO, HydrogenStationDO.class);
        hydrogenStationMapper.insert(hydrogenStation);
        return hydrogenStation.getId();
    }

    @Override
    public void updateHydrogenStation(HydrogenStationSaveReqVO updateReqVO) {
        validateHydrogenStationExists(updateReqVO.getId());
        
        // 业务校验
        validateHydrogenStationBusiness(updateReqVO);
        
        HydrogenStationDO updateObj = BeanUtils.toBean(updateReqVO, HydrogenStationDO.class);
        hydrogenStationMapper.updateById(updateObj);
    }

    @Override
    public void deleteHydrogenStation(Long id) {
        validateHydrogenStationExists(id);
        hydrogenStationMapper.deleteById(id);
    }

    private void validateHydrogenStationExists(Long id) {
        if (hydrogenStationMapper.selectById(id) == null) {
            throw exception(HYDROGEN_STATION_NOT_EXISTS);
        }
    }

    /**
     * 业务校验
     */
    private void validateHydrogenStationBusiness(HydrogenStationSaveReqVO reqVO) {
        // 校验营业时间
        if (reqVO.getStartBusiness() != null && reqVO.getEndBusiness() != null) {
            if (reqVO.getEndBusiness().isBefore(reqVO.getStartBusiness())) {
                throw exception(HYDROGEN_STATION_END_BUSINESS_BEFORE_START_BUSINESS);
            }
        }
    }

    @Override
    public HydrogenStationDO getHydrogenStation(Long id) {
        return hydrogenStationMapper.selectById(id);
    }

    @Override
    public PageResult<HydrogenStationDO> getHydrogenStationPage(HydrogenStationPageReqVO pageReqVO) {
        return hydrogenStationMapper.selectPage(pageReqVO);
    }

    @Override
    public List<HydrogenStationDO> getHydrogenStationSimpleList() {
        return hydrogenStationMapper.selectSimpleList();
    }

}
