feat: 修改了WorkbenchLogic逻辑,优化了代码结构,统一了查询条件,并调整了部分方法的参数。

This commit is contained in:
mkm 2024-06-22 10:07:28 +08:00
parent e25fe49941
commit ccdbb687a5
3 changed files with 96 additions and 104 deletions

View File

@ -49,7 +49,7 @@ class WorkbenchController extends BaseAdminController
{ {
$params = $this->request->get(); $params = $this->request->get();
if(!isset($params['store_id']) ||$params['store_id']==''){ if(!isset($params['store_id']) ||$params['store_id']==''){
$params['store_id'] =1; $params['store_id'] =0;
} }
if(!isset($params['start_time']) ||$params['start_time']==''){ if(!isset($params['start_time']) ||$params['start_time']==''){
$time=explode('-', $this->getDay('')); $time=explode('-', $this->getDay(''));

View File

@ -306,9 +306,9 @@ class StoreOrderLogic extends BaseLogic
* @param $extra * @param $extra
* @return float|\think\db\Query * @return float|\think\db\Query
*/ */
public function storeOrderSumByDate($storeId, $start, $end, $extra = [], $field = 'pay_price') public function storeOrderSumByDate($start, $end, $extra = [], $field = 'pay_price')
{ {
return StoreOrder::where('store_id', $storeId)->where('paid', 1)->where($extra)->whereBetweenTime('pay_time', $start, $end)->sum($field); return StoreOrder::where($extra)->whereBetweenTime('pay_time', $start, $end)->sum($field);
} }
/** /**

View File

@ -52,30 +52,33 @@ class WorkbenchLogic extends BaseLogic
$endTime = $params['end_time']; $endTime = $params['end_time'];
$endTime = date('Y-m-d', strtotime($endTime) + 86400); $endTime = date('Y-m-d', strtotime($endTime) + 86400);
$dateDiff = (new \DateTime($endTime))->diff(new \DateTime($startTime)); $dateDiff = (new \DateTime($endTime))->diff(new \DateTime($startTime));
$where = ['paid' => 1];
$cashFinanceWhere = [];
$storeFinanceWhere = [];
if ($params['store_id'] != 0) {
$where['store_id'] = $params['store_id'];
$cashFinanceWhere = ['store_id' => $params['store_id']];
$storeFinanceWhere = ['store_id' => $params['store_id'],'financial_type'=>11,'financial_pm'=>0];
}
$orderLogic = new StoreOrderLogic(); $orderLogic = new StoreOrderLogic();
//订单总金额 //订单总金额
$data['order_amount'] = $orderLogic->storeOrderSumByDate($params['store_id'], $startTime, $endTime); $data['order_amount'] = $orderLogic->storeOrderSumByDate($startTime, $endTime, $where);
//余额支付总金额 //余额支付总金额
$data['balance_amount'] = $orderLogic->storeOrderSumByDate($params['store_id'], $startTime, $endTime, ['pay_type' => PayEnum::BALANCE_PAY]); $data['balance_amount'] = $orderLogic->storeOrderSumByDate($startTime, $endTime, array_merge($where, ['pay_type' => PayEnum::BALANCE_PAY]));
//线下收银总金额 //线下收银总金额
$data['cashier_amount'] = $orderLogic->storeOrderSumByDate($params['store_id'], $startTime, $endTime, ['shipping_type' => 3]); $data['cashier_amount'] = $orderLogic->storeOrderSumByDate($startTime, $endTime, array_merge($where, ['shipping_type' => 3]));
//现金收银总金额 //现金收银总金额
$data['cash_amount'] = StoreCashFinanceFlow::where('store_id', $params['store_id'])->whereBetweenTime('create_time', $startTime, $endTime)->sum('cash_price'); $data['cash_amount'] = StoreCashFinanceFlow::where($cashFinanceWhere)->whereBetweenTime('create_time', $startTime, $endTime)->sum('cash_price');
//核销订单金额 //核销订单金额
$data['verify_amount'] = $orderLogic->storeOrderSumByDate($params['store_id'], $startTime, $endTime, ['shipping_type' => 2]); $data['verify_amount'] = $orderLogic->storeOrderSumByDate($startTime, $endTime, array_merge($where, ['shipping_type' => 2]));
//门店收益金额 //门店收益金额
$data['income_amount'] = $orderLogic->storeOrderSumByDate($params['store_id'], $startTime, $endTime, [], 'profit'); $data['income_amount'] = $orderLogic->storeOrderSumByDate($startTime, $endTime, [], 'profit');
//门店收款金额 //门店收款金额
$data['receipt_amount'] = UserRecharge::where([ $data['receipt_amount'] = UserRecharge::where($where)->sum('price');
'store_id'=>$params['store_id'],
'paid'=>YesNoEnum::YES
])->sum('price');
$data['deposit_amount'] = StoreFinanceFlow::where('store_id', $params['store_id'])->where('financial_type',11)->where('financial_pm',0)->whereBetweenTime('create_time', $startTime, $endTime)->sum('number'); $data['deposit_amount'] = StoreFinanceFlow::where($storeFinanceWhere)->whereBetweenTime('create_time', $startTime, $endTime)->sum('number');
//门店成交用户数 //门店成交用户数
$data['user_number'] = StoreOrder::where('store_id', $params['store_id']) $data['user_number'] = StoreOrder::where($where)
->where('paid', 1)
->whereBetweenTime('pay_time', $startTime, $endTime) ->whereBetweenTime('pay_time', $startTime, $endTime)
->group('uid') ->group('uid')
->count(); ->count();
@ -96,8 +99,7 @@ class WorkbenchLogic extends BaseLogic
$i++; $i++;
} }
$field = 'from_unixtime(pay_time,"%m-%d") as pay_time,sum(pay_price) as pay_price'; $field = 'from_unixtime(pay_time,"%m-%d") as pay_time,sum(pay_price) as pay_price';
} } else {
else {
$group = 'MONTH(pay_time)'; $group = 'MONTH(pay_time)';
$i = 0; $i = 0;
$month = 0; $month = 0;
@ -117,15 +119,13 @@ class WorkbenchLogic extends BaseLogic
$field = 'from_unixtime(pay_time,"%Y-%m") as pay_time,sum(pay_price) as pay_price'; $field = 'from_unixtime(pay_time,"%Y-%m") as pay_time,sum(pay_price) as pay_price';
} }
$orderList = StoreOrder::field($field) $orderList = StoreOrder::field($field)
->where('store_id', $params['store_id']) ->where($where)
->where('paid', 1)
->whereBetweenTime('pay_time', $startTime, $endTime) ->whereBetweenTime('pay_time', $startTime, $endTime)
->group($group) ->group($group)
->select() ->select()
->toArray(); ->toArray();
$userList = StoreOrder::field($field . ',count(uid) as user_num') $userList = StoreOrder::field($field . ',count(uid) as user_num')
->where('store_id', $params['store_id']) ->where($where)
->where('paid', 1)
->whereBetweenTime('pay_time', $startTime, $endTime) ->whereBetweenTime('pay_time', $startTime, $endTime)
->group($group . ',uid') ->group($group . ',uid')
->select() ->select()
@ -155,8 +155,7 @@ class WorkbenchLogic extends BaseLogic
'user_number' => array_values($userListTmp) 'user_number' => array_values($userListTmp)
] ]
]; ];
$data['order_list'] = StoreOrder::with('user')->where('store_id', $params['store_id']) $data['order_list'] = StoreOrder::with('user')->where($where)
->where('paid', 1)
->whereBetweenTime('pay_time', $startTime, $endTime) ->whereBetweenTime('pay_time', $startTime, $endTime)
->order('pay_time', 'desc') ->order('pay_time', 'desc')
->limit(10) ->limit(10)
@ -554,7 +553,7 @@ class WorkbenchLogic extends BaseLogic
{ {
//当日营业额的统计 //当日营业额的统计
$today = StoreOrder::where(['paid'=>YesNoEnum::YES,'store_id'=>$params['store_id']]); $today = StoreOrder::where(['paid' => YesNoEnum::YES, 'store_id' => $params['store_id']]);
$turnover_today = $today $turnover_today = $today
->whereDay('create_time') ->whereDay('create_time')
->sum('pay_price'); ->sum('pay_price');
@ -568,35 +567,34 @@ class WorkbenchLogic extends BaseLogic
->sum('cost'); ->sum('cost');
//当日加到保证金的 //当日加到保证金的
$deposit = StoreFinanceFlow::where(['store_id'=>$params['store_id'],'status'=>YesNoEnum::YES]) $deposit = StoreFinanceFlow::where(['store_id' => $params['store_id'], 'status' => YesNoEnum::YES])
->where('financial_type',OrderEnum::ORDER_MARGIN); ->where('financial_type', OrderEnum::ORDER_MARGIN);
$deposit_today =$deposit $deposit_today = $deposit
->whereDay('create_time')
->sum('number');
//当日的现金收银
$cash_today = StoreCashFinanceFlow::where('store_id',$params['store_id'])
->whereDay('create_time') ->whereDay('create_time')
->where('status',YesNoEnum::YES) ->sum('number');
//当日的现金收银
$cash_today = StoreCashFinanceFlow::where('store_id', $params['store_id'])
->whereDay('create_time')
->where('status', YesNoEnum::YES)
->sum('receivable'); ->sum('receivable');
//总的营业额的统计 总的利润的统计 总的成本合集的统计 总的加到保证金的 //总的营业额的统计 总的利润的统计 总的成本合集的统计 总的加到保证金的
$all = StoreOrder::where(['paid'=>YesNoEnum::YES,'store_id'=>$params['store_id']]); $all = StoreOrder::where(['paid' => YesNoEnum::YES, 'store_id' => $params['store_id']]);
$deposit_all = StoreFinanceFlow::where(['store_id'=>$params['store_id'],'status'=>YesNoEnum::YES]) $deposit_all = StoreFinanceFlow::where(['store_id' => $params['store_id'], 'status' => YesNoEnum::YES])
->sum('number'); ->sum('number');
$cash_all = StoreCashFinanceFlow::where('store_id',$params['store_id']) $cash_all = StoreCashFinanceFlow::where('store_id', $params['store_id'])
->where('status',YesNoEnum::YES) ->where('status', YesNoEnum::YES)
->sum('receivable'); ->sum('receivable');
if(isset($params['month']) && $params['month']){ if (isset($params['month']) && $params['month']) {
$all = StoreOrder::where(['paid'=>YesNoEnum::YES,'store_id'=>$params['store_id']]) $all = StoreOrder::where(['paid' => YesNoEnum::YES, 'store_id' => $params['store_id']])
->whereMonth('create_time', $params['month']) ->whereMonth('create_time', $params['month']);
; $deposit_all = SystemStore::where('id', $params['store_id'])
$deposit_all = SystemStore::where('id',$params['store_id'])
->whereMonth('create_time', $params['month']) ->whereMonth('create_time', $params['month'])
->value('paid_deposit'); ->value('paid_deposit');
$cash_all = StoreCashFinanceFlow::where('store_id',$params['store_id']) $cash_all = StoreCashFinanceFlow::where('store_id', $params['store_id'])
->where('status',YesNoEnum::YES) ->where('status', YesNoEnum::YES)
->whereMonth('create_time', $params['month']) ->whereMonth('create_time', $params['month'])
->sum('receivable'); ->sum('receivable');
} }
@ -605,32 +603,31 @@ class WorkbenchLogic extends BaseLogic
$profit_all = $all $profit_all = $all
->sum('profit'); ->sum('profit');
//消耗余额 V2.0 //消耗余额 V2.0
$cost_all = CapitalFlow:: $cost_all = CapitalFlow::where(['category' => 'user_order_balance_pay', 'store_id' => $params['store_id']])
where(['category'=>'user_order_balance_pay','store_id'=>$params['store_id']])
->sum('amount'); ->sum('amount');
$time = self::getLastSevenDays(); $time = self::getLastSevenDays();
$newArr = []; $newArr = [];
foreach ($time as $value){ foreach ($time as $value) {
$data = self::dealSearch($params['store_id'],$value); $data = self::dealSearch($params['store_id'], $value);
$newArr[$value] = $data; $newArr[$value] = $data;
} }
return [ return [
'today'=>[ 'today' => [
'turnover_today'=>$turnover_today, 'turnover_today' => $turnover_today,
'profit_today'=>$profit_today, 'profit_today' => $profit_today,
'cost_today'=>$cost_today, 'cost_today' => $cost_today,
'deposit_today'=>$deposit_today, 'deposit_today' => $deposit_today,
'cash_today'=>$cash_today, 'cash_today' => $cash_today,
], ],
'all'=>[ 'all' => [
'turnover_all'=>$turnover_all, 'turnover_all' => $turnover_all,
'profit_all'=>$profit_all, 'profit_all' => $profit_all,
'cost_all'=>$cost_all, 'cost_all' => $cost_all,
'deposit_all'=>$deposit_all, 'deposit_all' => $deposit_all,
'cash_all'=>$cash_all, 'cash_all' => $cash_all,
], ],
'time'=>$newArr 'time' => $newArr
]; ];
@ -665,7 +662,6 @@ class WorkbenchLogic extends BaseLogic
->where('status',YesNoEnum::YES); ->where('status',YesNoEnum::YES);
$cash_count = $cash->count(); $cash_count = $cash->count();
$cash_receipt = $cash->sum('receipts');*/ $cash_receipt = $cash->sum('receipts');*/
} }
@ -684,11 +680,11 @@ class WorkbenchLogic extends BaseLogic
} }
public static function dealSearch($store_id,$startTime) public static function dealSearch($store_id, $startTime)
{ {
$endTime = date('Y-m-d', strtotime($startTime) + 86400); $endTime = date('Y-m-d', strtotime($startTime) + 86400);
//当日营业额的统计 当日利润的统计 当日成本合集的统计 当日加到保证金的 当日的现金收银 //当日营业额的统计 当日利润的统计 当日成本合集的统计 当日加到保证金的 当日的现金收银
$today = StoreOrder::where(['paid'=>YesNoEnum::YES,'store_id'=>$store_id]); $today = StoreOrder::where(['paid' => YesNoEnum::YES, 'store_id' => $store_id]);
$turnover_today = $today $turnover_today = $today
->whereBetweenTime('create_time', $startTime, $endTime) ->whereBetweenTime('create_time', $startTime, $endTime)
->sum('pay_price'); ->sum('pay_price');
@ -699,33 +695,32 @@ class WorkbenchLogic extends BaseLogic
->whereBetweenTime('create_time', $startTime, $endTime) ->whereBetweenTime('create_time', $startTime, $endTime)
->sum('cost'); ->sum('cost');
$deposit = StoreFinanceFlow::where(['store_id'=>$store_id]) $deposit = StoreFinanceFlow::where(['store_id' => $store_id])
->where('financial_type',OrderEnum::ORDER_MARGIN); ->where('financial_type', OrderEnum::ORDER_MARGIN);
$deposit_today =$deposit $deposit_today = $deposit
->whereBetweenTime('create_time', $startTime, $endTime) ->whereBetweenTime('create_time', $startTime, $endTime)
->sum('number'); ->sum('number');
$cash_today = StoreCashFinanceFlow::where('store_id',$store_id) $cash_today = StoreCashFinanceFlow::where('store_id', $store_id)
->whereBetweenTime('create_time', $startTime, $endTime) ->whereBetweenTime('create_time', $startTime, $endTime)
->where('status',YesNoEnum::YES) ->where('status', YesNoEnum::YES)
->sum('receipts'); ->sum('receipts');
return [ return [
'turnover_today'=>$turnover_today, 'turnover_today' => $turnover_today,
'profit_today'=>$profit_today, 'profit_today' => $profit_today,
'cost_today'=>$cost_today, 'cost_today' => $cost_today,
'deposit_today'=>$deposit_today, 'deposit_today' => $deposit_today,
'cash_today'=>$cash_today, 'cash_today' => $cash_today,
]; ];
} }
public static function rechargeData($params) public static function rechargeData($params)
{ {
$data['receipt_amount'] = UserRecharge::where([ $data['receipt_amount'] = UserRecharge::where([
'store_id'=>$params['store_id'], 'store_id' => $params['store_id'],
'paid'=>YesNoEnum::YES 'paid' => YesNoEnum::YES
])->sum('price'); ])->sum('price');
return $data??[]; return $data ?? [];
} }
@ -750,32 +745,31 @@ class WorkbenchLogic extends BaseLogic
"series" => [ "series" => [
[ [
"name" => "商品浏览量", "name" => "商品浏览量",
"data" => self::store_visit_count($dates,$store_id), "data" => self::store_visit_count($dates, $store_id),
"type" => "line", "type" => "line",
"smooth" => "true", "smooth" => "true",
"yAxisIndex" => 1 "yAxisIndex" => 1
], ],
[ [
"name" => "商品访客量", "name" => "商品访客量",
"data" => self::store_visit_user($dates,$store_id), "data" => self::store_visit_user($dates, $store_id),
"type" => "line", "type" => "line",
"smooth" => "true", "smooth" => "true",
"yAxisIndex" => 1 "yAxisIndex" => 1
], ],
[ [
"name" => "支付金额", "name" => "支付金额",
"data" => self::payPrice($dates,$store_id), "data" => self::payPrice($dates, $store_id),
"type" => "bar" "type" => "bar"
], ],
[ [
"name" => "退款金额", "name" => "退款金额",
"data" => self::refundPrice($dates,$store_id), "data" => self::refundPrice($dates, $store_id),
"type" => "bar" "type" => "bar"
] ]
] ]
]; ];
return $data; return $data;
} }
@ -783,11 +777,11 @@ class WorkbenchLogic extends BaseLogic
/** /**
* 商品浏览量 * 商品浏览量
*/ */
public static function store_visit_count($dates,$store_id) public static function store_visit_count($dates, $store_id)
{ {
$data = []; $data = [];
foreach ($dates as $date) { foreach ($dates as $date) {
$data[] = StoreVisit::whereDay('create_time', $date)->where('store_id',$store_id)->cache('store_visit_count_' . $date, 300)->sum('count'); $data[] = StoreVisit::whereDay('create_time', $date)->where('store_id', $store_id)->cache('store_visit_count_' . $date, 300)->sum('count');
} }
return $data; return $data;
} }
@ -796,11 +790,11 @@ class WorkbenchLogic extends BaseLogic
/** /**
* 商品浏览量 * 商品浏览量
*/ */
public static function store_visit_user($dates,$store_id) public static function store_visit_user($dates, $store_id)
{ {
$data = []; $data = [];
foreach ($dates as $date) { foreach ($dates as $date) {
$data[] = StoreVisit::whereDay('create_time', $date)->where('store_id',$store_id)->cache('store_visit_user_' . $date, 300)->count('uid'); $data[] = StoreVisit::whereDay('create_time', $date)->where('store_id', $store_id)->cache('store_visit_user_' . $date, 300)->count('uid');
} }
return $data; return $data;
} }
@ -809,11 +803,11 @@ class WorkbenchLogic extends BaseLogic
/** /**
* 支付金额 * 支付金额
*/ */
public static function payPrice($dates,$store_id) public static function payPrice($dates, $store_id)
{ {
$data = []; $data = [];
foreach ($dates as $date) { foreach ($dates as $date) {
$data[] = StoreOrder::whereDay('create_time', $date)->where('store_id',$store_id)->cache('payPrice_' . $date, 300)->where('paid', 1)->where('refund_status', 0)->sum('pay_price'); $data[] = StoreOrder::whereDay('create_time', $date)->where('store_id', $store_id)->cache('payPrice_' . $date, 300)->where('paid', 1)->where('refund_status', 0)->sum('pay_price');
} }
return $data; return $data;
} }
@ -822,11 +816,11 @@ class WorkbenchLogic extends BaseLogic
/** /**
* 退款金额 * 退款金额
*/ */
public static function refundPrice($dates,$store_id) public static function refundPrice($dates, $store_id)
{ {
$data = []; $data = [];
foreach ($dates as $date) { foreach ($dates as $date) {
$data[] = StoreOrder::whereDay('create_time', $date)->where('store_id',$store_id)->where('status', 'in', [-1, -2])->cache('refundPrice_' . $date, 300)->where('paid', 1)->sum('pay_price'); $data[] = StoreOrder::whereDay('create_time', $date)->where('store_id', $store_id)->where('status', 'in', [-1, -2])->cache('refundPrice_' . $date, 300)->where('paid', 1)->sum('pay_price');
} }
return $data; return $data;
} }
@ -836,19 +830,19 @@ class WorkbenchLogic extends BaseLogic
/** /**
* 获取商品排名数据 * 获取商品排名数据
*/ */
public static function get_product_ranking($where,$store_id) public static function get_product_ranking($where, $store_id)
{ {
return self::product_ranking($where,$store_id); return self::product_ranking($where, $store_id);
} }
public static function product_ranking($where,$store_id) public static function product_ranking($where, $store_id)
{ {
$time = explode('-', $where['create_time']); $time = explode('-', $where['create_time']);
$time = [strtotime($time[0]), strtotime($time[1])]; $time = [strtotime($time[0]), strtotime($time[1])];
$list = StoreProductLog::whereBetweenTime('create_time',$time[0],$time[1]) $list = StoreProductLog::whereBetweenTime('create_time', $time[0], $time[1])
->where('store_id',$store_id) ->where('store_id', $store_id)
->field([ ->field([
'store_id', 'store_id',
'product_id', 'product_id',
@ -863,21 +857,19 @@ class WorkbenchLogic extends BaseLogic
'SUM(collect_num) as collect', 'SUM(collect_num) as collect',
'ROUND((COUNT(distinct(pay_uid))-1)/COUNT(distinct(uid)),2) as changes', 'ROUND((COUNT(distinct(pay_uid))-1)/COUNT(distinct(uid)),2) as changes',
'COUNT(distinct(pay_uid))-1 as repeats' 'COUNT(distinct(pay_uid))-1 as repeats'
])->group('product_id')->order('pay' , ' desc')->limit(20)->select()->toArray(); ])->group('product_id')->order('pay', ' desc')->limit(20)->select()->toArray();
foreach ($list as $key => &$item) { foreach ($list as $key => &$item) {
$find=StoreProduct::where('id',$item['product_id'])->field('store_name,image')->find(); $find = StoreProduct::where('id', $item['product_id'])->field('store_name,image')->find();
$item['store_name']=$find['store_name']; $item['store_name'] = $find['store_name'];
$item['image']=$find['image']; $item['image'] = $find['image'];
if ($item['profit'] == null) $item['profit'] = 0; if ($item['profit'] == null) $item['profit'] = 0;
if ($item['changes'] == null) $item['changes'] = 0; if ($item['changes'] == null) $item['changes'] = 0;
if ($item['repeats'] == null) { if ($item['repeats'] == null) {
$item['repeats'] = 0; $item['repeats'] = 0;
} else { } else {
$item['repeats'] = bcdiv(count(StoreProductLog::where($where)->where('type', 'pay')->where('product_id', $item['product_id'])->where('store_id',$store_id)->field('count(pay_uid) as p')->group('pay_uid')->having('p>1')->select()), $item['repeats'], 2); $item['repeats'] = bcdiv(count(StoreProductLog::where($where)->where('type', 'pay')->where('product_id', $item['product_id'])->where('store_id', $store_id)->field('count(pay_uid) as p')->group('pay_uid')->having('p>1')->select()), $item['repeats'], 2);
} }
} }
return array_merge($list); return array_merge($list);
} }
} }