Commit aecbf32a by 朱继来

调整

parent 6fbcddcf
Showing with 166 additions and 165 deletions
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Request;
use Excel;
use DB;
class KefuModel extends Model
{
protected $connection = 'order';
protected $table = 'lie_kefu';
protected $primaryKey = 'id';
protected $guarded = ['id'];
public $timestamps = true;
const CREATED_AT = 'create_time';
const UPDATED_AT = 'update_time';
/** * @param \DateTime|int $value * @return false|int * @author dividez */
public function fromDateTime($value)
{
return strtotime(parent::fromDateTime($value));
}
// 获取客服
public function lists($request, $export='')
{
$page = $request->input('page', 1);
$limit = $request->input('limit', 10);
$field = ['id', 'sale_id', 'sale_name', 'email', 'status', 'operator_id', 'operator_name', 'create_time', 'update_time'];
$map['sale_name'] = $request->input('sale_name', ''); // 客服名称
$map['email'] = $request->input('email', ''); // 邮箱
$map['begin_time'] = $request->input('begin_time', '') ? strtotime($request->input('begin_time')) : '';
$map['end_time'] = $request->input('end_time', '') ? strtotime($request->input('end_time')) + 86399 : '';
$list = $this->where(function($query) use ($map) {
// 客服名称
if ($map['sale_name']) {
$query->where('sale_name', 'like', $map['sale_name'].'%');
}
})->where(function($query) use ($map) {
// 邮箱
if ($map['email']) {
$query->where('email', '=', $map['email']);
}
})->where(function($query) use ($map) {
// 创建时间
if(!empty($map['begin_time']) && !empty($map['end_time'])) {
$query->whereBetween('create_time', [$map['begin_time'], $map['end_time']]);
}
else if(!empty($map['begin_time'])) {
$query->where('create_time', '>=', $map['begin_time']);
}
else if(!empty($map['end_time'])) {
$query->where('create_time', '<=', $map['end_time']);
}
})
->select($field)
->orderBy('status', 'desc')
->orderBy('id', 'asc');
if ($export) {
$list = $list->get()->toArray();
if (empty($list)) {
echo '<script>alert("导出数据为空");history.go(-1);</script>';die;
}
return $list;
} else {
$list = $list->paginate($limit, ['*'], 'page', $page)->toArray();
return [0, '获取成功', $list['data'], $list['total']];
}
}
// 新增客服
public function add($request)
{
$email = $request->input('email');
$CmsModel = new CmsModel;
$sales = $CmsModel->where('email', $email)->select('userId', 'name', 'status')->first();
if (!$sales) return [1, '客服邮箱不存在'];
if ($sales->status == 4) return [1, '客服已离职'];
$exists = $this->where('email', $email)->first();
if ($exists) return [1, '客服已存在'];
$sale_id = $sales->userId;
$data = [];
$data['sale_name'] = $sales->name;
$data['email'] = $email;
$data['operator_id'] = $request->user->userId;
$data['operator_name'] = $request->user->name;
$count = $this->count(); // 客服池数量
if (!$count) $data['status'] = 1; // 待分配
$res = $this->updateOrCreate(['sale_id'=>$sale_id], $data);
if ($res === false) return [1, '新增客服失败'];
return [0, '新增客服成功'];
}
// 编辑客服
public function edit($request)
{
$id = $request->input('id');
$email = $request->input('email');
$res = $this->where('id', $id)->update(['email'=>$email]);
if ($res === false) return [1, '更新客服失败'];
return [0, '更新客服成功'];
}
// 删除客服
public function del($request)
{
$id = $request->input('id');
$status = $request->input('status');
$res = $this->where('id', $id)->delete();
if ($res === false) return [1, '删除客服失败'];
if ($status == 1) {
$first = $this->select('id')->orderBy('id', 'asc')->first();
$res = $this->where('id', $first['id'])->update(['status'=>1]);
if ($res === false) return [1, '更新第一条客服状态失败'];
}
return [0, '删除客服成功'];
}
// 置顶客服
public function top($request)
{
$id = $request->input('id');
try {
DB::connection('order')->beginTransaction(); // 开启事务
DB::connection('order')->update('UPDATE lie_kefu SET status = 0');
$this->where('id', $id)->update(['status'=>1]);
DB::connection('order')->commit();
return [0, '置顶客服成功'];
} catch(Exception $e) {
DB::connection('order')->rollback();
return [1, '置顶客服失败,'.$e->getMessage()];
}
}
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Request;
use Excel;
use DB;
class KefuModel extends Model
{
protected $connection = 'web';
protected $table = 'lie_kefu';
protected $primaryKey = 'id';
protected $guarded = ['id'];
public $timestamps = true;
const CREATED_AT = 'create_time';
const UPDATED_AT = 'update_time';
/** * @param \DateTime|int $value * @return false|int * @author dividez */
public function fromDateTime($value)
{
return strtotime(parent::fromDateTime($value));
}
// 获取客服
public function lists($request, $export='')
{
$page = $request->input('page', 1);
$limit = $request->input('limit', 10);
$field = ['id', 'sale_id', 'sale_name', 'email', 'status', 'operator_id', 'operator_name', 'create_time', 'update_time'];
$map['sale_name'] = $request->input('sale_name', ''); // 客服名称
$map['email'] = $request->input('email', ''); // 邮箱
$map['begin_time'] = $request->input('begin_time', '') ? strtotime($request->input('begin_time')) : '';
$map['end_time'] = $request->input('end_time', '') ? strtotime($request->input('end_time')) + 86399 : '';
$list = $this->where(function($query) use ($map) {
// 客服名称
if ($map['sale_name']) {
$query->where('sale_name', 'like', $map['sale_name'].'%');
}
})->where(function($query) use ($map) {
// 邮箱
if ($map['email']) {
$query->where('email', '=', $map['email']);
}
})->where(function($query) use ($map) {
// 创建时间
if(!empty($map['begin_time']) && !empty($map['end_time'])) {
$query->whereBetween('create_time', [$map['begin_time'], $map['end_time']]);
}
else if(!empty($map['begin_time'])) {
$query->where('create_time', '>=', $map['begin_time']);
}
else if(!empty($map['end_time'])) {
$query->where('create_time', '<=', $map['end_time']);
}
})
->select($field)
->orderBy('status', 'desc')
->orderBy('id', 'asc');
if ($export) {
$list = $list->get()->toArray();
if (empty($list)) {
echo '<script>alert("导出数据为空");history.go(-1);</script>';die;
}
return $list;
} else {
$list = $list->paginate($limit, ['*'], 'page', $page)->toArray();
return [0, '获取成功', $list['data'], $list['total']];
}
}
// 新增客服
public function add($request)
{
$email = $request->input('email');
$CmsModel = new CmsModel;
$sales = $CmsModel->where('email', $email)->select('userId', 'name', 'status')->first();
if (!$sales) return [1, '客服邮箱不存在'];
if ($sales->status == 4) return [1, '客服已离职'];
$exists = $this->where('email', $email)->first();
if ($exists) return [1, '客服已存在'];
$sale_id = $sales->userId;
$data = [];
$data['sale_name'] = $sales->name;
$data['email'] = $email;
$data['operator_id'] = $request->user->userId;
$data['operator_name'] = $request->user->name;
$count = $this->count(); // 客服池数量
if (!$count) $data['status'] = 1; // 待分配
$res = $this->updateOrCreate(['sale_id'=>$sale_id], $data);
if ($res === false) return [1, '新增客服失败'];
return [0, '新增客服成功'];
}
// 编辑客服
public function edit($request)
{
$id = $request->input('id');
$email = $request->input('email');
$res = $this->where('id', $id)->update(['email'=>$email]);
if ($res === false) return [1, '更新客服失败'];
return [0, '更新客服成功'];
}
// 删除客服
public function del($request)
{
$id = $request->input('id');
$status = $request->input('status');
$res = $this->where('id', $id)->delete();
if ($res === false) return [1, '删除客服失败'];
if ($status == 1) {
$first = $this->select('id')->orderBy('id', 'asc')->first();
$res = $this->where('id', $first['id'])->update(['status'=>1]);
if ($res === false) return [1, '更新第一条客服状态失败'];
}
return [0, '删除客服成功'];
}
// 置顶客服
public function top($request)
{
$id = $request->input('id');
try {
DB::connection('order')->beginTransaction(); // 开启事务
DB::connection('order')->update('UPDATE lie_kefu SET status = 0');
$this->where('id', $id)->update(['status'=>1]);
DB::connection('order')->commit();
return [0, '置顶客服成功'];
} catch(Exception $e) {
DB::connection('order')->rollback();
return [1, '置顶客服失败,'.$e->getMessage()];
}
}
}
\ No newline at end of file
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