作者 李美松

AICC与V6项目关联 - 逻辑优化

<?php
namespace App\Http\Controllers\Bside\Aicc;
use App\Enums\Common\Code;
use App\Exceptions\BsideGlobalException;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Aside\Aicc\AiccWeChatLogic;
use Illuminate\Http\Request;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class AiccWeChatController extends BaseController
{
private $aiccWeChatLogic;
public function __construct(Request $request)
{
$this->aiccWeChatLogic = new AiccWeChatLogic();
parent::__construct($request);
}
/**
* V6与AICC数据关联
* @return void
* @throws BsideGlobalException
*/
public function save()
{
$project_id = (int)request()->post('project_id', 0);
if (empty($project_id)) {
$this->fail('请选择项目!', Code::USER_PARAMS_ERROE);
}
$status = (bool)request()->post('status', 1); # 1 - 正常, 0 - 禁用
$wx_user_id = (int)request()->post('user_id', 0);
if (empty($wx_user_id) && $status) {
$this->fail('请选择要绑定的AICC用户!', Code::USER_PARAMS_ERROE);
}
$wx_id = (int)request()->post('wx_id', 0);
if (empty($wx_id) && $status) {
$this->fail('请选择要绑定的AICC项目!', Code::USER_PARAMS_ERROE);
}
$wx_nickname = request()->post('nickname', '');
$wx_user_name = request()->post('user_name', '');
$wx_image = request()->post('image', '');
$data = compact('project_id', 'wx_id', 'wx_nickname', 'wx_user_id', 'wx_user_name', 'wx_image');
$this->aiccWeChatLogic->saveData($data);
$this->response('success');
}
/**
* 数据推送到AICC
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function dataPush()
{
$page = (int)request()->get('page', 1);
$perPage = (int)request()->get('perPage', 15); # 分页页数
$data = $this->aiccWeChatLogic->AiccWeChatLists($page, $perPage);
$this->response('success', Code::SUCCESS, $data);
}
}
... ...
<?php
namespace App\Http\Logic\Aside\Aicc;
use App\Enums\Common\Code;
use App\Http\Logic\Logic;
use App\Models\AICC\AiccWechat;
use Illuminate\Support\Facades\DB;
class AiccWeChatLogic extends Logic
{
public function saveData($data)
{
$wx = new AiccWechat();
DB::beginTransaction();
try {
$status = $wx->saveData($data);
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
$e->getMessage();
errorLog('V6与AICC关联失败', $wx, $e);
$this->fail('请检查操作是否正确!', Code::SERVER_MYSQL_ERROR);
}
return $status;
}
/**
* @return array
*/
public function AiccWeChatLists($page = 1, $perPage = 15)
{
$status = 1; # 1 - 正常, 0 - 禁用
$lists = AiccWechat::query()->where('status', $status)
->whereNotNull('project_id')
->whereNotNull('wx_user_id')
->paginate($perPage, ['project_id', 'wx_id', 'wx_user_id'], 'page', $page);
$items = $lists->Items();
$totalPage = $lists->lastPage();
$total = $lists->total();
$currentPage = $lists->currentPage();
return compact('total', 'items', 'totalPage', 'currentPage');
}
}
... ...
<?php
namespace App\Models\AICC;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class AiccWechat extends Model
{
protected $table = 'gl_aicc_wechat';
/**
* 保存|修改数据
* @param array $data
* @return bool
*/
public function saveData(array $data): bool
{
$project_id = (int)$data['project_id'] ?? 0;
$isRes = self::query()->where('project_id', $project_id)->first();
if (!$isRes) {
$isRes = new self();
$isRes->project_id = $project_id;
}
$isRes->wx_id = $data['wx_id'];
$isRes->wx_nickname = $data['wx_nickname'];
$isRes->wx_user_id = $data['wx_user_id'];
$isRes->wx_user_name = $data['wx_user_name'];
$isRes->wx_image = $data['wx_image'];
return $isRes->save();
}
/**
* 检查项目是否存在
* @param $project_id
* @return Builder|Model|object|null
*/
public function check($project_id)
{
return self::query()->where('project_id', $project_id)->first();
}
}
... ...