TencentAdqController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace Modules\Callback\Http\Controllers;
  3. use Carbon\Carbon;
  4. use Illuminate\Http\Request;
  5. use Catch\Base\CatchController;
  6. use Modules\Callback\Services\CallbackConst;
  7. use Modules\Callback\Models\TencentAdqRateConfigLogModel;
  8. use Modules\Callback\Models\TencentAdqCallbackConfigModel;
  9. use Modules\Callback\Services\JLEventService;
  10. use Modules\Common\Errors\Errors;
  11. use Illuminate\Foundation\Validation\ValidatesRequests;
  12. use Modules\Common\Exceptions\CommonBusinessException;
  13. class TencentAdqController extends CatchController
  14. {
  15. use ValidatesRequests;
  16. public function __construct(protected readonly TencentAdqCallbackConfigModel $tencentAdqCallbackConfigModel,
  17. protected readonly TencentAdqRateConfigLogModel $tencentAdqRateConfigLogModel )
  18. {
  19. }
  20. /**
  21. * 回传配置列表
  22. * @param Request $request
  23. */
  24. public function index(Request $request)
  25. {
  26. $id = $request->get('id');
  27. $name = $request->get('name');
  28. $promotionId = $request->get('promotion_id');
  29. $unbind = $request->get('unbind', 0);
  30. $result = $this->tencentAdqCallbackConfigModel
  31. ->leftJoin('promotions', function($join){
  32. $join->on('promotions.callback_config_id', '=', 'config.id')
  33. ->where('promotions.callback_type' , CallbackConst::TYPE_TENCENT_ADQ_DIRECT);
  34. })
  35. ->where([
  36. 'user_id' => $this->getLoginUserId(),
  37. ])->when($id, function ($query, $id){
  38. return $query->where('config.id', $id);
  39. })->when($name, function ($query, $name){
  40. return $query->where('config.name', 'like', '%' .$name. '%');
  41. })->when($promotionId, function ($query, $promotionId){
  42. return $query->where('promotions.id', $promotionId)
  43. ->where('promotions.callback_type' , CallbackConst::TYPE_TENCENT_ADQ_DIRECT);
  44. })->when(1 == $unbind, function ($query){
  45. return $query->whereNull('promotions.id');
  46. })
  47. ->select('config.id', 'config.name', 'config.charge_type', 'config.updated_at',
  48. 'promotions.name as promotion_name', 'promotions.id as promotion_id',
  49. 'config.charge_money_map', 'config.is_roi')
  50. ->orderBy('id', 'desc')
  51. ->paginate($request->get('limit', 15));
  52. foreach ($result as $item){
  53. $item->charge_type_str = CallbackConst::CHARGE_TYPE_MAP[$item->charge_type] ?? '-';
  54. $item->charge_money_map = json_decode($item->charge_money_map) ?? [];
  55. }
  56. return $result;
  57. }
  58. /**
  59. * 新增回传配置
  60. * @param Request $request
  61. */
  62. public function store(Request $request)
  63. {
  64. $this->validate($request, [
  65. 'name' => 'required|string|max:64',
  66. // roi 全量上报:1-全量上报,2-不全量上报
  67. 'is_roi' => 'required|integer|in:1,2',
  68. // 充值行为:1-首充,2-所有充值
  69. 'charge_type' => 'required_if:is_roi,2|integer|in:1,2',
  70. // 金额项
  71. 'charge_money_map' => 'required_if:is_roi,2|array',
  72. 'charge_money_map.*.min_money' => 'integer|min:0',
  73. ]);
  74. if(2 == $request->input('is_roi') &&
  75. (!JLEventService::judgeChargeMoneyOk($request->input('charge_money_map')))) {
  76. CommonBusinessException::throwError(Errors::CALLBACK_CHARGE_MONEY_MAP_ERROR);
  77. }
  78. $userId = $this->getLoginUserId();
  79. $model = $this->tencentAdqCallbackConfigModel->create(array_merge($request->only('name', 'is_roi', 'charge_type'), [
  80. 'charge_money_map' => json_encode($request->input('charge_money_map')),
  81. 'user_id' => $userId,
  82. ]));
  83. $this->saveRateConfigLog($userId, $model->id, $request->input('charge_money_map', []));
  84. return 'ok';
  85. }
  86. public function update(Request $request)
  87. {
  88. $this->validate($request, [
  89. 'ids' => 'required|array',
  90. 'ids.*' => 'integer',
  91. // roi 全量上报:1-全量上报,2-不全量上报
  92. 'is_roi' => 'required|integer|in:1,2',
  93. // 充值行为:1-首充,2-所有充值
  94. 'charge_type' => 'required_if:is_roi,2|integer|in:1,2',
  95. // 金额项
  96. 'charge_money_map' => 'required_if:is_roi,2|array',
  97. 'charge_money_map.*.min_money' => 'integer|min:0',
  98. ]);
  99. if(1 == count($request->post('ids'))) {
  100. $updateData = $request->only('is_roi', 'charge_type', 'name');
  101. } else {
  102. $updateData = $request->only('is_roi', 'charge_type');
  103. }
  104. $now = Carbon::now();
  105. $userId = $this->getLoginUserId();
  106. $configIds = $this->tencentAdqCallbackConfigModel->whereIn('id', $request->post('ids'))
  107. ->where('user_id', $userId)->select('id')->get()->pluck('id');
  108. $this->tencentAdqCallbackConfigModel->whereIn('id', $configIds)
  109. ->update(array_merge($updateData, [
  110. 'charge_money_map' => json_encode($request->post('charge_money_map', [])),
  111. 'updated_at' => $now,
  112. ]));
  113. foreach ($configIds as $configId) {
  114. $this->saveRateConfigLog($userId, $configId, $request->post('charge_money_map', []));
  115. }
  116. return 'ok';
  117. }
  118. private function saveRateConfigLog($userId, $configId, $chargeMoneyMap){
  119. $now = Carbon::now();
  120. $this->tencentAdqRateConfigLogModel
  121. ->where([
  122. 'user_id' => $userId, 'config_id' => $configId,
  123. 'is_enabled' => 1,
  124. ])->update([
  125. 'is_enabled' => 0, 'updated_at' => $now,
  126. ]);
  127. $insertData = [];
  128. foreach ($chargeMoneyMap as $item) {
  129. if(3 == $item['callback_type']) {
  130. $insertData[] = [
  131. 'user_id' => $userId,
  132. 'config_id' => $configId,
  133. 'rate_str' => $item['callback_param'],
  134. 'created_at' => $now, 'updated_at' => $now,
  135. 'min_money' => $item['min_money'],
  136. 'max_money' => $item['max_money']
  137. ];
  138. }
  139. }
  140. if(count($insertData)) {
  141. $this->tencentAdqRateConfigLogModel->insert($insertData);
  142. }
  143. }
  144. }