JLEventController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace Modules\Callback\Http\Controllers\JLEvent;
  3. use Catch\Base\CatchController;
  4. use Illuminate\Foundation\Validation\ValidatesRequests;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. use Modules\Callback\Services\CallbackConst;
  8. use Modules\Callback\Services\JLEventService;
  9. use Modules\Common\Errors\Errors;
  10. use Modules\Common\Exceptions\CommonBusinessException;
  11. /**
  12. * 巨量2.0事件-微信小程序
  13. */
  14. class JLEventController extends CatchController
  15. {
  16. use ValidatesRequests;
  17. /**
  18. * 回传配置列表
  19. * @param Request $request
  20. */
  21. public function list(Request $request) {
  22. $id = $request->input('id');
  23. $name = $request->input('name');
  24. $promotionId = $request->input('promotion_id');
  25. $unbind = $request->input('unbind', 0);
  26. $result = DB::table('jl_event_callback_config as config')
  27. ->leftJoin('promotions', 'promotions.callback_config_id', '=', 'config.id')
  28. ->where([
  29. 'user_id' => $this->getLoginUserId(),
  30. ])->when($id, function ($query, $id){
  31. return $query->where('config.id', $id);
  32. })->when($name, function ($query, $name){
  33. return $query->where('config.name', 'like', '%' .$name. '%');
  34. })->when($promotionId, function ($query, $promotionId){
  35. return $query->where('promotions.id', $promotionId)
  36. ->where('promotions.callback_type' , CallbackConst::TYPE_JL_EVENT_20);
  37. })->when(1 == $unbind, function ($query){
  38. return $query->whereNull('promotions.id');
  39. })
  40. ->select('config.id', 'config.name', 'config.charge_type', 'config.updated_at',
  41. 'promotions.name as promotion_name', 'promotions.id as promotion_id',
  42. 'config.charge_money_map', 'config.is_roi')
  43. ->orderBy('id', 'desc')
  44. ->paginate($request->input('limit', 15));
  45. foreach ($result as $item){
  46. $item->charge_type_str = CallbackConst::CHARGE_TYPE_MAP[$item->charge_type] ?? '-';
  47. $item->charge_money_map = \json_decode($item->charge_money_map) ?? [];
  48. }
  49. return $result;
  50. }
  51. /**
  52. * 新增回传配置
  53. * @param Request $request
  54. */
  55. public function add(Request $request) {
  56. $this->validate($request, [
  57. 'name' => 'required|string|max:64',
  58. // roi 全量上报:1-全量上报,2-不全量上报
  59. 'is_roi' => 'required|integer|in:1,2',
  60. // 充值行为:1-首充,2-所有充值
  61. 'charge_type' => 'required_if:is_roi,2|integer|in:1,2',
  62. // 金额项
  63. 'charge_money_map' => 'required_if:is_roi,2|array',
  64. 'charge_money_map.*.min_money' => 'integer|min:0',
  65. ]);
  66. $now = date('Y-m-d H:i:s');
  67. if(2 == $request->input('is_roi') &&
  68. (!JLEventService::judgeChargeMoneyOk($request->input('charge_money_map')))) {
  69. CommonBusinessException::throwError(Errors::CALLBACK_CHARGE_MONEY_MAP_ERROR);
  70. }
  71. DB::table('jl_event_callback_config')
  72. ->insert(array_merge($request->only('name', 'is_roi', 'charge_type'), [
  73. 'created_at' => $now, 'updated_at' => $now,
  74. 'charge_money_map' => \json_encode($request->input('charge_money_map')),
  75. 'user_id' => $this->getLoginUserId()
  76. ]));
  77. return 'ok';
  78. }
  79. /**
  80. * 更新回传配置
  81. * @param Request $request
  82. */
  83. public function update(Request $request) {
  84. $this->validate($request, [
  85. 'ids' => 'required|array',
  86. 'ids.*' => 'integer',
  87. // roi 全量上报:1-全量上报,2-不全量上报
  88. 'is_roi' => 'required|integer|in:1,2',
  89. // 充值行为:1-首充,2-所有充值
  90. 'charge_type' => 'required_if:is_roi,2|integer|in:1,2',
  91. // 金额项
  92. 'charge_money_map' => 'required_if:is_roi,2|array',
  93. 'charge_money_map.*.min_money' => 'integer|min:0',
  94. ]);
  95. if(1 == count($request->input('ids'))) {
  96. $updateData = $request->only('is_roi', 'charge_type', 'name');
  97. } else {
  98. $updateData = $request->only('is_roi', 'charge_type');
  99. }
  100. $now = date('Y-m-d H:i:s');
  101. DB::table('jl_event_callback_config')
  102. ->whereIn('id', $request->input('ids'))
  103. ->where('user_id', $this->getLoginUserId())
  104. ->update(array_merge($updateData, [
  105. 'charge_money_map' => \json_encode($request->input('charge_money_map', [])),
  106. 'updated_at' => $now,
  107. ]));
  108. // todo: 更新对应配置的计算比例信息
  109. return 'ok';
  110. }
  111. }