package org.qinan.safetyeval.domain.service;

import org.qinan.safetyeval.domain.entity.OrgPersonnelEntity;
import org.qinan.safetyeval.domain.exception.BizException;
import org.qinan.safetyeval.domain.exception.ErrorCode;
import org.qinan.safetyeval.domain.gateway.OrgPersonnelGateway;
import org.qinan.safetyeval.domain.query.OrgPersonnelQuery;
import org.qinan.safetyeval.domain.query.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * 人员信息领域服务
 */
@Service
public class OrgPersonnelDomainService {

    @Resource
    private OrgPersonnelGateway orgPersonnelGateway;

    public OrgPersonnelEntity add(OrgPersonnelEntity entity) {
        assertAccountUnique(entity.getAccount(), null);
        if (entity.getEmploymentStatusCode() == null) {
            entity.setEmploymentStatusCode(1);
            entity.setEmploymentStatusName("在职");
        }
        return orgPersonnelGateway.save(entity);
    }

    public OrgPersonnelEntity get(Long id) {
        return orgPersonnelGateway.get(id);
    }

    public OrgPersonnelEntity modify(OrgPersonnelEntity entity) {
        OrgPersonnelEntity existing = orgPersonnelGateway.get(entity.getId());
        if (existing == null) {
            throw new BizException(ErrorCode.ORG_PERSONNEL_NOT_FOUND);
        }
        assertAccountUnique(entity.getAccount(), entity.getId());
        mergeForModify(existing, entity);
        return orgPersonnelGateway.modify(existing);
    }

    private void mergeForModify(OrgPersonnelEntity target, OrgPersonnelEntity patch) {
        if (StringUtils.hasText(patch.getUserName())) {
            target.setUserName(patch.getUserName());
        }
        if (StringUtils.hasText(patch.getAccount())) {
            target.setAccount(patch.getAccount());
        }
        if (patch.getGenderCode() != null) {
            target.setGenderCode(patch.getGenderCode());
            target.setGenderName(patch.getGenderName());
        }
        if (patch.getBirthDate() != null) {
            target.setBirthDate(patch.getBirthDate());
        }
        if (patch.getIdCardNo() != null) {
            target.setIdCardNo(patch.getIdCardNo());
        }
        if (patch.getCurrentAddress() != null) {
            target.setCurrentAddress(patch.getCurrentAddress());
        }
        if (patch.getOfficeAddress() != null) {
            target.setOfficeAddress(patch.getOfficeAddress());
        }
        if (patch.getEducationCode() != null) {
            target.setEducationCode(patch.getEducationCode());
            target.setEducationName(patch.getEducationName());
        }
        if (patch.getGraduateSchool() != null) {
            target.setGraduateSchool(patch.getGraduateSchool());
        }
        if (patch.getMajor() != null) {
            target.setMajor(patch.getMajor());
        }
        if (patch.getEmploymentStatusCode() != null) {
            target.setEmploymentStatusCode(patch.getEmploymentStatusCode());
            target.setEmploymentStatusName(patch.getEmploymentStatusName());
        }
        if (patch.getDeptId() != null) {
            target.setDeptId(patch.getDeptId());
        }
        if (patch.getPostId() != null) {
            target.setPostId(patch.getPostId());
        }
        if (StringUtils.hasText(patch.getPersonTypeCode())) {
            target.setPersonTypeCode(patch.getPersonTypeCode());
            target.setPersonTypeName(patch.getPersonTypeName());
        }
        if (patch.getQualScope() != null) {
            target.setQualScope(patch.getQualScope());
        }
        if (StringUtils.hasText(patch.getProfessionalLevelCode())) {
            target.setProfessionalLevelCode(patch.getProfessionalLevelCode());
            target.setProfessionalLevelName(patch.getProfessionalLevelName());
        }
        if (patch.getEvaluatorCertNo() != null) {
            target.setEvaluatorCertNo(patch.getEvaluatorCertNo());
        }
        if (StringUtils.hasText(patch.getEducationTypeCode())) {
            target.setEducationTypeCode(patch.getEducationTypeCode());
            target.setEducationTypeName(patch.getEducationTypeName());
        }
        if (StringUtils.hasText(patch.getEducationLevelCode())) {
            target.setEducationLevelCode(patch.getEducationLevelCode());
            target.setEducationLevelName(patch.getEducationLevelName());
        }
        if (patch.getTitleName() != null) {
            target.setTitleName(patch.getTitleName());
        }
        if (patch.getRegisterEngineerFlag() != null) {
            target.setRegisterEngineerFlag(patch.getRegisterEngineerFlag());
        }
        if (patch.getPublications() != null) {
            target.setPublications(patch.getPublications());
        }
        if (patch.getAbilityDeclaration() != null) {
            target.setAbilityDeclaration(patch.getAbilityDeclaration());
        }
        if (patch.getWorkExperience() != null) {
            target.setWorkExperience(patch.getWorkExperience());
        }
        if (patch.getProofMaterialUrl() != null) {
            target.setProofMaterialUrl(patch.getProofMaterialUrl());
        }
    }

    public void delete(Long id) {
        OrgPersonnelEntity existing = orgPersonnelGateway.get(id);
        if (existing == null) {
            throw new BizException(ErrorCode.ORG_PERSONNEL_NOT_FOUND);
        }
        orgPersonnelGateway.delete(id);
    }

    public PageResult<OrgPersonnelEntity> page(OrgPersonnelQuery query) {
        return orgPersonnelGateway.page(query);
    }

    private void assertAccountUnique(String account, Long excludeId) {
        if (!StringUtils.hasText(account)) {
            return;
        }
        OrgPersonnelEntity found = orgPersonnelGateway.getByAccount(account);
        if (found != null && (excludeId == null || !found.getId().equals(excludeId))) {
            throw new BizException(ErrorCode.ORG_PERSONNEL_ACCOUNT_EXISTS);
        }
    }
}
