Commit b3085218 authored by yinxiaoling's avatar yinxiaoling

策略新建

parent b7fa07b3
......@@ -15,14 +15,20 @@ class LoginApiController extends BaseController
$param = $request->except('url');
$url = $request->url;
Log::channel('api')->info(substr(md5(json_encode($param)),1,8));
/* Log::channel('api')->info($url);
/*Log::channel('api')->info(substr(md5(json_encode($param)),1,8));
Log::channel('api')->info($url);
if($url != substr(md5(json_encode($param)),1,8)){
return $this->errorWithInfo('url验证不通过', 401);
} */
//固定登录用户
$allowLoginUser = ['username'=>'yxl001','user_sn'=>'SYSUSER|9da548a2e392a679eb274a0b2abeb627','parent_sn'=>'ljs'];
if(empty($param['username']) || empty($param['password'] )){
return $this->errorWithInfo('参数验证不通过', 401);
return $this->errorWithInfo('用户名、密码必填', 401);
}
if($param['username'] != $allowLoginUser['username']){ //固定登录用户
return $this->errorWithInfo('用户名不正确', 401);
}
$user = DB::table('users')->select('status','user_name','password','user_sn','team_name','password','expired','ai_count','caller_group','api_key','parent_sn')->where('user_name',$param['username'])
......
......@@ -35,6 +35,12 @@ class StrategySetController extends Controller
return $this->errorWithInfo('表单验证失败', 401);
}
$params = $request->all();
if ($params['frequency_type'] == 2){ //一天两次时间、项目格式验证,#号分割
if (substr_count($params['call_time_set'], '#') != 1 || substr_count($params['project_sn'], '#') != 1){
return $this->errorWithInfo('呼叫设置验证失败', 401);
}
}
$successTotal = $failTotal = 0;
$failDataFile = '';
if (isset($params['upload'])){ //excel文件上传
......@@ -61,14 +67,32 @@ class StrategySetController extends Controller
$setReturn = $StrategySet->save();
if ($setReturn){ //策略保存成功,处理excel数据
if (isset($successTotal) && $successTotal > 0){
$StrategyService->insertStrategyExcel($strategySn, $successData);
$insertExcelReturn = $StrategyService->insertStrategyExcel($strategySn, $successData);
}
}
//返回成功数、失败数、失败导出文件
$returnArr = compact('successTotal','failTotal','failDataFile');
$returnArr = compact('total','successTotal','failTotal','failDataFile');
return $this->successWithInfo(['data'=>$returnArr]);
}
//项目列表
public function getProjectsList(){
$projectInfo = [['sn'=>'projects|a3a4259352c8e964ad6ea6c5ee8a7208','name'=>'liuy-转人工测试']];
return $this->successWithInfo(['data'=>$projectInfo]);
}
//下载模板及下载导入失败excel数据
public function downloadModel(Request $request,StrategyService $StrategyService){
$excelFile = $request->filename;
if (!empty($excelFile)){//下载失败导入数据
}else{//下载模板
$excelName = "策略任务数据导入模板";
$datas[0][0]= "姓名";
$datas[0][1]= "*手机号码";
$datas[0][2]= "值班日期";
$StrategyService->storeExcelStyle($excelName, $datas,'','',1);
}
}
}
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Service\StrategyService;
class StrategyTaskJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $paramsArr;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($params)
{
$this->paramsArr = $params;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$StrategyService = new StrategyService();
$StrategyService->createTaskDataJob($this->paramsArr['strategySn']);
}
}
......@@ -4,6 +4,9 @@ namespace App\Service;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\Log;
use App\models\StrategyExcel;
use App\models\StrategySet;
use App\models\StrategyTask;
use App\Jobs\StrategyTaskJob;
class StrategyService
{
......@@ -54,6 +57,7 @@ class StrategyService
//判断excel数据问题,$day_type//外呼日期类型 0每天 1日期前一天和当天 1需要验证值班日期必填
public function validExcelData($successData,$day_type){
$failData = [];
$total = count($successData);
foreach ($successData as $k=>$v){
$dataLab = 1;
if (strlen($v[0]) > 20){ //姓名最大20个字符,中文处理,放入失败数据中
......@@ -83,6 +87,8 @@ class StrategyService
}
}
}else{//不需要验证附初始值
$successData[$k][2] = date('Y-m-d');
}
if ($dataLab == 0){
unset($successData[$k]);
......@@ -98,20 +104,20 @@ class StrategyService
$failExcelName = "导入失败数据_".date('YmdHis');
//dump($failExcelName);
$failFile = $this->storeExcelStyle($failExcelName, $failData,'','import');
//$failDataFile = $failExcelName.'.xlsx';
if ($failFile){
$failDataFile = storage_path('export').'\\'.$failExcelName.'.xlsx';
$failDataFile = $failExcelName.'.xlsx';
}else{ //失败数据写入失败
Log::channel('api')->error('错误文件excel写入失败');
}
}
return compact('successData','failData','failTotal','successTotal','failDataFile');
return compact('successData','total','failData','failTotal','successTotal','failDataFile');
}
//保存内容到excel文件并下载
public function storeExcelStyle($excelName,$datas,$endLen='',$path=''){
//保存内容到excel文件并下载,$exportType excel文件操作类型 0存储数据直接保存, 1直接导出数据 , 2保存并导出数据
public function storeExcelStyle($excelName,$datas,$endLen='',$path='',$exportType=0){
try{
$arr = range('A','Z');
if (!empty($endLen)){
......@@ -145,11 +151,27 @@ class StrategyService
});
});
if (empty($path)){//默认路径storage/exports
$e->store('xlsx');
}else{//自定义路径 storage/$path
$e->store('xlsx', storage_path($path));
if ($exportType == 1){
if (empty($path)){//默认路径storage/exports
$e->export('xlsx');
}else{//自定义路径 storage/$path
$e->export('xlsx', storage_path($path));
}
}elseif ($exportType == 2){
if (empty($path)){//默认路径storage/exports
$e->store('xlsx')->export('xlsx');
}else{//自定义路径 storage/$path
$e->store('xlsx', storage_path($path))->export('xlsx', storage_path($path));
}
}else{
if (empty($path)){//默认路径storage/exports
$e->store('xlsx');
}else{//自定义路径 storage/$path
$e->store('xlsx', storage_path($path));
}
}
return true;
}catch(\Exception $e){
Log::channel('api')->error($e->getFile().'-'.$e->getLine().'-'.$e->getMessage());
......@@ -173,10 +195,105 @@ class StrategyService
$insertData[] = $insertTemp;
}
//dd($insertData);
return StrategyExcel::insert($insertData);
$insertExcelReturn = StrategyExcel::insert($insertData);
if ($insertExcelReturn){//创建任务数据,这一步剥离出来,不然接口返回太慢,方案==,使用队列处理
$jobParams = ['strategySn'=>$strategySn];
$job = (new StrategyTaskJob($jobParams))->onQueue('StrategyTask');
dispatch($job);
//$this->createTaskDataJob($strategySn);
return true;
}
}
return false;
}
//跟进策略任务数据生成待新建任务的excel数据,(两种情况 一、有新数据时当时生成,二、每月1号生成)$operaterType类型 1为每月1号生成,0是按照当前日期计算
public function createTaskDataJob($strategySn){
//set_time_limit(0);
if (empty($strategyInfo)){
$strategyInfo = StrategySet::where('strategy_sn',$strategySn)->first()->toArray();
}
$cmonth = date('n');//当前月份(不带0)
$data = StrategyExcel::where([['strategy_sn',$strategySn],['last_handle_month','<>',$cmonth]])->get();
$batch = 500; //单批处理多少条
$total = count($data);
$now = date('Y-m-d H:i:s');
$taskData = [];
//一天呼叫两次类型,拆分时间,拆分项目
$call_time = explode("#", $strategyInfo['call_time_set']);
$project_sn = explode("#", $strategyInfo['project_sn']);
$tempi = 0;
if ($strategyInfo['day_type'] == 0){//每天拨打
$startDay = date('Y-m-d'); //执行当天时间
//这个月最后一天日期
$endDay = date('Y-m-d',strtotime("-1 day",strtotime("+1 month",strtotime(date('Y-m-01')))));
for ($i=$startDay;$i<=$endDay;$i=date("Y-m-d",strtotime("+1 day",strtotime($i)))){
foreach ($data as $k=>$v){
$tempi ++;
$taskData[] = [
'name'=>$v->name,
'phone'=>$v->phone,
'duty_date'=>$v->duty_date,
'parent_sn'=>$strategyInfo['parent_sn'],
'call_number'=>$strategyInfo['call_number'],
'project_sn'=>$project_sn[0],//$strategyInfo['project_sn'],
'call_date'=>$i,
'call_time'=>$call_time[0],//$strategyInfo['call_time'],
'created_at'=>$now
];
if(count($taskData) == $batch || $total == $batch || $tempi == $total){
if (!empty($taskData)){
StrategyTask::insert($taskData);
$taskData = [];
}
}
}
}
}else{
$doubleTotal = $total*2;
foreach ($data as $k=>$v){
$tempi = $tempi+2;
$yesterday = date('Y-m-d',strtotime("-1 day",strtotime($v->duty_date)));
$taskData[] = [
'name'=>$v->name,
'phone'=>$v->phone,
'duty_date'=>$v->duty_date,
'parent_sn'=>$strategyInfo['parent_sn'],
'call_number'=>$strategyInfo['call_number'],
'project_sn'=>$project_sn[0],
'call_date'=>$yesterday,
'call_time'=>$call_time[0],
'created_at'=>$now
];
$taskData[] = [
'name'=>$v->name,
'phone'=>$v->phone,
'duty_date'=>$v->duty_date,
'parent_sn'=>$strategyInfo['parent_sn'],
'call_number'=>$strategyInfo['call_number'],
'project_sn'=>$project_sn[1],
'call_date'=>$v['duty_date'],
'call_time'=>$call_time[1],
'created_at'=>$now
];
if(count($taskData) == $batch || $doubleTotal == $batch || $tempi == $doubleTotal){
if (!empty($taskData)){
StrategyTask::insert($taskData);
$taskData = [];
}
}
}
}
}
}
......
......@@ -54,6 +54,7 @@ class DatabaseSeeder extends Seeder
$table->char('name',50)->nullable(); //姓名
$table->date('duty_date')->nullable(); //值班日期
$table->unsignedBigInteger('phone'); //手机号
$table->unsignedSmallInteger('last_handle_month')->default(0);//最后处理月份,当月处理后不再处理
$table->timestamp('updated_at')->nullable();
$table->timestamp('created_at')->nullable();
});
......
......@@ -20,11 +20,14 @@ Route::middleware('auth:api')->get('/user', function (Request $request) {
//接口路由
Route::post('login', 'LoginApiController@index');
Route::group(['middleware' => 'check.apikey','namespace' => 'strategy'], function () {
Route::match(['get', 'post'],'strategyLists', 'InfoController@index');//初始化
Route::match(['get', 'post'],'strategyLists', 'InfoController@index');//初始化
Route::group(['middleware' => 'check.apikey','namespace'=>'strategy'], function () {
Route::post('strategy/getProjectsList', 'StrategySetController@getProjectsList')->name('strategy.getProjectsList');//项目列表
Route::get('strategy/downloadModel', 'StrategySetController@downloadModel')->name('strategy.downloadModel');//导出模板
Route::apiResource('strategySet', StrategySetController::class);
Route::apiResource('strategy', 'StrategySetController');
});
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 to comment