package com.yc.ship.module.resource.service.category;

import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.resource.controller.admin.category.vo.CategoryExtraSaveReqVO;
import com.yc.ship.module.resource.dal.dataobject.category.CategoryExtraDO;
import com.yc.ship.module.resource.dal.mysql.category.CategoryExtraMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.resource.enums.ErrorCodeConstants.CATEGORY_EXTRA_NOT_EXISTS;


/**
 * 小程序产品分类 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class CategoryExtraServiceImpl implements CategoryExtraService {

    @Resource
    private CategoryExtraMapper categoryExtraMapper;

    @Override
    public Long createCategoryExtra(CategoryExtraSaveReqVO createReqVO) {
        // 插入
        CategoryExtraDO categoryExtra = BeanUtils.toBean(createReqVO, CategoryExtraDO.class);
        categoryExtraMapper.insert(categoryExtra);
        // 返回
        return categoryExtra.getId();
    }

    @Override
    public void updateCategoryExtra(CategoryExtraSaveReqVO updateReqVO) {
        // 校验存在
        validateCategoryExtraExists(updateReqVO.getId());
        // 更新
        CategoryExtraDO updateObj = BeanUtils.toBean(updateReqVO, CategoryExtraDO.class);
        categoryExtraMapper.updateById(updateObj);
    }

    @Override
    public void deleteCategoryExtra(Long id) {
        // 校验存在
        validateCategoryExtraExists(id);
        // 删除
        categoryExtraMapper.deleteById(id);
    }

    private void validateCategoryExtraExists(Long id) {
        if (categoryExtraMapper.selectById(id) == null) {
            throw exception(CATEGORY_EXTRA_NOT_EXISTS);
        }
    }

    @Override
    public CategoryExtraDO getCategoryExtra(Long id) {
        return categoryExtraMapper.selectById(id);
    }

}
