package com.xindazhou.vehicle.service.theft;

import com.xindazhou.framework.common.exception.util.ServiceExceptionUtil;
import com.xindazhou.framework.common.pojo.PageResult;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleTheftPageReqVO;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleTheftRespVO;
import com.xindazhou.vehicle.convert.theft.VehicleTheftConvert;
import com.xindazhou.vehicle.dal.dataobject.theft.VehicleTheftDO;
import com.xindazhou.vehicle.dal.mysql.theft.VehicleTheftMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

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

/**
 * 失窃监控 Service 实现类
 *
 * @author xindazhou
 */
@Service
@Validated
public class VehicleTheftServiceImpl implements VehicleTheftService {

    @Resource
    private VehicleTheftMapper vehicleTheftMapper;

    @Override
    public PageResult<VehicleTheftRespVO> getVehicleTheftPage(VehicleTheftPageReqVO pageReqVO) {
        PageResult<VehicleTheftDO> pageResult = vehicleTheftMapper.selectPage(pageReqVO);
        return VehicleTheftConvert.INSTANCE.convertPage(pageResult);
    }

    @Override
    public VehicleTheftRespVO getVehicleTheft(Long id) {
        VehicleTheftDO theft = vehicleTheftMapper.selectById(id);
        if (theft == null) {
            throw ServiceExceptionUtil.exception(VEHICLE_THEFT_NOT_EXISTS);
        }
        return VehicleTheftConvert.INSTANCE.convert(theft);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void markVehicleNormal(Long id) {
        VehicleTheftDO theft = vehicleTheftMapper.selectById(id);
        if (theft == null) {
            throw ServiceExceptionUtil.exception(VEHICLE_THEFT_NOT_EXISTS);
        }
        // 更新车辆档案的失窃状态为正常
        // TODO: 调用车辆档案服务更新失窃状态
        // 删除失窃记录
        vehicleTheftMapper.deleteById(id);
    }

}
