ActivityJoin.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. namespace app\models\activity;
  3. use app\models\mining\UserMiningMachine;
  4. use app\models\system\SystemMoney;
  5. use app\models\user\User;
  6. use app\models\user\UserBill;
  7. use app\models\user\UserMoney;
  8. use Exception;
  9. use think\db\exception\DataNotFoundException;
  10. use think\db\exception\DbException;
  11. use think\db\exception\ModelNotFoundException;
  12. use crmeb\traits\ModelTrait;
  13. use crmeb\basic\BaseModel;
  14. /**
  15. * TODO 文章Model
  16. * Class Article
  17. * @package app\models\article
  18. */
  19. class ActivityJoin extends BaseModel
  20. {
  21. /**
  22. * 数据表主键
  23. * @var string
  24. */
  25. protected $pk = 'id';
  26. /**
  27. * 模型名称
  28. * @var string
  29. */
  30. protected $name = 'activity_join';
  31. use ModelTrait;
  32. public function user()
  33. {
  34. return self::hasOne(User::class, 'uid', 'uid')->field('uid,nickname,phone,email,account,avatar');
  35. }
  36. public function activity()
  37. {
  38. return self::hasOne(Activity::class, 'id', 'aid')->field('name');
  39. }
  40. /**
  41. * @param $page
  42. * @param $limit
  43. * @return array
  44. */
  45. public static function getList($where): array
  46. {
  47. $model = new self();
  48. $data = $model->with(['user', 'activity'])
  49. ->where('aid', $where['id'])
  50. ->page((int)$where['page'], (int)$where['limit'])
  51. ->select()->each(function ($item) {
  52. $item['status_name'] = ($item['status'] == 1 ? '一级' : ($item['status'] == 2 ? '二级' : ($item['status'] == 3 ? '三级' : '未知')));
  53. $item['add_time_text'] = date('Y-m-d H:i:s', $item['add_time']);
  54. });
  55. $count = $model->count();
  56. return compact('data', 'count');
  57. }
  58. /**
  59. * @param $id
  60. * @param $uid
  61. */
  62. public static function join($id, $uid)
  63. {
  64. $activity = Activity::get($id);
  65. if (!$activity) return self::setErrorInfo('找不到活动');
  66. $user_join = self::where('uid', $uid)
  67. ->where('aid', $id)
  68. ->order('add_time', 'desc')
  69. ->find();
  70. if (!$user_join) {
  71. //首次参与
  72. BaseModel::beginTrans();
  73. try {
  74. //支付参与金额给上级
  75. $user = User::getUserInfo($uid);
  76. $gp_point = self::getUpPoint($user, $id);
  77. if (!$gp_point) {
  78. BaseModel::rollbackTrans();
  79. return self::setErrorInfo(self::getErrorInfo());
  80. }
  81. $res1 = self::create([
  82. 'uid' => $uid,
  83. 'aid' => $id,
  84. 'status' => 1,
  85. 'group_num' => $gp_point,
  86. 'add_time' => time(),
  87. ]);
  88. $up_user = intval(ceil(($gp_point - 1) / $activity['explode_num']));
  89. $info = self::where('aid', $id)->where('group_num', $up_user)->find();
  90. $res2 = ActivityCheck::create([
  91. 'uid' => $uid,
  92. 'aid' => $id,
  93. 'uaid' => $res1->id,
  94. 'money_type' => $activity['l1_money_type'],
  95. 'money' => $activity['l1_money'],
  96. 'status' => 1,
  97. 'layer' => 1,
  98. 'to_uid' => $info ? $info['uid'] : 0,
  99. 'add_time' => time(),
  100. ]);
  101. $res3 = UserMoney::activityTradeMoney($id, $uid, $info ? $info['uid'] : 0, $res2->id, 1, $activity['l1_money_type'], $activity['l1_money']);
  102. if ($res1 && $res2 && $res3) {
  103. BaseModel::commitTrans();
  104. return true;
  105. } else {
  106. BaseModel::rollbackTrans();
  107. return self::setErrorInfo('参与失败:' .
  108. (!$res1 ? '生成参与记录失败,' : '') .
  109. (!$res2 ? '生成审核记录失败,' : '') .
  110. (!$res3 ? UserMoney::getErrorInfo() : ''));
  111. }
  112. } catch (Exception $e) {
  113. BaseModel::rollbackTrans();
  114. return self::setErrorInfo($e->getMessage());
  115. }
  116. } else {
  117. //已有记录
  118. if ($user_join['status'] >= 3) {
  119. //是否出局
  120. $explode_num = $activity['explode_num'];
  121. $gp_point_list = [];
  122. $p = 1;
  123. for ($i = $explode_num - 2; $i >= -1; $i--) {
  124. $gp_point_list[] = $user_join['group_num'] * $explode_num - $i;
  125. }
  126. while ($p <= 3) {
  127. $member_list = self::where('aid', $id)->where('status', '>=', $p)->where('group_num', 'in', $gp_point_list)->order('group_num', 'asc')->select();
  128. if (count($member_list) < count($gp_point_list)) {
  129. return self::setErrorInfo('尚未出局');
  130. }
  131. $gp_point_list = [];
  132. foreach ($member_list as $v) {
  133. for ($i = $explode_num - 2; $i >= -1; $i--) {
  134. $gp_point_list[] = $v['group_num'] * $explode_num - $i;
  135. }
  136. }
  137. $p++;
  138. }
  139. //再次参与
  140. BaseModel::beginTrans();
  141. try {
  142. //支付参与金额给上级
  143. $user = User::getUserInfo($uid);
  144. $gp_point = self::getUpPoint($user, $id, 1);
  145. if (!$gp_point) {
  146. BaseModel::rollbackTrans();
  147. return self::setErrorInfo(self::getErrorInfo());
  148. }
  149. $res1 = self::create([
  150. 'uid' => $uid,
  151. 'aid' => $id,
  152. 'status' => 1,
  153. 'group_num' => $gp_point,
  154. 'add_time' => time(),
  155. ]);
  156. $up_user = intval(ceil(($gp_point - 1) / $activity['explode_num']));
  157. $info = self::where('aid', $id)->where('group_num', $up_user)->find();
  158. $res2 = ActivityCheck::create([
  159. 'uid' => $uid,
  160. 'aid' => $id,
  161. 'uaid' => $res1->id,
  162. 'money_type' => $activity['l1_money_type'],
  163. 'money' => $activity['l1_money'],
  164. 'status' => 1,
  165. 'layer' => 1,
  166. 'to_uid' => $info ? $info['uid'] : 0,
  167. 'add_time' => time(),
  168. ]);
  169. $res3 = UserMoney::activityTradeMoney($id, $uid, $info ? $info['uid'] : 0, $res2->id, 1, $activity['l1_money_type'], $activity['l1_money']);
  170. if ($res1 && $res2 && $res3) {
  171. BaseModel::commitTrans();
  172. return true;
  173. } else {
  174. BaseModel::rollbackTrans();
  175. return self::setErrorInfo('参与失败:' .
  176. (!$res1 ? '生成参与记录失败,' : '') .
  177. (!$res2 ? '生成审核记录失败,' : '') .
  178. (!$res3 ? UserMoney::getErrorInfo() : ''));
  179. }
  180. } catch (Exception $e) {
  181. BaseModel::rollbackTrans();
  182. return self::setErrorInfo($e->getMessage());
  183. }
  184. } else {
  185. //升级
  186. BaseModel::beginTrans();
  187. try {
  188. $gp_point = $user_join['group_num'];
  189. for ($i = 0; $i < $user_join['status'] + 1; $i++) {
  190. $gp_point = intval(ceil(($gp_point - 1) / $activity['explode_num']));
  191. }
  192. $info = self::where('aid', $id)->where('group_num', $gp_point)->find();
  193. $res2 = ActivityCheck::create([
  194. 'uid' => $uid,
  195. 'aid' => $id,
  196. 'uaid' => $user_join['id'],
  197. 'money_type' => $activity['l' . ($user_join['status'] + 1) . '_money_type'],
  198. 'money' => $activity['l' . ($user_join['status'] + 1) . '_money'],
  199. 'status' => 1,
  200. 'layer' => $user_join['status'] + 1,
  201. 'to_uid' => $info ? $info['uid'] : 0,
  202. 'add_time' => time(),
  203. ]);
  204. $res3 = UserMoney::activityTradeMoney($id, $uid, $info ? $info['uid'] : 0, $res2->id, $user_join['status'] + 1, $activity['l' . ($user_join['status'] + 1) . '_money_type'], $activity['l' . ($user_join['status'] + 1) . '_money'], (isset($info) && $info['status'] >= ($user_join['status'] + 1)) ? 1 : 0);
  205. $res1 = self::where('id', $user_join['id'])->update(['status' => $user_join['status'] + 1]);
  206. $res4 = self::sendMoney($id, $uid, $user_join['status'] + 1);
  207. if ($res1 && $res2 && $res3 && $res4) {
  208. BaseModel::commitTrans();
  209. return true;
  210. } else {
  211. BaseModel::rollbackTrans();
  212. return self::setErrorInfo('参与失败:' .
  213. (!$res1 ? '修改参与记录失败,' : '') .
  214. (!$res2 ? '生成审核记录失败,' : '') .
  215. (!$res3 ? (UserMoney::getErrorInfo() . ',') : '') .
  216. (!$res4 ? self::getErrorInfo() : ''));
  217. }
  218. } catch (Exception $e) {
  219. BaseModel::rollbackTrans();
  220. return self::setErrorInfo($e->getMessage());
  221. }
  222. }
  223. }
  224. }
  225. public static function sendMoney($id, $uid, $layer)
  226. {
  227. $list = UserBill::where(['pm' => 1, 'status' => 0])
  228. ->where('type', 'activity_income')
  229. ->where('aid', $id)->where('uid', $uid)->where('layer', $layer)
  230. ->select();
  231. if ($list) {
  232. try {
  233. foreach ($list as $v) {
  234. UserMoney::sendMoney($v['id']);
  235. }
  236. return true;
  237. } catch (Exception $e) {
  238. return self::setErrorInfo($e->getMessage());
  239. }
  240. }
  241. return true;
  242. }
  243. /**
  244. * 找公排点
  245. * @param $user
  246. * @param $id
  247. * @param bool $re
  248. * @return int|bool
  249. * @throws DataNotFoundException
  250. * @throws DbException
  251. * @throws ModelNotFoundException
  252. */
  253. public static function getUpPoint($user, $id, bool $re = false)
  254. {
  255. $activity = Activity::get($id);
  256. if (!$activity) return self::setErrorInfo('找不到活动');
  257. $gp_point = 1;
  258. if (!self::where('aid', $id)->find()) {
  259. return $gp_point;
  260. }
  261. $user_join = self::where('aid', $id)->order('add_time', 'asc')->find();
  262. if ($re || $activity['group_type'] == 1) goto end;
  263. while ($user) {
  264. $user_join_check = self::where('uid', $user['spread_uid'])->where('aid', $id)->order('add_time', 'desc')->find();
  265. if (!$user_join_check) {
  266. $user = User::getUserInfo($user['spread_uid']);
  267. } else {
  268. $user_join = $user_join_check;
  269. break;
  270. }
  271. }
  272. end:
  273. //找推荐人的公排末位
  274. $gp_point = 1;
  275. $explode_num = $activity['explode_num'];
  276. $gp_point_list = [];
  277. for ($i = $explode_num - 2; $i >= -1; $i--) {
  278. $gp_point_list[] = $user_join['group_num'] * $explode_num - $i;
  279. }
  280. while (1) {
  281. $member_list = self::where('aid', $id)->where('group_num', 'in', $gp_point_list)->order('group_num', 'asc')->select();
  282. if (count($member_list) < count($gp_point_list)) {
  283. foreach ($gp_point_list as $k => $v) {
  284. if (!isset($member_list[$k]) || $v != $member_list[$k]['group_num']) {
  285. $gp_point = $v;
  286. break;
  287. }
  288. }
  289. break;
  290. }
  291. $gp_point_list = [];
  292. foreach ($member_list as $v) {
  293. for ($i = $explode_num - 2; $i >= -1; $i--) {
  294. $gp_point_list[] = $v['group_num'] * $explode_num - $i;
  295. }
  296. }
  297. }
  298. return $gp_point;
  299. }
  300. }