package org.qinan.safetyeval.domain.service;

import org.qinan.safetyeval.domain.entity.OrgDepartmentEntity;
import org.qinan.safetyeval.domain.exception.BizException;
import org.qinan.safetyeval.domain.exception.ErrorCode;
import org.qinan.safetyeval.domain.gateway.OrgDepartmentGateway;
import org.qinan.safetyeval.domain.query.OrgDepartmentQuery;
import org.qinan.safetyeval.domain.query.PageResult;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 部门领域服务
 *
 * @author safety-eval
 */
@Service
public class OrgDepartmentDomainService {

    @Resource
    private OrgDepartmentGateway orgDepartmentGateway;

    public OrgDepartmentEntity add(OrgDepartmentEntity entity) {
        return orgDepartmentGateway.save(entity);
    }

    public OrgDepartmentEntity get(Long id) {
        return orgDepartmentGateway.get(id);
    }

    public OrgDepartmentEntity modify(OrgDepartmentEntity entity) {
        OrgDepartmentEntity existing = orgDepartmentGateway.get(entity.getId());
        if (existing == null) {
            throw new BizException(ErrorCode.ORG_DEPARTMENT_NOT_FOUND);
        }
        return orgDepartmentGateway.modify(entity);
    }

    public void delete(Long id) {
        OrgDepartmentEntity existing = orgDepartmentGateway.get(id);
        if (existing == null) {
            throw new BizException(ErrorCode.ORG_DEPARTMENT_NOT_FOUND);
        }
        orgDepartmentGateway.delete(id);
    }

    public PageResult<OrgDepartmentEntity> page(OrgDepartmentQuery query) {
        return orgDepartmentGateway.page(query);
    }
}
