JuliangAccountController.php 16 KB

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