Commit bd802a79 by 宁成龙

新增自动分配

parent a589e910
<?php
namespace App\Admin\Service;
use Illuminate\Support\Facades\DB;
class AutoAssignCustomer
{
public static function getNowAssignInfo()
{
try {
DB::beginTransaction();
$assignInfo = \App\Models\AutoAssignCustomer::getNowAssignInfo();
if (empty($res)) {
$assignInfo = \App\Models\AutoAssignCustomer::getInfoByOrderSort();
\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]);
}
DB::commit();
return $assignInfo;
} catch (\Throwable $throwable) {
DB::rollBack();
throw $throwable;
}
}
public static function incAssignNumByDepartmentId($departmentId, $num = 1)
{
$assignInfo = \App\Models\AutoAssignCustomer::getInfoByDepartmentId($departmentId);
if (empty($assignInfo)) {
return false;
}
$nowNum = $assignInfo['now_num'] + $num;
if ($nowNum > $assignInfo['max_num']) {
return false;
}
if ($nowNum == $assignInfo['max_num']) {
$nextAssignInfo = \App\Models\AutoAssignCustomer::getInfoByOrderSort($assignInfo['sort']);
if (empty($nextAssignInfo)) {
return false;
}
\App\Models\AutoAssignCustomer::updateAll(['is_now' => \App\Models\AutoAssignCustomer::IS_NOW_NO]);
\App\Models\AutoAssignCustomer::updateById($nextAssignInfo['id'],
['is_now' => \App\Models\AutoAssignCustomer::IS_NOW_YES]);
}
return \App\Models\AutoAssignCustomer::updateById($assignInfo['id'], ['now_num' => $nowNum]);
}
}
<?php
namespace App\Models;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Model;
/**
* @property $id 主键id
* @property $department_id 部门id
* @property $department_name 部门名
* @property $assign_sale_name 分配业务员名
* @property $assign_sale_id 分配业务员id
* @property $sort 排序
* @property $now_num 当前数量
* @property $max_num 分配总数
* @property $is_now 是否为当前分配部门
*/
class AutoAssignCustomer extends Model
{
use HasDateTimeFormatter;
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 getNowAssignInfo()
{
$res = self::where('is_now', self::IS_NOW_YES)->get();
return ($res) ? $res->toArray() : [];
}
public static function getInfoByOrderSort($sort = 0)
{
$res = self::where('sort', ">=", $sort)->orderBy("sort", "ASC")->get();
return ($res) ? $res->toArray() : [];
}
public static function deleteById($addressId)
{
return self::where('address_id', $addressId)->delete();
}
public static function updateById($id, $update)
{
return self::where("address_id", $id)->update($update);
}
public static function updateAll( $update)
{
return self::update($update);
}
public static function updateByUserId($userId, $update)
{
return self::where("user_id", $userId)->update($update);
}
public static function getInfoById($id)
{
$res = self::where('id', $id)->first();
return ($res) ? $res->toArray() : [];
}
public static function getInfoByDepartmentId($id)
{
$res = self::where('department_id', $id)->first();
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