JuliangAccountController.php 14 KB

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