package com.xindazhou.vehicle.service.category;

import com.xindazhou.framework.common.exception.util.ServiceExceptionUtil;
import com.xindazhou.framework.common.pojo.PageResult;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleCategoryPageReqVO;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleCategoryRespVO;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleCategorySaveReqVO;
import com.xindazhou.vehicle.convert.category.VehicleCategoryConvert;
import com.xindazhou.vehicle.dal.dataobject.category.VehicleCategoryDO;
import com.xindazhou.vehicle.dal.mysql.category.VehicleCategoryMapper;
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_CATEGORY_NOT_EXISTS;

/**
 * 分类管理 Service 实现类
 *
 * @author xindazhou
 */
@Service
@Validated
public class VehicleCategoryServiceImpl implements VehicleCategoryService {

    @Resource
    private VehicleCategoryMapper vehicleCategoryMapper;

    @Override
    public PageResult<VehicleCategoryRespVO> getVehicleCategoryPage(VehicleCategoryPageReqVO pageReqVO) {
        PageResult<VehicleCategoryDO> pageResult = vehicleCategoryMapper.selectPage(pageReqVO);
        return VehicleCategoryConvert.INSTANCE.convertPage(pageResult);
    }

    @Override
    public VehicleCategoryRespVO getVehicleCategory(Long id) {
        VehicleCategoryDO category = vehicleCategoryMapper.selectById(id);
        if (category == null) {
            throw ServiceExceptionUtil.exception(VEHICLE_CATEGORY_NOT_EXISTS);
        }
        return VehicleCategoryConvert.INSTANCE.convert(category);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createVehicleCategory(VehicleCategorySaveReqVO createReqVO) {
        VehicleCategoryDO category = VehicleCategoryConvert.INSTANCE.convert(createReqVO);
        vehicleCategoryMapper.insert(category);
        return category.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateVehicleCategory(VehicleCategorySaveReqVO updateReqVO) {
        // 校验存在
        validateVehicleCategoryExists(updateReqVO.getId());
        // 更新
        VehicleCategoryDO updateObj = VehicleCategoryConvert.INSTANCE.convert(updateReqVO);
        vehicleCategoryMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteVehicleCategory(Long id) {
        // 校验存在
        validateVehicleCategoryExists(id);
        // 删除
        vehicleCategoryMapper.deleteById(id);
    }

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

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

    private void validateVehicleCategoryExists(Long id) {
        if (vehicleCategoryMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(VEHICLE_CATEGORY_NOT_EXISTS);
        }
    }

}

