StoreExchangeOrder.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. return compact('data', 'count');
  82. }
  83. public static function getOrderWhere($where, $model, $aler = '', $join = '')
  84. {
  85. if (isset($where['status']) && $where['status'] != '') $model = self::statusWhere($where['status'], $model, $aler);
  86. if (isset($where['store_id']) && $where['store_id'] != '' && $where['store_id'] != -1) $model = $model->where($aler . 'store_id', $where['store_id']);
  87. if (isset($where['user_phone']) && $where['user_phone'] != '')
  88. $model = $model->where($join . '.phone', 'LIKE', "%$where[user_phone]%");
  89. if (isset($where['real_name']) && $where['real_name'] != '')
  90. $model = $model->where($aler . 'order_id|' . $join . '.phone' . ($join ? '|' . $join . '.nickname|' . $join . '.uid' : ''), 'LIKE', "%$where[real_name]%");
  91. if (isset($where['data']) && $where['data'] !== '')
  92. $model = self::getModelTime($where, $model, $aler . 'add_time');
  93. return $model;
  94. }
  95. public static function getAdminOrderDetail($orderId, $field = '*', $other_where = [])
  96. {
  97. return self::where('order_id', $orderId)->where($other_where)->field($field)->find();
  98. }
  99. public static function statusWhere($status, $model = null, $alert = '')
  100. {
  101. if ($model == null) $model = new self;
  102. if ('' === $status)
  103. return $model;
  104. else if ($status == 0)//待使用
  105. return $model->where($alert . 'status', 0)->where($alert . 'refund_status', 0);
  106. else if ($status == 1)//已使用
  107. return $model->where($alert . 'status', 1)->where($alert . 'refund_status', 0);
  108. else if ($status == -1)//退款中
  109. return $model->where($alert . 'refund_status', 1);
  110. else if ($status == -2)//已退款
  111. return $model->where($alert . 'refund_status', 2);
  112. else if ($status == -3)//退款
  113. return $model->where($alert . 'refund_status', 'in', '1,2');
  114. else
  115. return $model;
  116. }
  117. public static function getUserOrderSearchList($uid, $type, $page, $limit, $search)
  118. {
  119. if ($search) {
  120. $order = self::searchUserOrder($uid, $search) ?: [];
  121. $list = $order == false ? [] : [$order];
  122. } else {
  123. $list = self::getUserOrderList($uid, $type, $page, $limit);
  124. }
  125. foreach ($list as $k => $order) {
  126. $list[$k]['gift_user'] = User::get($order['gift_uid']);
  127. $list[$k]['user'] = User::get($order['uid']);
  128. $list[$k]['order'] = StoreOrder::get($order['oid']);
  129. $list[$k]['cart_info'] = StoreOrderCartInfo::where('oid', $order['oid'])
  130. ->where('cart_id', $order['cart_id'])
  131. ->value('cart_info');
  132. if (is_string($list[$k]['cart_info'])) $list[$k]['cart_info'] = json_decode($list[$k]['cart_info'], true);
  133. }
  134. return $list;
  135. }
  136. public static function searchUserOrder($uid, $order_id)
  137. {
  138. $order = self::where('uid|gift_uid', $uid)->where('order_id', $order_id)
  139. ->order('id DESC')->select();
  140. if (!$order)
  141. return false;
  142. else
  143. return $order;
  144. }
  145. public static function getUserOrderList($uid, $status = '', $page = 0, $limit = 8)
  146. {
  147. if ($page) $list = self::statusByWhere($status, $uid)->where('uid|gift_uid', $uid)
  148. ->order('id DESC')->page((int)$page, (int)$limit)->select()->toArray();
  149. else $list = self::statusByWhere($status, $uid)->where('uid|gift_uid', $uid)
  150. ->order('id DESC')->page((int)$page, (int)$limit)->select()->toArray();
  151. return $list;
  152. }
  153. public static function statusByWhere($status, $uid = 0, $model = null)
  154. {
  155. if ($model == null) $model = new self;
  156. if ('' === $status)
  157. return $model;
  158. else if ($status == 0)//待使用
  159. return $model->where('status', 0)->where('refund_status', 0);
  160. else if ($status == 1)//已使用
  161. return $model->where('status', 1)->where('refund_status', 0);
  162. else if ($status == -1)//退款中
  163. return $model->where('refund_status', 1);
  164. else if ($status == -2)//已退款
  165. return $model->where('refund_status', 2);
  166. else if ($status == -3)//退款
  167. return $model->where('refund_status', 'IN', '1,2');
  168. else if ($status == 9)//已送出
  169. return $model->where('uid', $uid)->where('gift_uid', '>', 0)->where('gift_uid', '<>', $uid);
  170. else
  171. return $model;
  172. }
  173. }