213 lines
5.9 KiB
PHP
213 lines
5.9 KiB
PHP
<?php
|
|
/**
|
|
* This service file is part of item.
|
|
*
|
|
* @author ctexthuang
|
|
* @contact ctexthuang@qq.com
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service\Admin\System;
|
|
|
|
use App\Constants\Admin\UserCode;
|
|
use App\Constants\Common\RoleCode;
|
|
use App\Exception\ErrException;
|
|
use App\Model\AdminSection;
|
|
use App\Model\AdminUser;
|
|
use App\Model\DriverSequence;
|
|
use App\Model\Site;
|
|
use App\Service\Admin\BaseService;
|
|
use Hyperf\DbConnection\Db;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
|
|
class DriverService extends BaseService
|
|
{
|
|
/**
|
|
* 注入部门类
|
|
* @var AdminSection
|
|
*/
|
|
#[Inject]
|
|
protected readonly AdminSection $adminSectionModel;
|
|
|
|
/**
|
|
* @var AdminUser
|
|
*/
|
|
#[Inject]
|
|
protected readonly AdminUser $adminUserModel;
|
|
|
|
/**
|
|
* @var DriverSequence
|
|
*/
|
|
#[Inject]
|
|
protected readonly DriverSequence $driverSequenceModel;
|
|
|
|
/**
|
|
* @var Site
|
|
*/
|
|
#[Inject]
|
|
protected readonly Site $siteModel;
|
|
|
|
|
|
public function handle()
|
|
{
|
|
//todo Write logic
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 司机列表
|
|
* @return array
|
|
*/
|
|
public function driverList(): array
|
|
{
|
|
$limit = (int)$this->request->input('limit', 10);
|
|
$cityId = (int)$this->request->input('query_driver_city_id',0);
|
|
$name = $this->request->input('query_driver_name');
|
|
|
|
// $where[] = [
|
|
// ['is_del', '=', UserCode::IS_NO_DEL],
|
|
// ['status','=',UserCode::ENABLE],
|
|
// ['role_id','=',RoleCode::DRIVER]
|
|
// ];
|
|
|
|
$list = $this
|
|
->adminUserModel
|
|
->where('is_del',UserCode::IS_NO_DEL)
|
|
->where('status',UserCode::ENABLE)
|
|
->where('role_id',RoleCode::DRIVER)
|
|
->when($name, function ($query) use ($name) {
|
|
$query->where('chinese_name', 'like', "$name%");
|
|
})
|
|
->when($cityId > 0, function ($query) use ($cityId) {
|
|
$query->where('city_id', $cityId);
|
|
})
|
|
->when($id = $this->request->input('query_driver_id'), function ($query) use ($id) {
|
|
$query->where('id', $id);
|
|
})
|
|
->paginate($limit,['chinese_name','id','mobile','status'])->toArray();
|
|
|
|
return $this->return->success('success',$list);
|
|
}
|
|
|
|
/**
|
|
* 司机配送顺序列表
|
|
* @return array
|
|
*/
|
|
public function driverSequenceList(): array
|
|
{
|
|
$cityId = (int)$this->request->input('city_id',0);
|
|
if (empty($cityId)) return $this->return->success('success',['list' => []]);
|
|
|
|
$list = $this
|
|
->adminUserModel
|
|
->leftJoin('driver_sequence', 'admin_user.id', '=', 'driver_sequence.driver_id')
|
|
->where('admin_user.is_del',UserCode::IS_NO_DEL)
|
|
->where('admin_user.status',UserCode::ENABLE)
|
|
->where('admin_user.role_id',RoleCode::DRIVER)
|
|
->where('admin_user.city_id', $cityId)
|
|
->get(['admin_user.id','admin_user.chinese_name','driver_sequence.driver_num','driver_sequence.sequence']);
|
|
|
|
if (empty($list)) return $this->return->success('success',['list' => []]);
|
|
|
|
return $this->return->success('success',['list' => $list->toArray()]);
|
|
}
|
|
|
|
/**
|
|
* 设置司机排序
|
|
* @return array
|
|
*/
|
|
public function settingDriverSequence(): array
|
|
{
|
|
$driverArr = explode(',',$this->request->input('driver_ids'));
|
|
|
|
$list = $this->driverSequenceModel->whereIn('driver_id',$driverArr)->pluck('driver_num','driver_id')->toArray();
|
|
|
|
$updateArr = [];
|
|
|
|
foreach ($driverArr as $key => $one) {
|
|
$oneSequence = $key + 1;
|
|
$updateArr[] = [
|
|
'driver_id' => $one,
|
|
'sequence' => $oneSequence,
|
|
'driver_num' => $list[$one] ?? 0,
|
|
];
|
|
}
|
|
|
|
if (!empty($updateArr)) {
|
|
Db::table('driver_sequence')->upsert($updateArr,['driver_id']);
|
|
}
|
|
|
|
return $this->return->success();
|
|
}
|
|
|
|
/**
|
|
* 设置司机号码
|
|
* @return array
|
|
*/
|
|
public function settingDriverNum(): array
|
|
{
|
|
$driverId = (int)$this->request->input('driver_id');
|
|
$driverNum = $this->request->input('driver_num');
|
|
|
|
$info = $this->driverSequenceModel->getInfoByDriverId($driverId);
|
|
|
|
if (empty($info)) {
|
|
$info = new DriverSequence();
|
|
|
|
$info->driver_id = $driverId;
|
|
$info->driver_num = $driverNum;
|
|
$info->sequence = 999;
|
|
} else {
|
|
$info->driver_num = $driverNum;
|
|
}
|
|
|
|
if (!$info->save()) throw new ErrException('设置司机号码失败');
|
|
|
|
return $this->return->success();
|
|
}
|
|
|
|
/**
|
|
* 站点顺序列表
|
|
* @return array
|
|
*/
|
|
public function siteSequenceListByDriver(): array
|
|
{
|
|
$driverId = (int)$this->request->input('driver_id');
|
|
|
|
$list = $this->siteModel->getSiteSequenceListByDriver($driverId);
|
|
|
|
if (empty($list)) return $this->return->success('success',['list' => []]);
|
|
|
|
return $this->return->success('success',['list' => $list->toArray()]);
|
|
}
|
|
|
|
/**
|
|
* 站点设置顺序
|
|
* @return array
|
|
*/
|
|
public function settingSiteSequence(): array
|
|
{
|
|
$siteArr = explode(',',$this->request->input('site_ids'));
|
|
$driverId = (int)$this->request->input('driver_id');
|
|
|
|
$list = $this->siteModel->getSiteSequenceListByDriver($driverId);
|
|
if (empty($list)) throw new ErrException('站点列表为空');
|
|
|
|
if (count($list->toArray()) != count($siteArr)) throw new ErrException('站点数据不对,刷新后重新设置');
|
|
|
|
$updateArr = [];
|
|
foreach ($siteArr as $key => $one) {
|
|
$updateArr[] = [
|
|
'id' => $one,
|
|
'sequence' => $key + 1,
|
|
];
|
|
}
|
|
|
|
$updateHandle = (new Site)->update($updateArr);
|
|
if (!$updateHandle) throw new ErrException('设置失败');
|
|
|
|
return $this->return->success();
|
|
}
|
|
} |