123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 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;
- /**
- * 订单
- * Class StoreOrderDao
- * @package app\dao\order
- */
- class StoreOrderDao extends BaseDao
- {
- /**
- * 限制精确查询字段
- * @var string[]
- */
- protected $withField = ['uid', 'order_id', 'real_name', 'user_phone', 'title', 'total_num'];
- /**
- * @return string
- */
- protected function setModel(): string
- {
- return StoreOrder::class;
- }
- /**
- * 订单搜索
- * @param array $where
- * @return \crmeb\basic\BaseModel|mixed|\think\Model
- */
- public function search(array $where = [])
- {
- 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)->when($isDel, function ($query) use ($where) {
- $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['is_coupon']), function ($query) {
- $query->where('coupon_id', '>', 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, 4])->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)->whereIn('status', [1, 5])->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', 5);
- break;
- case 5://核销订单
- $query->where('shipping_type', 2);
- break;
- case 6://收银台订单
- $query->where('shipping_type', 4);
- break;
- case 7://配送订单
- $query->whereIn('shipping_type', [1, 3]);
- break;
- case 8://预售
- $query->where('type', 6);
- break;
- case 9://新人专享
- $query->where('type', 7);
- break;
- case 10://抽奖
- $query->where('type', 8);
- break;
- case 11://拼单
- $query->where('type', 9);
- break;
- case 12://桌码
- $query->where('type', 10);
- break;
- }
- })->when(isset($where['order_type']) && $where['order_type'] !== '', function ($query) use ($where) {
- switch ($where['order_type']) {
- case 5://核销订单
- $query->where('shipping_type', 2);
- break;
- case 6://收银台订单
- $query->where('shipping_type', 4);
- break;
- case 7://配送订单
- $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;
- }
- })->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|verify_code', '%' . $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->where('refund_type', 'in', '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;
- }
- })->when(isset($where['is_refund']) && $where['is_refund'] !== '', function ($query) use ($where) {
- if ($where['is_refund'] == 1) {
- $query->where('refund_status', 2);
- } else {
- $query->where('refund_status', 0);
- }
- })->when(isset($where['area']) && !empty($where['area']), function ($query) use ($where) {
- if (is_array($where['area'])) {
- if (($where['area']['order_province'] ?? '') != '') {
- $query->where('order_province', $where['area']['order_province']);
- if (($where['area']['order_city'] ?? '') != '') {
- $query->where('order_city', $where['area']['order_city']);
- if (($where['area']['order_district'] ?? '') != '') {
- $query->where('order_district', $where['area']['order_district']);
- }
- }
- }
- }
- });
- }
- /**
- * 获取某一个月订单数量
- * @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 \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\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 mixed
- */
- 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 \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getOrderList(array $where, array $field, int $page = 0, int $limit = 0, array $with = [], string $order = 'add_time DESC,id DESC')
- {
- return $this->search($where)->field($field)->with(array_merge(['user', 'spread', 'refund'], $with))->when($page && $limit, function ($query) use ($page, $limit) {
- $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 \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\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 = "%Y-%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 = "%Y-%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 \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\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])->where('store_id', $store_id)->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'])->where('store_id', $store_id)->sum('pay_price');
- }
- /**
- * 获取特定时间内订单量
- * @param $time
- * @return int
- */
- public function totalOrderCount($time)
- {
- return $this->search(['pid' => 0, 'time' => $time ?: 'today', 'timeKey' => 'add_time'])->where('paid', 1)->whereIn('refund_status', [0, 3])->where('store_id', 0)->count();
- }
- /**
- * 获取订单详情
- * @param string $key
- * @param int $uid
- * @param array $with
- * @return array|\think\Model|null
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getUserOrderDetail(string $key, int $uid, array $with = [])
- {
- return $this->getOne(['order_id|id|unique' => $key, 'uid' => $uid, 'is_del' => 0], '*', $with);
- }
- /**
- * 获取用户推广订单
- * @param array $where
- * @param string $field
- * @param int $page
- * @param int $limit
- * @param array $with
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\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], 'is_system_del' => 0])
- ->where('store_id', $store_id)
- ->where('add_time', '>=', $start)
- ->where('add_time', '<', $stop)
- ->field('sum(pay_price) as num,FROM_UNIXTIME(add_time, \'%Y-%m-%d\') as time')
- ->group("FROM_UNIXTIME(add_time, '%Y-%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('store_id', $store_id)
- ->where('add_time', '>=', $start)
- ->where('add_time', '<', $stop)
- ->field('count(id) as num,FROM_UNIXTIME(add_time, \'%Y-%m-%d\') as time')
- ->group("FROM_UNIXTIME(add_time, '%Y-%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 \crmeb\basic\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('store_id', $store_id)
- ->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('store_id', 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
- * @param string $orderStatus
- * @param null $area
- * @return mixed
- */
- public function getProductTrend($time, $timeType, $field, $str, $orderStatus = '', $area = null)
- {
- 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);
- }
- }
- })->where('pid', '>=', 0)->where('store_id', 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);
- }
- })->where(function ($query) use ($area) {
- if (is_array($area)) {
- if (($area['order_province'] ?? '') != '') {
- $query->where('order_province', $area['order_province']);
- if (($area['order_city'] ?? '') != '') {
- $query->where('order_city', $area['order_city']);
- if (($area['order_district'] ?? '') != '') {
- $query->where('order_district', $area['order_district']);
- }
- }
- }
- }
- })->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 \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\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;
- $where['store_id'] = $store_id;
- 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 \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\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 \crmeb\basic\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 \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\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 \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\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 int $store_id
- * @param int $uid
- * @return array|\think\Model|null
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function payCashierOrder(int $store_id, int $uid)
- {
- return $this->getModel()->where('uid', $uid)->where('store_id', $store_id)->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 $id
- * @param int $type 0:普通、1:秒杀、2:砍价、3:拼团、4:积分、5:套餐、6:预售、7:新人礼
- * @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();
- }
- /**
- * @param int $pid
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\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 \think\db\exception\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 \think\db\exception\DbException
- */
- public function getSubOrderNotTake(int $pid, int $order_id)
- {
- return $this->getModel()->where('pid', $pid)->where('status', 1)->where('id', '<>', $order_id)->count();
- }
- }
|