package com.xindazhou.vehicle.service.archive;

import com.xindazhou.framework.common.exception.util.ServiceExceptionUtil;
import com.xindazhou.framework.common.pojo.PageResult;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleArchiveChangeOwnerReqVO;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleArchiveLockReqVO;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleArchivePageReqVO;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleArchiveRespVO;
import com.xindazhou.vehicle.convert.archive.VehicleArchiveConvert;
import com.xindazhou.vehicle.dal.dataobject.archive.VehicleArchiveDO;
import com.xindazhou.vehicle.dal.mysql.archive.VehicleArchiveMapper;
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 static com.xindazhou.vehicle.enums.ErrorCodeConstants.VEHICLE_ARCHIVE_NOT_EXISTS;

/**
 * 车辆档案 Service 实现类
 *
 * @author xindazhou
 */
@Service
@Validated
public class VehicleArchiveServiceImpl implements VehicleArchiveService {

    @Resource
    private VehicleArchiveMapper vehicleArchiveMapper;

    @Override
    public PageResult<VehicleArchiveRespVO> getVehicleArchivePage(VehicleArchivePageReqVO pageReqVO) {
        PageResult<VehicleArchiveDO> pageResult = vehicleArchiveMapper.selectPage(pageReqVO);
        return VehicleArchiveConvert.INSTANCE.convertPage(pageResult);
    }

    @Override
    public VehicleArchiveRespVO getVehicleArchive(Long id) {
        VehicleArchiveDO archive = vehicleArchiveMapper.selectById(id);
        if (archive == null) {
            throw ServiceExceptionUtil.exception(VEHICLE_ARCHIVE_NOT_EXISTS);
        }
        return VehicleArchiveConvert.INSTANCE.convert(archive);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void changeVehicleOwner(VehicleArchiveChangeOwnerReqVO reqVO) {
        // 校验车辆存在
        VehicleArchiveDO archive = vehicleArchiveMapper.selectById(reqVO.getVehicleId());
        if (archive == null) {
            throw ServiceExceptionUtil.exception(VEHICLE_ARCHIVE_NOT_EXISTS);
        }
        // 更新车主信息（这里需要调用用户服务获取车主信息）
        // TODO: 调用用户服务获取新车主信息
        // archive.setOwnerId(reqVO.getNewOwnerId());
        vehicleArchiveMapper.updateById(archive);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void activateVehicle(Long id) {
        // 校验车辆存在
        VehicleArchiveDO archive = vehicleArchiveMapper.selectById(id);
        if (archive == null) {
            throw ServiceExceptionUtil.exception(VEHICLE_ARCHIVE_NOT_EXISTS);
        }
        // 更新激活状态
        archive.setActivationStatus(1);
        archive.setActivationTime(LocalDateTime.now());
        vehicleArchiveMapper.updateById(archive);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void lockVehicle(VehicleArchiveLockReqVO reqVO) {
        // 校验车辆存在
        VehicleArchiveDO archive = vehicleArchiveMapper.selectById(reqVO.getId());
        if (archive == null) {
            throw ServiceExceptionUtil.exception(VEHICLE_ARCHIVE_NOT_EXISTS);
        }
        // 更新锁定状态
        archive.setLockStatus(reqVO.getLocked() ? 1 : 0);
        vehicleArchiveMapper.updateById(archive);
    }

}

