JuliangAccountController.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace Modules\Callback\Http\Controllers;
  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\Common\Errors\Errors;
  8. use Modules\Common\Exceptions\CommonBusinessException;
  9. use Modules\User\Http\Controllers\UserTrait;
  10. class JuliangAccountController extends CatchController
  11. {
  12. use UserTrait;
  13. use ValidatesRequests;
  14. public function list(Request $request) {
  15. $advAccountId = $request->input('account_id');
  16. $advAccountName = $request->input('account_name');
  17. return DB::table('juliang_account_callback_config')
  18. ->where(['company_uid' => $this->getOptimizerUid()])
  19. ->when($advAccountId, function ($query, $advAccountId) {
  20. return $query->where('adv_account_id' , $advAccountId);
  21. })->when($advAccountName, function ($query, $advAccountName) {
  22. return $query->where('adv_account_name', 'like', '%'. $advAccountName. '%');
  23. })
  24. ->paginate($request->input('limit', 30));
  25. }
  26. public function addAccount(Request $request) {
  27. $this->validate($request, [
  28. 'account_id' => 'required',
  29. 'account_name' => 'required|string|max:64',
  30. 'callback_state' => 'required|integer|in:0,1',
  31. 'protect_num' => 'required|integer|min:0',
  32. 'default_rate' => 'required|min:0|max:100',
  33. 'rate_time_config' => 'nullable|array',
  34. 'min_money' => 'required|min:0',
  35. 'max_money' => 'required|min:0'
  36. ]);
  37. if(DB::table('juliang_account_callback_config')
  38. ->where(['company_uid' => $this->getOptimizerUid(), 'adv_account_id' => $request->input('account_id')])
  39. ->exists()) {
  40. CommonBusinessException::throwError(Errors::JULIANG_ACCOUNT_EXISTS);
  41. }
  42. if($request->input('rate_time_config') &&
  43. !$this->is_time_cross($request->input('rate_time_config'))) {
  44. CommonBusinessException::throwError(Errors::CALLBACK_RATE_TIME_RANGE_ERROR);
  45. }
  46. $now = date('Y-m-d H:i:s');
  47. DB::table('juliang_account_callback_config')
  48. ->insert([
  49. 'adv_account_id' => $request->input('account_id'),
  50. 'adv_account_name' => $request->input('account_name'),
  51. 'state' => $request->input('state'),
  52. 'protect_num' => $request->input('protect_num'),
  53. 'default_rate' => $request->input('default_rate'),
  54. 'rate_time_config' => \json_encode($request->input('rate_time_config', [])),
  55. 'min_money' => $request->input('min_money'),
  56. 'max_money' => $request->input('max_money'),
  57. 'company_uid' => $this->getOptimizerUid(),
  58. 'created_at' => $now,
  59. 'updated_at' => $now,
  60. ]);
  61. DB::table('juliang_account_rate_config_log')
  62. ->insert([
  63. 'company_uid' => $this->getOptimizerUid(),
  64. 'account_id' => $request->input('account_id'),
  65. 'config_per' => $request->input('default_rate'),
  66. 'created_at' => $now,
  67. 'updated_at' => $now,
  68. ]);
  69. if($request->input('rate_time_config')) {
  70. $this->saveTimeConfig($this->getOptimizerUid(), $request->input('account_id'), $request);
  71. }
  72. return 'ok';
  73. }
  74. public function updateCallbackConfig(Request $request) {
  75. $this->validate($request, [
  76. 'ids' => 'required|array',
  77. 'state' => 'required|integer|in:0,1',
  78. 'protect_num' => 'required|integer|min:0',
  79. 'default_rate' => 'required|min:0|max:100',
  80. 'rate_time_config' => 'nullable|array',
  81. 'min_money' => 'required|min:0',
  82. 'max_money' => 'required|min:0'
  83. ]);
  84. if($request->input('callback_rate_time_config') &&
  85. !$this->is_time_cross($request->input('rate_time_config'))) {
  86. CommonBusinessException::throwError(Errors::CALLBACK_RATE_TIME_RANGE_ERROR);
  87. }
  88. $now = date('Y-m-d H:i:s');
  89. foreach ($request->input('ids') as $id) {
  90. DB::table('juliang_account_callback_config')
  91. ->where(['id' => $id, 'company_uid' => $this->getOptimizerUid()])
  92. ->update([
  93. 'state' => $request->input('state'),
  94. 'protect_num' => $request->input('protect_num'),
  95. 'default_rate' => $request->input('default_rate'),
  96. 'rate_time_config' => \json_encode($request->input('rate_time_config', [])),
  97. 'min_money' => $request->input('min_money'),
  98. 'max_money' => $request->input('max_money'),
  99. 'updated_at' => $now,
  100. ]);
  101. }
  102. $advAccountIds = DB::table('juliang_account_callback_config')
  103. ->whereIn('id', $request->input('ids'))
  104. ->where('company_uid', $this->getOptimizerUid())
  105. ->select('adv_account_id')->get()->pluck('adv_account_id');
  106. if($advAccountIds->isNotEmpty()) {
  107. DB::table('juliang_account_rate_config_log')
  108. ->where('company_uid', $this->getOptimizerUid())
  109. ->whereIn('account_id', $advAccountIds)
  110. ->where('is_enabled', 1)
  111. ->update(['is_enabled' => 0, 'updated_at' => $now]);
  112. DB::table('juliang_account_promotion_protect_record')
  113. ->where('optimizer_uid', $this->getOptimizerUid())
  114. ->whereIn('advertiser_id', $advAccountIds)
  115. ->where('is_enabled', 1)
  116. ->update(['is_enabled' => 0, 'updated_at' => $now]);
  117. foreach ($advAccountIds as $accountId) {
  118. DB::table('juliang_account_rate_config_log')
  119. ->insert([
  120. 'company_uid' => $this->getOptimizerUid(),
  121. 'account_id' => $accountId,
  122. 'config_per' => $request->input('default_rate'),
  123. 'created_at' => $now,
  124. 'updated_at' => $now,
  125. ]);
  126. $this->saveTimeConfig($this->getOptimizerUid(), $accountId, $request);
  127. }
  128. }
  129. return 'ok';
  130. }
  131. /**
  132. * 判断时 设置的时间范围是否有交集
  133. *
  134. * @param string $config_time 开始时间1
  135. * @return bool
  136. */
  137. protected function is_time_cross($config_time) {
  138. $timeline = [];
  139. foreach ($config_time as $key => $value) {
  140. $start_time = $value['start_time'];
  141. $start = explode(':', $start_time);
  142. $cur_start = (int)$start[0] * 60 + $start[1];
  143. $end_time = $value['end_time'];
  144. $end = explode(':', $end_time);
  145. $cur_end = (int)$end[0] * 60 + $end[1];
  146. if ($cur_end <= $cur_start) {
  147. return false;
  148. }
  149. if ($timeline) {
  150. foreach ($timeline as $k => $v) {
  151. if ($cur_start >= $v['start'] && $cur_start <= $v['end']) {
  152. return false;
  153. }
  154. if ($cur_end >= $v['start'] && $cur_end <= $v['end']) {
  155. return false;
  156. }
  157. }
  158. }
  159. $timeline[] = ['start'=>$cur_start,'end'=>$cur_end];
  160. }
  161. return true;
  162. }
  163. public function saveTimeConfig($companyUid, $accountId, $configInfo )
  164. {
  165. DB::table('juliang_account_promotion_config_time')
  166. ->where('is_enable',1)
  167. ->where('company_uid',$companyUid)
  168. ->where('account_id',$accountId)->update(['is_enable'=>0]);
  169. $time_config = $configInfo['rate_time_config'];
  170. if (empty($time_config)) {
  171. return false;
  172. }
  173. $is = $this->is_time_cross($time_config);
  174. if (!$is) {
  175. return false;
  176. }
  177. $data = [];
  178. $temp['company_uid'] = $companyUid;
  179. $temp['account_id'] = $accountId;
  180. $temp['other_data']['default_rate'] = $configInfo['default_rate'];
  181. $temp['other_data']['min_money'] = $configInfo['min_money'];
  182. $temp['other_data']['max_money'] = $configInfo['max_money'];
  183. $temp['other_data']['protect_num'] = $configInfo['protect_num'];
  184. $temp['other_data']['state'] = $configInfo['state'];
  185. $temp['other_data'] = json_encode($temp['other_data']);
  186. $temp['next_exec_time'] = date('Y-m-d');
  187. $temp['is_enable'] = 1;
  188. $temp['created_at'] = date('Y-m-d H:i:s',time());
  189. foreach ($time_config as $value) {
  190. $start_time = $value['start_time'];
  191. $end_time = $value['end_time'];
  192. $temp['config_time'] = $start_time;
  193. $temp['config_per'] = $value['config_per'];
  194. $data[] = $temp;
  195. //结束后 百分比改为默认的
  196. $temp['config_time'] = $end_time;
  197. $temp['config_per'] = $configInfo['default_rate'];
  198. $data[] = $temp;
  199. }
  200. //插入设置最新的时间段百分比
  201. DB::table('juliang_account_promotion_config_time')->insert($data);
  202. }
  203. }