刷工龄字段首先要确认清楚逻辑,不要全量刷,要用定时器做变量刷新。

我这里以万有汽车项目的一个工龄计算规则需求来示例一段代码,供老师们参考:


规则是: 司龄 siling字段对应时间entry_date,  工龄gongling字段对应s_field16  当然他这个命名有点不太美观,我们暂不提。

司龄:进入本单位时间(entry_date)跟当前时间比不满足一年的,司龄为1年,大于1年的,原基础上取整+1,

比如今天是2020-04-05,他entry_date是2019-04-01 那么就为两年,如果今天是2020-03-01,那么就是1年。

工龄:与上述司龄一致。


下面我们上云函数参考:


代码块

class CalculateCompanyAge(object):
page_size = 500
api = 'hcm.model.list'
get_count_param = {"model": "Employee", "page_index": 1, "page_size": 1}
extra_property = {
"fields": [
{"field": ["siling"],
"key": "siling"},
{"field": ["entry_date"],
"key": "entry_date"},
{"field": ["s_field16"],
"key": "s_field16"},
{"field": ["gongling"],
"key": "gongling"}
]
}

def execute(self, param):
today = datetime.date.today()
now_date = datetime.datetime.now().strftime('%Y-%m-%d')
reduce_param = DateTimeUtils.add_months(today, -12).strftime('%Y-%m-%d')
count = CustomerUtil.call_open_api(self.api, self.get_count_param)['count']
# 拿到总人数
for data_item in self.get_data_with_pagination(count):
for data in data_item:
edit_id = data['id']
entry_date = data.get('entry_date')
siling = data.get('siling')
s_field16 = data.get('s_field16')
gongling = data.get('gongling')
service_length = None
company_length = None
if entry_date:
if reduce_param > entry_date:
now_obj = datetime.datetime.strptime(now_date, '%Y-%m-%d')
entry_dates_obj = datetime.datetime.strptime(entry_date, '%Y-%m-%d')
service_length = self.round_up((now_obj.date() - entry_dates_obj.date()).days / 365)
else:
service_length = 1 # 司龄
if s_field16:
if reduce_param > s_field16:
now_obj = datetime.datetime.strptime(now_date, '%Y-%m-%d')
company_length_obj = datetime.datetime.strptime(s_field16, '%Y-%m-%d')
company_length = self.round_up((now_obj.date() - company_length_obj.date()).days / 365)
else:
company_length = 1 # 工龄
if str(siling) != str(service_length) or str(gongling) != str(company_length):
try:
l = CustomerUtil.call_open_api('hcm.model.edit', {"id_": edit_id, "model": "Employee",
"info": {"siling": service_length,
"gongling": company_length}})
except Exception as e:
logging.info(getEmpCardJson(edit_id)['name'] + "更新失败")

def get_data_with_pagination(self, count):
for page_index in range(1, int(count / self.page_size) + 2):
# siling字段对应entry_date,gongling字段对应s_field16
data_list = CustomerUtil.call_open_api("hcm.model.list",
param={"model": "Employee",
"extra_property": self.extra_property,
"page_index": page_index, "page_size": self.page_size})[
'list']
yield data_list

def round_up(self, day_num):
b = math.floor(day_num)
return b + 1

def test(self, param):
return self.execute(param)



写的可能略有粗糙,最终想表达的意思就是 1. 不要整体取数 ,2 . 不要全量刷  3. 注意取数权限  4 注意命名规范


感谢查看


  • 无标签