OrderModel.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * OrderModel.php UTF-8
  4. * web
  5. *
  6. * @date : 2020/9/14 17:07
  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\tool\StrUtils;
  15. use think\Cache;
  16. class OrderModel extends CommonModel {
  17. protected $name = 'pay';
  18. protected $pk = 'id';
  19. /* 开启自动写入时间戳字段 */
  20. protected $autoWriteTimestamp = true;
  21. /* 当前版本订单缓存不写在model,故订单不使用缓存 */
  22. protected $use_cache = false;
  23. protected $cache_key_prefix = CacheConst::CACHE_SDK_ORDER_PREFIX;
  24. /**
  25. * 获取单条记录缓存key
  26. *
  27. * @param int $id ID
  28. *
  29. * @return string
  30. */
  31. protected function getSingleCacheKey($id) {
  32. return $this->cache_key_prefix.$id;
  33. }
  34. /**
  35. * 获取单条记录缓存key
  36. *
  37. * @param string $order_id 订单ID
  38. *
  39. * @return string
  40. */
  41. protected function getOrderCacheKey($order_id = '') {
  42. return $this->cache_key_prefix.'o_'.$order_id;
  43. }
  44. /**
  45. * 添加数据
  46. *
  47. * @param array $data 需要添加的数据
  48. *
  49. * @return false|int 添加失败返回 false 添加成功 返回添加的ID
  50. */
  51. public function addData($data) {
  52. $_data = $data;
  53. if (empty($_data['order_id'])) {
  54. $_agent_id = get_val($data, 'agent_id', 0);
  55. $_mem_id = get_val($data, 'mem_id', 0);
  56. $_data['order_id'] = StrUtils::genOrderId($_agent_id, $_agent_id, $_mem_id);
  57. }
  58. if (empty($_data['create_time'])) {
  59. unset($_data['create_time']);
  60. }
  61. $_id = parent::addData($_data);
  62. if (false === $_id) {
  63. return false;
  64. }
  65. /* TAG缓存操作 */
  66. return $_id;
  67. }
  68. /**
  69. * 通过ID获取信息
  70. *
  71. * @param int $id 主键ID
  72. *
  73. * @return array|false
  74. */
  75. public function getInfoById($id) {
  76. /* 缓存操作 */
  77. if ($this->use_cache) {
  78. $_single_cache_key = $this->getSingleCacheKey($id);
  79. $_data = Cache::get($_single_cache_key);
  80. if (!empty($_data)) {
  81. return $_data;
  82. }
  83. }
  84. $_data = parent::getInfoById($id);
  85. if (empty($_data)) {
  86. return [];
  87. }
  88. if ($this->use_cache) {
  89. Cache::set($_single_cache_key, $_data);
  90. }
  91. return $_data;
  92. }
  93. /**
  94. * 更新单条数据
  95. *
  96. * @param array $data 数据
  97. * @param int $id ID
  98. *
  99. * @return bool
  100. */
  101. public function updateData($data, $id) {
  102. if ($this->use_cache) {
  103. $_old_data = $this->getInfoById($id);
  104. }
  105. $_data = $data;
  106. $_rs = parent::updateData($_data, $id);
  107. if (false === $_rs) {
  108. return false;
  109. }
  110. if ($this->use_cache) {
  111. /* 缓存操作 */
  112. $_single_cache_key = $this->getSingleCacheKey($id);
  113. Cache::rm($_single_cache_key);
  114. }
  115. /* TAG缓存操作 */
  116. return true;
  117. }
  118. /**
  119. * 删除单条数据
  120. *
  121. * @param int $id ID
  122. * @param bool $is_complete 是否完成删除
  123. *
  124. * @return bool
  125. */
  126. public function deleteData($id, $is_complete = true) {
  127. if ($this->use_cache) {
  128. $_old_data = $this->getInfoById($id);
  129. }
  130. $_rs = parent::deleteData($id, $is_complete);
  131. if (false == $_rs) {
  132. return false;
  133. }
  134. if ($this->use_cache) {
  135. /* 缓存操作 */
  136. $_single_cache_key = $this->getSingleCacheKey($id);
  137. Cache::rm($_single_cache_key);
  138. /* 删除缓存 */
  139. $_cache_key = $this->getOrderCacheKey($_old_data['order_id']);
  140. Cache::rm($_cache_key);
  141. }
  142. /* TAG缓存操作 */
  143. return $_rs;
  144. }
  145. /**
  146. * 根据订单ID获取ID
  147. *
  148. * @param $order_id
  149. *
  150. * @return int
  151. */
  152. public function getIdByOrderId($order_id) {
  153. if (empty($order_id)) {
  154. return 0;
  155. }
  156. if ($this->use_cache) {
  157. $_cache_key = $this->getOrderCacheKey($order_id);
  158. $_id = Cache::get($_cache_key);
  159. if (!empty($_id)) {
  160. return $_id;
  161. }
  162. }
  163. $_map = [
  164. 'order_id' => $order_id
  165. ];
  166. $_id = $this->where($_map)->value('id');
  167. if (empty($_id)) {
  168. return 0;
  169. }
  170. if ($this->use_cache) {
  171. Cache::set($_cache_key, $_id);
  172. }
  173. return $_id;
  174. }
  175. /**
  176. * 根据订单ID获取记录
  177. *
  178. * @param string $order_id
  179. *
  180. * @return array|false
  181. */
  182. public function getInfoByOrderId($order_id) {
  183. $_id = $this->getIdByOrderId($order_id);
  184. if (empty($_id)) {
  185. return [];
  186. }
  187. $_data = $this->getInfoById($_id);
  188. return $_data;
  189. }
  190. /**
  191. * 获取状态
  192. *
  193. * @param $order_id
  194. *
  195. * @return array|bool
  196. */
  197. public function getStatus($order_id) {
  198. $_data = $this->getInfoByOrderId($order_id);
  199. if (empty($_data)) {
  200. return false;
  201. }
  202. $_rdata = [
  203. 'status' => get_val($_data, 'status', 0),
  204. 'cp_status' => get_val($_data, 'cp_status', 0),
  205. ];
  206. return $_rdata;
  207. }
  208. /***
  209. * 根据条件获取订单号数组
  210. *
  211. * @param array $where
  212. *
  213. * @return array
  214. */
  215. public function getOrderIdsByWhere($where) {
  216. $_map = $where;
  217. return $this->where($_map)->column('order_id');
  218. }
  219. }