323 lines
12 KiB
PHP
Executable File
323 lines
12 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use App\Admin\Forms\OrderCancelForm;
|
|
use App\Admin\Forms\ShopBalanceForm;
|
|
use App\Enums\FadanType;
|
|
use App\Enums\LottState;
|
|
use App\Enums\LottType;
|
|
use App\Enums\OptimizeType;
|
|
|
|
use App\Enums\OrderType;
|
|
use App\Enums\PayState;
|
|
use App\Enums\SellerLevel;
|
|
use App\Model\Customer\Customer;
|
|
use App\Model\Lottery;
|
|
use App\Model\LotteryType;
|
|
use App\Model\Order;
|
|
use App\Model\Seller\Seller;
|
|
use App\Model\Zq\JczqOdds;
|
|
use App\Model\Zq\JczqResult;
|
|
use App\Service\JczqService;
|
|
use App\Utils\Helps;
|
|
use Dcat\Admin\Admin;
|
|
use Dcat\Admin\Grid;
|
|
use Dcat\Admin\Form;
|
|
use App\Model\Seller\Shop;
|
|
use App\Model\ShopAgent;
|
|
|
|
use App\Admin\Forms\ShopAgentForm;
|
|
|
|
use Dcat\Admin\Http\Controllers\AdminController;
|
|
use Dcat\Admin\Layout\Content;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
|
|
|
class OrderController extends AdminController
|
|
{
|
|
use ControllerTrait;
|
|
protected $title = '订单管理';
|
|
|
|
/**
|
|
* Make a grid builder.
|
|
*
|
|
* @return Grid
|
|
*/
|
|
protected function grid()
|
|
{
|
|
return Grid::make(Order::class, function (Grid $grid) {
|
|
$grid->model()->with([
|
|
'materials:id,order_id,path',
|
|
'lotteryType:id,type,name',
|
|
'customer:id,nickname,name,avatar,phone',
|
|
'receiver:id,nickname,name,shop_id',
|
|
'receiver.shop:id,name,shop_sn',
|
|
'shop:id,agent_seller_id',
|
|
'shop.agentSeller:id,name,nickname',
|
|
])->orderBy('id', 'desc');
|
|
|
|
$grid->column('id', '订单ID');
|
|
$grid->column('order_sn', '订单编号')->modal(function (Grid\Displayers\Modal $modal){
|
|
|
|
// 标题
|
|
$modal->title('取消订单');
|
|
// 自定义图标
|
|
$modal->icon('feather icon-edit');
|
|
// 传递当前行字段值
|
|
return OrderCancelForm::make()->payload(['id' => $this->id]);
|
|
});
|
|
$grid->column('fadan_type', '发单类型')->display(function() {
|
|
if ($this->type != OrderType::FADAN) {
|
|
return '-';
|
|
}
|
|
return FadanType::getDescription($this->fadan_type);
|
|
});
|
|
|
|
$grid->column('money1', '订单金额')->display(function () {
|
|
|
|
return $this->getPayMoney();
|
|
});
|
|
$grid->column('status', '订单状态')->display(function () {
|
|
return LottState::getDescription($this->lottery_state);
|
|
});
|
|
$grid->column('piao', '票据')->display(function ($pictures) {
|
|
$arr = $this->materials->toArray();
|
|
return array_column($arr, 'path');
|
|
})->image(env('APP_FILE_URL'), 100, 100);
|
|
|
|
$grid->column('customer.nickname', '彩民');
|
|
$grid->column('optimize_type', '奖金优化')->display(function () {
|
|
return OptimizeType::getDescription($this->optimize_type);
|
|
});
|
|
|
|
$grid->column('customer.phone', '彩民手机号');
|
|
|
|
$grid->column('shop.agentSeller.id', '代理人')->display(function() {
|
|
return $this->shop->agentSeller ? $this->shop->agentSeller->nickname : '-';
|
|
});
|
|
$grid->column('receiver.nickname', '接单人');
|
|
$grid->column('receiver.shop.name', '接单店铺名');
|
|
$grid->column('receiver.shop.shop_sn', '接单店铺编号');
|
|
|
|
$grid->column('created_at', '创建时间');
|
|
$grid->fixColumns(-2);
|
|
$grid->filter(function ($filter) {
|
|
$agentSellers = Seller::where('level', SellerLevel::MASTER)->pluck('nickname', 'id');
|
|
$filter->panel();
|
|
$filter->like('order_sn', '订单编号');
|
|
$filter->equal('draft_shop_id', '出票店铺编号')->select(Shop::pluck('shop_sn', 'id'));
|
|
$agentSellers = Seller::where('level', SellerLevel::MASTER)->pluck('nickname', 'id');
|
|
$filter->equal('shop.agentSeller.id', '代理人')->select($agentSellers);
|
|
$filter->equal('fadan_type', '发单类型')->select(FadanType::typeAsArray());
|
|
$filter->between('created_at', '创建时间')->datetime();
|
|
});
|
|
|
|
$grid->disableViewButton();
|
|
$grid->disableEditButton();
|
|
$grid->disableDeleteButton();
|
|
$grid->scrollbarX();//数据展开
|
|
// $grid->disableActions();
|
|
// $grid->disableRowSelector();
|
|
});
|
|
}
|
|
|
|
protected function form()
|
|
{
|
|
return Form::make(new Order(), function (Form $form) {
|
|
|
|
$form->divider();
|
|
$form->saving(function ($form) {
|
|
|
|
});
|
|
$form->footer(function ($footer) {
|
|
// 去掉`查看`checkbox
|
|
$footer->disableViewCheck();
|
|
// 去掉`继续编辑`checkbox
|
|
$footer->disableEditingCheck();
|
|
});
|
|
});
|
|
}
|
|
|
|
|
|
public function shopOrderMoney(Request $request)
|
|
{
|
|
|
|
$month = $request->input('month');
|
|
$start = $request->input('start');
|
|
$end = $request->input('end');
|
|
|
|
if ($month) {
|
|
$start = $month . '01';
|
|
$end = $month . '31';
|
|
}
|
|
|
|
$sql = "SELECT b.agent_seller_id,c.real_name,a.shop_id,b.name,sum(money) as c FROM `order` as a left join shop as b on a.shop_id=b.id LEFT JOIN seller as c on b.agent_seller_id=c.id where a.lottery_state in (1,2,3,7) and a.created_date>='{$start}' and a.created_date<='{$end}' and ((a.type =3 and a.pid=a.id) or a.type!=3) group by a.shop_id order by c desc;";
|
|
|
|
$data = DB::select($sql);
|
|
$excel = new Spreadsheet();
|
|
$sheet = $excel->getActiveSheet();
|
|
$sheet->setCellValue('A1','代理人ID');
|
|
$sheet->setCellValue('B1','代理人名称');
|
|
$sheet->setCellValue('C1','店铺ID');
|
|
$sheet->setCellValue('D1','店铺名称');
|
|
$sheet->setCellValue('E1','出单流水');
|
|
|
|
$i = 2;
|
|
foreach ($data as $item) {
|
|
$sheet->setCellValueByColumnAndRow(1, $i, $item->agent_seller_id);
|
|
$sheet->setCellValueByColumnAndRow(2, $i, $item->real_name);
|
|
$sheet->setCellValueByColumnAndRow(3, $i, $item->shop_id);
|
|
$sheet->setCellValueByColumnAndRow(4, $i, $item->name);
|
|
$sheet->setCellValueByColumnAndRow(5, $i, Helps::floatFormat($item->c));
|
|
$i++;
|
|
}
|
|
|
|
// 设置响应头,告诉浏览器文件类型
|
|
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
header('Content-Disposition: attachment;filename="'.sprintf('%s-%s', $start, $end).'店铺流水.xlsx"');
|
|
header('Cache-Control: max-age=0');
|
|
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($excel, 'Xlsx');
|
|
$writer->save('php://output');
|
|
}
|
|
|
|
public function autoFadan(Request $request, Content $content) {
|
|
$lianHong = $request->input('lian_hong', 3);
|
|
$chuanNum = $request->input('chuan_num', 2);
|
|
$moneyRateMin = $request->input('money_rate_min', 1.6);
|
|
$moneyRateMax = $request->input('money_rate_max', 7);
|
|
$customerId = $request->input('customer_id', 0);
|
|
$betsNum = $request->input('bets_num', 1); // 倍数
|
|
|
|
$customer = null;
|
|
if ($customerId) {
|
|
$customer = Customer::find($customerId);
|
|
if (!$customer) {
|
|
return $this->jsonFail('彩民不存在');
|
|
}
|
|
}
|
|
|
|
|
|
$i = 1;
|
|
$orders = [];
|
|
while ($i < $lianHong) {
|
|
if (!$customer) {
|
|
break;
|
|
}
|
|
if (count($orders) >= $lianHong) {
|
|
break;
|
|
}
|
|
$date = date('Y-m-d', strtotime("-{$i} day"));
|
|
$i++;
|
|
/** @var JczqResult $result */
|
|
$result = JczqResult::where('issue_date', $date)
|
|
->orderBy(DB::raw('RAND()'))
|
|
->first();
|
|
if (!$result) {
|
|
continue;
|
|
}
|
|
|
|
$errors = [];
|
|
try {
|
|
$odds = JczqResult::getAutoOrderOdds($date, $chuanNum, $moneyRateMin, $moneyRateMax);
|
|
$order = $this->createAutoOrder($customer, $odds, $betsNum, $chuanNum);
|
|
if ($order) {
|
|
$orders[] = $order;
|
|
}
|
|
} catch (\Exception $exception) {
|
|
$errors[] = $exception->getMessage();
|
|
}
|
|
}
|
|
|
|
$customers = Customer::pluck('nickname', 'id')->toArray();
|
|
return $content->title('后台发单')
|
|
->body(view('admin.auto_fadan', [
|
|
'lian_hong' => $lianHong,
|
|
'chuan_num' => $chuanNum,
|
|
'money_rate_min' => $moneyRateMin,
|
|
'money_rate_max' => $moneyRateMax,
|
|
'customer_id' => $customerId,
|
|
'bets_num' => $betsNum,
|
|
'orders' => $orders,
|
|
'customers' => $customers,
|
|
]));
|
|
|
|
}
|
|
|
|
public function createAutoOrder(Customer $customer,$odds, $betsNum, $chuan) {
|
|
$passMode = [$chuan . '.1'];
|
|
/** @var JczqService $service */
|
|
$service = app((JczqService::class));
|
|
|
|
$prizeInfo = $service->computePrizeInfo($odds, $betsNum, $passMode);
|
|
$jczqOdds = JczqOdds::whereIn('id', array_keys($odds))->get();
|
|
$lotterType = LotteryType::where('type', LottType::JCZQ)->first();
|
|
$lottery = Lottery::where('shop_id', $customer->shop_id)
|
|
->where('lottery_type_id', $lotterType->id)
|
|
->first();
|
|
$closeTimes = [];
|
|
$oddsCloseTimes = [];
|
|
foreach ($jczqOdds as $match) {
|
|
$realCloseTime = strtotime($match->real_close_time) - $lottery->earlySecond();
|
|
$real_close_time = date('Y-m-d H:i:s', $realCloseTime);
|
|
$closeTimes[] = $real_close_time;
|
|
$oddsCloseTimes[] = $match->real_close_time;
|
|
}
|
|
sort($closeTimes);
|
|
sort($oddsCloseTimes);
|
|
$earlyTime = $closeTimes[0];
|
|
$oddsCloseTime = $oddsCloseTimes[0];
|
|
$lateTime = $closeTimes[count($closeTimes) - 1];
|
|
|
|
$prize = $prizeInfo['prize_max'];
|
|
if ($prize <= 0) {
|
|
Log::error('backend createAutoOrder error, prize is 0', [
|
|
'odds' => $odds,
|
|
'prize' => $prizeInfo
|
|
]);
|
|
return null;
|
|
}
|
|
$money = $betsNum * 2;
|
|
$order = new Order();
|
|
$order->pid = 0;
|
|
$order->customer_id = $customer->id;
|
|
$order->lottery_id = $lottery->id;
|
|
$order->shop_id = $customer->shop_id;
|
|
$order->lottery_type_id = $lotterType->id;
|
|
$order->order_sn = Order::makeOrderSn();
|
|
$order->play_type = 'mixed';
|
|
$order->bets_num = $betsNum;
|
|
$order->bets_expect_num = $prize / $money;
|
|
$order->zhu_num = 1;
|
|
$order->piao_num = 1;
|
|
$order->money = $money;
|
|
$order->prize_min = $prize;
|
|
$order->prize_max = $prize;
|
|
$order->pay_state = PayState::SUCCESS;
|
|
$order->pass_mode = $passMode;
|
|
$order->odds_close_time = $oddsCloseTime;
|
|
$order->odds_early_close_time = $earlyTime;
|
|
$order->odds_late_close_time = $lateTime;
|
|
$order->type = OrderType::FADAN;
|
|
$order->type_mode = 3;
|
|
$order->type_desc = '快来跟我一起中大奖吧!';
|
|
$order->odds = $odds;
|
|
$order->optimize_type = 0;
|
|
$order->fadan_type = FadanType::SYSTEM;
|
|
$order->created_date = date('Ymd');
|
|
$order->lottery_state = LottState::SEND;
|
|
$order->lottery_prize = $prize;
|
|
$order->lottery_send_prize = $prize;
|
|
$order->lottery_should_send_prize = $prize;
|
|
$order->lottery_tax_prize = $prize;
|
|
$hour = rand(2,10);
|
|
$order->created_at = date('Y-m-d H:i:s', strtotime($earlyTime) - $hour);
|
|
$order->updated_at = date('Y-m-d H:i:s', strtotime($earlyTime) - $hour);
|
|
$order->save();
|
|
return $order;
|
|
}
|
|
}
|