* @version : HUOSDK 8.5 */ namespace huoIdentify\model; use huo\model\common\CommonModel; use huolib\constant\CacheConst; use huolib\constant\CommonConst; use think\Cache; class IdentifyHolidaySetModel extends CommonModel { protected $name = 'identify_holiday_set'; protected $pk = 'id'; protected $cache_key_prefix = CacheConst::CACHE_IDENTIFY_HOLIDAY_SET_PREFIX; /* 开启自动写入时间戳字段 */ protected $autoWriteTimestamp = true; protected $type = ['holiday' => 'array', 'workday' => 'array']; /** * 获取单条记录缓存key * * @param int $id ID * * @return string :string */ protected function getSingleCacheKey($id) { return $this->cache_key_prefix.$id; } /** * 根据年份获取缓存key * * @param int $year 年份 * * @return string :string */ protected function getCacheKeyByYear($year) { return $this->cache_key_prefix.'year_'.$year; } /** * 添加数据 * * @param array $data 需要添加的数据 * * @return false|int 添加失败返回 false 添加成功 返回添加的ID */ public function addData($data) { $_data = $data; $_id = parent::addData($_data); if (false === $_id) { return false; } return $_id; } /** * 通过ID获取信息 * * @param int $id 主键ID * * @return array */ public function getInfoById($id) { /* 缓存操作 */ $_single_cache_key = $this->getSingleCacheKey($id); $_data = Cache::get($_single_cache_key); if (!empty($_data)) { return $_data; } $_data = parent::getInfoById($id); if (empty($_data)) { return []; } Cache::set($_single_cache_key, $_data); return $_data; } /** * 更新单条数据 * * @param array $data 数据 * @param int $id ID * * @return bool */ public function updateData($data, $id) { $_map[$this->pk] = $id; $_data = $data; $_rs = $this->allowField(true)->isUpdate(true)->save($_data, $_map); if (false === $_rs) { return false; } /* 缓存操作 */ $_single_cache_key = $this->getSingleCacheKey($id); Cache::rm($_single_cache_key); return true; } /** * 删除单条数据 * * @param int $id ID * @param bool $is_complete 是否完成删除 * * @return bool */ public function deleteData($id, $is_complete = true) { $_old_data = $this->getInfoById($id); $_rs = parent::deleteData($id, $is_complete); if (false == $_rs) { return false; } /* 缓存操作 */ $_single_cache_key = $this->getSingleCacheKey($id); Cache::rm($_single_cache_key); /* 删除年份 缓存 */ $_cache_key = $this->getCacheKeyByYear($_old_data['year']); Cache::rm($_cache_key); return $_rs; } /** * 根据年份获取id * * @param int $year 年份 * * @return int */ public function getIdByYear($year) { $_cache_key = $this->getCacheKeyByYear($year); $_id = Cache::get($_cache_key); if (!empty($_id)) { return $_id; } $_map = [ 'year' => $year ]; $_id = $this->where($_map)->value('id'); if (empty($_id)) { return CommonConst::CONST_ZERO; } Cache::set($_cache_key, $_id); return $_id; } /** * 根据年份获取实名信息 * * @param int $year 年份 * * @return array */ public function getInfoByYear($year) { $_id = $this->getIdByYear($year); return $this->getInfoById($_id); } /** * 获取列表 * * @param array $param 查找参数 * @param string $page 分页 * @param string $order 排序 * * @return array */ public function getList($param, $page = '1,10', $order = '-year') { $_map = []; if (!empty($param['year'])) { $_map['year'] = $param['year']; } $_count = $this->where($_map)->count('id'); if (empty($_count)) { return [ 'count' => CommonConst::CONST_ZERO, 'list' => [], ]; } $_order = $this->orderFilter($order); $_list = $this->where($_map)->order($_order)->page($page)->select(); if (is_object($_list)) { $_list = $_list->toArray(); } foreach ($_list as $_k => $_v) { $_list[$_k]['holiday'] = array_to_str($_v['holiday']); $_list[$_k]['workday'] = array_to_str($_v['workday']); } return [ 'count' => $_count, 'list' => $_list, ]; } }