Commit 86271534 by 宁成龙

完善自动分配功能

parent 5c0f1a95
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace App\Admin\Controllers; namespace App\Admin\Controllers;
use App\Admin\Metrics\Examples; use App\Admin\Metrics\Examples;
use App\Admin\Service\AutoAssignCustomerService;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Dcat\Admin\Http\Controllers\Dashboard; use Dcat\Admin\Http\Controllers\Dashboard;
use Dcat\Admin\Layout\Column; use Dcat\Admin\Layout\Column;
...@@ -13,6 +14,10 @@ class HomeController extends Controller ...@@ -13,6 +14,10 @@ class HomeController extends Controller
{ {
public function index(Content $content) public function index(Content $content)
{ {
$assign = AutoAssignCustomerService::getNowAssignInfo();
var_dump($assign);
var_dump(AutoAssignCustomerService::incAssignNumByDepartmentId($assign['department_id'],1));
var_dump(123);die;
return $content return $content
->header('Dashboard') ->header('Dashboard')
->description('Description...') ->description('Description...')
......
...@@ -4,17 +4,18 @@ namespace App\Admin\Service; ...@@ -4,17 +4,18 @@ namespace App\Admin\Service;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
class AutoAssignCustomer class AutoAssignCustomerService
{ {
public static function getNowAssignInfo() public static function getNowAssignInfo()
{ {
try { try {
DB::beginTransaction(); DB::beginTransaction();
$assignInfo = \App\Models\AutoAssignCustomer::getNowAssignInfo(); $assignInfo = \App\Models\AutoAssignCustomer::getNowAssignInfo();
if (empty($res)) { if (empty($assignInfo)) {
$assignInfo = \App\Models\AutoAssignCustomer::getInfoByOrderSort(); $assignInfo = \App\Models\AutoAssignCustomer::getInfoByOrderSort();
\App\Models\AutoAssignCustomer::updateAll(['is_now' => \App\Models\AutoAssignCustomer::IS_NOW_NO]); \App\Models\AutoAssignCustomer::updateAll(['is_now' => \App\Models\AutoAssignCustomer::IS_NOW_NO]);
\App\Models\AutoAssignCustomer::updateById($assignInfo['id'], ['is_now' => \App\Models\AutoAssignCustomer::IS_NOW_YES]); \App\Models\AutoAssignCustomer::updateById($assignInfo['id'],
['is_now' => \App\Models\AutoAssignCustomer::IS_NOW_YES]);
} }
DB::commit(); DB::commit();
return $assignInfo; return $assignInfo;
...@@ -30,33 +31,65 @@ class AutoAssignCustomer ...@@ -30,33 +31,65 @@ class AutoAssignCustomer
if (empty($assignInfo)) { if (empty($assignInfo)) {
return false; return false;
} }
$nowNum = $assignInfo['now_num'] + $num; if ($assignInfo['is_now'] == \App\Models\AutoAssignCustomer::IS_NOW_NO) {
if ($nowNum > $assignInfo['max_num']) {
return false; return false;
} }
if ($nowNum == $assignInfo['max_num']) { $nowNum = $assignInfo['now_num'] + $num;
if ($nowNum > $assignInfo['max_num']) {
$nextAssignInfo = \App\Models\AutoAssignCustomer::getInfoByOrderSort($assignInfo['sort']); $nextAssignInfo = \App\Models\AutoAssignCustomer::getInfoByOrderSort($assignInfo['sort']);
if (empty($nextAssignInfo)) { if (empty($nextAssignInfo)) {
$nextAssignInfo = \App\Models\AutoAssignCustomer::getInfoByOrderSort(-999999999);
}
if (empty($nextAssignInfo)) {
return false; return false;
} }
\App\Models\AutoAssignCustomer::updateAll(['is_now' => \App\Models\AutoAssignCustomer::IS_NOW_NO]); \App\Models\AutoAssignCustomer::updateAll([
'is_now' => \App\Models\AutoAssignCustomer::IS_NOW_NO,
"now_num" => 0
]);
\App\Models\AutoAssignCustomer::updateById($nextAssignInfo['id'], \App\Models\AutoAssignCustomer::updateById($nextAssignInfo['id'],
['is_now' => \App\Models\AutoAssignCustomer::IS_NOW_YES]); ['is_now' => \App\Models\AutoAssignCustomer::IS_NOW_YES]);
} }
return \App\Models\AutoAssignCustomer::updateById($assignInfo['id'], ['now_num' => $nowNum]); return \App\Models\AutoAssignCustomer::updateById($assignInfo['id'], ['now_num' => $nowNum]);
} }
public static function updateDepartmentNum(){ public static function updateDepartmentNum()
{
//获取自动分配部门 //获取自动分配部门
$assignInfo = \App\Models\AutoAssignCustomer::getNowAssignInfo(); $assignList = \App\Models\AutoAssignCustomer::getList();
if (empty($assignInfo)) { if (empty($assignList)) {
return false; return false;
} }
//获取部门人员 //获取部门人员
foreach ($assignList as $assign) {
$departmentId = $assign['department_id'];
$departmentIds = [];
$departmentIds = self::getChildDepartmentList($departmentId, $departmentIds);
$departmentIds[] = $departmentId;
$departmentIds = array_unique($departmentIds);
//获取该部门下的用户
$userList = \App\Models\Cms\CmsUser::getUserListByDepartmentId($departmentIds);
$updateData = [
'max_num' => count($userList),
];
var_dump($updateData);
\App\Models\AutoAssignCustomer::updateById($assign['id'], $updateData);
}
}
public static function getChildDepartmentList($departmentId, &$departmentIds)
{
// 获取下级部门
$sub_department = \App\Models\Cms\CmsUserDepartmentModel::getDepartmentByParentId($departmentId);
if ($sub_department) {
foreach ($sub_department as $key => $v) {
self::getChildDepartmentList($key, $departmentIds);
} }
}
$departmentIds[] = $departmentId;
return $departmentIds;
}
} }
...@@ -32,31 +32,38 @@ class AutoAssignCustomer extends Model ...@@ -32,31 +32,38 @@ class AutoAssignCustomer extends Model
return self::insertGetId($data); return self::insertGetId($data);
} }
//获取列表
public static function getList()
{
$res = self::orderBy('sort', 'asc')->get();
return ($res) ? $res->toArray() : [];
}
public static function getNowAssignInfo() public static function getNowAssignInfo()
{ {
$res = self::where('is_now', self::IS_NOW_YES)->get(); $res = self::where('is_now', self::IS_NOW_YES)->first();
return ($res) ? $res->toArray() : []; return ($res) ? $res->toArray() : [];
} }
public static function getInfoByOrderSort($sort = 0) public static function getInfoByOrderSort($sort = 0)
{ {
$res = self::where('sort', ">=", $sort)->orderBy("sort", "ASC")->get(); $res = self::where('sort', ">", $sort)->orderBy("sort", "ASC")->first();
return ($res) ? $res->toArray() : []; return ($res) ? $res->toArray() : [];
} }
public static function deleteById($addressId) public static function deleteById($addressId)
{ {
return self::where('address_id', $addressId)->delete(); return self::where('id', $addressId)->delete();
} }
public static function updateById($id, $update) public static function updateById($id, $update)
{ {
return self::where("address_id", $id)->update($update); return self::where("id", $id)->update($update);
} }
public static function updateAll( $update) public static function updateAll($update)
{ {
return self::update($update); return self::where([])->update($update);
} }
public static function updateByUserId($userId, $update) public static function updateByUserId($userId, $update)
...@@ -69,6 +76,7 @@ class AutoAssignCustomer extends Model ...@@ -69,6 +76,7 @@ class AutoAssignCustomer extends Model
$res = self::where('id', $id)->first(); $res = self::where('id', $id)->first();
return ($res) ? $res->toArray() : []; return ($res) ? $res->toArray() : [];
} }
public static function getInfoByDepartmentId($id) public static function getInfoByDepartmentId($id)
{ {
$res = self::where('department_id', $id)->first(); $res = self::where('department_id', $id)->first();
......
...@@ -15,4 +15,11 @@ class CmsUser extends Model ...@@ -15,4 +15,11 @@ class CmsUser extends Model
$res = self::where('userId', $userId)->first(); $res = self::where('userId', $userId)->first();
return ($res) ? $res->toArray() : []; return ($res) ? $res->toArray() : [];
} }
//根据部门id获取列表
public static function getUserListByDepartmentId($departmentIds)
{
$res = self::whereIn('department_id', $departmentIds)->get();
return ($res) ? $res->toArray() : [];
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment