GamePriceModel.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. /**
  3. * GamePriceModel.php UTF-8
  4. * http://doc.1tsdk.com/170?page_id=5639
  5. * 游戏计费点
  6. *
  7. * @date : 2020/9/14 17:21
  8. *
  9. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  10. * @author : chenbingling <cbl@huosdk.com>
  11. * @version : H5IOS 1.0
  12. */
  13. namespace huosdk\h5ios\core\model;
  14. use huosdk\h5ios\core\constant\CacheConst;
  15. use think\Cache;
  16. class GamePriceModel extends CommonModel {
  17. protected $name = 'game_price';
  18. protected $pk = 'id';
  19. /* 开启自动写入时间戳字段 */
  20. protected $autoWriteTimestamp = true;
  21. protected $cache_key_prefix = CacheConst::CACHE_GAME_PRICE_PREFIX;
  22. /**
  23. * 获取单条记录缓存key
  24. *
  25. * @param int $id ID
  26. *
  27. * @return string
  28. */
  29. protected function getSingleCacheKey($id) {
  30. return $this->cache_key_prefix.$id;
  31. }
  32. /**
  33. * 获取缓存KEY
  34. *
  35. * @param int $app_id
  36. * @param string $product_code
  37. *
  38. * @return string
  39. */
  40. public function getGpCacheKey($app_id = 0, $product_code = '') {
  41. $product_code = preg_replace('/[?<*.>\'\"]/is', '', $product_code);
  42. $cache_key = $this->cache_key_prefix.$app_id.'_p_'.$product_code;
  43. return $cache_key;
  44. }
  45. /**
  46. * 获取缓存KEY
  47. *
  48. * @param int $app_id
  49. * @param string $ch_product_code CP产品ID
  50. *
  51. * @return string
  52. */
  53. public function getGcpCacheKey($app_id = 0, $ch_product_code = '') {
  54. $product_code = preg_replace('/[?<*.>\'\"]/is', '', $ch_product_code);
  55. $cache_key = $this->cache_key_prefix.$app_id.'_cp_'.$product_code;
  56. return $cache_key;
  57. }
  58. /**
  59. * 添加数据
  60. *
  61. * @param array $data 需要添加的数据
  62. *
  63. * @return false|int 添加失败返回 false 添加成功 返回添加的ID
  64. */
  65. public function addData($data) {
  66. if (empty($data['app_id']) || empty($data['product_code'])) {
  67. return false;
  68. }
  69. /* 校验是否存在 */
  70. $_is_exist = $this->getIdByAppProduct($data['app_id'], $data['product_code']);
  71. if (!empty($_is_exist)) {
  72. return true;
  73. }
  74. $_data = $data;
  75. $_id = parent::addData($_data);
  76. if (false === $_id) {
  77. return false;
  78. }
  79. /* TAG缓存操作 */
  80. return $_id;
  81. }
  82. /**
  83. * 通过ID获取信息
  84. *
  85. * @param int $id 主键ID
  86. *
  87. * @return array|false
  88. */
  89. public function getInfoById($id) {
  90. /* 缓存操作 */
  91. $_single_cache_key = $this->getSingleCacheKey($id);
  92. $_data = Cache::get($_single_cache_key);
  93. if (!empty($_data)) {
  94. return $_data;
  95. }
  96. $_data = parent::getInfoById($id);
  97. if (empty($_data)) {
  98. return [];
  99. }
  100. Cache::set($_single_cache_key, $_data);
  101. return $_data;
  102. }
  103. /**
  104. * 更新单条数据
  105. *
  106. * @param array $data 数据
  107. * @param int $id ID
  108. *
  109. * @return bool
  110. */
  111. public function updateData($data, $id) {
  112. $_old_data = $this->getInfoById($id);
  113. $_data = $data;
  114. $_rs = parent::updateData($_data, $id);
  115. if (false === $_rs) {
  116. return false;
  117. }
  118. /* 缓存操作 */
  119. $_single_cache_key = $this->getSingleCacheKey($id);
  120. Cache::rm($_single_cache_key);
  121. $_ap_cache_key = $this->getGpCacheKey($_old_data['app_id'], $_old_data['product_code']);
  122. Cache::rm($_ap_cache_key);
  123. $_acp_cache_key = $this->getGcpCacheKey($_old_data['app_id'], $_old_data['ch_product_code']);
  124. Cache::rm($_acp_cache_key);
  125. /* TAG缓存操作 */
  126. return true;
  127. }
  128. /**
  129. * 删除单条数据
  130. *
  131. * @param int $id ID
  132. * @param bool $is_complete 是否完成删除
  133. *
  134. * @return bool
  135. */
  136. public function deleteData($id, $is_complete = false) {
  137. $_old_data = $this->getInfoById($id);
  138. $_rs = parent::deleteData($id, $is_complete);
  139. if (false == $_rs) {
  140. return false;
  141. }
  142. /* 缓存操作 */
  143. $_single_cache_key = $this->getSingleCacheKey($id);
  144. Cache::rm($_single_cache_key);
  145. $_ap_cache_key = $this->getGpCacheKey($_old_data['app_id'], $_old_data['product_code']);
  146. Cache::rm($_ap_cache_key);
  147. $_acp_cache_key = $this->getGcpCacheKey($_old_data['app_id'], $_old_data['ch_product_code']);
  148. Cache::rm($_acp_cache_key);
  149. /* TAG缓存操作 */
  150. return $_rs;
  151. }
  152. /**
  153. * 批量添加计费点
  154. *
  155. * @param array $datas
  156. *
  157. * @return bool
  158. */
  159. public function addDatas($datas) {
  160. if (empty($datas)) {
  161. return false;
  162. }
  163. foreach ($datas as $data) {
  164. $_rs = $this->addData($data);
  165. if (false == $_rs) {
  166. return false;
  167. }
  168. }
  169. return true;
  170. }
  171. /**
  172. * 通过游戏ID删除数据
  173. *
  174. * @param int $app_id 游戏ID
  175. *
  176. * @return bool|int
  177. */
  178. public function deleteDataByAppId($app_id) {
  179. $_map['app_id'] = $app_id;
  180. $_ids = $this->where($_map)->column('id');
  181. $_rs = true;
  182. foreach ($_ids as $_id) {
  183. $_rs = $this->deleteData($_id, true);
  184. if (false == $_rs) {
  185. return false;
  186. }
  187. }
  188. return $_rs;
  189. }
  190. /**
  191. * 获取计费价格
  192. *
  193. * @param int $id 应用ID
  194. *
  195. * @return mixed|string
  196. */
  197. public function getProductPrice($id) {
  198. $_data = $this->getInfoById($id);
  199. return get_val($_data, 'product_price', 0);
  200. }
  201. /**
  202. * 通过游戏与产品CODE获取计费点ID
  203. *
  204. * @param int $app_id 应用ID
  205. * @param string $product_code 产品ID
  206. *
  207. * @return INT
  208. */
  209. public function getIdByAppProduct($app_id, $product_code) {
  210. $_ap_cache_key = $this->getGpCacheKey($app_id, $product_code);
  211. $_id = Cache::get($_ap_cache_key);
  212. if (!empty($_id)) {
  213. return $_id;
  214. }
  215. $_map = [
  216. 'app_id' => $app_id,
  217. 'product_code' => $product_code
  218. ];
  219. $_id = $this->where($_map)->value('id');
  220. if (empty($_id)) {
  221. return 0;
  222. }
  223. Cache::set($_ap_cache_key, $_id);
  224. return $_id;
  225. }
  226. /**
  227. * 通过游戏与产品CODE获取计费点ID
  228. *
  229. * @param int $app_id 应用ID
  230. * @param string $product_code 产品ID
  231. *
  232. * @return mixed|string
  233. */
  234. public function getPriceByAppProduct($app_id, $product_code) {
  235. $_id = $this->getIdByAppProduct($app_id, $product_code);
  236. return $this->getProductPrice($_id);
  237. }
  238. /**
  239. * 通过游戏与产品CODE获取计费点ID
  240. *
  241. * @param int $app_id 应用ID
  242. * @param string $ch_product_code CP产品ID
  243. *
  244. * @return INT
  245. */
  246. public function getIdByAppChProduct($app_id, $ch_product_code) {
  247. $_ap_cache_key = $this->getGcpCacheKey($app_id, $ch_product_code);
  248. $_id = Cache::get($_ap_cache_key);
  249. if (!empty($_id)) {
  250. return $_id;
  251. }
  252. $_map = [
  253. 'app_id' => $app_id,
  254. 'ch_product_code' => $ch_product_code
  255. ];
  256. $_id = $this->where($_map)->value('id');
  257. if (empty($_id)) {
  258. return 0;
  259. }
  260. Cache::set($_ap_cache_key, $_id);
  261. return $_id;
  262. }
  263. /**
  264. * 通过游戏与CP产品CODE获取苹果计费点CODE
  265. *
  266. * @param int $app_id 应用ID
  267. * @param string $ch_product_code CP产品ID
  268. *
  269. * @return mixed|string
  270. */
  271. public function getProductByAppChProduct($app_id, $ch_product_code) {
  272. $_id = $this->getIdByAppChProduct($app_id, $ch_product_code);
  273. $_info = $this->getInfoById($_id);
  274. return get_val($_info, 'product_code', '');
  275. }
  276. }