Commit 0d8576fa by 杨树贤

用户对接自动分发

parent afe33c71
......@@ -15,7 +15,7 @@ class InvalidRequestException extends \Exception
public $field;
public function __construct($message, $field, $code = 200)
public function __construct($message, $field = null, $code = 200)
{
$this->field = $field;
parent::__construct($message, $code);
......
......@@ -3,6 +3,7 @@
namespace App\Http\Controllers\Api;
use App\Http\Requests\UserRegister;
use App\Http\Services\UserService;
use App\Models\UserModel;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Foundation\Auth\ThrottlesLogins;
......@@ -33,7 +34,7 @@ class AuthApiController extends Controller
if ($cachedEmailCode != $request->input('email_code')) {
return $this->setError('Email code invalid');
}
$userId = UserModel::createUser($request->all());
$userId = UserService::createUser($request->all());
Redis::del($redisKey);
\Auth::loginUsingId($userId);
return $this->setSuccess('Register success');
......
......@@ -2,6 +2,7 @@
namespace App\Http\Controllers;
use App\Http\Services\AutoAssignCustomerService;
use App\Http\Services\UserService;
use Illuminate\Http\Request;
......
<?php
namespace App\Http\Services;
use App\Exceptions\InvalidRequestException;
use App\Models\AutoAssignCustomer;
use Illuminate\Support\Facades\DB;
class AutoAssignCustomerService
{
public static function getNowAssignInfo()
{
try {
DB::beginTransaction();
$assignInfo = AutoAssignCustomer::getNowAssignInfo();
if (empty($assignInfo)) {
$assignInfo = AutoAssignCustomer::getInfoByOrderSort();
AutoAssignCustomer::updateAll(['is_now' => AutoAssignCustomer::IS_NOW_NO]);
AutoAssignCustomer::updateById($assignInfo['id'],
['is_now' => AutoAssignCustomer::IS_NOW_YES]);
}
DB::commit();
return $assignInfo;
} catch (\Exception $exception) {
DB::rollBack();
throw new InvalidRequestException($exception->getMessage());
}
}
public static function incAssignNumByDepartmentId($departmentId, $num = 1)
{
$assignInfo = AutoAssignCustomer::getInfoByDepartmentId($departmentId);
if (empty($assignInfo)) {
return false;
}
if ($assignInfo['is_now'] == AutoAssignCustomer::IS_NOW_NO) {
return false;
}
$nowNum = $assignInfo['now_num'] + $num;
if ($nowNum > $assignInfo['max_num']) {
$nextAssignInfo = AutoAssignCustomer::getInfoByOrderSort($assignInfo['sort']);
if (empty($nextAssignInfo)) {
$nextAssignInfo = AutoAssignCustomer::getInfoByOrderSort(-999999999);
}
if (empty($nextAssignInfo)) {
return false;
}
AutoAssignCustomer::updateAll([
'is_now' => AutoAssignCustomer::IS_NOW_NO,
"now_num" => 0
]);
AutoAssignCustomer::updateById($nextAssignInfo['id'],
['is_now' => AutoAssignCustomer::IS_NOW_YES]);
return false;
}
return AutoAssignCustomer::updateById($assignInfo['id'], ['now_num' => $nowNum]);
}
}
......@@ -2,9 +2,11 @@
namespace App\Http\Services;
use App\Models\Cms\CmsUserModel;
use App\Models\CountryModel;
use App\Models\UserModel;
use Carbon\Carbon;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redis;
class UserService
......@@ -15,4 +17,32 @@ class UserService
return 'SU' . $id;
}
public static function createUser($data)
{
$assign = AutoAssignCustomerService::getNowAssignInfo();
$salesId = $assign['assign_sale_id'];
$sales = CmsUserModel::getInfoByUserId($salesId);
$userId = UserModel::insertGetId([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'phone' => $data['phone'],
'email_verified_at' => now(),
'company_name' => $data['company_name'],
'account_properties' => $data['account_properties'],
'sale_id' => $salesId,
'sale_name' => \Arr::get($sales, 'name'),
'create_time' => time(),
'password' => Hash::make($data['password']),
]);
$userSn = UserService::generateSn($userId);
$updateRes = UserModel::where('id', $userId)->update([
'user_sn' => $userSn,
]);
if ($updateRes) {
AutoAssignCustomerService::incAssignNumByDepartmentId($assign['department_id']);
}
return $updateRes;
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class AutoAssignCustomer extends Model
{
public $timestamps = false;
protected $table = 'auto_assign_customer';
const IS_NOW_YES = 1;
const IS_NOW_NO = 0;
public static function insertData($data)
{
return self::insertGetId($data);
}
//获取列表
public static function getList()
{
$res = self::orderBy('sort', 'asc')->get();
return ($res) ? $res->toArray() : [];
}
public static function getNowAssignInfo()
{
$res = self::where('is_now', self::IS_NOW_YES)->first();
return ($res) ? $res->toArray() : [];
}
public static function getInfoByOrderSort($sort = 0)
{
$res = self::where('sort', ">", $sort)->orderBy("sort", "ASC")->first();
return ($res) ? $res->toArray() : [];
}
public static function updateById($id, $update)
{
return self::where("id", $id)->update($update);
}
public static function updateAll($update)
{
return self::where([])->update($update);
}
public static function getInfoByDepartmentId($id)
{
$res = self::where('department_id', $id)->first();
return ($res) ? $res->toArray() : [];
}
}
<?php
namespace App\Models\Cms;
use Illuminate\Database\Eloquent\Model;
class CmsDepartmentModel extends Model
{
protected $table = 'department';
public $timestamps = false;
protected $primaryKey = 'departmentId';
//采购部门的总ID
const PURCHASE_DEPARTMENT_ID = 8;
//销售部门的总ID
const SALES_DEPARTMENT_ID = 7;
// 获取部门人员
public static function getUsersByDepartmentId($departmentId, $status = '', $filter = '')
{
$departmentIds = [];
self::getSubDepartmentId($departmentId, $departmentIds);
return CmsUserInfoModel::whereIn('department_id', $departmentIds)
->where(function ($query) use ($status) {
if ($status !== '') {
$query->where('status', '=', $status);
}
})
->where(function ($query) use ($filter) {
if ($filter) {
$query->whereRaw($filter);
}
})
->get()->toArray();
}
// 获取下级部门ID
public static function getSubDepartmentId($departmentId, &$departmentIds)
{
// 获取下级部门
$sub_department = CmsUserDepartmentModel::where('parent_id', $departmentId)->select([
'department_id',
'department_name'
])->get();
if ($sub_department) {
foreach ($sub_department as $v) {
self::getSubDepartmentId($v['department_id'], $departmentIds);
}
}
$departmentIds[] = $departmentId;
return $departmentIds;
}
}
<?php
namespace App\Models\Cms;
use Illuminate\Database\Eloquent\Model;
class CmsUserDepartmentModel extends Model
{
protected $connection = 'cms';
protected $table = 'user_department';
public $timestamps = false;
public static function getDepartmentIdsParrentIds($parrent_ids = [])
{
$res = self::whereIn('parent_id', $parrent_ids)->pluck('department_id');
return ($res) ? $res->toArray() : [];
}
// 根据父ID获取指定的部门
public static function getDepartmentByParentId($parent_id)
{
if (is_array($parent_id)) {
$list = self::whereIn('parent_id', $parent_id);
} else {
$list = self::where('parent_id', $parent_id);
}
return $list->pluck('department_name', 'department_id')->toArray();
}
public static function getDepartmentInfo($department_id)
{
$res = self::where('department_id', $department_id)->first();
return ($res) ? $res->toArray() : [];
}
public static function getDepartmentListByDepartmentIdArr($departmentIdArr)
{
$res = self::whereIn('department_id', $departmentIdArr)->get();
return ($res) ? $res->toArray() : [];
}
public static function getDepartmentList($departmentName, $departmentIds)
{
$res = self::select('department_name')->where('department_name', 'like',
'%' . $departmentName . '%')->whereIn('department_id', $departmentIds)->get();
return ($res) ? $res->toArray() : [];
}
public static function getDepartmentListByName($departmentName)
{
$res = self::select('department_name')->distinct('department_name')->where('department_name', 'like',
"%{$departmentName}%")->orderByDesc('department_id')->pluck('department_name');
return ($res) ? $res->toArray() : [];
}
// 根据部门名称获取部门ID
public static function getDepartmentIdByName($department_name)
{
return self::where('department_name', $department_name)->value('department_id');
}
public static function getDepartmentId($department)
{
$res = self::select('department_id')->distinct('department_name')->where('department_name',
$department)->pluck('department_id');
return ($res) ? $res->toArray() : [];
}
// 获取部门人员
public static function getUserByDepartmentId($departmentId, $status = '', $filter = '')
{
$departmentIds = [];
self::getSubDepartmentId($departmentId, $departmentIds);
return CmsUserInfoModel::whereIn('department_id', $departmentIds)
->where(function ($query) use ($status) {
if ($status !== '') {
$query->where('status', '=', $status);
}
})
->where(function ($query) use ($filter) {
if ($filter) {
$query->whereRaw($filter);
}
})
->select('userId', 'name', 'status')
->get();
}
// 获取下级部门ID
public static function getSubDepartmentId($departmentId, &$departmentIds)
{
// 获取下级部门
$sub_department = self::where('parent_id', $departmentId)->select('department_id',
'department_name')->get();
if ($sub_department) {
foreach ($sub_department as $v) {
self::getSubDepartmentId($v['department_id'], $departmentIds);
}
}
$departmentIds[] = $departmentId;
return $departmentIds;
}
/*
* 获取上级部门,只需要往上找一层
* 比如:线上一组=》线上销售部
*/
public static function getParentDepartment($dep_id)
{
$parent_id = self::where("department_id", intval($dep_id))->value("parent_id");
if(!$parent_id){
return [];
}
$parentInfo = self::where("department_id",$parent_id)->select("department_id", "department_name")->first();
return $parentInfo ? $parentInfo->toArray():[];
}
// 获取下级部门
public static function getSubDepartmentById($department_id)
{
return self::where('parent_id', $department_id)->select('department_id', 'department_name')->get()->toArray();
}
// 获取顶级部门名称
public static function getTopDepartmentNameById($department_id)
{
return self::where('department_id', $department_id)->where('parent_id', 0)->value('department_name');
}
}
<?php
namespace App\Models\Cms;
use Illuminate\Database\Eloquent\Model;
class CmsUserModel extends Model
{
protected $connection = 'cms';
protected $table = 'user_info';
public $timestamps = false;
public static function getInfoByUserId($userId)
{
$res = self::where('userId', $userId)->first();
return ($res) ? $res->toArray() : [];
}
public static function getUserList()
{
return CmsUserModel::where([])->rule(\App\Models\User::$ruleViewList, 'userId')->pluck('name', 'userId')->toArray();
}
//根据部门id获取列表
public static function getUserListByDepartmentId($departmentIds)
{
$res = self::whereIn('department_id', $departmentIds)->get();
return ($res) ? $res->toArray() : [];
}
// 根据部门ID获取用户
public static function getUserIdsByDepartmentIds($department_ids = [], $field = [], $status = '')
{
$res = self::whereIn('department_id', $department_ids);
if ($status !== '') {
$res = $res->where('status', $status);
}
if (!$field) {
$res = $res->pluck('userId');
} else {
$res = $res->select($field)->get();
}
return ($res) ? $res->toArray() : [];
}
}
......@@ -2,6 +2,7 @@
namespace App\Models;
use App\Http\Services\AutoAssignCustomerService;
use App\Http\Services\UserService;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
......@@ -41,31 +42,4 @@ class UserModel extends Authenticatable implements MustVerifyEmail
'email_verified_at' => 'datetime',
];
public static function createUser($data)
{
$userId = UserModel::insertGetId([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'phone' => $data['phone'],
'email_verified_at' => now(),
'company_name' => $data['company_name'],
'account_properties' => $data['account_properties'],
'password' => Hash::make($data['password']),
]);
$userSn = UserService::generateSn($userId);
return self::where('id', $userId)->update([
'user_sn' => $userSn,
]);
}
//获取用户详情
public static function getUserInfo($user_id)
{
$map = [
"id" => $user_id
];
$res = self::where($map)->first();
return ($res) ? $res->toArray() : [];
}
}
......@@ -62,7 +62,25 @@ return [
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'cms' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_CMS_HOST', '127.0.0.1'),
'port' => env('DB_CMS_PORT', '3306'),
'database' => env('DB_CMS_DATABASE', 'forge'),
'username' => env('DB_CMS_USERNAME', 'forge'),
'password' => env('DB_CMS_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'pgsql' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
......
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