package cn.ey.biz.module.system.service.category;

import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.module.system.controller.admin.category.vo.CategoryAppCreateReqVO;
import cn.ey.biz.module.system.controller.admin.category.vo.CategoryAppExportReqVO;
import cn.ey.biz.module.system.controller.admin.category.vo.CategoryAppPageReqVO;
import cn.ey.biz.module.system.controller.admin.category.vo.CategoryAppUpdateReqVO;
import cn.ey.biz.module.system.convert.category.CategoryAppConvert;
import cn.ey.biz.module.system.dal.dataobject.category.CategoryAppDO;
import cn.ey.biz.module.system.dal.mapper.category.CategoryAppMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import static cn.ey.biz.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ey.biz.module.system.enums.ErrorCodeConstants.CATEGORY_APP_NOT_EXISTS;

/**
 * 字典分类应用 Service 实现类
 *
 */
@Service
@Validated
public class CategoryAppServiceImpl implements CategoryAppService {

    @Resource
    private CategoryAppMapper categoryAppMapper;

    @Override
    public Long createCategoryApp(CategoryAppCreateReqVO createReqVO) {
        // 插入
        CategoryAppDO categoryApp = CategoryAppConvert.INSTANCE.convert(createReqVO);
        categoryAppMapper.insert(categoryApp);
        // 返回
        return categoryApp.getId();
    }

    @Override
    public void updateCategoryApp(CategoryAppUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateCategoryAppExists(updateReqVO.getId());
        // 更新
        CategoryAppDO updateObj = CategoryAppConvert.INSTANCE.convert(updateReqVO);
        categoryAppMapper.updateById(updateObj);
    }

    @Override
    public void deleteCategoryApp(Long id) {
        // 校验存在
        this.validateCategoryAppExists(id);
        // 删除
        categoryAppMapper.deleteById(id);
    }

    private void validateCategoryAppExists(Long id) {
        if (categoryAppMapper.selectById(id) == null) {
            throw exception(CATEGORY_APP_NOT_EXISTS);
        }
    }

    @Override
    public CategoryAppDO getCategoryApp(Long id) {
        return categoryAppMapper.selectById(id);
    }

    @Override
    public List<CategoryAppDO> getCategoryAppList(Collection<Long> ids) {
        return categoryAppMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CategoryAppDO> getCategoryAppPage(CategoryAppPageReqVO pageReqVO) {
        return categoryAppMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CategoryAppDO> getCategoryAppList(CategoryAppExportReqVO exportReqVO) {
        return categoryAppMapper.selectList(exportReqVO);
    }

}
