package org.qinan.safetyeval.domain.service;

import org.qinan.safetyeval.domain.entity.OrgInfoEntity;
import org.qinan.safetyeval.domain.exception.BizException;
import org.qinan.safetyeval.domain.exception.ErrorCode;
import org.qinan.safetyeval.domain.gateway.OrgInfoGateway;
import org.qinan.safetyeval.domain.query.OrgInfoQuery;
import org.qinan.safetyeval.domain.query.PageResult;

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

/**
 * 机构信息领域服务
 *
 * @author safety-eval
 */
@Service
public class OrgInfoDomainService {

    @Resource
    private OrgInfoGateway orgInfoGateway;

    public OrgInfoEntity add(OrgInfoEntity entity) {
        return orgInfoGateway.save(entity);
    }

    public OrgInfoEntity get(Long id) {
        return orgInfoGateway.get(id);
    }

    public OrgInfoEntity modify(OrgInfoEntity entity) {
        OrgInfoEntity existing = orgInfoGateway.get(entity.getId());
        if (existing == null) {
            throw new BizException(ErrorCode.ORG_INFO_NOT_FOUND);
        }
        return orgInfoGateway.modify(entity);
    }

    public void delete(Long id) {
        OrgInfoEntity existing = orgInfoGateway.get(id);
        if (existing == null) {
            throw new BizException(ErrorCode.ORG_INFO_NOT_FOUND);
        }
        orgInfoGateway.delete(id);
    }

    public void updateState(Long id, Integer state) {
        OrgInfoEntity existing = orgInfoGateway.get(id);
        if (existing == null) {
            throw new BizException(ErrorCode.ORG_INFO_NOT_FOUND);
        }
        orgInfoGateway.updateState(id, state);
    }

    public PageResult<OrgInfoEntity> page(OrgInfoQuery query) {
        return orgInfoGateway.page(query);
    }

    public OrgInfoEntity getInfo() {
        return orgInfoGateway.getInfo();
    }

    public void resetPassword(Long id) {
        orgInfoGateway.resetPassword(id);
    }
}
