JuliangAccountController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. $unBind = $request->input('unbind', 0);
  18. $alreadyBindConfigIds = null;
  19. if($unBind) {
  20. $alreadyBindConfigIds = DB::table('promotions')
  21. ->where([
  22. 'uid' => $this->getOptimizerUid(),
  23. 'callback_type' => 1,
  24. 'status' => 1,
  25. 'is_enabled' => 1,
  26. ])->where('callback_config_id' , '<>', 0)
  27. ->distinct()
  28. ->select('callback_config_id')
  29. ->get()->pluck('callback_config_id')->toArray();
  30. }
  31. $list = DB::table('juliang_account_callback_config')
  32. ->where(['company_uid' => $this->getOptimizerUid()])
  33. ->when($advAccountId, function ($query, $advAccountId) {
  34. return $query->where('adv_account_id' , $advAccountId);
  35. })->when($advAccountName, function ($query, $advAccountName) {
  36. return $query->where('adv_account_name', 'like', '%'. $advAccountName. '%');
  37. })->when($alreadyBindConfigIds, function ($query, $alreadyBindConfigIds) {
  38. return $query->whereNotIn('id', $alreadyBindConfigIds);
  39. })
  40. ->orderBy('id', 'desc')
  41. ->paginate($request->input('limit', 30));
  42. $ids = collect($list->items())->pluck('id');
  43. $promotions = DB::table('promotions')
  44. ->where([
  45. 'uid' => $this->getOptimizerUid(),
  46. 'callback_type' => 1,
  47. 'status' => 1,
  48. 'is_enabled' => 1,
  49. ])->whereIn('callback_config_id', $ids)
  50. ->select('name', 'id', 'callback_config_id')
  51. ->get()->keyBy('callback_config_id');
  52. foreach ($list as $item) {
  53. $item->promotion_name = $promotions->get($item->id)->name ?? '';
  54. $item->promotion_id = $promotions->get($item->id)->id ?? '';
  55. }
  56. return $list;
  57. }
  58. public function addAccount(Request $request) {
  59. $this->validate($request, [
  60. 'account_id' => 'required',
  61. 'account_name' => 'required|string|max:64',
  62. 'state' => 'required|integer|in:0,1',
  63. 'protect_num' => 'required|integer|min:0',
  64. 'default_rate' => 'required|min:0|max:100',
  65. 'rate_time_config' => 'nullable|array',
  66. 'min_money' => 'required|min:0',
  67. 'max_money' => 'required|min:0'
  68. ]);
  69. if(DB::table('juliang_account_callback_config')
  70. ->where(['company_uid' => $this->getOptimizerUid(), 'adv_account_id' => $request->input('account_id')])
  71. ->exists()) {
  72. CommonBusinessException::throwError(Errors::JULIANG_ACCOUNT_EXISTS);
  73. }
  74. if($request->input('rate_time_config') &&
  75. !$this->is_time_cross($request->input('rate_time_config'))) {
  76. CommonBusinessException::throwError(Errors::CALLBACK_RATE_TIME_RANGE_ERROR);
  77. }
  78. $now = date('Y-m-d H:i:s');
  79. DB::table('juliang_account_callback_config')
  80. ->insert([
  81. 'adv_account_id' => $request->input('account_id'),
  82. 'adv_account_name' => $request->input('account_name'),
  83. 'state' => $request->input('state'),
  84. 'protect_num' => $request->input('protect_num'),
  85. 'default_rate' => $request->input('default_rate'),
  86. 'rate_time_config' => \json_encode($request->input('rate_time_config', [])),
  87. 'min_money' => $request->input('min_money'),
  88. 'max_money' => $request->input('max_money'),
  89. 'company_uid' => $this->getOptimizerUid(),
  90. 'created_at' => $now,
  91. 'updated_at' => $now,
  92. ]);
  93. DB::table('juliang_account_rate_config_log')
  94. ->insert([
  95. 'company_uid' => $this->getOptimizerUid(),
  96. 'account_id' => $request->input('account_id'),
  97. 'config_per' => $request->input('default_rate'),
  98. 'created_at' => $now,
  99. 'updated_at' => $now,
  100. ]);
  101. if($request->input('rate_time_config')) {
  102. $this->saveTimeConfig($this->getOptimizerUid(), $request->input('account_id'), $request);
  103. }
  104. return 'ok';
  105. }
  106. public function updateCallbackConfig(Request $request) {
  107. $this->validate($request, [
  108. 'ids' => 'required|array',
  109. 'state' => 'required|integer|in:0,1',
  110. 'protect_num' => 'required|integer|min:0',
  111. 'default_rate' => 'required|min:0|max:100',
  112. 'rate_time_config' => 'nullable|array',
  113. 'min_money' => 'required|min:0',
  114. 'max_money' => 'required|min:0'
  115. ]);
  116. if($request->input('callback_rate_time_config') &&
  117. !$this->is_time_cross($request->input('rate_time_config'))) {
  118. CommonBusinessException::throwError(Errors::CALLBACK_RATE_TIME_RANGE_ERROR);
  119. }
  120. $now = date('Y-m-d H:i:s');
  121. foreach ($request->input('ids') as $id) {
  122. DB::table('juliang_account_callback_config')
  123. ->where(['id' => $id, 'company_uid' => $this->getOptimizerUid()])
  124. ->update([
  125. 'state' => $request->input('state'),
  126. 'protect_num' => $request->input('protect_num'),
  127. 'default_rate' => $request->input('default_rate'),
  128. 'rate_time_config' => \json_encode($request->input('rate_time_config', [])),
  129. 'min_money' => $request->input('min_money'),
  130. 'max_money' => $request->input('max_money'),
  131. 'updated_at' => $now,
  132. ]);
  133. }
  134. $advAccountIds = DB::table('juliang_account_callback_config')
  135. ->whereIn('id', $request->input('ids'))
  136. ->where('company_uid', $this->getOptimizerUid())
  137. ->select('adv_account_id')->get()->pluck('adv_account_id');
  138. if($advAccountIds->isNotEmpty()) {
  139. DB::table('juliang_account_rate_config_log')
  140. ->where('company_uid', $this->getOptimizerUid())
  141. ->whereIn('account_id', $advAccountIds)
  142. ->where('is_enabled', 1)
  143. ->update(['is_enabled' => 0, 'updated_at' => $now]);
  144. DB::table('juliang_account_promotion_protect_record')
  145. ->where('optimizer_uid', $this->getOptimizerUid())
  146. ->whereIn('advertiser_id', $advAccountIds)
  147. ->where('is_enabled', 1)
  148. ->update(['is_enabled' => 0, 'updated_at' => $now]);
  149. foreach ($advAccountIds as $accountId) {
  150. DB::table('juliang_account_rate_config_log')
  151. ->insert([
  152. 'company_uid' => $this->getOptimizerUid(),
  153. 'account_id' => $accountId,
  154. 'config_per' => $request->input('default_rate'),
  155. 'created_at' => $now,
  156. 'updated_at' => $now,
  157. ]);
  158. $this->saveTimeConfig($this->getOptimizerUid(), $accountId, $request);
  159. }
  160. }
  161. return 'ok';
  162. }
  163. /**
  164. * 判断时 设置的时间范围是否有交集
  165. *
  166. * @param string $config_time 开始时间1
  167. * @return bool
  168. */
  169. protected function is_time_cross($config_time) {
  170. $timeline = [];
  171. foreach ($config_time as $key => $value) {
  172. $start_time = $value['start_time'];
  173. $start = explode(':', $start_time);
  174. $cur_start = (int)$start[0] * 60 + $start[1];
  175. $end_time = $value['end_time'];
  176. $end = explode(':', $end_time);
  177. $cur_end = (int)$end[0] * 60 + $end[1];
  178. if ($cur_end <= $cur_start) {
  179. return false;
  180. }
  181. if ($timeline) {
  182. foreach ($timeline as $k => $v) {
  183. if ($cur_start >= $v['start'] && $cur_start <= $v['end']) {
  184. return false;
  185. }
  186. if ($cur_end >= $v['start'] && $cur_end <= $v['end']) {
  187. return false;
  188. }
  189. }
  190. }
  191. $timeline[] = ['start'=>$cur_start,'end'=>$cur_end];
  192. }
  193. return true;
  194. }
  195. public function saveTimeConfig($companyUid, $accountId, $configInfo )
  196. {
  197. DB::table('juliang_account_promotion_config_time')
  198. ->where('is_enable',1)
  199. ->where('company_uid',$companyUid)
  200. ->where('account_id',$accountId)->update(['is_enable'=>0]);
  201. $time_config = $configInfo['rate_time_config'];
  202. if (empty($time_config)) {
  203. return false;
  204. }
  205. $is = $this->is_time_cross($time_config);
  206. if (!$is) {
  207. return false;
  208. }
  209. $data = [];
  210. $temp['company_uid'] = $companyUid;
  211. $temp['account_id'] = $accountId;
  212. $temp['other_data']['default_rate'] = $configInfo['default_rate'];
  213. $temp['other_data']['min_money'] = $configInfo['min_money'];
  214. $temp['other_data']['max_money'] = $configInfo['max_money'];
  215. $temp['other_data']['protect_num'] = $configInfo['protect_num'];
  216. $temp['other_data']['state'] = $configInfo['state'];
  217. $temp['other_data'] = json_encode($temp['other_data']);
  218. $temp['next_exec_time'] = date('Y-m-d');
  219. $temp['is_enable'] = 1;
  220. $temp['created_at'] = date('Y-m-d H:i:s',time());
  221. foreach ($time_config as $value) {
  222. $start_time = $value['start_time'];
  223. $end_time = $value['end_time'];
  224. $temp['config_time'] = $start_time;
  225. $temp['config_per'] = $value['config_per'];
  226. $data[] = $temp;
  227. //结束后 百分比改为默认的
  228. $temp['config_time'] = $end_time;
  229. $temp['config_per'] = $configInfo['default_rate'];
  230. $data[] = $temp;
  231. }
  232. //插入设置最新的时间段百分比
  233. DB::table('juliang_account_promotion_config_time')->insert($data);
  234. }
  235. /**
  236. * 回传开关
  237. * @param Request $request
  238. * @throws \Illuminate\Validation\ValidationException
  239. */
  240. public function turnCallbackState(Request $request) {
  241. $this->validate($request, ['state' => 'required|integer|in:0,1',
  242. 'id' => 'required']);
  243. $now = date('Y-m-d H:i:s');
  244. $config = DB::table('juliang_account_callback_config')
  245. ->where([
  246. 'id' => $request->input('id'),
  247. 'company_uid' => $this->getOptimizerUid(),
  248. ])->first();
  249. if(!$config) {
  250. CommonBusinessException::throwError(Errors::JULIANG_ACCOUNT_CONFIG_NOT_EXISTS);
  251. }
  252. DB::table('juliang_account_callback_config')
  253. ->where('id', $request->input('id'))
  254. ->update([
  255. 'state' => $request->input('state'),
  256. 'updated_at' => $now
  257. ]);
  258. if(1 == $request->input('state')) {
  259. DB::table('juliang_account_rate_config_log')
  260. ->where('company_uid', $this->getOptimizerUid())
  261. ->where('account_id', $config->adv_account_id)
  262. ->where('is_enabled', 1)
  263. ->update(['is_enabled' => 0, 'updated_at' => $now]);
  264. DB::table('juliang_account_promotion_protect_record')
  265. ->where('optimizer_uid', $this->getOptimizerUid())
  266. ->where('advertiser_id', $config->adv_account_id)
  267. ->where('is_enabled', 1)
  268. ->update(['is_enabled' => 0, 'updated_at' => $now]);
  269. DB::table('juliang_account_rate_config_log')
  270. ->insert([
  271. 'company_uid' => $this->getOptimizerUid(),
  272. 'account_id' => $config->adv_account_id,
  273. 'config_per' => $config->default_rate,
  274. 'created_at' => $now,
  275. 'updated_at' => $now,
  276. ]);
  277. // 让所有的时间区间比例配置,在定时任务中,重新执行一遍
  278. DB::table('juliang_account_promotion_config_time')
  279. ->where('is_enable',1)
  280. ->where('company_uid',$this->getOptimizerUid())
  281. ->where('account_id',$config->adv_account_id)
  282. ->update(['next_exec_time' => date('Y-m-d'), 'updated_at' => $now]);
  283. }
  284. return 'ok';
  285. }
  286. /**
  287. * 解绑推广
  288. * @param Request $request
  289. */
  290. public function unbindPromotion(Request $request) {
  291. $this->validate($request, [
  292. 'id' => 'required'
  293. ]);
  294. $config = DB::table('juliang_account_callback_config')
  295. ->where([
  296. 'id' => $request->input('id'),
  297. 'company_uid' => $this->getOptimizerUid(),
  298. ])->first();
  299. if(!$config) {
  300. CommonBusinessException::throwError(Errors::JULIANG_ACCOUNT_CONFIG_NOT_EXISTS);
  301. }
  302. $now = date('Y-m-d H:i:s');
  303. $affected = DB::table('promotions')
  304. ->where([
  305. 'callback_type' => 1,
  306. 'callback_config_id' => $request->input('id'),
  307. 'is_enabled' => 1,
  308. 'status' => 1,
  309. ])->update([
  310. 'status' => 0,
  311. 'updated_at' => $now,
  312. ]);
  313. if($affected) {
  314. DB::table('juliang_account_rate_config_log')
  315. ->where('company_uid', $this->getOptimizerUid())
  316. ->where('account_id', $config->adv_account_id)
  317. ->where('is_enabled', 1)
  318. ->update(['is_enabled' => 0, 'updated_at' => $now]);
  319. DB::table('juliang_account_promotion_protect_record')
  320. ->where('optimizer_uid', $this->getOptimizerUid())
  321. ->where('advertiser_id', $config->adv_account_id)
  322. ->where('is_enabled', 1)
  323. ->update(['is_enabled' => 0, 'updated_at' => $now]);
  324. DB::table('juliang_account_rate_config_log')
  325. ->insert([
  326. 'company_uid' => $this->getOptimizerUid(),
  327. 'account_id' => $config->adv_account_id,
  328. 'config_per' => $config->default_rate,
  329. 'created_at' => $now,
  330. 'updated_at' => $now,
  331. ]);
  332. // 让所有的时间区间比例配置,在定时任务中,重新执行一遍
  333. DB::table('juliang_account_promotion_config_time')
  334. ->where('is_enable',1)
  335. ->where('company_uid',$this->getOptimizerUid())
  336. ->where('account_id',$config->adv_account_id)
  337. ->update(['next_exec_time' => date('Y-m-d'), 'updated_at' => $now]);
  338. }
  339. return 'ok';
  340. }
  341. }