dlh_test_server/app/admin/logic/finance/OrderLogic.php

351 lines
11 KiB
PHP

<?php
namespace app\admin\logic\finance;
use app\common\basics\Logic;
use app\common\enum\PayEnum;
use app\common\enum\WithdrawEnum;
use app\common\model\LocalStoreOrder;
use app\common\model\order\Order;
use app\common\model\user\UserLevel;
use app\common\model\WithdrawApply;
use app\common\server\UrlServer;
use app\common\model\user\User;
/**
* 订单管理-逻辑
* Class GoodsLogic
* @package app\shop\logic\goods
*/
class OrderLogic extends Logic
{
/**
* @notes 订单统计
* @param array $get
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author suny
* @date 2021/7/14 10:04 上午
*/
public static function statistics($get = [], $is_export = false)
{
$order = new Order();
$where = [];
$page = $get['page'] ?? 1;
$limit = $get['limit'] ?? 10;
//订单搜素
if (!empty($get['search_key']) && !empty($get['keyword'])) {
$keyword = $get['keyword'];
switch ($get['search_key']) {
case 'order_sn':
$where[] = ['o.order_sn', '=', $keyword];
break;
case 'user_sn':
$where[] = ['u.sn', '=', $keyword];
break;
case 'shop_name':
$where[] = ['s.name', 'like', '%' . $keyword . '%'];
break;
case 'goods_name':
$where[] = ['g.goods_name', 'like', '%' . $keyword . '%'];
break;
case 'user_id':
$where[] = ['o.user_id', '=', $keyword];
break;
case 'nickname':
$where[] = ['u.nickname', 'like', '%' . $keyword . '%'];
break;
case 'user_mobile':
$where[] = ['u.mobile', '=', $keyword];
break;
case 'consignee':
$where[] = ['consignee', '=', $keyword];
break;
case 'consignee_mobile':
$where[] = ['o.mobile', '=', $keyword];
break;
}
}
//商家名称
if (isset($get['shop_name']) && $get['shop_name'] != '') {
$where[] = ['s.name', 'like', '%' . $get['shop_name'] . '%'];
}
//商品名称
if (isset($get['goods_name']) && $get['goods_name'] != '') {
$where[] = ['g.goods_name', 'like', '%' . $get['goods_name'] . '%'];
}
//配送方式
if (isset($get['delivery_type']) && $get['delivery_type'] != '') {
$where[] = ['o.delivery_type', '=', $get['delivery_type']];
}
//订单类型
if (isset($get['order_type']) && $get['order_type'] != '') {
$where[] = ['o.order_type', '=', $get['order_type']];
}
//付款方式
if (isset($get['pay_way']) && $get['pay_way'] != '') {
$where[] = ['o.pay_way', '=', $get['pay_way']];
}
//订单来源
if (isset($get['order_source']) && $get['order_source'] != '') {
$where[] = ['o.order_source', '=', $get['order_source']];
}
//下单时间
if (isset($get['start_time']) && $get['start_time'] != '') {
$where[] = ['o.create_time', '>=', strtotime($get['start_time'])];
}
if (isset($get['end_time']) && $get['end_time'] != '') {
$where[] = ['o.create_time', '<=', strtotime($get['end_time'])];
}
if (isset($get['page']) && $get['page'] != '') {
$page = $get['page'];
}
if (isset($get['limit']) && $get['limit'] != '') {
$limit = $get['limit'];
}
// 导出excel
if (true === $is_export) {
return self::export($where);
}
$field = 'o.*,s.name as shop_name,u.level';
$count = $order
->alias('o')
->field($field)
->join('shop s', 's.id = o.shop_id')
->join('user u', 'u.id = o.user_id')
->join('order_goods g', 'g.order_id = o.id')
->with(['order_goods', 'user', 'shop'])
->where($where)
->where('o.pay_status', '>', PayEnum::UNPAID)
->group('o.id')
->count();
$lists = $order
->alias('o')
->field($field)
->join('shop s', 's.id = o.shop_id')
->join('user u', 'u.id = o.user_id')
->join('order_goods g', 'g.order_id = o.id')
->with(['order_goods', 'user', 'shop'])
->where($where)
->where('o.pay_status', '>', PayEnum::UNPAID)
->append([
'delivery_address', 'pay_status_text', 'order_source_text',
'order_status_text', 'delivery_type_text', 'order_type_text',
'pay_way_text',
])
->page($page, $limit)
->order('o.id desc')
->group('o.id')
->select();
$user_level = UserLevel::where(['del' => 0])->column('name', 'id');
foreach ($lists as $list) {
$list['pay_time'] = $list['pay_time'] == '0' ? '未支付' : date('Y-m-d H:i:s', $list['pay_time']);
$list['user']['avatar'] = UrlServer::getFileUrl($list['user']['avatar']);
$list['shop']['logo'] = UrlServer::getFileUrl($list['shop']['logo']);
foreach ($list['order_goods'] as $order_good) {
$order_good['image'] = UrlServer::getFileUrl($order_good['image']);
}
// 会员等级
$list['user_level'] = '暂无等级';
if (isset($user_level[$list['level']])) {
$list['user_level'] = $user_level[$list['level']];
}
}
return ['count' => $count, 'lists' => $lists];
}
/**
* @notes 拼装统计信息
* @param $order_status
* @return array
* @author suny
* @date 2021/7/14 10:08 上午
*/
public static function getStat($order_status)
{
foreach ($order_status as $key => $value) {
$num = Order::where('order_status', $key)->count('id');
$stat = $value . "(" . $num . ")";
$data[] = $stat;
}
return $data;
}
/**
* @notes 获取各列表数量
* @return int
* @author suny
* @date 2021/7/14 10:08 上午
*/
public static function getAll()
{
$all = Order::where([
['pay_status', '>', PayEnum::UNPAID]
])
->count('id');
return $all;
}
public static function getLocalAll(){
$localAll = LocalStoreOrder::where([
'local_our_company'=>0,
'state'=>1
])->count();
return $localAll;
}
public static function localLists($get){
$page = $get['page'] ?? 1;
$limit = $get['limit'] ?? 10;
$where = [];
// 会员信息
if (!empty($get['search_key']) && !empty($get['keyword'])) {
$keyword = $get['keyword'];
if ($get['search_key'] == 'user_sn') {
$where[] = ['u.sn', '=', $keyword];
} elseif ($get['search_key'] == 'nickname') {
$where[] = ['u.nickname', 'like', '%' . $keyword . '%'];
}
}
//订单编号
if (isset($get['order_no']) && $get['order_no'] != '') {
$where[] = ['w.order_no', 'like', '%' . $get['order_no']. '%'];
}
//商家名称
if (isset($get['shopName']) && $get['shopName'] != '') {
$where[] = ['s.shopName', 'like', '%' . $get['shopName']. '%'];
}
//支付方式
if (isset($get['pay_way']) && $get['pay_way'] != '') {
$where[] = ['w.pay_way', '=', $get['pay_way']];
}
if (isset($get['start_time']) && $get['start_time'] && isset($get['end_time']) && $get['end_time']) {
$where[] = ['w.create_time', 'between', [$get['start_time'], $get['end_time']]];
}
$lists = LocalStoreOrder::alias('w')
->field('w.*, u.nickname,u.avatar, u.sn as user_sn, u.mobile,p.transaction_id,s.store_nickname,s.shopName,g.title,g.price')
->leftJoin('user u', 'u.id = w.user_id')
->leftJoin('local_pay p','w.order_no = p.order_sn')
->leftJoin('local_store s','p.local_id = s.lstore_id')
->leftJoin('local_store_goods g','p.goods_id = g.id')
->where($where)
->where([
'w.local_our_company' => 0,
'w.state'=>1
])
->page($page, $limit)
->order('w.id desc')
->select();
$count = LocalStoreOrder::alias('w')
->field('w.*, u.nickname,u.avatar, u.sn as user_sn, u.mobile,p.transaction_id,s.store_nickname,s.shopName,g.title,g.price')
->leftJoin('user u', 'u.id = w.user_id')
->leftJoin('local_pay p','w.order_no = p.order_sn')
->leftJoin('local_store s','p.local_id = s.lstore_id')
->leftJoin('local_store_goods g','p.goods_id = g.id')
->where($where)
->where([
'w.local_our_company' => 0,
'w.state'=>1
])
->order('w.id desc')
->count();
foreach ($lists as &$item) {
$item['avatar'] = UrlServer::getFileUrl($item['avatar']);
$item['pay_way' ] = ($item['pay_way'] = 0)?'微信':'余额';
}
return ['count' => $count, 'lists' => $lists];
}
public static function userMoneyLists($get){
$page = $get['page'] ?? 1;
$limit = $get['limit'] ?? 10;
$where = [];
// 会员信息
if (!empty($get['search_key']) && !empty($get['keyword'])) {
$keyword = $get['keyword'];
if ($get['search_key'] == 'user_sn') {
$where[] = ['u.sn', '=', $keyword];
} elseif ($get['search_key'] == 'nickname') {
$where[] = ['u.nickname', 'like', '%' . $keyword . '%'];
}
}
$lists = User::where('del','0')
->field('id,nickname,avatar, sn as user_sn, mobile,user_money,earnings')
->where($where)
->page($page, $limit)
->select();
$count = User::where('del','0')
->field('id,nickname,avatar, sn as user_sn, mobile,user_money,earnings')
->where($where)
->count();
foreach ($lists as &$item){
$withdraw = WithdrawApply::where('user_id',$item['id'])->select();
$item['avatar'] = UrlServer::getFileUrl($item['avatar']);
$item['withdraw_way'] =0;
$item['withdraw_yet'] =0;
if(isset($withdraw)){
foreach ($withdraw as $key=>$value){
if($value['status'] == 2){
$item['withdraw_way'] += $value['money'];
}
if($value['status'] == 3){
$item['withdraw_yet'] += $value['money'];
}
}
}
}
return ['count' => $count, 'lists' => $lists];
}
}