|
@@ -0,0 +1,215 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Modules\Callback\Http\Controllers;
|
|
|
+
|
|
|
+use Catch\Base\CatchController;
|
|
|
+use Illuminate\Foundation\Validation\ValidatesRequests;
|
|
|
+use Illuminate\Http\Request;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+use Modules\Common\Errors\Errors;
|
|
|
+use Modules\Common\Exceptions\CommonBusinessException;
|
|
|
+use Modules\User\Http\Controllers\UserTrait;
|
|
|
+
|
|
|
+class JuliangAccountController extends CatchController
|
|
|
+{
|
|
|
+ use UserTrait;
|
|
|
+ use ValidatesRequests;
|
|
|
+
|
|
|
+ public function list(Request $request) {
|
|
|
+ $advAccountId = $request->input('account_id');
|
|
|
+ $advAccountName = $request->input('account_name');
|
|
|
+
|
|
|
+ return DB::table('juliang_account_callback_config')
|
|
|
+ ->where(['company_uid' => $this->getCompanyUid()])
|
|
|
+ ->when($advAccountId, function ($query, $advAccountId) {
|
|
|
+ return $query->where('adv_account_id' , $advAccountId);
|
|
|
+ })->when($advAccountName, function ($query, $advAccountName) {
|
|
|
+ return $query->where('adv_account_name', 'like', '%'. $advAccountName. '%');
|
|
|
+ })
|
|
|
+ ->paginate($request->input('limit', 30));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function addAccount(Request $request) {
|
|
|
+ $this->validate($request, [
|
|
|
+ 'account_id' => 'required',
|
|
|
+ 'account_name' => 'required|string|max:64',
|
|
|
+ 'callback_state' => 'required|integer|in:0,1',
|
|
|
+ 'protect_num' => 'required|integer|min:0',
|
|
|
+ 'default_rate' => 'required|min:0|max:100',
|
|
|
+ 'rate_time_config' => 'nullable|array',
|
|
|
+ 'min_money' => 'required|min:0',
|
|
|
+ 'max_money' => 'required|min:0'
|
|
|
+ ]);
|
|
|
+
|
|
|
+ if(DB::table('juliang_account_callback_config')
|
|
|
+ ->where(['company_uid' => $this->getCompanyUid(), 'adv_account_id' => $request->input('account_id')])
|
|
|
+ ->exists()) {
|
|
|
+ CommonBusinessException::throwError(Errors::JULIANG_ACCOUNT_EXISTS);
|
|
|
+ }
|
|
|
+ if($request->input('rate_time_config') &&
|
|
|
+ !$this->is_time_cross($request->input('rate_time_config'))) {
|
|
|
+ CommonBusinessException::throwError(Errors::CALLBACK_RATE_TIME_RANGE_ERROR);
|
|
|
+ }
|
|
|
+ $now = date('Y-m-d H:i:s');
|
|
|
+ DB::table('juliang_account_callback_config')
|
|
|
+ ->insert([
|
|
|
+ 'adv_account_id' => $request->input('account_id'),
|
|
|
+ 'adv_account_name' => $request->input('account_name'),
|
|
|
+ 'state' => $request->input('state'),
|
|
|
+ 'protect_num' => $request->input('protect_num'),
|
|
|
+ 'default_rate' => $request->input('default_rate'),
|
|
|
+ 'rate_time_config' => \json_encode($request->input('rate_time_config', [])),
|
|
|
+ 'min_money' => $request->input('min_money'),
|
|
|
+ 'max_money' => $request->input('max_money'),
|
|
|
+ 'company_uid' => $this->getCompanyUid(),
|
|
|
+ 'created_at' => $now,
|
|
|
+ 'updated_at' => $now,
|
|
|
+ ]);
|
|
|
+ DB::table('juliang_account_rate_config_log')
|
|
|
+ ->insert([
|
|
|
+ 'company_uid' => $this->getCompanyUid(),
|
|
|
+ 'account_id' => $request->input('account_id'),
|
|
|
+ 'config_per' => $request->input('default_rate'),
|
|
|
+ 'created_at' => $now,
|
|
|
+ 'updated_at' => $now,
|
|
|
+ ]);
|
|
|
+ if($request->input('rate_time_config')) {
|
|
|
+ $this->saveTimeConfig($this->getCompanyUid(), $request->input('account_id'), $request);
|
|
|
+ }
|
|
|
+ return 'ok';
|
|
|
+ }
|
|
|
+
|
|
|
+ public function updateCallbackConfig(Request $request) {
|
|
|
+ $this->validate($request, [
|
|
|
+ 'ids' => 'required|array',
|
|
|
+ 'state' => 'required|integer|in:0,1',
|
|
|
+ 'protect_num' => 'required|integer|min:0',
|
|
|
+ 'default_rate' => 'required|min:0|max:100',
|
|
|
+ 'rate_time_config' => 'nullable|array',
|
|
|
+ 'min_money' => 'required|min:0',
|
|
|
+ 'max_money' => 'required|min:0'
|
|
|
+ ]);
|
|
|
+ if($request->input('callback_rate_time_config') &&
|
|
|
+ !$this->is_time_cross($request->input('rate_time_config'))) {
|
|
|
+ CommonBusinessException::throwError(Errors::CALLBACK_RATE_TIME_RANGE_ERROR);
|
|
|
+ }
|
|
|
+ $now = date('Y-m-d H:i:s');
|
|
|
+ foreach ($request->input('ids') as $id) {
|
|
|
+ DB::table('juliang_account_callback_config')
|
|
|
+ ->where(['id' => $id, 'company_uid' => $this->getCompanyUid()])
|
|
|
+ ->update([
|
|
|
+ 'state' => $request->input('state'),
|
|
|
+ 'protect_num' => $request->input('protect_num'),
|
|
|
+ 'default_rate' => $request->input('default_rate'),
|
|
|
+ 'rate_time_config' => \json_encode($request->input('rate_time_config', [])),
|
|
|
+ 'min_money' => $request->input('min_money'),
|
|
|
+ 'max_money' => $request->input('max_money'),
|
|
|
+ 'updated_at' => $now,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ $advAccountIds = DB::table('juliang_account_callback_config')
|
|
|
+ ->whereIn('id', $request->input('ids'))
|
|
|
+ ->where('company_uid', $this->getCompanyUid())
|
|
|
+ ->select('adv_account_id')->get()->pluck('adv_account_id');
|
|
|
+ if($advAccountIds->isNotEmpty()) {
|
|
|
+ DB::table('juliang_account_rate_config_log')
|
|
|
+ ->where('company_uid', $this->getCompanyUid())
|
|
|
+ ->whereIn('account_id', $advAccountIds)
|
|
|
+ ->where('is_enabled', 1)
|
|
|
+ ->update(['is_enabled' => 0, 'updated_at' => $now]);
|
|
|
+ foreach ($advAccountIds as $accountId) {
|
|
|
+ DB::table('juliang_account_rate_config_log')
|
|
|
+ ->insert([
|
|
|
+ 'company_uid' => $this->getCompanyUid(),
|
|
|
+ 'account_id' => $accountId,
|
|
|
+ 'config_per' => $request->input('default_rate'),
|
|
|
+ 'created_at' => $now,
|
|
|
+ 'updated_at' => $now,
|
|
|
+ ]);
|
|
|
+ $this->saveTimeConfig($this->getCompanyUid(), $accountId, $request);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return 'ok';
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断时 设置的时间范围是否有交集
|
|
|
+ *
|
|
|
+ * @param string $config_time 开始时间1
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ protected function is_time_cross($config_time) {
|
|
|
+ $timeline = [];
|
|
|
+ foreach ($config_time as $key => $value) {
|
|
|
+ $start_time = $value['start_time'];
|
|
|
+ $start = explode(':', $start_time);
|
|
|
+ $cur_start = (int)$start[0] * 60 + $start[1];
|
|
|
+ $end_time = $value['end_time'];
|
|
|
+
|
|
|
+ $end = explode(':', $end_time);
|
|
|
+ $cur_end = (int)$end[0] * 60 + $end[1];
|
|
|
+ if ($cur_end <= $cur_start) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($timeline) {
|
|
|
+ foreach ($timeline as $k => $v) {
|
|
|
+ if ($cur_start >= $v['start'] && $cur_start <= $v['end']) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if ($cur_end >= $v['start'] && $cur_end <= $v['end']) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $timeline[] = ['start'=>$cur_start,'end'=>$cur_end];
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function saveTimeConfig($companyUid, $accountId, $configInfo )
|
|
|
+ {
|
|
|
+ DB::table('juliang_account_promotion_config_time')
|
|
|
+ ->where('is_enable',1)
|
|
|
+ ->where('company_uid',$companyUid)
|
|
|
+ ->where('account_id',$accountId)->update(['is_enable'=>0]);
|
|
|
+ $time_config = $configInfo['rate_time_config'];
|
|
|
+ if (empty($time_config)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $is = $this->is_time_cross($time_config);
|
|
|
+ if (!$is) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $data = [];
|
|
|
+ $temp['company_uid'] = $companyUid;
|
|
|
+
|
|
|
+ $temp['account_id'] = $accountId;
|
|
|
+ $temp['other_data']['default_rate'] = $configInfo['default_rate'];
|
|
|
+ $temp['other_data']['min_money'] = $configInfo['min_money'];
|
|
|
+ $temp['other_data']['max_money'] = $configInfo['max_money'];
|
|
|
+ $temp['other_data']['protect_num'] = $configInfo['protect_num'];
|
|
|
+ $temp['other_data']['state'] = $configInfo['state'];
|
|
|
+ $temp['other_data'] = json_encode($temp['other_data']);
|
|
|
+ $temp['next_exec_time'] = date('Y-m-d');
|
|
|
+
|
|
|
+ $temp['is_enable'] = 1;
|
|
|
+ $temp['created_at'] = date('Y-m-d H:i:s',time());
|
|
|
+
|
|
|
+ foreach ($time_config as $value) {
|
|
|
+ $start_time = $value['start_time'];
|
|
|
+ $end_time = $value['end_time'];
|
|
|
+ $temp['config_time'] = $start_time;
|
|
|
+ $temp['config_per'] = $value['config_per'];
|
|
|
+ $data[] = $temp;
|
|
|
+ //结束后 百分比改为默认的
|
|
|
+ $temp['config_time'] = $end_time;
|
|
|
+ $temp['config_per'] = $configInfo['default_rate'];
|
|
|
+ $data[] = $temp;
|
|
|
+ }
|
|
|
+ //插入设置最新的时间段百分比
|
|
|
+ DB::table('juliang_account_promotion_config_time')->insert($data);
|
|
|
+ }
|
|
|
+}
|