PayAppleModel.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /**
  3. * PayAppleModel.php UTF-8
  4. * 苹果支付记录表
  5. *
  6. * @date : 2020/9/14 17:00
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : H5IOS 1.0
  11. */
  12. namespace huosdk\h5ios\core\model;
  13. use huosdk\h5ios\core\constant\CacheConst;
  14. use huosdk\h5ios\core\constant\CommonConst;
  15. use think\Cache;
  16. class PayAppleModel extends CommonModel {
  17. protected $name = 'pay_apple';
  18. protected $pk = 'id';
  19. // 开启自动写入时间戳字段
  20. protected $autoWriteTimestamp = true;
  21. protected $cache_key_prefix = CacheConst::CACHE_SDK_APPLE_ORDER_PREFIX;
  22. /**
  23. * 根据id获取单条记录缓存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. * 根据order_id获取单条记录缓存key
  34. *
  35. * @param string $order_id 订单ID
  36. *
  37. * @return string
  38. */
  39. protected function getOrderCacheKey($order_id = '') {
  40. return $this->cache_key_prefix.'o_'.$order_id;
  41. }
  42. /**
  43. * 根据trans_id获取单条记录缓存key
  44. *
  45. * @param string $trans_id 苹果订单号
  46. *
  47. * @return string
  48. */
  49. protected function getTransCacheKey($trans_id = '') {
  50. return $this->cache_key_prefix.'t_'.$trans_id;
  51. }
  52. /**
  53. * @param array $data
  54. *
  55. * @return bool|array
  56. */
  57. public function addData($data) {
  58. $_data = $data;
  59. $_id = parent::addData($_data);
  60. if (false === $_id) {
  61. return false;
  62. }
  63. /* TAG缓存操作 */
  64. return $_id;
  65. }
  66. /**
  67. * 通过ID获取信息
  68. *
  69. * @param int $id 主键ID
  70. *
  71. * @return array|false
  72. */
  73. public function getInfoById($id) {
  74. /* 缓存操作 */
  75. $_single_cache_key = $this->getSingleCacheKey($id);
  76. $_data = Cache::get($_single_cache_key);
  77. if (!empty($_data)) {
  78. return $_data;
  79. }
  80. $_data = parent::getInfoById($id);
  81. if (empty($_data)) {
  82. return [];
  83. }
  84. Cache::set($_single_cache_key, $_data);
  85. return $_data;
  86. }
  87. /**
  88. * 更新单条数据
  89. *
  90. * @param array $data 数据
  91. * @param int $id ID
  92. *
  93. * @return bool
  94. */
  95. public function updateData($data, $id) {
  96. $_data = $data;
  97. $_rs = parent::updateData($_data, $id);
  98. if (false === $_rs) {
  99. return false;
  100. }
  101. /* 缓存操作 */
  102. $_single_cache_key = $this->getSingleCacheKey($id);
  103. Cache::rm($_single_cache_key);
  104. /* TAG缓存操作 */
  105. return true;
  106. }
  107. /**
  108. * 删除单条数据
  109. *
  110. * @param int $id ID
  111. * @param bool $is_complete 是否完成删除
  112. *
  113. * @return bool
  114. */
  115. public function deleteData($id, $is_complete = true) {
  116. $_rs = parent::deleteData($id, $is_complete);
  117. if (false == $_rs) {
  118. return false;
  119. }
  120. /* 缓存操作 */
  121. $_single_cache_key = $this->getSingleCacheKey($id);
  122. Cache::rm($_single_cache_key);
  123. /* TAG缓存操作 */
  124. return $_rs;
  125. }
  126. /**
  127. * 根据订单ID获取ID
  128. *
  129. * @param $order_id
  130. *
  131. * @return int
  132. */
  133. public function getIdByOrderId($order_id) {
  134. if (empty($order_id)) {
  135. return CommonConst::CONST_ZERO;
  136. }
  137. $_cache_key = $this->getOrderCacheKey($order_id);
  138. $_id = Cache::get($_cache_key);
  139. if (!empty($_id)) {
  140. return $_id;
  141. }
  142. $_map = [
  143. 'order_id' => $order_id
  144. ];
  145. $_id = $this->where($_map)->value('id');
  146. if (empty($_id)) {
  147. return CommonConst::CONST_ZERO;
  148. }
  149. Cache::set($_cache_key, $_id);
  150. return $_id;
  151. }
  152. /**
  153. * 根据苹果订单ID获取ID
  154. *
  155. * @param $trans_id
  156. *
  157. * @return int
  158. */
  159. public function getIdByTransId($trans_id) {
  160. if (empty($trans_id)) {
  161. return CommonConst::CONST_ZERO;
  162. }
  163. $_cache_key = $this->getTransCacheKey($trans_id);
  164. $_id = Cache::get($_cache_key);
  165. if (!empty($_id)) {
  166. return $_id;
  167. }
  168. $_map = [
  169. 'trans_id' => $trans_id
  170. ];
  171. $_id = $this->where($_map)->value('id');
  172. if (empty($_id)) {
  173. return CommonConst::CONST_ZERO;
  174. }
  175. Cache::set($_cache_key, $_id);
  176. return $_id;
  177. }
  178. /**
  179. * 通过订单号获取详情
  180. *
  181. * @param string $order_id 平台订单号
  182. *
  183. * @return array
  184. */
  185. public function getInfoByOrderId($order_id) {
  186. $_id = $this->getIdByOrderId($order_id);
  187. if (empty($_id)) {
  188. return [];
  189. }
  190. $_data = $this->getInfoById($_id);
  191. return $_data;
  192. }
  193. /**
  194. * 通过苹果订单号获取详情
  195. *
  196. * @param string $trans_id 苹果订单号
  197. *
  198. * @return array
  199. */
  200. public function getInfoByTransId($trans_id) {
  201. $_id = $this->getIdByTransId($trans_id);
  202. if (empty($_id)) {
  203. return [];
  204. }
  205. $_data = $this->getInfoById($_id);
  206. return $_data;
  207. }
  208. /**
  209. * 获取最近订单信息
  210. *
  211. * @param string $end_time 结束时间
  212. * @param string $idfv IDFV
  213. * @param string $apple_id 苹果ID
  214. * @param string $product_id 商品ID
  215. *
  216. * @return array|false
  217. */
  218. public function getLastOrderData($end_time, $idfv, $apple_id, $product_id) {
  219. /* 3 天以内最近的订单 */
  220. $_end_time = $end_time;
  221. $_start_time = $_end_time - 3 * CommonConst::CONST_DAY_SECONDS;
  222. $_map['create_time'] = ['between', [$_start_time, $_end_time]];
  223. $_map['idfv'] = $idfv;
  224. $_map['apple_id'] = $apple_id;
  225. $_map['product_id'] = $product_id;
  226. $_map['trans_id'] = ['neq', ''];
  227. $_data = $this->where($_map)->order('id desc')->find();
  228. if (false == $_data) {
  229. return false;
  230. }
  231. if (is_object($_data)) {
  232. $_data = $_data->toArray();
  233. }
  234. return $_data;
  235. }
  236. public function mem() {
  237. return $this->belongsTo(MemberModel::className(), 'mem_id', 'id');
  238. }
  239. public function mg() {
  240. return $this->belongsTo(MemGameModel::className(), 'mg_mem_id', 'id')->field('id,nickname');
  241. }
  242. public function game() {
  243. return $this->belongsTo(GameModel::className(), 'apple_id', 'apple_id')->field('id,apple_id,name,icon');
  244. }
  245. }