作者 李美松

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

  1 +<?php
  2 +
  3 +namespace App\Http\Controllers\Bside\Aicc;
  4 +
  5 +use App\Enums\Common\Code;
  6 +use App\Exceptions\BsideGlobalException;
  7 +use App\Http\Controllers\Bside\BaseController;
  8 +use App\Http\Logic\Aside\Aicc\AiccWeChatLogic;
  9 +use Illuminate\Http\Request;
  10 +use Psr\Container\ContainerExceptionInterface;
  11 +use Psr\Container\NotFoundExceptionInterface;
  12 +
  13 +class AiccWeChatController extends BaseController
  14 +{
  15 + private $aiccWeChatLogic;
  16 +
  17 + public function __construct(Request $request)
  18 + {
  19 + $this->aiccWeChatLogic = new AiccWeChatLogic();
  20 + parent::__construct($request);
  21 + }
  22 +
  23 + /**
  24 + * V6与AICC数据关联
  25 + * @return void
  26 + * @throws BsideGlobalException
  27 + */
  28 + public function save()
  29 + {
  30 + $project_id = (int)request()->post('project_id', 0);
  31 + if (empty($project_id)) {
  32 + $this->fail('请选择项目!', Code::USER_PARAMS_ERROE);
  33 + }
  34 + $status = (bool)request()->post('status', 1); # 1 - 正常, 0 - 禁用
  35 + $wx_user_id = (int)request()->post('user_id', 0);
  36 + if (empty($wx_user_id) && $status) {
  37 + $this->fail('请选择要绑定的AICC用户!', Code::USER_PARAMS_ERROE);
  38 + }
  39 + $wx_id = (int)request()->post('wx_id', 0);
  40 + if (empty($wx_id) && $status) {
  41 + $this->fail('请选择要绑定的AICC项目!', Code::USER_PARAMS_ERROE);
  42 + }
  43 + $wx_nickname = request()->post('nickname', '');
  44 + $wx_user_name = request()->post('user_name', '');
  45 + $wx_image = request()->post('image', '');
  46 + $data = compact('project_id', 'wx_id', 'wx_nickname', 'wx_user_id', 'wx_user_name', 'wx_image');
  47 + $this->aiccWeChatLogic->saveData($data);
  48 + $this->response('success');
  49 + }
  50 +
  51 + /**
  52 + * 数据推送到AICC
  53 + * @return void
  54 + * @throws ContainerExceptionInterface
  55 + * @throws NotFoundExceptionInterface
  56 + */
  57 + public function dataPush()
  58 + {
  59 + $page = (int)request()->get('page', 1);
  60 + $perPage = (int)request()->get('perPage', 15); # 分页页数
  61 + $data = $this->aiccWeChatLogic->AiccWeChatLists($page, $perPage);
  62 + $this->response('success', Code::SUCCESS, $data);
  63 + }
  64 +}
  1 +<?php
  2 +
  3 +namespace App\Http\Logic\Aside\Aicc;
  4 +
  5 +use App\Enums\Common\Code;
  6 +use App\Http\Logic\Logic;
  7 +use App\Models\AICC\AiccWechat;
  8 +use Illuminate\Support\Facades\DB;
  9 +
  10 +class AiccWeChatLogic extends Logic
  11 +{
  12 + public function saveData($data)
  13 + {
  14 + $wx = new AiccWechat();
  15 + DB::beginTransaction();
  16 + try {
  17 + $status = $wx->saveData($data);
  18 + DB::commit();
  19 + } catch (\Exception $e) {
  20 + DB::rollBack();
  21 + $e->getMessage();
  22 + errorLog('V6与AICC关联失败', $wx, $e);
  23 + $this->fail('请检查操作是否正确!', Code::SERVER_MYSQL_ERROR);
  24 + }
  25 + return $status;
  26 + }
  27 +
  28 + /**
  29 + * @return array
  30 + */
  31 + public function AiccWeChatLists($page = 1, $perPage = 15)
  32 + {
  33 + $status = 1; # 1 - 正常, 0 - 禁用
  34 + $lists = AiccWechat::query()->where('status', $status)
  35 + ->whereNotNull('project_id')
  36 + ->whereNotNull('wx_user_id')
  37 + ->paginate($perPage, ['project_id', 'wx_id', 'wx_user_id'], 'page', $page);
  38 + $items = $lists->Items();
  39 + $totalPage = $lists->lastPage();
  40 + $total = $lists->total();
  41 + $currentPage = $lists->currentPage();
  42 + return compact('total', 'items', 'totalPage', 'currentPage');
  43 + }
  44 +}
  1 +<?php
  2 +
  3 +namespace App\Models\AICC;
  4 +
  5 +use Illuminate\Database\Eloquent\Builder;
  6 +use Illuminate\Database\Eloquent\Model;
  7 +
  8 +class AiccWechat extends Model
  9 +{
  10 + protected $table = 'gl_aicc_wechat';
  11 +
  12 + /**
  13 + * 保存|修改数据
  14 + * @param array $data
  15 + * @return bool
  16 + */
  17 + public function saveData(array $data): bool
  18 + {
  19 + $project_id = (int)$data['project_id'] ?? 0;
  20 + $isRes = self::query()->where('project_id', $project_id)->first();
  21 + if (!$isRes) {
  22 + $isRes = new self();
  23 + $isRes->project_id = $project_id;
  24 + }
  25 + $isRes->wx_id = $data['wx_id'];
  26 + $isRes->wx_nickname = $data['wx_nickname'];
  27 + $isRes->wx_user_id = $data['wx_user_id'];
  28 + $isRes->wx_user_name = $data['wx_user_name'];
  29 + $isRes->wx_image = $data['wx_image'];
  30 + return $isRes->save();
  31 + }
  32 +
  33 + /**
  34 + * 检查项目是否存在
  35 + * @param $project_id
  36 + * @return Builder|Model|object|null
  37 + */
  38 + public function check($project_id)
  39 + {
  40 + return self::query()->where('project_id', $project_id)->first();
  41 + }
  42 +}