package com.xindazhou.vehicle.service.guide;

import com.xindazhou.framework.common.exception.util.ServiceExceptionUtil;
import com.xindazhou.framework.common.pojo.PageResult;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleGuidePageReqVO;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleGuideRespVO;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleGuideSaveReqVO;
import com.xindazhou.vehicle.convert.guide.VehicleGuideConvert;
import com.xindazhou.vehicle.dal.dataobject.guide.VehicleGuideDO;
import com.xindazhou.vehicle.dal.mysql.guide.VehicleGuideMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.Collection;

import static com.xindazhou.vehicle.enums.ErrorCodeConstants.VEHICLE_GUIDE_NOT_EXISTS;

/**
 * 使用指南 Service 实现类
 *
 * @author xindazhou
 */
@Service
@Validated
public class VehicleGuideServiceImpl implements VehicleGuideService {

    @Resource
    private VehicleGuideMapper vehicleGuideMapper;

    @Override
    public PageResult<VehicleGuideRespVO> getVehicleGuidePage(VehicleGuidePageReqVO pageReqVO) {
        PageResult<VehicleGuideDO> pageResult = vehicleGuideMapper.selectPage(pageReqVO);
        return VehicleGuideConvert.INSTANCE.convertPage(pageResult);
    }

    @Override
    public VehicleGuideRespVO getVehicleGuide(Long id) {
        VehicleGuideDO guide = vehicleGuideMapper.selectById(id);
        if (guide == null) {
            throw ServiceExceptionUtil.exception(VEHICLE_GUIDE_NOT_EXISTS);
        }
        return VehicleGuideConvert.INSTANCE.convert(guide);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createVehicleGuide(VehicleGuideSaveReqVO createReqVO) {
        VehicleGuideDO guide = VehicleGuideConvert.INSTANCE.convert(createReqVO);
        vehicleGuideMapper.insert(guide);
        return guide.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateVehicleGuide(VehicleGuideSaveReqVO updateReqVO) {
        validateVehicleGuideExists(updateReqVO.getId());
        VehicleGuideDO updateObj = VehicleGuideConvert.INSTANCE.convert(updateReqVO);
        vehicleGuideMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteVehicleGuide(Long id) {
        validateVehicleGuideExists(id);
        vehicleGuideMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDeleteVehicleGuide(Collection<Long> ids) {
        ids.forEach(id -> vehicleGuideMapper.deleteById(id));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void onShelfVehicleGuide(Long id) {
        VehicleGuideDO guide = vehicleGuideMapper.selectById(id);
        if (guide == null) {
            throw ServiceExceptionUtil.exception(VEHICLE_GUIDE_NOT_EXISTS);
        }
        guide.setStatus(1);
        guide.setOnShelfTime(LocalDateTime.now());
        vehicleGuideMapper.updateById(guide);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDisplayVehicleGuide(Collection<Long> ids, Integer isDisplay) {
        ids.forEach(id -> {
            VehicleGuideDO guide = vehicleGuideMapper.selectById(id);
            if (guide != null) {
                guide.setIsDisplay(isDisplay);
                vehicleGuideMapper.updateById(guide);
            }
        });
    }

    private void validateVehicleGuideExists(Long id) {
        if (vehicleGuideMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(VEHICLE_GUIDE_NOT_EXISTS);
        }
    }

}

