StoreExchangeOrder.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/18
  6. */
  7. namespace app\models\store;
  8. use app\models\user\User;
  9. use crmeb\basic\BaseModel;
  10. use crmeb\traits\ModelTrait;
  11. /**
  12. * TODO 秒杀产品Model
  13. * Class StoreSeckill
  14. * @package app\models\store
  15. */
  16. class StoreExchangeOrder extends BaseModel
  17. {
  18. /**
  19. * 数据表主键
  20. * @var string
  21. */
  22. protected $pk = 'id';
  23. /**
  24. * 模型名称
  25. * @var string
  26. */
  27. protected $name = 'store_exchange_order';
  28. use ModelTrait;
  29. public static function orderList($where)
  30. {
  31. $model = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->field('a.*');
  32. if ($where['order'] != '') {
  33. $model = $model->order(self::setOrder($where['order']));
  34. } else {
  35. $model = $model->order('a.id desc');
  36. }
  37. return ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
  38. }
  39. public static function getList($where)
  40. {
  41. $model = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->field('a.*');
  42. if ($where['order'] != '') {
  43. $model = $model->order(self::setOrder($where['order']));
  44. } else {
  45. $model = $model->order('a.id desc');
  46. }
  47. $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
  48. foreach ($data as &$v) {
  49. $v['nickname'] = User::where('uid', $v['uid'])->value('nickname');
  50. $v['giftnickname'] = User::where('uid', $v['gift_uid'])->value('nickname');
  51. $v['status_name'] = $v['status'] == 1 ? "已使用" : "未使用";
  52. $_info = StoreOrderCartInfo::where('cart_id', $v['cart_id'])->field('cart_info')->select();
  53. $_info = count($_info) ? $_info->toArray() : [];
  54. foreach ($_info as $k => $vv) {
  55. $cart_info = $vv['cart_info'];
  56. if (!isset($cart_info['productInfo'])) $cart_info['productInfo'] = [];
  57. $_info[$k]['cart_info'] = $cart_info;
  58. unset($cart_info);
  59. }
  60. $v['_info'] = $_info;
  61. }
  62. $count = $model->count();
  63. return compact('data', 'count');
  64. }
  65. public static function getOrderWhere($where, $model, $aler = '', $join = '')
  66. {
  67. if (isset($where['status']) && $where['status'] != '') $model = self::statusWhere($where['status'], $model, $aler);
  68. if (isset($where['store_id']) && $where['store_id'] != '' && $where['store_id'] != -1) $model = $model->where($aler . 'store_id', $where['store_id']);
  69. if (isset($where['user_phone']) && $where['user_phone'] != '')
  70. $model = $model->where($aler . 'user_phone', 'LIKE', "%$where[user_phone]%");
  71. if (isset($where['real_name']) && $where['real_name'] != '')
  72. $model = $model->where($aler . 'order_id|' . $aler . 'real_name|' . $aler . 'user_phone' . ($join ? '|' . $join . '.nickname|' . $join . '.uid' : ''), 'LIKE', "%$where[real_name]%");
  73. if (isset($where['data']) && $where['data'] !== '')
  74. $model = self::getModelTime($where, $model, $aler . 'add_time');
  75. return $model;
  76. }
  77. public static function getAdminOrderDetail($orderId, $field = '*', $other_where = [])
  78. {
  79. return self::where('order_id', $orderId)->where($other_where)->field($field)->find();
  80. }
  81. public static function statusWhere($status, $model = null, $alert = '')
  82. {
  83. if ($model == null) $model = new self;
  84. if ('' === $status)
  85. return $model;
  86. else if ($status == 0)//待使用
  87. return $model->where($alert . 'status', 0)->where($alert . 'refund_status', 0);
  88. else if ($status == 1)//已使用
  89. return $model->where($alert . 'status', 1)->where($alert . 'refund_status', 0);
  90. else if ($status == -1)//退款中
  91. return $model->where($alert . 'refund_status', 1);
  92. else if ($status == -2)//已退款
  93. return $model->where($alert . 'refund_status', 2);
  94. else if ($status == -3)//退款
  95. return $model->where($alert . 'refund_status', 'in', '1,2');
  96. else
  97. return $model;
  98. }
  99. public static function getUserOrderSearchList($uid, $type, $page, $limit, $search)
  100. {
  101. if ($search) {
  102. $order = self::searchUserOrder($uid, $search) ?: [];
  103. $list = $order == false ? [] : [$order];
  104. } else {
  105. $list = self::getUserOrderList($uid, $type, $page, $limit);
  106. }
  107. foreach ($list as $k => $order) {
  108. $list[$k]['order'] = StoreOrder::get($order['oid']);
  109. $list[$k]['cart_info'] = StoreOrderCartInfo::where('oid', $order['oid'])
  110. ->where('cart_id', $order['cart_id'])
  111. ->value('cart_info');
  112. if (is_string($list[$k]['cart_info'])) $list[$k]['cart_info'] = json_decode($list[$k]['cart_info'], true);
  113. }
  114. return $list;
  115. }
  116. public static function searchUserOrder($uid, $order_id)
  117. {
  118. $order = self::where('uid|gift_uid', $uid)->where('order_id', $order_id)->where('is_del', 0)
  119. ->order('add_time DESC')->select();
  120. if (!$order)
  121. return false;
  122. else
  123. return $order;
  124. }
  125. public static function getUserOrderList($uid, $status = '', $page = 0, $limit = 8)
  126. {
  127. if ($page) $list = self::statusByWhere($status, $uid)->where('is_del', 0)->where('uid|gift_uid', $uid)
  128. ->order('add_time DESC')->page((int)$page, (int)$limit)->select()->toArray();
  129. else $list = self::statusByWhere($status, $uid)->where('is_del', 0)->where('uid|gift_uid', $uid)
  130. ->order('add_time DESC')->page((int)$page, (int)$limit)->select()->toArray();
  131. return $list;
  132. }
  133. public static function statusByWhere($status, $uid = 0, $model = null)
  134. {
  135. if ($model == null) $model = new self;
  136. if ('' === $status)
  137. return $model;
  138. else if ($status == 0)//待使用
  139. return $model->where('status', 0)->where('refund_status', 0);
  140. else if ($status == 1)//已使用
  141. return $model->where('status', 1)->where('refund_status', 0);
  142. else if ($status == -1)//退款中
  143. return $model->where('refund_status', 1);
  144. else if ($status == -2)//已退款
  145. return $model->where('refund_status', 2);
  146. else if ($status == -3)//退款
  147. return $model->where('refund_status', 'IN', '1,2');
  148. else
  149. return $model;
  150. }
  151. }