123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <?php
- /**
- * OrderModel.php UTF-8
- * web
- *
- * @date : 2020/9/14 17:07
- *
- * @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\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');
- }
- }
|