AgentRepository.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. /**
  3. * 描述.
  4. *
  5. * @author nj
  6. * @date 2021-05-18 20:45:46
  7. */
  8. namespace App\Repositories;
  9. use App\Constants\AgentConstant;
  10. use App\Constants\RedisKeysConstant;
  11. use App\Events\AgentCreatedEvent;
  12. use App\Events\AgentUpdatedEvent;
  13. use App\Exceptions\ApiException;
  14. use App\Models\Mysql\AgentModel;
  15. use App\Models\Mysql\ChannelModel;
  16. use Illuminate\Support\Facades\Redis;
  17. use KZ2\Laravel\Repositories\Eloquent\Repository;
  18. use App\Models\Mysql\ManagerModel;
  19. use Otp\GoogleAuthenticator;
  20. use function PHPUnit\Framework\isEmpty;
  21. class AgentRepository extends Repository
  22. {
  23. public function model()
  24. {
  25. return AgentModel::class;
  26. }
  27. /**
  28. * 生成密码.
  29. * @param string $plain 明文密码.
  30. * @param string $salt 盐.
  31. * @return string
  32. */
  33. public function generatePassword($plain, $salt)
  34. {
  35. return md5(md5($plain) . $salt);
  36. }
  37. /**
  38. * 是否存在相同用户名.
  39. * @param string $username 用户名.
  40. * @param string $excludeAgentUid 排除的用户ID.
  41. * @return bool
  42. */
  43. public function isExistsUsername($username, $excludeAgentUid = '')
  44. {
  45. $model = new ManagerModel();
  46. if (!empty($excludeManagerUid)) {
  47. $model = $model->where('uid', '!=', $excludeAgentUid);
  48. }
  49. $model = $model->where('username', $username);
  50. return $model->exists();
  51. }
  52. /**
  53. * 创建一个代理.
  54. * @param array $item
  55. * @param string $parentUid
  56. * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
  57. * @throws ApiException
  58. */
  59. public function createOne(array $item, $parentUid = '')
  60. {
  61. unset($item['parent_uid'], $item['parent_uids'], $item['child_uids']); // 不信任的字段, 后面全部由业务程序处理.
  62. $item['username'] = strtolower($item['username']); // 用户名小写.
  63. $nick = !empty($item['nick']) ? $item['nick'] : $item['username'];
  64. if (empty(trim($item['password']))) {
  65. throw new ApiException('密码不能为空');
  66. }
  67. $salt = $this->makeSalt();
  68. $md5Password = $this->createHashPassword($item['password'], $salt);
  69. $otpScrect = !empty($item['otp_screct']) ? $item['otp_screct'] : GoogleAuthenticator::generateRandom();
  70. $agentLevel = AgentConstant::TOP_LEVEL; // 代理层级.
  71. $commissionDiffRatio = AgentConstant::MAX_COMMISSION_RATIO; // 父级的实际提成比例.
  72. if (!empty($parentUid)) {
  73. $parentObj = AgentModel::query()->where(['uid' => $parentUid])->first([
  74. 'uid',
  75. 'agent_level',
  76. 'commission_ratio',
  77. 'jdata',
  78. 'deduction_switch',
  79. 'cpa_deduction_rule',
  80. 'cpa_price'
  81. ]);
  82. $agentLevel = $parentObj->agent_level + 1; // 基于上级层级关系,进行+1;
  83. if ($agentLevel == 6) {
  84. throw new ApiException('该层级不可创建代理.');
  85. }
  86. if ($item['commission_ratio'] > $parentObj->commission_ratio) {
  87. throw new ApiException('提成比例不能大于父级的提成比例.');
  88. }
  89. $commissionDiffRatio = $parentObj->commission_ratio - $item['commission_ratio'];
  90. //继承上级的设置
  91. $setarr['jdata'] = $parentObj->jdata;
  92. $setarr['deduction_switch'] = $parentObj->deduction_switch;
  93. $setarr['cpa_deduction_rule'] = $parentObj->cpa_deduction_rule;
  94. $setarr['cpa_price'] = $parentObj->cpa_price;
  95. $setarr['preferential_order'] = $parentObj->preferential_order;
  96. } else {
  97. // 当顶级了,提成比例就是commission_ratio.
  98. $commissionDiffRatio = $item['commission_ratio'];
  99. $setarr['jdata'] = $item['jdata'] ?? '';
  100. $setarr['deduction_switch'] = $item['deduction_switch'] ?? 0;
  101. $setarr['cpa_deduction_rule'] = $item['cpa_deduction_rule'];
  102. $setarr['cpa_price'] = $item['cpa_price'];
  103. }
  104. $strOwnAppUids = $item['own_app_uids'];
  105. if (is_array($item['own_app_uids'])) {
  106. $strOwnAppUids = !empty($item['own_app_uids']) ? implode(',', $item['own_app_uids']) : '';
  107. }
  108. if (empty($item['withdraw_amount'])) {
  109. $item['withdraw_amount'] = 0;
  110. }
  111. if (empty($item['withdraw_usdt'])) {
  112. $item['withdraw_usdt'] = 0;
  113. }
  114. $setarr['username'] = $item['username'];
  115. $setarr['withdraw_pwd'] = $item['withdraw_pwd'];
  116. $setarr['nick'] = $nick;
  117. $setarr['password'] = $md5Password;
  118. $setarr['salt'] = $salt;
  119. $setarr['status'] = $item['status'];
  120. $setarr['otp_screct'] = $otpScrect;
  121. $setarr['agent_level'] = $agentLevel;
  122. $setarr['parent_uid'] = $parentUid;
  123. $setarr['own_app_uids'] = $strOwnAppUids;
  124. $setarr['commission_ratio'] = $item['commission_ratio'];
  125. $setarr['commission_diff_ratio'] = $commissionDiffRatio;
  126. $setarr['preferential_order'] = $item['preferential_order'] ?? 0;
  127. $setarr['withdraw_amount'] = $item['withdraw_amount'];
  128. $setarr['withdraw_usdt'] = $item['withdraw_usdt'];
  129. $obj = AgentModel::query()->create($setarr);
  130. // 触发事件.
  131. event(new AgentCreatedEvent($obj->uid));
  132. return $obj;
  133. }
  134. /**
  135. * 修改一个代理.
  136. * @param array $item
  137. * @param string $uid
  138. * @return bool
  139. * @throws ApiException
  140. */
  141. public function updateOne(array $item, string $uid)
  142. {
  143. unset($item['uid']);
  144. $columns = [
  145. 'id',
  146. 'uid',
  147. 'nick',
  148. 'password',
  149. 'salt',
  150. 'otp_secret',
  151. 'own_app_uids',
  152. 'commission_ratio',
  153. 'commission_diff_ratio',
  154. 'parent_uid',
  155. 'preferential_order',
  156. 'cpa_deduction_rule',
  157. 'cpa_price'
  158. ];
  159. $agentModel = new AgentModel();
  160. $obj = $agentModel->where(['uid' => $uid])->first($columns);
  161. $oldObj = clone $obj;
  162. $saveResult = false; // 预先定义保存失败.
  163. if ($obj->count()) {
  164. $nick = !empty($item['nick']) ? $item['nick'] : $item['username'];
  165. $salt = $obj->salt;
  166. if (!empty($item['own_app_uids'])) {
  167. $strOwnAppUids = $item['own_app_uids'];
  168. if (is_array($item['own_app_uids'])) {
  169. $strOwnAppUids = !empty($item['own_app_uids']) ? implode(',', $item['own_app_uids']) : '';
  170. }
  171. $obj->own_app_uids = $strOwnAppUids;
  172. }
  173. $parentUid = $obj->parent_uid;
  174. $lowerRatio = $agentModel->where('parent_uid', $obj['uid'])->orderBy('commission_ratio',
  175. 'DESC')->first('commission_ratio');
  176. if (!empty($lowerRatio)) {
  177. if (!empty($item['commission_ratio'])) {
  178. if ($item['commission_ratio'] < $lowerRatio->commission_ratio) {
  179. throw new ApiException('提成比例不能小与下级提成比例.');
  180. }
  181. }
  182. }
  183. if (!empty($parentUid)) {
  184. $parentObj = $agentModel->where(['uid' => $parentUid])->first([
  185. 'uid',
  186. 'agent_level',
  187. 'commission_ratio'
  188. ]);
  189. if ($item['commission_ratio'] > $parentObj->commission_ratio) {
  190. throw new ApiException('提成比例不能大于父级的提成比例.');
  191. }
  192. $commissionDiffRatio = $parentObj->commission_ratio - $item['commission_ratio'];
  193. $obj->commission_diff_ratio = $commissionDiffRatio;
  194. $obj->commission_ratio = $item['commission_ratio'];
  195. } else {
  196. // 当顶级了,提成比例就是commission_ratio.
  197. if (!empty($item['commission_ratio'])) {
  198. $commissionDiffRatio = $item['commission_ratio'];
  199. $obj->commission_ratio = $item['commission_ratio'];
  200. $obj->commission_diff_ratio = $commissionDiffRatio;
  201. }
  202. }
  203. $obj->nick = $nick;
  204. if (!empty($item['password'])) {
  205. $md5Password = $this->createHashPassword($item['password'], $salt);
  206. $obj->password = $md5Password;
  207. }
  208. if (!empty($item['withdraw_pwd'])) {
  209. $obj->withdraw_pwd = $item['withdraw_pwd'];
  210. }
  211. if (!empty($item['status'])) {
  212. $obj->status = $item['status'];
  213. }
  214. if (!empty($item['deduction_switch'])) {
  215. $obj->deduction_switch = $item['deduction_switch'];
  216. }
  217. if (!empty($item['jdata'])) {
  218. $obj->jdata = $item['jdata'];
  219. }
  220. if (!empty($item['cpa_deduction_rule'])) {
  221. $obj->cpa_deduction_rule = $item['cpa_deduction_rule'];
  222. }
  223. if (!empty($item['cpa_price'])) {
  224. $obj->cpa_price = $item['cpa_price'];
  225. }
  226. if (!empty($item['preferential_order'])) {
  227. $obj->preferential_order = $item['preferential_order'];
  228. }
  229. if (!empty($item['withdraw_amount'])) {
  230. $obj->withdraw_amount = $item['withdraw_amount'];
  231. }
  232. if (!empty($item['withdraw_usdt'])) {
  233. $obj->withdraw_usdt = $item['withdraw_usdt'];
  234. }
  235. $saveResult = $obj->save();
  236. // 前后对比提成比例,不相同则执行.
  237. if ($oldObj->commission_ratio != $obj->commission_ratio) {
  238. $childs = AgentModel::query()->where(['parent_uid' => $uid])->get([
  239. 'uid',
  240. 'parent_uid',
  241. 'commission_ratio',
  242. 'commission_diff_ratio'
  243. ]);
  244. if ($childs->isNotEmpty()) {
  245. // 水平纬度,重新计算子用户的提成差.
  246. foreach ($childs as $item) {
  247. $diffRatio = $obj->commission_ratio - $item->commission_ratio;
  248. AgentModel::query()->where('uid', $item->uid)->update(['commission_diff_ratio' => $diffRatio]);
  249. }
  250. }
  251. }
  252. // 触发更新事件.
  253. event(new AgentUpdatedEvent($uid));
  254. }
  255. return $saveResult;
  256. }
  257. /**
  258. * 代理修改自己的信息
  259. * @param array $array
  260. * @param string $uid
  261. * @return bool
  262. */
  263. public function agentEdit(array $array, string $uid)
  264. {
  265. $agent = (new AgentModel())->where('uid', $uid)->first();
  266. if (!empty($array['nick'])) {
  267. $agent->nick = $array['nick'];
  268. }
  269. if (!empty($array['password'])) {
  270. $md5Password = $this->createHashPassword($array['password'], $agent->salt);
  271. $agent->password = $md5Password;
  272. }
  273. if (!empty($array['withdraw_pwd'])) {
  274. $agent->withdraw_pwd = $array['withdraw_pwd'];
  275. }
  276. return $agent->save();
  277. }
  278. /**
  279. * 创建盐.
  280. * @return string
  281. */
  282. public function makeSalt()
  283. {
  284. return random(8);
  285. }
  286. /**
  287. * 创建hash密码.
  288. * @param string $password
  289. * @param string $salt
  290. * @return string
  291. */
  292. public function createHashPassword(string $password, string $salt)
  293. {
  294. return md5(md5($password) . $salt);
  295. }
  296. /**
  297. * 删除代理扣量缓存
  298. *
  299. * @param $deductionType
  300. */
  301. public function delAgentRuleCache($agentUid = '')
  302. {
  303. $channerList = ChannelModel::query()->where('agent_uid', $agentUid)->get(['mom_app_uid', 'uid']);
  304. if ($channerList->isEmpty()) {
  305. return '';
  306. }
  307. $cpsKey = RedisKeysConstant::DEDUCTION_RULES_HASH;
  308. $cpaKey = RedisKeysConstant::DEDUCTION_RULES_HASH_CPA;
  309. foreach ($channerList as $val) {
  310. Redis::del($cpsKey . $val->mom_app_uid . $val->uid);
  311. Redis::del($cpaKey . $val->mom_app_uid . $val->uid);
  312. }
  313. }
  314. }