StoreExchangeOrder.php 7.9 KB

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