123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- <?php
- /**
- * PayAppleModel.php UTF-8
- * 苹果支付记录表
- *
- * @date : 2020/9/14 17:00
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : chenbingling <cbl@huosdk.com>
- * @version : H5IOS 1.0
- */
- namespace huosdk\h5ios\core\model;
- use huosdk\h5ios\core\constant\CacheConst;
- use huosdk\h5ios\core\constant\CommonConst;
- use think\Cache;
- class PayAppleModel extends CommonModel {
- protected $name = 'pay_apple';
- protected $pk = 'id';
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = true;
- protected $cache_key_prefix = CacheConst::CACHE_SDK_APPLE_ORDER_PREFIX;
- /**
- * 根据id获取单条记录缓存key
- *
- * @param int $id ID
- *
- * @return string
- */
- protected function getSingleCacheKey($id) {
- return $this->cache_key_prefix.$id;
- }
- /**
- * 根据order_id获取单条记录缓存key
- *
- * @param string $order_id 订单ID
- *
- * @return string
- */
- protected function getOrderCacheKey($order_id = '') {
- return $this->cache_key_prefix.'o_'.$order_id;
- }
- /**
- * 根据trans_id获取单条记录缓存key
- *
- * @param string $trans_id 苹果订单号
- *
- * @return string
- */
- protected function getTransCacheKey($trans_id = '') {
- return $this->cache_key_prefix.'t_'.$trans_id;
- }
- /**
- * @param array $data
- *
- * @return bool|array
- */
- public function addData($data) {
- $_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) {
- $_data = $data;
- $_rs = parent::updateData($_data, $id);
- if (false === $_rs) {
- return false;
- }
- /* 缓存操作 */
- $_single_cache_key = $this->getSingleCacheKey($id);
- Cache::rm($_single_cache_key);
- /* TAG缓存操作 */
- return true;
- }
- /**
- * 删除单条数据
- *
- * @param int $id ID
- * @param bool $is_complete 是否完成删除
- *
- * @return bool
- */
- public function deleteData($id, $is_complete = true) {
- $_rs = parent::deleteData($id, $is_complete);
- if (false == $_rs) {
- return false;
- }
- /* 缓存操作 */
- $_single_cache_key = $this->getSingleCacheKey($id);
- Cache::rm($_single_cache_key);
- /* TAG缓存操作 */
- return $_rs;
- }
- /**
- * 根据订单ID获取ID
- *
- * @param $order_id
- *
- * @return int
- */
- public function getIdByOrderId($order_id) {
- if (empty($order_id)) {
- return CommonConst::CONST_ZERO;
- }
- $_cache_key = $this->getOrderCacheKey($order_id);
- $_id = Cache::get($_cache_key);
- if (!empty($_id)) {
- return $_id;
- }
- $_map = [
- 'order_id' => $order_id
- ];
- $_id = $this->where($_map)->value('id');
- if (empty($_id)) {
- return CommonConst::CONST_ZERO;
- }
- Cache::set($_cache_key, $_id);
- return $_id;
- }
- /**
- * 根据苹果订单ID获取ID
- *
- * @param $trans_id
- *
- * @return int
- */
- public function getIdByTransId($trans_id) {
- if (empty($trans_id)) {
- return CommonConst::CONST_ZERO;
- }
- $_cache_key = $this->getTransCacheKey($trans_id);
- $_id = Cache::get($_cache_key);
- if (!empty($_id)) {
- return $_id;
- }
- $_map = [
- 'trans_id' => $trans_id
- ];
- $_id = $this->where($_map)->value('id');
- if (empty($_id)) {
- return CommonConst::CONST_ZERO;
- }
- Cache::set($_cache_key, $_id);
- return $_id;
- }
- /**
- * 通过订单号获取详情
- *
- * @param string $order_id 平台订单号
- *
- * @return array
- */
- public function getInfoByOrderId($order_id) {
- $_id = $this->getIdByOrderId($order_id);
- if (empty($_id)) {
- return [];
- }
- $_data = $this->getInfoById($_id);
- return $_data;
- }
- /**
- * 通过苹果订单号获取详情
- *
- * @param string $trans_id 苹果订单号
- *
- * @return array
- */
- public function getInfoByTransId($trans_id) {
- $_id = $this->getIdByTransId($trans_id);
- if (empty($_id)) {
- return [];
- }
- $_data = $this->getInfoById($_id);
- return $_data;
- }
- /**
- * 获取最近订单信息
- *
- * @param string $end_time 结束时间
- * @param string $idfv IDFV
- * @param string $apple_id 苹果ID
- * @param string $product_id 商品ID
- *
- * @return array|false
- */
- public function getLastOrderData($end_time, $idfv, $apple_id, $product_id) {
- /* 3 天以内最近的订单 */
- $_end_time = $end_time;
- $_start_time = $_end_time - 3 * CommonConst::CONST_DAY_SECONDS;
- $_map['create_time'] = ['between', [$_start_time, $_end_time]];
- $_map['idfv'] = $idfv;
- $_map['apple_id'] = $apple_id;
- $_map['product_id'] = $product_id;
- $_map['trans_id'] = ['neq', ''];
- $_data = $this->where($_map)->order('id desc')->find();
- if (false == $_data) {
- return false;
- }
- if (is_object($_data)) {
- $_data = $_data->toArray();
- }
- return $_data;
- }
- public function mem() {
- return $this->belongsTo(MemberModel::className(), 'mem_id', 'id');
- }
- public function mg() {
- return $this->belongsTo(MemGameModel::className(), 'mg_mem_id', 'id')->field('id,nickname');
- }
- public function game() {
- return $this->belongsTo(GameModel::className(), 'apple_id', 'apple_id')->field('id,apple_id,name,icon');
- }
- }
|