|
@@ -0,0 +1,165 @@
|
|
|
|
+<?php
|
|
|
|
+
|
|
|
|
+namespace Modules\Callback\Http\Controllers;
|
|
|
|
+
|
|
|
|
+use Carbon\Carbon;
|
|
|
|
+use Illuminate\Http\Request;
|
|
|
|
+use Catch\Base\CatchController;
|
|
|
|
+use Modules\Callback\Services\CallbackConst;
|
|
|
|
+use Modules\Callback\Models\TencentAdqRateConfigLogModel;
|
|
|
|
+use Modules\Callback\Models\TencentAdqCallbackConfigModel;
|
|
|
|
+use Modules\Callback\Services\JLEventService;
|
|
|
|
+use Modules\Common\Errors\Errors;
|
|
|
|
+use Illuminate\Foundation\Validation\ValidatesRequests;
|
|
|
|
+use Modules\Common\Exceptions\CommonBusinessException;
|
|
|
|
+
|
|
|
|
+class TencentAdqController extends CatchController
|
|
|
|
+{
|
|
|
|
+ use ValidatesRequests;
|
|
|
|
+
|
|
|
|
+ public function __construct(protected readonly TencentAdqCallbackConfigModel $tencentAdqCallbackConfigModel,
|
|
|
|
+ protected readonly TencentAdqRateConfigLogModel $tencentAdqRateConfigLogModel )
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 回传配置列表
|
|
|
|
+ * @param Request $request
|
|
|
|
+ */
|
|
|
|
+ public function index(Request $request)
|
|
|
|
+ {
|
|
|
|
+ $id = $request->get('id');
|
|
|
|
+ $name = $request->get('name');
|
|
|
|
+ $promotionId = $request->get('promotion_id');
|
|
|
|
+ $unbind = $request->get('unbind', 0);
|
|
|
|
+
|
|
|
|
+ $result = $this->tencentAdqCallbackConfigModel
|
|
|
|
+ ->leftJoin('promotions', function($join){
|
|
|
|
+ $join->on('promotions.callback_config_id', '=', 'config.id')
|
|
|
|
+ ->where('promotions.callback_type' , CallbackConst::TYPE_TENCENT_ADQ_DIRECT);
|
|
|
|
+ })
|
|
|
|
+ ->where([
|
|
|
|
+ 'user_id' => $this->getLoginUserId(),
|
|
|
|
+ ])->when($id, function ($query, $id){
|
|
|
|
+ return $query->where('config.id', $id);
|
|
|
|
+ })->when($name, function ($query, $name){
|
|
|
|
+ return $query->where('config.name', 'like', '%' .$name. '%');
|
|
|
|
+ })->when($promotionId, function ($query, $promotionId){
|
|
|
|
+ return $query->where('promotions.id', $promotionId)
|
|
|
|
+ ->where('promotions.callback_type' , CallbackConst::TYPE_TENCENT_ADQ_DIRECT);
|
|
|
|
+ })->when(1 == $unbind, function ($query){
|
|
|
|
+ return $query->whereNull('promotions.id');
|
|
|
|
+ })
|
|
|
|
+ ->select('config.id', 'config.name', 'config.charge_type', 'config.updated_at',
|
|
|
|
+ 'promotions.name as promotion_name', 'promotions.id as promotion_id',
|
|
|
|
+ 'config.charge_money_map', 'config.is_roi')
|
|
|
|
+ ->orderBy('id', 'desc')
|
|
|
|
+ ->paginate($request->get('limit', 15));
|
|
|
|
+ foreach ($result as $item){
|
|
|
|
+ $item->charge_type_str = CallbackConst::CHARGE_TYPE_MAP[$item->charge_type] ?? '-';
|
|
|
|
+ $item->charge_money_map = json_decode($item->charge_money_map) ?? [];
|
|
|
|
+ }
|
|
|
|
+ return $result;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 新增回传配置
|
|
|
|
+ * @param Request $request
|
|
|
|
+ */
|
|
|
|
+ public function store(Request $request)
|
|
|
|
+ {
|
|
|
|
+ $this->validate($request, [
|
|
|
|
+ 'name' => 'required|string|max:64',
|
|
|
|
+ // roi 全量上报:1-全量上报,2-不全量上报
|
|
|
|
+ 'is_roi' => 'required|integer|in:1,2',
|
|
|
|
+ // 充值行为:1-首充,2-所有充值
|
|
|
|
+ 'charge_type' => 'required_if:is_roi,2|integer|in:1,2',
|
|
|
|
+ // 金额项
|
|
|
|
+ 'charge_money_map' => 'required_if:is_roi,2|array',
|
|
|
|
+ 'charge_money_map.*.min_money' => 'integer|min:0',
|
|
|
|
+ ]);
|
|
|
|
+ if(2 == $request->input('is_roi') &&
|
|
|
|
+ (!JLEventService::judgeChargeMoneyOk($request->input('charge_money_map')))) {
|
|
|
|
+ CommonBusinessException::throwError(Errors::CALLBACK_CHARGE_MONEY_MAP_ERROR);
|
|
|
|
+ }
|
|
|
|
+ $userId = $this->getLoginUserId();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ $model = $this->tencentAdqCallbackConfigModel->create(array_merge($request->only('name', 'is_roi', 'charge_type'), [
|
|
|
|
+ 'charge_money_map' => json_encode($request->input('charge_money_map')),
|
|
|
|
+ 'user_id' => $userId,
|
|
|
|
+ ]));
|
|
|
|
+
|
|
|
|
+ $this->saveRateConfigLog($userId, $model->id, $request->input('charge_money_map', []));
|
|
|
|
+
|
|
|
|
+ return 'ok';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public function update(Request $request)
|
|
|
|
+ {
|
|
|
|
+ $this->validate($request, [
|
|
|
|
+ 'ids' => 'required|array',
|
|
|
|
+ 'ids.*' => 'integer',
|
|
|
|
+ // roi 全量上报:1-全量上报,2-不全量上报
|
|
|
|
+ 'is_roi' => 'required|integer|in:1,2',
|
|
|
|
+ // 充值行为:1-首充,2-所有充值
|
|
|
|
+ 'charge_type' => 'required_if:is_roi,2|integer|in:1,2',
|
|
|
|
+ // 金额项
|
|
|
|
+ 'charge_money_map' => 'required_if:is_roi,2|array',
|
|
|
|
+ 'charge_money_map.*.min_money' => 'integer|min:0',
|
|
|
|
+ ]);
|
|
|
|
+
|
|
|
|
+ if(1 == count($request->post('ids'))) {
|
|
|
|
+ $updateData = $request->only('is_roi', 'charge_type', 'name');
|
|
|
|
+ } else {
|
|
|
|
+ $updateData = $request->only('is_roi', 'charge_type');
|
|
|
|
+ }
|
|
|
|
+ $now = Carbon::now();
|
|
|
|
+ $userId = $this->getLoginUserId();
|
|
|
|
+ $configIds = $this->tencentAdqCallbackConfigModel->whereIn('id', $request->post('ids'))
|
|
|
|
+ ->where('user_id', $userId)->select('id')->get()->pluck('id');
|
|
|
|
+ $this->tencentAdqCallbackConfigModel->whereIn('id', $configIds)
|
|
|
|
+ ->update(array_merge($updateData, [
|
|
|
|
+ 'charge_money_map' => json_encode($request->post('charge_money_map', [])),
|
|
|
|
+ 'updated_at' => $now,
|
|
|
|
+ ]));
|
|
|
|
+ foreach ($configIds as $configId) {
|
|
|
|
+ $this->saveRateConfigLog($userId, $configId, $request->post('charge_money_map', []));
|
|
|
|
+ }
|
|
|
|
+ return 'ok';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private function saveRateConfigLog($userId, $configId, $chargeMoneyMap){
|
|
|
|
+ $now = Carbon::now();
|
|
|
|
+ $this->tencentAdqRateConfigLogModel
|
|
|
|
+ ->where([
|
|
|
|
+ 'user_id' => $userId, 'config_id' => $configId,
|
|
|
|
+ 'is_enabled' => 1,
|
|
|
|
+ ])->update([
|
|
|
|
+ 'is_enabled' => 0, 'updated_at' => $now,
|
|
|
|
+ ]);
|
|
|
|
+
|
|
|
|
+ $insertData = [];
|
|
|
|
+ foreach ($chargeMoneyMap as $item) {
|
|
|
|
+ if(3 == $item['callback_type']) {
|
|
|
|
+ $insertData[] = [
|
|
|
|
+ 'user_id' => $userId,
|
|
|
|
+ 'config_id' => $configId,
|
|
|
|
+ 'rate_str' => $item['callback_param'],
|
|
|
|
+ 'created_at' => $now, 'updated_at' => $now,
|
|
|
|
+ 'min_money' => $item['min_money'],
|
|
|
|
+ 'max_money' => $item['max_money']
|
|
|
|
+ ];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if(count($insertData)) {
|
|
|
|
+ $this->tencentAdqRateConfigLogModel->insert($insertData);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|