183 lines
6.1 KiB
PHP
183 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace app\api\model\order;
|
|
|
|
use think\Model;
|
|
use think\Db;
|
|
use app\common\model\order\Status as StatusModel;
|
|
class Order extends Model
|
|
{
|
|
|
|
/**
|
|
* 获取订单列表
|
|
* @param $userId
|
|
* @param $params
|
|
* @return array
|
|
*/
|
|
public function getList($userId,$params = []){
|
|
$where = [];
|
|
$where['deleted'] = 0;
|
|
|
|
// 组织用户维度查询条件
|
|
$userCondition = function ($query) use ($userId) {
|
|
$orderTableName = (new \app\admin\model\order\Order())->getQuery()->getTable();
|
|
$query->where(function ($q) use ($userId) {
|
|
// 原始用户订单查询
|
|
$q->where('user_id', $userId);
|
|
});
|
|
|
|
if(\app\admin\library\project\App::isInstall('channels')){
|
|
$userInfo = user_info();
|
|
if($userInfo && $userInfo['mobile']){
|
|
$userMobile = $userInfo['mobile'];
|
|
$channelTable = (new \app\api\model\app\channels\Order())->getQuery()->getTable();
|
|
$query->whereOr(function ($q) use ($userId, $orderTableName, $channelTable,$userMobile) {
|
|
// 第三方平台订单关联查询(需要传入mobile)
|
|
$q->whereExists(function ($subQuery) use ($orderTableName, $channelTable, $userMobile) {
|
|
$subQuery->table($channelTable)
|
|
->where($channelTable . '.order_id=' . $orderTableName . '.order_no')
|
|
->where($channelTable . '.mobile', $userMobile);
|
|
})->where($orderTableName . '.user_id', 0);
|
|
});
|
|
}
|
|
}
|
|
};
|
|
|
|
// 应用用户维度条件
|
|
$where[] = $userCondition;
|
|
|
|
// 其他过滤条件(状态/搜索等)
|
|
if($params['status']){
|
|
$statusFilter = StatusModel::statusGetFilter($params['status']);
|
|
if(!empty($statusFilter)){
|
|
$where = array_merge($where,$statusFilter);
|
|
}
|
|
}
|
|
|
|
$itemName = "";
|
|
if($params['search']){
|
|
if($params['search_field'] == 'order_no'){
|
|
$where['order_no'] = ['like',"%{$params['search']}%"];
|
|
}else{
|
|
$itemName = $params['search'];
|
|
}
|
|
}
|
|
|
|
$rows = self::where($where);
|
|
|
|
// 保留商品名称查询条件
|
|
if(!empty($itemName)){
|
|
$rows = $rows->whereExists(function ($query) use ($itemName) {
|
|
$itemTableName = (new \app\admin\model\order\Item())->getQuery()->getTable();
|
|
$orderTableName = (new \app\admin\model\order\Order())->getQuery()->getTable();
|
|
$query->table($itemTableName)
|
|
->where($itemTableName . '.order_no=' . $orderTableName . '.order_no')
|
|
->where('item_name', 'like', "%{$itemName}%");
|
|
});
|
|
}
|
|
|
|
$list = $rows
|
|
->with(['item'])
|
|
->limit($params['limit'])->page($params['page'])
|
|
->order('createtime','desc')
|
|
->select();
|
|
|
|
|
|
$data = [];
|
|
|
|
$statusModel = new StatusModel();
|
|
|
|
if(!empty($list)){
|
|
foreach ($list as $index => $item){
|
|
$item->visible(['controll','createtime','real_price','real_pay_price','evaluate','status','order_type','order_no','item','pay_type','is_virtual_pay']);
|
|
|
|
$data[$index] = $item->toArray();
|
|
|
|
$isVirtualPay = !empty($data[$index]['is_virtual_pay']);
|
|
|
|
$data[$index]['is_virtual_pay'] = $isVirtualPay ? 1 : 0;
|
|
|
|
if ($isVirtualPay) {
|
|
$data[$index] = \app\common\library\pay\VirtualPayService::convertPriceFields($data[$index], ['real_price', 'real_pay_price']);
|
|
}
|
|
|
|
if(!empty($item['item'])){
|
|
foreach ($item['item'] as $key => $option){
|
|
|
|
$data[$index]['item'][$key] = $this->limitItemField($data[$index]['item'][$key]);
|
|
|
|
if ($isVirtualPay && isset($data[$index]['item'][$key]['snapshoot']['price'])) {
|
|
$data[$index]['item'][$key]['snapshoot'] = \app\common\library\pay\VirtualPayService::convertPriceFields($data[$index]['item'][$key]['snapshoot'], ['price']);
|
|
}
|
|
}
|
|
}
|
|
|
|
$data[$index]['controll'] = $statusModel->getOrderControllStatus($data[$index]);
|
|
}
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 解析商品中的字段
|
|
* @param $snapshoot
|
|
* @return array
|
|
*/
|
|
public function limitItemField($item){
|
|
$item['snapshoot'] = $this->limitSnapshootField($item['snapshoot'] );
|
|
return [
|
|
'snapshoot'=>$item['snapshoot'],
|
|
'item_id'=>$item['item_id'],
|
|
'createtime'=>$item['createtime'],
|
|
'count'=>$item['count'],
|
|
'id'=>$item['id']
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 解析快照中的字段
|
|
* @param $snapshoot
|
|
* @return array
|
|
*/
|
|
public function limitSnapshootField($snapshoot){
|
|
if(!$snapshoot){
|
|
return $snapshoot;
|
|
}
|
|
if(!is_array($snapshoot)){
|
|
$snapshoot = json_decode($snapshoot,true);
|
|
}
|
|
$data = [
|
|
'id'=>$snapshoot['id'],
|
|
'cover'=>$snapshoot['cover'],
|
|
'type'=>$snapshoot['type'],
|
|
'name'=>$snapshoot['name'],
|
|
'price'=>$snapshoot['price']
|
|
];
|
|
|
|
if(isset($snapshoot['id'])){
|
|
$data['id'] = $snapshoot['id'];
|
|
}
|
|
|
|
if(isset($snapshoot['updatetime'])){
|
|
$data['updatetime'] = $snapshoot['updatetime'];
|
|
}
|
|
if(isset($snapshoot['views'])){
|
|
$data['views'] = $snapshoot['views'];
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
public function item()
|
|
{
|
|
return $this->hasMany('app\common\model\order\Item', 'order_no', 'order_no');
|
|
}
|
|
|
|
|
|
public function channelsorder()
|
|
{
|
|
return $this->belongsTo('app\api\model\app\channels\Order', 'order_no', 'order_id', [], 'left')->setEagerlyType(0);
|
|
}
|
|
|
|
}
|