package com.xindazhou.vehicle.service.gallery;

import com.xindazhou.framework.common.exception.util.ServiceExceptionUtil;
import com.xindazhou.framework.common.pojo.PageResult;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleGalleryPageReqVO;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleGalleryRespVO;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleImagePageReqVO;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleImageRespVO;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleImageMoveReqVO;
import com.xindazhou.vehicle.convert.gallery.VehicleGalleryConvert;
import com.xindazhou.vehicle.convert.gallery.VehicleImageConvert;
import com.xindazhou.vehicle.dal.dataobject.gallery.VehicleGalleryDO;
import com.xindazhou.vehicle.dal.dataobject.gallery.VehicleImageDO;
import com.xindazhou.vehicle.dal.mysql.gallery.VehicleGalleryMapper;
import com.xindazhou.vehicle.dal.mysql.gallery.VehicleImageMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.Collection;

import static com.xindazhou.vehicle.enums.ErrorCodeConstants.VEHICLE_GALLERY_NOT_EXISTS;
import static com.xindazhou.vehicle.enums.ErrorCodeConstants.VEHICLE_IMAGE_NOT_EXISTS;

/**
 * 车辆图库 Service 实现类
 *
 * @author xindazhou
 */
@Service
@Validated
public class VehicleGalleryServiceImpl implements VehicleGalleryService {

    @Resource
    private VehicleGalleryMapper vehicleGalleryMapper;

    @Resource
    private VehicleImageMapper vehicleImageMapper;

    @Override
    public PageResult<VehicleGalleryRespVO> getVehicleGalleryPage(VehicleGalleryPageReqVO pageReqVO) {
        PageResult<VehicleGalleryDO> pageResult = vehicleGalleryMapper.selectPage(pageReqVO);
        return VehicleGalleryConvert.INSTANCE.convertPage(pageResult);
    }

    @Override
    public VehicleGalleryRespVO getVehicleGalleryDetail(String categoryId, String modelId) {
        // TODO: 根据categoryId或modelId查询图库详情
        VehicleGalleryDO gallery = vehicleGalleryMapper.selectById(Long.parseLong(categoryId != null ? categoryId : modelId));
        if (gallery == null) {
            throw ServiceExceptionUtil.exception(VEHICLE_GALLERY_NOT_EXISTS);
        }
        return VehicleGalleryConvert.INSTANCE.convert(gallery);
    }

    @Override
    public PageResult<VehicleImageRespVO> getVehicleImagePage(VehicleImagePageReqVO pageReqVO) {
        PageResult<VehicleImageDO> pageResult = vehicleImageMapper.selectPage(pageReqVO);
        return VehicleImageConvert.INSTANCE.convertPage(pageResult);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void uploadVehicleImage(Long galleryId, String category, String url, String name) {
        VehicleImageDO image = VehicleImageDO.builder()
                .galleryId(galleryId)
                .category(category)
                .url(url)
                .name(name)
                .build();
        vehicleImageMapper.insert(image);
        // 更新图库的图片数量
        VehicleGalleryDO gallery = vehicleGalleryMapper.selectById(galleryId);
        if (gallery != null) {
            gallery.setImageCount((gallery.getImageCount() == null ? 0 : gallery.getImageCount()) + 1);
            vehicleGalleryMapper.updateById(gallery);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteVehicleImage(Long id) {
        VehicleImageDO image = vehicleImageMapper.selectById(id);
        if (image == null) {
            throw ServiceExceptionUtil.exception(VEHICLE_IMAGE_NOT_EXISTS);
        }
        vehicleImageMapper.deleteById(id);
        // 更新图库的图片数量
        if (image.getGalleryId() != null) {
            VehicleGalleryDO gallery = vehicleGalleryMapper.selectById(image.getGalleryId());
            if (gallery != null) {
                gallery.setImageCount(Math.max(0, (gallery.getImageCount() == null ? 0 : gallery.getImageCount()) - 1));
                vehicleGalleryMapper.updateById(gallery);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDeleteVehicleImage(Collection<Long> ids) {
        ids.forEach(this::deleteVehicleImage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void moveVehicleImage(VehicleImageMoveReqVO reqVO) {
        reqVO.getImageIds().forEach(id -> {
            VehicleImageDO image = vehicleImageMapper.selectById(id);
            if (image != null) {
                image.setCategory(reqVO.getTargetCategory());
                vehicleImageMapper.updateById(image);
            }
        });
    }

}

