StoreExchangeOrder.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 app\models\user\UserBill;
  11. use crmeb\basic\BaseModel;
  12. use crmeb\services\PHPExcelService;
  13. use crmeb\traits\ModelTrait;
  14. /**
  15. * TODO 秒杀产品Model
  16. * Class StoreSeckill
  17. * @package app\models\store
  18. */
  19. class StoreExchangeOrder extends BaseModel
  20. {
  21. /**
  22. * 数据表主键
  23. * @var string
  24. */
  25. protected $pk = 'id';
  26. /**
  27. * 模型名称
  28. * @var string
  29. */
  30. protected $name = 'store_exchange_order';
  31. use ModelTrait;
  32. public static function orderList($where)
  33. {
  34. $model = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->field('a.*');
  35. if ($where['order'] != '') {
  36. $model = $model->order(self::setOrder($where['order']));
  37. } else {
  38. $model = $model->order('a.id desc');
  39. }
  40. return ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()->each(function ($v) {
  41. $v['nickname'] = User::where('uid', $v['uid'])->value('nickname');
  42. $v['giftnickname'] = User::where('uid', $v['gift_uid'])->value('nickname');
  43. $v['status_name'] = $v['status'] == 1 ? "已使用" : "未使用";
  44. $_info = StoreOrderCartInfo::where('cart_id', $v['cart_id'])->field('cart_info')->select();
  45. $_info = count($_info) ? $_info->toArray() : [];
  46. foreach ($_info as $k => $vv) {
  47. $cart_info = $vv['cart_info'];
  48. if (!isset($cart_info['productInfo'])) $cart_info['productInfo'] = [];
  49. $_info[$k]['cart_info'] = $cart_info;
  50. unset($cart_info);
  51. }
  52. $v['_info'] = $_info;
  53. $v['store'] = SystemStore::where('id', $v['store_id'])->value('name') ?: '--';
  54. $v['add_time'] = $v['add_time'] ? date('Y-m-d H:i:s', $v['add_time']) : '';
  55. })) && count($data) ? $data->toArray() : [];
  56. }
  57. public static function getList($where)
  58. {
  59. $model = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->field('a.*');
  60. if ($where['order'] != '') {
  61. $model = $model->order(self::setOrder($where['order']));
  62. } else {
  63. $model = $model->order('a.id desc');
  64. }
  65. $count = $model->count();
  66. if ($where['excel']) {
  67. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  68. } else {
  69. $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
  70. }
  71. foreach ($data as &$v) {
  72. $v['nickname'] = User::where('uid', $v['uid'])->value('nickname');
  73. $v['giftnickname'] = User::where('uid', $v['gift_uid'])->value('nickname');
  74. $v['status_name'] = $v['status'] == 1 ? "已使用" : ($v['status'] == 2 ? "已失效" : "未使用");
  75. $_info = StoreOrderCartInfo::where('cart_id', $v['cart_id'])->field('cart_info')->select();
  76. $_info = count($_info) ? $_info->toArray() : [];
  77. foreach ($_info as $k => $vv) {
  78. $cart_info = $vv['cart_info'];
  79. if (!isset($cart_info['productInfo'])) $cart_info['productInfo'] = [];
  80. $_info[$k]['cart_info'] = $cart_info;
  81. unset($cart_info);
  82. }
  83. $v['_info'] = $_info;
  84. $v['store'] = SystemStore::where('id', $v['store_id'])->value('name') ?: '--';
  85. $v['add_time'] = $v['add_time'] ? date('Y-m-d H:i:s', $v['add_time']) : '';
  86. $v['verify_time'] = $v['verify_time'] ? date('Y-m-d H:i:s', $v['verify_time']) : '';
  87. $v['gift_time'] = $v['gift_time'] ? date('Y-m-d H:i:s', $v['gift_time']) : '';
  88. }
  89. if (isset($where['excel']) && $where['excel'] == 1) {
  90. self::SaveExcel($data);
  91. }
  92. return compact('data', 'count');
  93. }
  94. public static function SaveExcel($list)
  95. {
  96. $export = [];
  97. foreach ($list as $index => $item) {
  98. $_info = $item['_info'];
  99. $goodsName = [];
  100. foreach ($_info as $k => $v) {
  101. $suk = '';
  102. if (isset($v['cart_info']['productInfo']['attrInfo'])) {
  103. if (isset($v['cart_info']['productInfo']['attrInfo']['suk'])) {
  104. $suk = '(' . $v['cart_info']['productInfo']['attrInfo']['suk'] . ')';
  105. }
  106. }
  107. $goodsName[] = implode(
  108. [$v['cart_info']['productInfo']['store_name'],
  109. $suk,
  110. "[1 * {$v['cart_info']['truePrice']}]"
  111. ], ' ');
  112. }
  113. $export[] = [
  114. $item['order_id'],
  115. $goodsName,
  116. $item['nickname'] . '|' . $item['uid'],
  117. $item['giftnickname'] . '|' . $item['gift_uid'],
  118. $item['store'],
  119. $item['pay_price'],
  120. $item['deposit'],
  121. $item['deposit_back'],
  122. $item['add_time'],
  123. $item['gift_time'],
  124. $item['verify_time'],
  125. $item['status_name'],
  126. ];
  127. }
  128. PHPExcelService::setExcelHeader(['订单号', '商品信息', '订单用户', '赠送人', '核销店铺', '支付金额', '支付押金', '已退押金', '生成时间', '赠送时间', '核销时间', '兑换券状态'])
  129. ->setExcelTile('兑换券导出' . date('YmdHis', time()), '兑换券信息' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time()))
  130. ->setExcelContent($export)
  131. ->ExcelSave();
  132. }
  133. public static function getOrderWhere($where, $model, $aler = '', $join = '')
  134. {
  135. if (isset($where['status']) && $where['status'] != '') $model = self::statusWhere($where['status'], $model, $aler);
  136. if (isset($where['store_id']) && $where['store_id'] != '' && $where['store_id'] != -1) $model = $model->where($aler . 'store_id', $where['store_id']);
  137. if (isset($where['user_phone']) && $where['user_phone'] != '')
  138. $model = $model->where($join . '.phone', 'LIKE', "%$where[user_phone]%");
  139. if (isset($where['real_name']) && $where['real_name'] != '')
  140. $model = $model->where($aler . 'order_id|' . $join . '.phone' . ($join ? '|' . $join . '.nickname|' . $join . '.uid' : ''), 'LIKE', "%$where[real_name]%");
  141. if (isset($where['data']) && $where['data'] !== '')
  142. $model = self::getModelTime($where, $model, $aler . 'add_time');
  143. return $model;
  144. }
  145. public static function getAdminOrderDetail($orderId, $field = '*', $other_where = [])
  146. {
  147. return self::where('order_id', $orderId)->where($other_where)->field($field)->find();
  148. }
  149. public static function statusWhere($status, $model = null, $alert = '')
  150. {
  151. if ($model == null) $model = new self;
  152. if ('' === $status)
  153. return $model;
  154. else if ($status == 0)//待使用
  155. return $model->where($alert . 'status', 0)->where($alert . 'refund_status', 0);
  156. else if ($status == 1)//已使用
  157. return $model->where($alert . 'status', 1)->where($alert . 'refund_status', 0);
  158. else if ($status == -1)//退款中
  159. return $model->where($alert . 'refund_status', 1);
  160. else if ($status == -2)//已退款
  161. return $model->where($alert . 'refund_status', 2);
  162. else if ($status == -3)//退款
  163. return $model->where($alert . 'refund_status', 'in', '1,2');
  164. else
  165. return $model;
  166. }
  167. public static function getUserOrderSearchList($uid, $type, $page, $limit, $search)
  168. {
  169. if ($search) {
  170. $order = self::searchUserOrder($uid, $search) ?: [];
  171. $list = $order == false ? [] : [$order];
  172. } else {
  173. $list = self::getUserOrderList($uid, $type, $page, $limit);
  174. }
  175. foreach ($list as $k => $order) {
  176. $list[$k]['gift_user'] = User::get($order['gift_uid']);
  177. $list[$k]['user'] = User::get($order['uid']);
  178. $list[$k]['order'] = StoreOrder::get($order['oid']);
  179. $list[$k]['cart_info'] = StoreOrderCartInfo::where('oid', $order['oid'])
  180. ->where('cart_id', $order['cart_id'])
  181. ->value('cart_info');
  182. if (is_string($list[$k]['cart_info'])) $list[$k]['cart_info'] = json_decode($list[$k]['cart_info'], true);
  183. }
  184. return $list;
  185. }
  186. public static function searchUserOrder($uid, $order_id)
  187. {
  188. $order = self::where('uid|gift_uid', $uid)->where('order_id', $order_id)
  189. ->order('id DESC')->select();
  190. if (!$order)
  191. return false;
  192. else
  193. return $order;
  194. }
  195. public static function getUserOrderList($uid, $status = '', $page = 0, $limit = 8)
  196. {
  197. if ($page) $list = self::statusByWhere($status, $uid)->where('uid|gift_uid', $uid)
  198. ->order('id DESC')->page((int)$page, (int)$limit)->select()->toArray();
  199. else $list = self::statusByWhere($status, $uid)->where('uid|gift_uid', $uid)
  200. ->order('id DESC')->page((int)$page, (int)$limit)->select()->toArray();
  201. return $list;
  202. }
  203. public static function statusByWhere($status, $uid = 0, $model = null)
  204. {
  205. if ($model == null) $model = new self;
  206. if ('' === $status)
  207. return $model;
  208. else if ($status == 0)//待使用
  209. return $model->where('status', 0)->where('refund_status', 0);
  210. else if ($status == 1)//已使用
  211. return $model->where('status', 1)->where('refund_status', 0);
  212. else if ($status == -1)//退款中
  213. return $model->where('refund_status', 1);
  214. else if ($status == -2)//已退款
  215. return $model->where('refund_status', 2);
  216. else if ($status == -3)//退款
  217. return $model->where('refund_status', 'IN', '1,2');
  218. else if ($status == 9)//已送出
  219. return $model->where('uid', $uid)->where('gift_uid', '>', 0)->where('gift_uid', '<>', $uid);
  220. else
  221. return $model;
  222. }
  223. }