JuliangAccountController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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->getCompanyUid()])
  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. })
  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. 'callback_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->getCompanyUid(), '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->getCompanyUid(),
  58. 'created_at' => $now,
  59. 'updated_at' => $now,
  60. ]);
  61. DB::table('juliang_account_rate_config_log')
  62. ->insert([
  63. 'company_uid' => $this->getCompanyUid(),
  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->getCompanyUid(), $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->getCompanyUid()])
  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->getCompanyUid())
  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->getCompanyUid())
  109. ->whereIn('account_id', $advAccountIds)
  110. ->where('is_enabled', 1)
  111. ->update(['is_enabled' => 0, 'updated_at' => $now]);
  112. foreach ($advAccountIds as $accountId) {
  113. DB::table('juliang_account_rate_config_log')
  114. ->insert([
  115. 'company_uid' => $this->getCompanyUid(),
  116. 'account_id' => $accountId,
  117. 'config_per' => $request->input('default_rate'),
  118. 'created_at' => $now,
  119. 'updated_at' => $now,
  120. ]);
  121. $this->saveTimeConfig($this->getCompanyUid(), $accountId, $request);
  122. }
  123. }
  124. return 'ok';
  125. }
  126. /**
  127. * 判断时 设置的时间范围是否有交集
  128. *
  129. * @param string $config_time 开始时间1
  130. * @return bool
  131. */
  132. protected function is_time_cross($config_time) {
  133. $timeline = [];
  134. foreach ($config_time as $key => $value) {
  135. $start_time = $value['start_time'];
  136. $start = explode(':', $start_time);
  137. $cur_start = (int)$start[0] * 60 + $start[1];
  138. $end_time = $value['end_time'];
  139. $end = explode(':', $end_time);
  140. $cur_end = (int)$end[0] * 60 + $end[1];
  141. if ($cur_end <= $cur_start) {
  142. return false;
  143. }
  144. if ($timeline) {
  145. foreach ($timeline as $k => $v) {
  146. if ($cur_start >= $v['start'] && $cur_start <= $v['end']) {
  147. return false;
  148. }
  149. if ($cur_end >= $v['start'] && $cur_end <= $v['end']) {
  150. return false;
  151. }
  152. }
  153. }
  154. $timeline[] = ['start'=>$cur_start,'end'=>$cur_end];
  155. }
  156. return true;
  157. }
  158. public function saveTimeConfig($companyUid, $accountId, $configInfo )
  159. {
  160. DB::table('juliang_account_promotion_config_time')
  161. ->where('is_enable',1)
  162. ->where('company_uid',$companyUid)
  163. ->where('account_id',$accountId)->update(['is_enable'=>0]);
  164. $time_config = $configInfo['rate_time_config'];
  165. if (empty($time_config)) {
  166. return false;
  167. }
  168. $is = $this->is_time_cross($time_config);
  169. if (!$is) {
  170. return false;
  171. }
  172. $data = [];
  173. $temp['company_uid'] = $companyUid;
  174. $temp['account_id'] = $accountId;
  175. $temp['other_data']['default_rate'] = $configInfo['default_rate'];
  176. $temp['other_data']['min_money'] = $configInfo['min_money'];
  177. $temp['other_data']['max_money'] = $configInfo['max_money'];
  178. $temp['other_data']['protect_num'] = $configInfo['protect_num'];
  179. $temp['other_data']['state'] = $configInfo['state'];
  180. $temp['other_data'] = json_encode($temp['other_data']);
  181. $temp['next_exec_time'] = date('Y-m-d');
  182. $temp['is_enable'] = 1;
  183. $temp['created_at'] = date('Y-m-d H:i:s',time());
  184. foreach ($time_config as $value) {
  185. $start_time = $value['start_time'];
  186. $end_time = $value['end_time'];
  187. $temp['config_time'] = $start_time;
  188. $temp['config_per'] = $value['config_per'];
  189. $data[] = $temp;
  190. //结束后 百分比改为默认的
  191. $temp['config_time'] = $end_time;
  192. $temp['config_per'] = $configInfo['default_rate'];
  193. $data[] = $temp;
  194. }
  195. //插入设置最新的时间段百分比
  196. DB::table('juliang_account_promotion_config_time')->insert($data);
  197. }
  198. }