JuliangAccountController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. })->orderBy('id', 'desc')
  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. '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. /**
  204. * 回传开关
  205. * @param Request $request
  206. * @throws \Illuminate\Validation\ValidationException
  207. */
  208. public function turnCallbackState(Request $request) {
  209. $this->validate($request, ['state' => 'required|integer|in:0,1',
  210. 'id' => 'required']);
  211. $now = date('Y-m-d H:i:s');
  212. $config = DB::table('juliang_account_callback_config')
  213. ->where([
  214. 'id' => $request->input('id'),
  215. 'company_uid' => $this->getOptimizerUid(),
  216. ])->first();
  217. if(!$config) {
  218. CommonBusinessException::throwError(Errors::JULIANG_ACCOUNT_CONFIG_NOT_EXISTS);
  219. }
  220. DB::table('juliang_account_callback_config')
  221. ->where('id', $request->input('id'))
  222. ->update([
  223. 'state' => $request->input('state'),
  224. 'updated_at' => $now
  225. ]);
  226. if(1 == $request->input('state')) {
  227. DB::table('juliang_account_rate_config_log')
  228. ->where('company_uid', $this->getOptimizerUid())
  229. ->where('account_id', $config->adv_account_id)
  230. ->where('is_enabled', 1)
  231. ->update(['is_enabled' => 0, 'updated_at' => $now]);
  232. DB::table('juliang_account_promotion_protect_record')
  233. ->where('optimizer_uid', $this->getOptimizerUid())
  234. ->where('advertiser_id', $config->adv_account_id)
  235. ->where('is_enabled', 1)
  236. ->update(['is_enabled' => 0, 'updated_at' => $now]);
  237. DB::table('juliang_account_rate_config_log')
  238. ->insert([
  239. 'company_uid' => $this->getOptimizerUid(),
  240. 'account_id' => $config->adv_account_id,
  241. 'config_per' => $config->default_rate,
  242. 'created_at' => $now,
  243. 'updated_at' => $now,
  244. ]);
  245. // 让所有的时间区间比例配置,在定时任务中,重新执行一遍
  246. DB::table('juliang_account_promotion_config_time')
  247. ->where('is_enable',1)
  248. ->where('company_uid',$this->getOptimizerUid())
  249. ->where('account_id',$config->adv_account_id)
  250. ->update(['next_exec_time' => date('Y-m-d'), 'updated_at' => $now]);
  251. }
  252. return 'ok';
  253. }
  254. /**
  255. * 解绑推广
  256. * @param Request $request
  257. */
  258. public function unbindPromotion(Request $request) {
  259. $this->validate($request, [
  260. 'id' => 'required'
  261. ]);
  262. $config = DB::table('juliang_account_callback_config')
  263. ->where([
  264. 'id' => $request->input('id'),
  265. 'company_uid' => $this->getOptimizerUid(),
  266. ])->first();
  267. if(!$config) {
  268. CommonBusinessException::throwError(Errors::JULIANG_ACCOUNT_CONFIG_NOT_EXISTS);
  269. }
  270. $now = date('Y-m-d H:i:s');
  271. DB::table('promotions')
  272. ->where([
  273. 'callback_type' => 1,
  274. 'callback_config_id' => $request->input('id'),
  275. 'is_enabled' => 1,
  276. ])->update([
  277. 'callback_config_id' => 0,
  278. 'updated_at' => $now,
  279. ]);
  280. DB::table('juliang_account_rate_config_log')
  281. ->where('company_uid', $this->getOptimizerUid())
  282. ->where('account_id', $config->adv_account_id)
  283. ->where('is_enabled', 1)
  284. ->update(['is_enabled' => 0, 'updated_at' => $now]);
  285. DB::table('juliang_account_promotion_protect_record')
  286. ->where('optimizer_uid', $this->getOptimizerUid())
  287. ->where('advertiser_id', $config->adv_account_id)
  288. ->where('is_enabled', 1)
  289. ->update(['is_enabled' => 0, 'updated_at' => $now]);
  290. DB::table('juliang_account_rate_config_log')
  291. ->insert([
  292. 'company_uid' => $this->getOptimizerUid(),
  293. 'account_id' => $config->adv_account_id,
  294. 'config_per' => $config->default_rate,
  295. 'created_at' => $now,
  296. 'updated_at' => $now,
  297. ]);
  298. // 让所有的时间区间比例配置,在定时任务中,重新执行一遍
  299. DB::table('juliang_account_promotion_config_time')
  300. ->where('is_enable',1)
  301. ->where('company_uid',$this->getOptimizerUid())
  302. ->where('account_id',$config->adv_account_id)
  303. ->update(['next_exec_time' => date('Y-m-d'), 'updated_at' => $now]);
  304. return 'ok';
  305. }
  306. }