Mapping.php
3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?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];
}
}