* @day: 2017/12/18 */ namespace app\models\store; use app\admin\model\system\SystemStore; use app\models\user\User; use app\models\user\UserBill; use crmeb\basic\BaseModel; use crmeb\services\PHPExcelService; use crmeb\traits\ModelTrait; /** * TODO 秒杀产品Model * Class StoreSeckill * @package app\models\store */ class StoreExchangeOrder extends BaseModel { /** * 数据表主键 * @var string */ protected $pk = 'id'; /** * 模型名称 * @var string */ protected $name = 'store_exchange_order'; use ModelTrait; public static function orderList($where) { $model = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->field('a.*'); if ($where['order'] != '') { $model = $model->order(self::setOrder($where['order'])); } else { $model = $model->order('a.id desc'); } return ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()->each(function ($v) { $v['nickname'] = User::where('uid', $v['uid'])->value('nickname'); $v['giftnickname'] = User::where('uid', $v['gift_uid'])->value('nickname'); $v['status_name'] = $v['status'] == 1 ? "已使用" : "未使用"; $_info = StoreOrderCartInfo::where('cart_id', $v['cart_id'])->field('cart_info')->select(); $_info = count($_info) ? $_info->toArray() : []; foreach ($_info as $k => $vv) { $cart_info = $vv['cart_info']; if (!isset($cart_info['productInfo'])) $cart_info['productInfo'] = []; $_info[$k]['cart_info'] = $cart_info; unset($cart_info); } $v['_info'] = $_info; $v['store'] = SystemStore::where('id', $v['store_id'])->value('name') ?: '--'; $v['add_time'] = $v['add_time'] ? date('Y-m-d H:i:s', $v['add_time']) : ''; })) && count($data) ? $data->toArray() : []; } public static function getList($where) { $model = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->field('a.*'); if ($where['order'] != '') { $model = $model->order(self::setOrder($where['order'])); } else { $model = $model->order('a.id desc'); } $count = $model->count(); if ($where['excel']) { $data = ($data = $model->select()) && count($data) ? $data->toArray() : []; } else { $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : []; } foreach ($data as &$v) { $v['nickname'] = User::where('uid', $v['uid'])->value('nickname'); $v['giftnickname'] = User::where('uid', $v['gift_uid'])->value('nickname'); $v['status_name'] = $v['status'] == 1 ? "已使用" : ($v['status'] == 2 ? "已失效" : "未使用"); $_info = StoreOrderCartInfo::where('cart_id', $v['cart_id'])->field('cart_info')->select(); $_info = count($_info) ? $_info->toArray() : []; foreach ($_info as $k => $vv) { $cart_info = $vv['cart_info']; if (!isset($cart_info['productInfo'])) $cart_info['productInfo'] = []; $_info[$k]['cart_info'] = $cart_info; unset($cart_info); } $v['_info'] = $_info; $v['store'] = SystemStore::where('id', $v['store_id'])->value('name') ?: '--'; $v['add_time'] = $v['add_time'] ? date('Y-m-d H:i:s', $v['add_time']) : ''; $v['verify_time'] = $v['verify_time'] ? date('Y-m-d H:i:s', $v['verify_time']) : ''; $v['gift_time'] = $v['gift_time'] ? date('Y-m-d H:i:s', $v['gift_time']) : ''; } if (isset($where['excel']) && $where['excel'] == 1) { self::SaveExcel($data); } return compact('data', 'count'); } public static function SaveExcel($list) { $export = []; foreach ($list as $index => $item) { $_info = $item['_info']; $goodsName = []; foreach ($_info as $k => $v) { $suk = ''; if (isset($v['cart_info']['productInfo']['attrInfo'])) { if (isset($v['cart_info']['productInfo']['attrInfo']['suk'])) { $suk = '(' . $v['cart_info']['productInfo']['attrInfo']['suk'] . ')'; } } $goodsName[] = implode( [$v['cart_info']['productInfo']['store_name'], $suk, "[1 * {$v['cart_info']['truePrice']}]" ], ' '); } $export[] = [ $item['order_id'], $goodsName, $item['nickname'] . '|' . $item['uid'], $item['giftnickname'] . '|' . $item['gift_uid'], $item['store'], $item['pay_price'], $item['deposit'], $item['deposit_back'], $item['add_time'], $item['gift_time'], $item['verify_time'], $item['status_name'], ]; } PHPExcelService::setExcelHeader(['订单号', '商品信息', '订单用户', '赠送人', '核销店铺', '支付金额', '支付押金', '已退押金', '生成时间', '赠送时间', '核销时间', '兑换券状态']) ->setExcelTile('兑换券导出' . date('YmdHis', time()), '兑换券信息' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time())) ->setExcelContent($export) ->ExcelSave(); } public static function getOrderWhere($where, $model, $aler = '', $join = '') { if (isset($where['status']) && $where['status'] != '') $model = self::statusWhere($where['status'], $model, $aler); if (isset($where['store_id']) && $where['store_id'] != '' && $where['store_id'] != -1) $model = $model->where($aler . 'store_id', $where['store_id']); if (isset($where['user_phone']) && $where['user_phone'] != '') $model = $model->where($join . '.phone', 'LIKE', "%$where[user_phone]%"); if (isset($where['real_name']) && $where['real_name'] != '') $model = $model->where($aler . 'order_id|' . $join . '.phone' . ($join ? '|' . $join . '.nickname|' . $join . '.uid' : ''), 'LIKE', "%$where[real_name]%"); if (isset($where['data']) && $where['data'] !== '') $model = self::getModelTime($where, $model, $aler . 'add_time'); return $model; } public static function getAdminOrderDetail($orderId, $field = '*', $other_where = []) { return self::where('order_id', $orderId)->where($other_where)->field($field)->find(); } public static function statusWhere($status, $model = null, $alert = '') { if ($model == null) $model = new self; if ('' === $status) return $model; else if ($status == 0)//待使用 return $model->where($alert . 'status', 0)->where($alert . 'refund_status', 0); else if ($status == 1)//已使用 return $model->where($alert . 'status', 1)->where($alert . 'refund_status', 0); else if ($status == -1)//退款中 return $model->where($alert . 'refund_status', 1); else if ($status == -2)//已退款 return $model->where($alert . 'refund_status', 2); else if ($status == -3)//退款 return $model->where($alert . 'refund_status', 'in', '1,2'); else return $model; } public static function getUserOrderSearchList($uid, $type, $page, $limit, $search) { if ($search) { $order = self::searchUserOrder($uid, $search) ?: []; $list = $order == false ? [] : [$order]; } else { $list = self::getUserOrderList($uid, $type, $page, $limit); } foreach ($list as $k => $order) { $list[$k]['gift_user'] = User::get($order['gift_uid']); $list[$k]['user'] = User::get($order['uid']); $list[$k]['order'] = StoreOrder::get($order['oid']); $list[$k]['cart_info'] = StoreOrderCartInfo::where('oid', $order['oid']) ->where('cart_id', $order['cart_id']) ->value('cart_info'); if (is_string($list[$k]['cart_info'])) $list[$k]['cart_info'] = json_decode($list[$k]['cart_info'], true); } return $list; } public static function searchUserOrder($uid, $order_id) { $order = self::where('uid|gift_uid', $uid)->where('order_id', $order_id) ->order('id DESC')->select(); if (!$order) return false; else return $order; } public static function getUserOrderList($uid, $status = '', $page = 0, $limit = 8) { if ($page) $list = self::statusByWhere($status, $uid)->where('uid|gift_uid', $uid) ->order('id DESC')->page((int)$page, (int)$limit)->select()->toArray(); else $list = self::statusByWhere($status, $uid)->where('uid|gift_uid', $uid) ->order('id DESC')->page((int)$page, (int)$limit)->select()->toArray(); return $list; } public static function statusByWhere($status, $uid = 0, $model = null) { if ($model == null) $model = new self; if ('' === $status) return $model; else if ($status == 0)//待使用 return $model->where('status', 0)->where('refund_status', 0); else if ($status == 1)//已使用 return $model->where('status', 1)->where('refund_status', 0); else if ($status == -1)//退款中 return $model->where('refund_status', 1); else if ($status == -2)//已退款 return $model->where('refund_status', 2); else if ($status == -3)//退款 return $model->where('refund_status', 'IN', '1,2'); else if ($status == 9)//已送出 return $model->where('uid', $uid)->where('gift_uid', '>', 0)->where('gift_uid', '<>', $uid); else return $model; } }