* @version : H5IOS 1.0 */ namespace huosdk\h5ios\core\model; use huosdk\h5ios\core\constant\CacheConst; use huosdk\h5ios\core\tool\StrUtils; use think\Cache; class OrderModel extends CommonModel { protected $name = 'pay'; protected $pk = 'id'; /* 开启自动写入时间戳字段 */ protected $autoWriteTimestamp = true; /* 当前版本订单缓存不写在model,故订单不使用缓存 */ protected $use_cache = false; protected $cache_key_prefix = CacheConst::CACHE_SDK_ORDER_PREFIX; /** * 获取单条记录缓存key * * @param int $id ID * * @return string */ protected function getSingleCacheKey($id) { return $this->cache_key_prefix.$id; } /** * 获取单条记录缓存key * * @param string $order_id 订单ID * * @return string */ protected function getOrderCacheKey($order_id = '') { return $this->cache_key_prefix.'o_'.$order_id; } /** * 添加数据 * * @param array $data 需要添加的数据 * * @return false|int 添加失败返回 false 添加成功 返回添加的ID */ public function addData($data) { $_data = $data; if (empty($_data['order_id'])) { $_agent_id = get_val($data, 'agent_id', 0); $_mem_id = get_val($data, 'mem_id', 0); $_data['order_id'] = StrUtils::genOrderId($_agent_id, $_agent_id, $_mem_id); } if (empty($_data['create_time'])) { unset($_data['create_time']); } $_id = parent::addData($_data); if (false === $_id) { return false; } /* TAG缓存操作 */ return $_id; } /** * 通过ID获取信息 * * @param int $id 主键ID * * @return array|false */ public function getInfoById($id) { /* 缓存操作 */ if ($this->use_cache) { $_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 []; } if ($this->use_cache) { Cache::set($_single_cache_key, $_data); } return $_data; } /** * 更新单条数据 * * @param array $data 数据 * @param int $id ID * * @return bool */ public function updateData($data, $id) { if ($this->use_cache) { $_old_data = $this->getInfoById($id); } $_data = $data; $_rs = parent::updateData($_data, $id); if (false === $_rs) { return false; } if ($this->use_cache) { /* 缓存操作 */ $_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) { if ($this->use_cache) { $_old_data = $this->getInfoById($id); } $_rs = parent::deleteData($id, $is_complete); if (false == $_rs) { return false; } if ($this->use_cache) { /* 缓存操作 */ $_single_cache_key = $this->getSingleCacheKey($id); Cache::rm($_single_cache_key); /* 删除缓存 */ $_cache_key = $this->getOrderCacheKey($_old_data['order_id']); Cache::rm($_cache_key); } /* TAG缓存操作 */ return $_rs; } /** * 根据订单ID获取ID * * @param $order_id * * @return int */ public function getIdByOrderId($order_id) { if (empty($order_id)) { return 0; } if ($this->use_cache) { $_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 0; } if ($this->use_cache) { Cache::set($_cache_key, $_id); } return $_id; } /** * 根据订单ID获取记录 * * @param string $order_id * * @return array|false */ public function getInfoByOrderId($order_id) { $_id = $this->getIdByOrderId($order_id); if (empty($_id)) { return []; } $_data = $this->getInfoById($_id); return $_data; } /** * 获取状态 * * @param $order_id * * @return array|bool */ public function getStatus($order_id) { $_data = $this->getInfoByOrderId($order_id); if (empty($_data)) { return false; } $_rdata = [ 'status' => get_val($_data, 'status', 0), 'cp_status' => get_val($_data, 'cp_status', 0), ]; return $_rdata; } /*** * 根据条件获取订单号数组 * * @param array $where * * @return array */ public function getOrderIdsByWhere($where) { $_map = $where; return $this->where($_map)->column('order_id'); } }