JuliangAccountController.php 16 KB

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