Files
huangjingfen/pro_v3.5.1/app/dao/order/StoreOrderDao.php
apple 78de918c37 Initial commit: queue workspace
Made-with: Cursor
2026-03-21 02:55:24 +08:00

1293 lines
50 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\dao\order;
use app\dao\BaseDao;
use app\model\order\StoreOrder;
use crmeb\basic\BaseModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
/**
* 订单
* Class StoreOrderDao
* @package app\dao\order
*/
class StoreOrderDao extends BaseDao
{
/**
* 限制精确查询字段
* @var string[]
*/
protected array $withField = ['uid', 'order_id', 'real_name', 'user_phone', 'title', 'total_num', 'delivery_id'];
/**
* @return string
*/
protected function setModel(): string
{
return StoreOrder::class;
}
/**
* 条件获取一条
* @param array $where
* @param string $field
* @param string $order
* @param array $with
* @return array|mixed
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author wuhaotian
* @email 442384644@qq.com
* @date 2025/5/10
*/
public function getOneData(array $where = [], string $field = '*', string $order = '', array $with = [])
{
return $this->getModel()->where($where)->with($with)->field($field)->order($order)->find();
}
/**
* 条件获取多条
* @param array $where
* @param string $field
* @param string $order
* @param array $with
* @return array|BaseModel[]|\think\Collection
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author wuhaotian
* @email 442384644@qq.com
* @date 2025/5/10
*/
public function getManyData(array $where = [], string $field = '*', string $order = '', array $with = [])
{
return $this->getModel()->where($where)->with($with)->field($field)->order($order)->select();
}
/**
* 订单搜索
* @param array $where
* @param bool $search
* @return BaseModel
* @throws \ReflectionException
*/
public function search(array $where = [], bool $search = false)
{
if (isset($where['real_name'])) {
$where['real_name'] = trim($where['real_name']);
}
$isDel = isset($where['is_del']) && $where['is_del'] !== '' && $where['is_del'] != -1;
$realName = $where['real_name'] ?? '';
$fieldKey = $where['field_key'] ?? '';
$fieldKey = $fieldKey == 'all' ? '' : $fieldKey;
return parent::search($where, $search)->when($isDel, function ($query) use ($where) {
if (is_array($where['is_del'])) {
$query->whereIn('is_del', $where['is_del']);
} else {
$query->where('is_del', $where['is_del']);
}
})->when(isset($where['plat_type']) && in_array($where['plat_type'], [-1, 0, 1, 2]), function ($query) use ($where) {
switch ($where['plat_type']) {
case -1://所有
break;
case 0://平台
$query->where('store_id', 0)->where('supplier_id', 0);
break;
case 1://门店
$query->where('store_id', '>', 0);
break;
case 2://供应商
$query->where('supplier_id', '>', 0);
break;
}
})->when(isset($where['is_system_del']), function ($query) {
$query->where('is_system_del', 0);
})->when(isset($where['staff_id']) && $where['staff_id'], function ($query) use ($where) {
$query->where('staff_id', $where['staff_id']);
})->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
switch ((int)$where['status']) {
case 0://未支付
$query->where('paid', 0)->where('status', 0)->where('refund_status', 0)->where('is_del', 0);
break;
case 1://已支付 未发货
$query->where('paid', 1)->whereIn('status', [0, 4])->whereIn('refund_status', [0, 3])->whereIn('shipping_type', [1, 3])->where('is_del', 0);
break;
case 7://已支付 部分发货
$query->where('paid', 1)->where('status', 4)->whereIn('refund_status', [0, 3])->where('is_del', 0);
break;
case 2://已支付 待收货 待核销
$query->where('paid', 1)->where(function ($query) {
$query->where(function ($q) {
$q->whereIn('status', [1, 5])->where('shipping_type', 1);
})
->whereOr(function ($e) {
$e->whereIn('status', [0, 5])->where('shipping_type', 2);
});
})->whereIn('refund_status', [0, 3])->where('is_del', 0);
break;
case 3:// 已支付 已收货 待评价
$query->where('paid', 1)->where('status', 2)->whereIn('refund_status', [0, 3])->where('is_del', 0);
break;
case 4:// 交易完成
$query->where('paid', 1)->where('status', 3)->whereIn('refund_status', [0, 3])->where('is_del', 0);
break;
case 5://已支付 待核销
$query->where('paid', 1)->whereIn('status', [0, 1, 5])->whereIn('refund_status', [0, 3])->where('shipping_type', 2)->where('is_del', 0);
break;
case 6://已支付 已核销 没有退款
$query->where('paid', 1)->where('status', 2)->whereIn('refund_status', [0, 3])->where('shipping_type', 2)->where('is_del', 0);
break;
case 8://已支付 核销订单
$query->where('paid', 1)->whereIn('status', [0, 1, 2, 5])->whereIn('refund_status', [0, 3])->where('shipping_type', 2)->where('is_del', 0);
break;
case 9://已配送
$query->where('paid', 1)->whereIn('status', [2, 3])->whereIn('refund_status', [0, 3])->where('is_del', 0);
break;
case -1://退款中
$query->where('paid', 1)->whereIn('refund_status', [1, 4])->where('is_del', 0);
break;
case -2://已退款
$query->where('paid', 1)->where('refund_status', 2)->where('is_del', 0);
break;
case -3://退款
$query->where('paid', 1)->whereIn('refund_status', [1, 2, 4])->where('is_del', 0);
break;
case -4://已删除
$query->where('is_del', 1);
break;
}
})->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
switch ($where['type']) {
case 0://普通
$query->where('type', 0);
break;
case 1://秒杀
$query->where('type', 1);
break;
case 2://砍价
$query->where('type', 2);
break;
case 3://拼团
$query->where('type', 3);
break;
case 4://积分订单
$query->where('type', 4);
break;
case 5://套餐
$query->where('type', 5);
break;
case 6://预售
$query->where('type', 6);
break;
case 7://新人专享
$query->where('type', 7);
break;
case 8://抽奖
$query->where('type', 8);
break;
case 20://采购商
$query->where('type', 20);
break;
case 9://兑换
$query->where('type', 9);
break;
case 10://送礼
$query->where('type', 10);
break;
case 105://核销订单
$query->where('shipping_type', 2);
break;
case 106://收银台订单
$query->where('shipping_type', 4);
break;
case 107://配送订单
$query->whereIn('shipping_type', [1, 3]);
break;
}
})->when(isset($where['pay_type']), function ($query) use ($where) {
switch ($where['pay_type']) {
case 1:
$query->where('pay_type', 'weixin');
break;
case 2:
$query->where('pay_type', 'yue');
break;
case 3:
$query->where('pay_type', 'offline');
break;
case 4:
$query->where('pay_type', 'alipay');
break;
case 5:
$query->where('pay_type', 'integral');
break;
case 6:
$query->where('pay_type', 'weixin')->where('yue_price', '>', 0);
break;
case 7:
$query->where('pay_type', 'alipay')->where('yue_price', '>', 0);
break;
case 8:
$query->where('pay_type', 'offline')->where('yue_price', '>', 0);
break;
}
})->when($realName && $fieldKey && in_array($fieldKey, $this->withField), function ($query) use ($where, $realName, $fieldKey) {
if ($fieldKey !== 'title') {
$query->where(trim($fieldKey), trim($realName));
} else {
$query->where('id', 'in', function ($que) use ($where) {
$que->name('store_order_cart_info')->whereIn('product_id', function ($q) use ($where) {
$q->name('store_product')->whereLike('store_name|keyword', '%' . $where['real_name'] . '%')->field(['id'])->select();
})->field(['oid'])->select();
});
}
})->when($realName && !$fieldKey, function ($query) use ($where) {
$query->where(function ($que) use ($where) {
$que->whereLike('order_id|real_name|user_phone|delivery_id', '%' . $where['real_name'] . '%')
->whereOr('uid', 'in', function ($q) use ($where) {
$q->name('user')->whereLike('nickname|uid|phone', '%' . $where['real_name'] . '%')->field(['uid'])->select();
})->whereOr('uid', 'in', function ($q) use ($where) {
$q->name('user_address')->whereLike('real_name|uid|phone', '%' . $where['real_name'] . '%')->field(['uid'])->select();
})->whereOr('id', 'in', function ($que) use ($where) {
$que->name('store_order_cart_info')->whereIn('product_id', function ($q) use ($where) {
$q->name('store_product')->whereLike('store_name|keyword', '%' . $where['real_name'] . '%')->field(['id'])->select();
})->field(['oid'])->select();
})->whereOr('activity_id', 'in', function ($que) use ($where) {
$que->name('store_seckill')->whereLike('title|info', '%' . $where['real_name'] . '%')->field(['id'])->select();
})->whereOr('activity_id', 'in', function ($que) use ($where) {
$que->name('store_bargain')->whereLike('title|info', '%' . $where['real_name'] . '%')->field(['id'])->select();
})->whereOr('activity_id', 'in', function ($que) use ($where) {
$que->name('store_combination')->whereLike('title|info', '%' . $where['real_name'] . '%')->field(['id'])->select();
});
});
})->when(isset($where['unique']), function ($query) use ($where) {
$query->where('unique', $where['unique']);
})->when(isset($where['is_remind']), function ($query) use ($where) {
$query->where('is_remind', $where['is_remind']);
})->when(isset($where['refundTypes']) && $where['refundTypes'] != '', function ($query) use ($where) {
switch ((int)$where['refundTypes']) {
case 1://申请中
$query->whereIn('refund_type', [0, 1, 2]);
break;
case 2://待退货
$query->where('refund_type', 4);
break;
case 3://退款中
$query->where('refund_type', 5);
break;
case 4://已退款
$query->where('refund_type', 6);
break;
case 5://处理中
$query->whereIn('refund_type', [0, 1, 2, 4, 5]);
break;
}
})->when(isset($where['division_id']) && $where['division_id'] != 0, function ($query) use ($where) {
if ($where['division_id'] == -1) {
$query->where('division_id', '>', 0);
} else {
$query->where('division_id', $where['division_id']);
}
})->when(isset($where['division_agent_id']) && $where['division_agent_id'] != 0, function ($query) use ($where) {
if ($where['division_agent_id'] == -1) {
$query->where('division_agent_id', '>', 0);
} else {
$query->where('division_agent_id', $where['division_agent_id']);
}
});
}
/**
* 获取某一个月订单数量
* @param array $where
* @param string $month
* @return int
*/
public function getMonthCount(array $where, string $month)
{
return $this->search($where)->whereMonth('add_time', $month)->count();
}
/**
* 获取某一个月订单金额
* @param array $where
* @param string $month
* @param string $field
* @return float
*/
public function getMonthMoneyCount(array $where, string $month, string $field)
{
return $this->search($where)->whereMonth('add_time', $month)->sum($field);
}
/**
* 获取购买历史用户
* @param int $storeId
* @param int $staffId
* @param int $limit
* @return mixed
*/
public function getOrderHistoryList(int $storeId, int $staffId, array $uid = [], int $limit = 20)
{
return $this->search(['store_id' => $storeId, 'staff_id' => $staffId])->when($uid, function ($query) use ($uid) {
$query->whereNotIn('uid', $uid);
})->where('uid', '<>', 0)->with('user')->limit($limit)
->group('uid')->order('add_time', 'desc')->field(['uid', 'store_id', 'staff_id'])->select()->toArray();
}
/**
* 订单搜索列表
* @param array $where
* @param array $field
* @param int $page
* @param int $limit
* @param array $with
* @return array
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function getList(array $where, array $field, int $page = 0, int $limit = 0, array $with = [])
{
return $this->search($where)->field($field)->with($with)->when($page && $limit, function ($query) use ($page, $limit) {
$query->page($page, $limit);
})->order('pay_time DESC,id DESC')->select()->toArray();
}
/**
* 获取待核销的订单列表
* @param array $where
* @param array|string[] $field
* @return array
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function getUnWirteOffList(array $where, array $field = ['*'])
{
return $this->search($where)->field($field)->where('paid', 1)->whereIn('status', [0, 1, 5])->whereIn('refund_status', [0, 3])->where('is_del', 0)->where('is_system_del', 0)
->where(function ($query) {
$query->where('shipping_type', 2)->whereOr('delivery_type', 'send');
})->order('pay_time DESC,id DESC')->select()->toArray();
}
/**
* 订单搜索列表
* @param array $where
* @param array $field
* @param int $page
* @param int $limit
* @param array $with
* @param string $order
* @return array
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function getOrderList(array $where, array $field, int $page = 0, int $limit = 0, array $with = [], string $order = 'add_time DESC,pay_time DESC,id DESC')
{
return $this->search($where, false)
->field($field)
->with(array_merge(['user', 'spread', 'refund'], $with))
->when($page && $limit, function ($query) use ($page, $limit, $where) {
//兼容大数据分页查询
if (!empty($where['pay_time']) && !empty($where['id'])) {
$query->where(function ($query) use ($where) {
$query->where('pay_time', '<', $where['pay_time'])
->whereOr(function ($query) use ($where) {
$query->where('pay_time', $where['pay_time'])
->where('id', '<', $where['id']);
});
})->page($page, $limit);
} else if (!empty($where['id'])) {
$query->where('id', '<', $where['id'])->page($page, $limit);
} else {
$query->page($page, $limit);
}
})->when(!$page && $limit, function ($query) use ($limit) {
$query->limit($limit);
})->order($order)->select()->toArray();
}
/**
* 聚合查询
* @param array $where
* @param string $field
* @param string $together
* @return int
*/
public function together(array $where, string $field, string $together = 'sum')
{
if (!in_array($together, ['sum', 'max', 'min', 'avg'])) {
return 0;
}
return $this->search($where)->{$together}($field);
}
/**
* 查找指定条件下的订单数据以数组形式返回
* @param array $where
* @param string $field
* @param string $key
* @param string $group
* @return array
*/
public function column(array $where, string $field, string $key = '', string $group = '')
{
return $this->search($where)->when($group, function ($query) use ($group) {
$query->group($group);
})->column($field, $key);
}
/**
* 获取订单id下没有删除的订单数量
* @param array $ids
* @return int
*/
public function getOrderIdsCount(array $ids)
{
return $this->getModel()->whereIn('id', $ids)->where('is_del', 0)->count();
}
/**
* 获取一段时间订单统计数量、金额
* @param array $where
* @param array $time
* @param string $timeType
* @param bool $is_pid
* @param string $countField
* @param string $sumField
* @param string $groupField
* @return array
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function orderAddTimeList(array $where, array $time, string $timeType = "week", bool $is_pid = true, string $countField = '*', string $sumField = 'pay_price', string $groupField = 'add_time')
{
return $this->getModel()->where($where)->when($is_pid, function ($query) {
$query->where('pid', '>=', 0);
})->when(!isset($where['refund_status']), function ($query) {
$query->whereIn('refund_status', [0, 3]);
})->where('paid', 1)->where('is_del', 0)->where('is_system_del', 0)
->where(isset($where['timekey']) && $where['timekey'] ? $where['timekey'] : 'add_time', 'between time', $time)
->when($timeType, function ($query) use ($timeType, $countField, $sumField, $groupField) {
switch ($timeType) {
case "hour":
$timeUnix = "%H";
break;
case "day" :
$timeUnix = "%m-%d";
break;
case "week" :
$timeUnix = "%w";
break;
case "month" :
$timeUnix = "%d";
break;
case "weekly" :
$timeUnix = "%W";
break;
case "year" :
$timeUnix = "%Y-%m";
break;
default:
$timeUnix = "%m-%d";
break;
}
$query->field("FROM_UNIXTIME(`" . $groupField . "`,'$timeUnix') as day,count(" . $countField . ") as count,sum(`" . $sumField . "`) as price");
$query->group('day');
})->order('add_time asc')->select()->toArray();
}
/**
* 统计总数上期
* @param array $where
* @param array $time
* @return array|\think\Model|null
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function preTotalFind(array $where, array $time, string $sumField = 'pay_price', string $groupField = 'add_time')
{
return $this->getModel()->where($where)->where('pid', '>=', 0)->where('paid', 1)->whereIn('refund_status', [0, 3])->where('is_del', 0)->where('is_system_del', 0)
->where($groupField, 'between time', $time)
->field("count(*) as count,sum(`" . $sumField . "`) as price")
->find();
}
/**
* 新订单ID
* @param $status
* @param int $store_id
* @return array
*/
public function newOrderId($status, int $store_id = 0)
{
return $this->search(['status' => $status, 'is_remind' => 0])->column('order_id', 'id');
}
/**
* 获取订单数量
* @param int $store_id
* @param int $type
* @param string $field
* @return int
*/
public function storeOrderCount(int $val = 0, int $type = -1, string $field = 'store_id')
{
$where = ['pid' => 0, 'status' => 1];
if ($type != -1) $where['type'] = $type;
return $this->search($where)->when($field && $val > 0, function ($query) use ($field, $val) {
$query->where($field, $val);
})->count();
}
/**
* 总销售额
* @param $time
* @param int $store_id
* @return float
*/
public function totalSales($time, int $store_id = 0)
{
return $this->search(['pid' => 0, 'paid' => 1, 'is_del' => 0, 'refund_status' => [0, 3], 'time' => $time ?: 'today', 'timekey' => 'pay_time'])->sum('pay_price');
}
/**
* 获取特定时间内订单量
* @param $time
* @return int
*/
public function totalOrderCount($time)
{
return $this->search(['pid' => 0, 'time' => $time ?: 'today', 'timeKey' => 'add_time'])->where('is_del', 0)->where('paid', 1)->whereIn('refund_status', [0, 3])->count();
}
/**
* 获取订单详情
* @param string $key
* @param int $uid
* @param array $with
* @return array|\think\Model|null
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function getUserOrderDetail(string $key, int $uid = 0, array $with = [])
{
$str = substr($key, 0, 4);
if (strpos($str, '_') !== false) {//二次支付订单
$key = ltrim($key, $str);
}
$where = ['order_id|id|unique' => $key];
if ($uid) $where = $where + ['uid|receive_gift_uid' => $uid];
return $this->getOne($where, '*', $with);
}
/**
* 获取用户最新的未支付订单信息
* @param array $where
* @param array $with
* @return mixed
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @throws \ReflectionException
*/
public function getUserNotPayOrderDetail(array $where, string $field, array $with = [])
{
return $this->search($where)->with($with)->field($field)->order('id DESC')->find();
}
/**
* 获取用户推广订单
* @param array $where
* @param string $field
* @param int $page
* @param int $limit
* @param array $with
* @return array
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function getStairOrderList(array $where, string $field, int $page, int $limit, array $with = [])
{
return $this->search($where)->with($with)->field($field)->when($page && $limit, function ($query) use ($page, $limit) {
$query->page($page, $limit);
})->order('id DESC')->select()->toArray();
}
/**
* 订单每月统计数据
* @param int $page
* @param int $limit
* @return array
*/
public function getOrderDataPriceCount(array $where, array $field, int $page, int $limit)
{
return $this->search($where)
->field($field)->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')")
->order('add_time DESC')->page($page, $limit)->select()->toArray();
}
/**
* 获取当前时间到指定时间的支付金额 管理员
* @param $start
* @param $stop
* @param int $store_id
* @return mixed
*/
public function chartTimePrice($start, $stop, int $store_id = 0)
{
return $this->search(['pid' => 0, 'is_del' => 0, 'paid' => 1, 'refund_status' => [0, 3]])
->where('add_time', '>=', $start)
->where('add_time', '<', $stop)
->field('sum(pay_price) as price,FROM_UNIXTIME(add_time, \'%m-%d\') as time')
->group("FROM_UNIXTIME(add_time, '%m-%d')")
->order('add_time ASC')->select()->toArray();
}
/**
* 获取当前时间到指定时间的支付订单数 管理员
* @param $start
* @param $stop
* @param int $store_id
* @return mixed
*/
public function chartTimeNumber($start, $stop, int $store_id = 0)
{
return $this->search(['pid' => 0, 'is_del' => 0, 'paid' => 1, 'refund_status' => [0, 3]])
->where('add_time', '>=', $start)
->where('add_time', '<', $stop)
->field('count(id) as num,FROM_UNIXTIME(add_time, \'%m-%d\') as time')
->group("FROM_UNIXTIME(add_time, '%m-%d')")
->order('add_time ASC')->select()->toArray();
}
/**
* 获取用户已购买此活动商品的个数
* @param $uid
* @param $type
* @param $activity_id
* @return int
*/
public function getBuyCount($uid, $type, $activity_id): int
{
return $this->getModel()
->where('uid', $uid)
->where('type', $type)
->where('activity_id', $activity_id)
->whereIn('pid', [0, -1])
->where(function ($query) {
$query->where('paid', 1)->whereOr(function ($query1) {
$query1->where('paid', 0)->where('is_del', 0);
});
})->value('sum(total_num)') ?? 0;
}
/**
* 获取没有支付的订单列表
* @param int $store_id
* @param int $page
* @param int $limit
* @return BaseModel
*/
public function getOrderUnPaid(int $store_id = 0, int $page = 0, int $limit = 0)
{
return $this->getModel()
->where(['pid' => 0, 'paid' => 0, 'is_del' => 0, 'status' => 0, 'refund_status' => 0])
->where('pay_type', '<>', 'offline')
->when($page && $limit, function ($query) use ($page, $limit) {
$query->page($page, $limit);
});
}
/**
* 用户趋势数据
* @param $time
* @param $type
* @param $timeType
* @return mixed
*/
public function getTrendData($time, $type, $timeType, $str)
{
return $this->getModel()->when($type != '', function ($query) use ($type) {
$query->where('channel_type', $type);
})->where('paid', 1)->where('pid', '>=', 0)->where(function ($query) use ($time) {
if ($time[0] == $time[1]) {
$query->whereDay('pay_time', $time[0]);
} else {
$time[1] = date('Y-m-d', strtotime($time[1]) + 86400);
$query->whereTime('pay_time', 'between', $time);
}
})->field("FROM_UNIXTIME(pay_time,'$timeType') as days,$str as num")
->group('days')->select()->toArray();
}
/**
* 用户地域数据
* @param $time
* @param $userType
* @return mixed
*/
public function getRegion($time, $userType)
{
return $this->getModel()->when($userType != '', function ($query) use ($userType) {
$query->where('channel_type', $userType);
})->where('pid', '>=', 0)->where(function ($query) use ($time) {
if ($time[0] == $time[1]) {
$query->whereDay('pay_time', $time[0]);
} else {
$time[1] = date('Y-m-d', strtotime($time[1]) + 86400);
$query->whereTime('pay_time', 'between', $time);
}
})->field('pay_price as payPrice,substring_index(user_address, " ", 1) as province')->select()->toArray();
}
/**
* 商品趋势
* @param $time
* @param $timeType
* @param $field
* @param $str
* @return mixed
*/
public function getProductTrend($time, $timeType, $field, $str, $orderStatus = '', $supplier_id = '')
{
return $this->getModel()->where(function ($query) use ($field, $orderStatus) {
if ($field == 'pay_time') {
$query->where('paid', 1);
} elseif ($field == 'refund_reason_time') {
$query->where('paid', 1)->where('refund_status', '>', 0);
} elseif ($field == 'add_time') {
if ($orderStatus == 'pay') {
$query->where('paid', 1)->where('pid', '>=', 0)->where('refund_status', 0);
} elseif ($orderStatus == 'refund') {
$query->where('paid', 1)->where('pid', '>=', 0)->where('refund_status', '>', 0);
} elseif ($orderStatus == 'coupon') {
$query->where('paid', 1)->where('pid', '>=', 0)->where('coupon_id', '>', 0);
}
}
})->when($supplier_id, function ($query) use ($supplier_id) {
$query->where('supplier_id', $supplier_id);
})->where('pid', '>=', 0)->where(function ($query) use ($time, $field) {
if ($time[0] == $time[1]) {
$query->whereDay($field, $time[0]);
} else {
$time[1] = date('Y-m-d', strtotime($time[1]) + 86400);
$query->whereTime($field, 'between', $time);
}
})->field("FROM_UNIXTIME($field,'$timeType') as days,$str as num")->group('days')->select()->toArray();
}
/**
* 按照支付时间统计支付金额
* @param array $where
* @param string $sumField
* @return mixed
*/
public function getDayTotalMoney(array $where, string $sumField)
{
return $this->search($where)
->when(isset($where['timeKey']), function ($query) use ($where) {
$query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
})
->sum($sumField);
}
/**
* 时间段订单数统计
* @param array $where
* @param string $countField
* @return int
*/
public function getDayOrderCount(array $where, string $countField = "*")
{
return $this->search($where)
->when(isset($where['timeKey']), function ($query) use ($where) {
$query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
})
->count($countField);
}
/**
* 时间分组订单付款金额统计
* @param array $where
* @param string $sumField
* @return mixed
*/
public function getDayGroupMoney(array $where, string $sumField, string $group)
{
return $this->search($where)
->when(isset($where['timeKey']), function ($query) use ($where, $sumField, $group) {
$query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
if ($where['timeKey']['days'] == 1) {
$timeUinx = "%H";
} elseif ($where['timeKey']['days'] == 30) {
$timeUinx = "%Y-%m-%d";
} elseif ($where['timeKey']['days'] == 365) {
$timeUinx = "%Y-%m";
} elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
$timeUinx = "%Y-%m-%d";
} elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
$timeUinx = "%Y-%m";
} else {
$timeUinx = "%Y-%m";
}
$query->field("sum($sumField) as number,FROM_UNIXTIME($group, '$timeUinx') as time");
$query->group("FROM_UNIXTIME($group, '$timeUinx')");
})
->order('pay_time ASC')->select()->toArray();
}
/**
* 时间分组订单数统计
* @param array $where
* @param string $sumField
* @return mixed
*/
public function getOrderGroupCount(array $where, string $sumField = "*")
{
return $this->search($where)
->when(isset($where['timeKey']), function ($query) use ($where, $sumField) {
$query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
if ($where['timeKey']['days'] == 1) {
$timeUinx = "%H";
} elseif ($where['timeKey']['days'] == 30) {
$timeUinx = "%Y-%m-%d";
} elseif ($where['timeKey']['days'] == 365) {
$timeUinx = "%Y-%m";
} elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
$timeUinx = "%Y-%m-%d";
} elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
$timeUinx = "%Y-%m";
} else {
$timeUinx = "%Y-%m";
}
$query->field("count($sumField) as number,FROM_UNIXTIME(pay_time, '$timeUinx') as time");
$query->group("FROM_UNIXTIME(pay_time, '$timeUinx')");
})
->order('pay_time ASC')->select()->toArray();
}
/**
* 时间段支付订单人数
* @param $where
* @return mixed
*/
public function getPayOrderPeople($where)
{
return $this->search($where)
->when(isset($where['timeKey']), function ($query) use ($where) {
$query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
})
->field('uid')
->distinct(true)
->select()->toArray();
}
/**
* 时间段分组统计支付订单人数
* @param $where
* @return mixed
*/
public function getPayOrderGroupPeople($where)
{
return $this->search($where)
->when(isset($where['timeKey']), function ($query) use ($where) {
$query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
if ($where['timeKey']['days'] == 1) {
$timeUinx = "%H";
} elseif ($where['timeKey']['days'] == 30) {
$timeUinx = "%Y-%m-%d";
} elseif ($where['timeKey']['days'] == 365) {
$timeUinx = "%Y-%m";
} elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
$timeUinx = "%Y-%m-%d";
} elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
$timeUinx = "%Y-%m";
} else {
$timeUinx = "%Y-%m";
}
$query->field("count(distinct uid) as number,FROM_UNIXTIME(pay_time, '$timeUinx') as time");
$query->group("FROM_UNIXTIME(pay_time, '$timeUinx')");
})
->order('pay_time ASC')->select()->toArray();
}
/**
* 获取批量打印电子面单数据
* @param array $where
* @param array $ids
* @param string $filed
* @param int $store_id
* @return array
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function getOrderDumpData(array $where, array $ids = [], $filed = "*", int $store_id = 0)
{
$where['pid'] = 0;
$where['status'] = 1;
$where['refund_status'] = 0;
$where['paid'] = 1;
$where['is_del'] = 0;
$where['shipping_type'] = 1;
$where['is_system_del'] = 0;
return $this->search($where)->when($ids, function ($query) use ($ids) {
$query->whereIn('id', $ids);
})->field($filed)->with(['pink'])->select()->toArray();
}
/**
* @param array $where
* @param string $field
* @return array
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function getOrderListByWhere(array $where, $field = "*")
{
return $this->search($where)->field($field)->select()->toArray();
}
/**
* 批量修改订单
* @param array $ids
* @param array $data
* @param string|null $key
* @return BaseModel
*/
public function batchUpdateOrder(array $ids, array $data, ?string $key = null)
{
return $this->getModel()::whereIn(is_null($key) ? $this->getPk() : $key, $ids)->update($data);
}
/**
* 获取拆单之后的子订单
* @param int $id
* @param string $field
* @param int $type 1:不包含自己2包含自己
* @return array
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function getSonOrder(int $id, string $field = '*', int $type = 1)
{
return in_array($type, [1, 2]) ? $this->getModel()::field($field)->when($type, function ($query) use ($id, $type) {
if ($type == 1) {
$query->where('pid', $id);
} else {
$query->where('pid', $id)->whereOr('id', $id);
}
})->select()->toArray() : [];
}
/**
* 查询退款订单
* @param $where
* @param $page
* @param $limit
* @return array
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function getRefundList($where, $page = 0, $limit = 0)
{
$model = $this->getModel()
->where('is_system_del', 0)
->where('paid', 1)
->when(isset($where['store_id']), function ($query) use ($where) {
$query->where('store_id', $where['store_id']);
})->when(isset($where['refund_type']) && $where['refund_type'] !== '', function ($query) use ($where) {
if ($where['refund_type'] == 0) {
$query->where('refund_type', '>', 0);
} else {
$query->where('refund_type', $where['refund_type']);
}
})->when(isset($where['not_pid']), function ($query) {
$query->where('pid', '<>', -1);
})->when($where['order_id'] != '', function ($query) use ($where) {
$query->where('order_id', $where['order_id']);
})->when(is_array($where['refund_reason_time']), function ($query) use ($where) {
$query->whereBetween('refund_reason_time', [strtotime($where['refund_reason_time'][0]), strtotime($where['refund_reason_time'][1]) + 86400]);
})->with(array_merge(['user', 'spread']));
$count = $model->count();
$list = $model->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
$query->page($page, $limit);
})->order('refund_reason_time desc')->select()->toArray();
return compact('list', 'count');
}
/**
* 订单搜索列表
* @param array $where
* @param array $field
* @param int $page
* @param int $limit
* @param array $with
* @param string $order
* @return array
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function getOutOrderList(array $where, array $field, int $page = 0, int $limit = 0, array $with = [], string $order = 'add_time DESC,id DESC'): array
{
return $this->search($where)->field($field)->with($with)->when($page && $limit, function ($query) use ($page, $limit) {
$query->page($page, $limit);
})->order($order)->select()->toArray();
}
/**
* 获取订单统计头部数据
* @param array $where 查询条件
* @param string $datebefor 开始日期
* @param string $dateafter 结束日期
* @param string $timeType 时间类型(week/month/year/30)
* @return array
*/
public function getStatisticsHeader($where, $datebefor, $dateafter, $timeType = "week")
{
return $this->search($where)
->where('add_time', 'between time', [$datebefor, $dateafter])
->when($timeType, function ($query) use ($timeType) {
switch ($timeType) {
case "week" :
$timeUnix = "%m-%d";
break;
case "month" :
$timeUnix = "%d";
break;
case "year" :
$timeUnix = "%m";
break;
case "30" :
$timeUnix = "%m-%d";
break;
default:
$timeUnix = "%m-%d";
break;
}
$query->field("FROM_UNIXTIME(add_time,'$timeUnix') as day,count(*) as count,sum(pay_price) as price");
$query->group("FROM_UNIXTIME(add_time, '$timeUnix')");
})
->order('add_time asc')
->select()->toArray();
}
/**
* 门店线上支付订单详情
* @param int $store_id
* @param int $uid
* @return array|\think\Model|null
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function payCashierOrder(int $store_id, int $uid)
{
return $this->getModel()->where('uid', $uid)->where('paid', 0)->where('is_del', 0)->where('is_system_del', 0)
->where('shipping_type', 4)
->order('add_time desc,id desc')
->find();
}
/**
* 商品趋势
* @param $time
* @param $timeType
* @param $field
* @param $str
* @param $orderStatus
* @return mixed
*/
public function getOrderStatistics($where, $time, $timeType, $field, $str, $orderStatus = '')
{
return $this->getModel()->where($where)->where(function ($query) use ($field, $orderStatus) {
if ($field == 'pay_time') {
$query->where('paid', 1);
} elseif ($field == 'refund_reason_time') {
$query->where('paid', 1)->where('refund_status', '>', 0);
} elseif ($field == 'add_time') {
if ($orderStatus == 'pay') {
$query->where('paid', 1)->where('pid', '>=', 0)->whereIn('refund_status', [0, 3]);
} elseif ($orderStatus == 'refund') {
$query->where('paid', 1)->where('pid', '>=', 0)->where('refund_type', 6);
}
}
})->where(function ($query) use ($time, $field) {
if ($time[0] == $time[1]) {
$query->whereDay($field, $time[0]);
} else {
$time[1] = date('Y-m-d', (!is_numeric($time[1]) ? strtotime($time[1]) : $time[1]) + 86400);
$query->whereTime($field, 'between', $time);
}
})->where('is_del', 0)->where('is_system_del', 0)
->field("FROM_UNIXTIME($field,'$timeType') as days,$str as num")->group('days')->select()->toArray();
}
/**
* @param int $pid
* @return array
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function getSubOrderNotSendList(int $pid)
{
return $this->getModel()->where('pid', $pid)->where('status', 1)->select()->toArray();
}
/**
* @param int $pid
* @param int $order_id
* @return int
* @throws DbException
*/
public function getSubOrderNotSend(int $pid, int $order_id)
{
return $this->getModel()->where('pid', $pid)->where('status', 0)->where('id', '<>', $order_id)->count();
}
/**
* @param int $pid
* @param int $order_id
* @return int
* @throws DbException
*/
public function getSubOrderNotTake(int $pid, int $order_id)
{
return $this->getModel()->where('pid', $pid)->where('status', 1)->where('id', '<>', $order_id)->count();
}
/**
* 获取活动订单列表:
* @param int $id
* @param int $type 0:普通、1秒杀、2:砍价、3:拼团、4:积分、5:套餐、6:预售、7:新人礼、8抽奖
* @param array $where
* @param int $page
* @param int $limit
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function activityStatisticsOrder(int $id, int $type = 1, array $where = [], int $page = 0, int $limit = 0)
{
return $this->search($where)->where('pid', 'in', [0, -1])->where('paid', 1)->where('type', $type)->where('activity_id', $id)
->when($page && $limit, function ($query) use ($page, $limit) {
$query->page($page, $limit);
})->field(['order_id', 'real_name', 'status', 'pay_price', 'total_num', 'add_time', 'pay_time', 'paid', 'shipping_type', 'refund_status', 'is_del', 'is_system_del'])->select()->toArray();
}
/**
* 秒杀参与人统计
* @param int $id
* @param string $keyword
* @param int $page
* @param int $limit
* @return mixed
*/
public function seckillPeople(int $id, string $keyword, int $page = 0, int $limit = 0)
{
return $this->getModel()
->when($id != 0, function ($query) use ($id) {
$query->where('type', 1)->where('activity_id', $id);
})->when($keyword != '', function ($query) use ($keyword) {
$query->where('real_name|uid|user_phone', 'like', '%' . $keyword . '%');
})->where('pid', 'in', [0, -1])->where('paid', 1)->field([
'real_name',
'uid',
'SUM(total_num) as goods_num',
'COUNT(id) as order_num',
'SUM(pay_price) as total_price',
'add_time'
])->group('uid')->order("add_time desc")->when($page && $limit, function ($query) use ($page, $limit) {
$query->page($page, $limit);
})->select()->toArray();
}
/**
* 获取division趋势
* @param $time
* @param $timeType
* @param $divisionId
* @param $field
* @return array
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author wuhaotian
* @email 442384644@qq.com
* @date 2024/8/21
*/
public function getDivisionTrend($time, $timeType, $divisionId, $field)
{
return $this->getModel()
->where('paid', 1)
->where('pid', '>=', 0)
->where(function ($query) use ($divisionId) {
if ($divisionId) {
$query->where('division_id', $divisionId);
} else {
$query->where('division_id', '>', 0);
}
})->where(function ($query) use ($time) {
if ($time[0] == $time[1]) {
$query->whereDay('add_time', $time[0]);
} else {
$time[1] = date('Y-m-d', strtotime($time[1]) + 86400);
$query->whereTime('add_time', 'between', $time);
}
})->field("FROM_UNIXTIME(add_time,'$timeType') as days,$field as num")->group('days')->select()->toArray();
}
/**
* 获取用户订单字段求和
* @param array $uids 用户ID数组
* @param string $field 求和字段
* @return array
*/
public function getUserOrderSum($uids, $field)
{
//查询uids的field字段的和并以uid分组
$res = $this->getModel()->whereIn('uid', $uids)
->whereIn('refund_status', [0, 3])
->where('paid', 1)
->where('is_del', 0)
->where('is_system_del', 0)
->where('pid', '>=', 0)
->field("uid,SUM($field) as num")
->group('uid')->select()->toArray();
//uid为键num为值的一维数组
return array_column($res, 'num', 'uid');
}
}