Mapping.php 3.25 KB
<?php
/**
 * Mapping.php
 * 接口字典,转义字段 v2
 * @author Deepseath
 * @version $Id$
 */
namespace deepseath\hris\ApiV2;

class Mapping
{
    /**
     * 基类服务对象
     * @var \deepseath\hris\Hris
     */
    protected $service = null;

    public function __construct(\deepseath\hris\Hris $service)
    {
        $this->service = $service;
    }

    

    /**
     * 获取指定字段的字典信息
     *
     * @param string $field
     * @param boolean $force
     * @return array
     */
    public function get(string $field = null, bool $force = false) : array
    {
        if ($field !== null) {
            $type = $this->types($field);
            if (is_array($type)) {
                return $type;
            }
        }

        $cache = $this->service->cache();
        $cachePool = $cache->getItem('mappings');
        if (!$cachePool->isHit() 
            || $force !== false 
                || !($mappings = $cachePool->get()) 
                    || !isset($mappings['expire_at']) 
                        || time() - $mappings['expire_at'] > $this->service->config['mapping_expire']) {
            
            $result = $this->service->apiRequest('get', 'mappingVO', []);
            $mappings = [];
            foreach ($result['mappings'] ?? [] as $mapping) {
                $mappings[$mapping['mappingType']][$mapping['mappingId']] = $mapping['mappingValue'];
            }
            $mappings = [
                'expire_at' => time(),
                'data' => $mappings
            ];
            $cachePool->set($mappings);
            $cachePool->expiresAfter($this->service->config['mapping_expire']);
            $cache->save($cachePool);
        }
//print_r(array_keys($mappings['data']));
        return $field === null ? $mappings['data'] : $mappings['data'][$type];
    }

    /**
     * 列出字典字段
     *
     * @param string|null $field
     * @return mixed
     */
    public function types(string $field = null) : mixed
    {
        $fieldMap = [
            'employeeStatus' => [1 => '在职', 3 => '离职'],
            'entityId' => 'entityIds',
            'locationId' => 'locations',
            'jobRole' => 'jobRoles',
            'employeeType' => 'employeeTypes',
            'gradeId' => 'grades',
            'nation' => 'nations',
            'nationality' => 'nationalities',
            'gender' => 'genders',
            'certificateType' => 'certificateTypes',
            'maritalStatus' => 'maritalStatuses',
            'highestEducationId' => 'highestEducations',
            'residencyProvinceId' => 'provinces',
            'residencyCityId' => 'cities',
            'residencyType' => 'residencyTypes',
            'residencyType2' => 'residencyTypes2',
            'emergency.relationshipId' => 'relationshipIds',
            'bankId' => 'banks',
            'recruitment' => 'recruitments',
            'recruitmentHc' => 'recruitmentHcs',
            'esop' => 'esop',
            'leaveCompensate' => 'leaveCompensate',
            'hireAgain' => 'hireAgain'
        ];

        if ($field === null) {
            return $fieldMap;
        }

        if (!isset($fieldMap[$field])) {
            throw new \Exception('Hris sdk error: mapping type not exists', 10009);
        }

        return $fieldMap[$field];
    }

}