* @version : HUOSDK 8.0 */ namespace huo\model\conf; use huo\model\common\CommonModel; use huolib\constant\CacheConst; use huolib\constant\CommonConst; use think\Cache; class PaywayModel extends CommonModel { protected $name = 'payway'; protected $pk = 'id'; protected $type = ['config' => 'array']; protected $cache_key_prefix = CacheConst::CACHE_PAY_WAY_PREFIX; protected $cache_tag = CacheConst::CACHE_PAY_WAY_TAG; /** * 获取单条记录缓存key * * @param int $id ID * * @return string :string */ protected function getSingleCacheKey($id) { return $this->cache_key_prefix.$id; } /** * 根据支付方式获取缓存key * * @param string $payname 支付方式 * * @return string :string */ protected function getCacheKeyByPayname($payname) { return $this->cache_key_prefix.$payname; } /** * 添加数据 * * @param array $data 需要添加的数据 * * @return false|int 添加失败返回 false 添加成功 返回添加的ID */ public function addData($data) { if (empty($data)) { return false; } $_data = $data; $_model = new static(); $_rs = $_model->allowField(true)->isUpdate(false)->save($_data, []); if (false !== $_rs) { return $_model->getLastInsID(); } Cache::clear($this->cache_tag); return false; } /** * 通过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) { $_data = $data; $_map[$this->pk] = $id; $_model = new static(); $_rs = $_model->isUpdate(true)->save($_data, $_map); if (false === $_rs) { return false; } /* 缓存操作 */ $_single_cache_key = $this->getSingleCacheKey($id); Cache::rm($_single_cache_key); Cache::clear($this->cache_tag); return true; } /** * 删除单条数据 * * @param int $id ID * @param bool $is_complete 是否完成删除 * * @return bool */ public function deleteData($id, $is_complete = true) { $_old_data = $this->getInfoById($id); $_map[$this->pk] = $id; if (true == $is_complete) { /* 彻底删除 */ $_rs = $this->where($_map)->delete(); } else { $_data['is_delete'] = CommonConst::CONST_DELETED; $_data['delete_time'] = time(); $_rs = self::updateData($_data, $id); } if (false === $_rs) { return false; } /* 缓存操作 */ $_single_cache_key = $this->getSingleCacheKey($id); Cache::rm($_single_cache_key); /* 删除缓存 */ $_cache_key = $this->getCacheKeyByPayname($_old_data['payname']); Cache::rm($_cache_key); Cache::clear($this->cache_tag); return $_rs; } /** * 根据payname 获取ID * * @param $payname * * @return mixed */ public function getIdByPayname($payname) { $_cache_key = $this->getCacheKeyByPayname($payname); $_id = Cache::get($_cache_key); if (!empty($_id)) { return $_id; } $_map = [ 'payname' => $payname ]; $_id = $this->where($_map)->value('id'); if (empty($_id)) { return CommonConst::CONST_ZERO; } Cache::set($_cache_key, $_id); return $_id; } /** * 根据payname 获取信息 * * @param $payname * * @return mixed */ public function getInfoByPayname($payname) { $_id = $this->getIdByPayname($payname); return $this->getInfoById($_id); } /** * 通过PayName获取列表 * * @param int $status * @param int $is_config * @param string $code * @param int $id * @param null $parent_id * * @return array */ public function getPayWaysByPayName($status = 2, $is_config = 0, $code = '', $id = 0, $parent_id = null) { $_map = []; if (!empty($status)) { $_map['status'] = $status; } if (!empty($id)) { $_map['id'] = $id; } if (!empty($is_config)) { $_map['is_config'] = $is_config; } if (!empty($parent_id)) { $_map['parent_id'] = $parent_id; } if (!empty($code)) { $_map['code'] = $code; } $_tag = $this->cache_tag; $_cache_key = md5($_tag.json_encode($_map)); $_field = 'realname'; $_payways = $this->where($_map) ->cache($_cache_key, CommonConst::CONST_DAY_SECONDS, $_tag) ->column($_field, 'payname'); return $_payways; } }