* @version : H5IOS 1.0 */ namespace huosdk\h5ios\core\model; use huosdk\h5ios\core\constant\CacheConst; use think\Cache; class GamePriceModel extends CommonModel { protected $name = 'game_price'; protected $pk = 'id'; /* 开启自动写入时间戳字段 */ protected $autoWriteTimestamp = true; protected $cache_key_prefix = CacheConst::CACHE_GAME_PRICE_PREFIX; /** * 获取单条记录缓存key * * @param int $id ID * * @return string */ protected function getSingleCacheKey($id) { return $this->cache_key_prefix.$id; } /** * 获取缓存KEY * * @param int $app_id * @param string $product_code * * @return string */ public function getGpCacheKey($app_id = 0, $product_code = '') { $product_code = preg_replace('/[?<*.>\'\"]/is', '', $product_code); $cache_key = $this->cache_key_prefix.$app_id.'_p_'.$product_code; return $cache_key; } /** * 获取缓存KEY * * @param int $app_id * @param string $ch_product_code CP产品ID * * @return string */ public function getGcpCacheKey($app_id = 0, $ch_product_code = '') { $product_code = preg_replace('/[?<*.>\'\"]/is', '', $ch_product_code); $cache_key = $this->cache_key_prefix.$app_id.'_cp_'.$product_code; return $cache_key; } /** * 添加数据 * * @param array $data 需要添加的数据 * * @return false|int 添加失败返回 false 添加成功 返回添加的ID */ public function addData($data) { if (empty($data['app_id']) || empty($data['product_code'])) { return false; } /* 校验是否存在 */ $_is_exist = $this->getIdByAppProduct($data['app_id'], $data['product_code']); if (!empty($_is_exist)) { return true; } $_data = $data; $_id = parent::addData($_data); if (false === $_id) { return false; } /* TAG缓存操作 */ return $_id; } /** * 通过ID获取信息 * * @param int $id 主键ID * * @return array|false */ 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) { $_old_data = $this->getInfoById($id); $_data = $data; $_rs = parent::updateData($_data, $id); if (false === $_rs) { return false; } /* 缓存操作 */ $_single_cache_key = $this->getSingleCacheKey($id); Cache::rm($_single_cache_key); $_ap_cache_key = $this->getGpCacheKey($_old_data['app_id'], $_old_data['product_code']); Cache::rm($_ap_cache_key); $_acp_cache_key = $this->getGcpCacheKey($_old_data['app_id'], $_old_data['ch_product_code']); Cache::rm($_acp_cache_key); /* TAG缓存操作 */ return true; } /** * 删除单条数据 * * @param int $id ID * @param bool $is_complete 是否完成删除 * * @return bool */ public function deleteData($id, $is_complete = false) { $_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); $_ap_cache_key = $this->getGpCacheKey($_old_data['app_id'], $_old_data['product_code']); Cache::rm($_ap_cache_key); $_acp_cache_key = $this->getGcpCacheKey($_old_data['app_id'], $_old_data['ch_product_code']); Cache::rm($_acp_cache_key); /* TAG缓存操作 */ return $_rs; } /** * 批量添加计费点 * * @param array $datas * * @return bool */ public function addDatas($datas) { if (empty($datas)) { return false; } foreach ($datas as $data) { $_rs = $this->addData($data); if (false == $_rs) { return false; } } return true; } /** * 通过游戏ID删除数据 * * @param int $app_id 游戏ID * * @return bool|int */ public function deleteDataByAppId($app_id) { $_map['app_id'] = $app_id; $_ids = $this->where($_map)->column('id'); $_rs = true; foreach ($_ids as $_id) { $_rs = $this->deleteData($_id, true); if (false == $_rs) { return false; } } return $_rs; } /** * 获取计费价格 * * @param int $id 应用ID * * @return mixed|string */ public function getProductPrice($id) { $_data = $this->getInfoById($id); return get_val($_data, 'product_price', 0); } /** * 通过游戏与产品CODE获取计费点ID * * @param int $app_id 应用ID * @param string $product_code 产品ID * * @return INT */ public function getIdByAppProduct($app_id, $product_code) { $_ap_cache_key = $this->getGpCacheKey($app_id, $product_code); $_id = Cache::get($_ap_cache_key); if (!empty($_id)) { return $_id; } $_map = [ 'app_id' => $app_id, 'product_code' => $product_code ]; $_id = $this->where($_map)->value('id'); if (empty($_id)) { return 0; } Cache::set($_ap_cache_key, $_id); return $_id; } /** * 通过游戏与产品CODE获取计费点ID * * @param int $app_id 应用ID * @param string $product_code 产品ID * * @return mixed|string */ public function getPriceByAppProduct($app_id, $product_code) { $_id = $this->getIdByAppProduct($app_id, $product_code); return $this->getProductPrice($_id); } /** * 通过游戏与产品CODE获取计费点ID * * @param int $app_id 应用ID * @param string $ch_product_code CP产品ID * * @return INT */ public function getIdByAppChProduct($app_id, $ch_product_code) { $_ap_cache_key = $this->getGcpCacheKey($app_id, $ch_product_code); $_id = Cache::get($_ap_cache_key); if (!empty($_id)) { return $_id; } $_map = [ 'app_id' => $app_id, 'ch_product_code' => $ch_product_code ]; $_id = $this->where($_map)->value('id'); if (empty($_id)) { return 0; } Cache::set($_ap_cache_key, $_id); return $_id; } /** * 通过游戏与CP产品CODE获取苹果计费点CODE * * @param int $app_id 应用ID * @param string $ch_product_code CP产品ID * * @return mixed|string */ public function getProductByAppChProduct($app_id, $ch_product_code) { $_id = $this->getIdByAppChProduct($app_id, $ch_product_code); $_info = $this->getInfoById($_id); return get_val($_info, 'product_code', ''); } }