UserMiningMachine.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <?php
  2. namespace app\models\mining;
  3. use app\models\user\User;
  4. use app\models\user\UserLevel;
  5. use app\models\user\UserMoney;
  6. use crmeb\basic\BaseModel;
  7. use crmeb\traits\ModelTrait;
  8. use think\Exception;
  9. class UserMiningMachine extends BaseModel
  10. {
  11. /**
  12. * 数据表主键
  13. * @var string
  14. */
  15. protected $pk = 'id';
  16. /**
  17. * 模型名称
  18. * @var string
  19. */
  20. protected $name = 'user_mining_machine';
  21. use ModelTrait;
  22. /**
  23. * @param int $status
  24. * @return UserMiningMachine
  25. */
  26. public static function valid($status = 1)
  27. {
  28. return self::where('status', $status)->where('mining_start_time', '<=', time())
  29. ->where('mining_end_time', '>', time())->where('paid', 1);
  30. }
  31. /**
  32. * @return UserMiningMachine
  33. */
  34. public static function dayMiningStatusStart()
  35. {
  36. return self::valid(0)->update(['status' => 1]);
  37. }
  38. /**
  39. * @return UserMiningMachine
  40. */
  41. public static function dayMiningStatusEnd()
  42. {
  43. return self::where('status', 'in', [0, 1])->where('mining_end_time', '<=', time())
  44. ->where('paid', 1)->update(['status' => 2]);
  45. }
  46. public static function dayMining()
  47. {
  48. $p = file_get_contents('mass_ratio');
  49. $p = explode('*-&-*', $p);
  50. if ($p[0] != date('Y-m-d')) {
  51. $p[1] = $p[1] + 1;
  52. }
  53. $p = $p[1] > 35 ? 35 : $p[1];
  54. file_put_contents('mass_ratio', date('Y-m-d') . '*-&-*' . $p);
  55. // var_dump($p);
  56. //今日已发放矿机
  57. BaseModel::beginTrans();
  58. self::dayMiningStatusEnd();
  59. self::dayMiningStatusStart();
  60. try {
  61. $res = true;
  62. $send_ids = UserMining::where('add_date', date('Y-m-d'))->column('umid');
  63. // var_dump(UserMining::getLastSql());
  64. // var_dump($send_ids);
  65. //今日需发放且未发放的矿机
  66. $list = self::where('status', 1)->where('mining_start_time', '<=', time() - (3600 * 24))
  67. ->where('mining_end_time', '>', time())->where('paid', 1)->whereNotIn('id', $send_ids)->select();
  68. // var_dump($list);
  69. // var_dump($list);
  70. if (count($list)) {
  71. // var_dump($list);
  72. $money_types = MiningMachine::group('get_money_type')->field('COUNT(id),get_money_type')->select();
  73. $day_gets = [];
  74. foreach ($money_types as $v) {
  75. if ($v['get_money_type'] == 'XCH') {
  76. $day_gets[$v['get_money_type']] = get_hpool_price($v['get_money_type']);
  77. } else if ($v['get_money_type'] == 'FIL') {
  78. $day_gets[$v['get_money_type']] = get_fil_price();
  79. } else {
  80. $day_gets[$v['get_money_type']] = get_hpool_price($v['get_money_type']);
  81. }
  82. }
  83. foreach ($list as $v) {
  84. $machine = MiningMachine::get($v['mid']);
  85. // var_dump(date('Y-m-d H:i:s',$v['third_step_start_time']));
  86. // exit;
  87. if (time() > $v['third_step_start_time']) {
  88. //第三阶段 释放质押和未发放完的部分
  89. $day_get = bcmul($machine['day_get'] > 0 ? $machine['day_get'] : $day_gets[$machine['get_money_type']], $v['num'], 8);
  90. if ($v['get_money_type'] == 'XCH')
  91. $day_get = bcmul($day_get, 0.84, 8);
  92. if ($v['get_money_type'] == 'MASS')
  93. $day_get = bcmul($day_get, 1.35 - $p * 0.01, 8);
  94. $send_stand = bcdiv($v['stand_money'], $machine['third_step_time'], 8);
  95. $day_unlock = 0;
  96. $locks = UserMining::where('umid', $v['id'])->where('add_date', '<>', strtotime('Y-m-d'))->where('lock_money', '>', 0)->select();
  97. foreach ($locks as $vv) {
  98. if ($vv['lock_money'] >= $vv['day_unlock_money']) {
  99. $day_unlock = bcadd($day_unlock, $vv['day_unlock_money'], 8);
  100. UserMining::where('id', $vv['id'])->dec('lock_money', $vv['day_unlock_money'])->update();
  101. } else {
  102. $day_unlock = bcadd($day_unlock, $vv['lock_money'], 8);
  103. UserMining::where('id', $vv['id'])->dec('lock_money', $vv['lock_money'])->update();
  104. }
  105. }
  106. if (bcadd($send_stand, $day_unlock, 8)) {
  107. $res = $res && UserMining::create([
  108. 'umid' => $v['id'],
  109. 'get_money' => bcadd($send_stand, $day_unlock, 8),
  110. 'get_money_type' => $v['get_money_type'],
  111. 'add_time' => time(),
  112. 'unstand' => $send_stand,
  113. 'unlock' => $day_unlock,
  114. 'step' => 3,
  115. 'add_date' => date('Y-m-d'),
  116. ]);
  117. }
  118. if ($day_unlock > 0) {
  119. $res = $res && UserMoney::incomeMoney($v['uid'], $v['get_money_type'], $day_unlock, 'mining', '挖矿', '第二阶段锁定金释放' . $day_unlock . init_money_type()[$v['get_money_type']])
  120. && self::sendGroupAward($v['uid'], $v['mid'], $v['get_money_type'], $day_unlock)
  121. && self::sendSystemAward($v['uid'], $v['get_money_type'], $day_unlock);
  122. }
  123. if ($send_stand > 0) {
  124. $res = $res && UserMoney::incomeMoney($v['uid'], $v['get_money_type'], $send_stand, 'mining', '挖矿', '第一阶段质押金释放' . $send_stand . init_money_type()[$v['get_money_type']]);
  125. }
  126. } elseif (time() > $v['second_step_start_time']) {
  127. $day_unlock = 0;
  128. $locks = UserMining::where('umid', $v['id'])->where('add_date', '<>', strtotime('Y-m-d'))->where('lock_money', '>', 0)->select();
  129. foreach ($locks as $vv) {
  130. if ($vv['lock_money'] >= $vv['day_unlock_money']) {
  131. $day_unlock = bcadd($day_unlock, $vv['day_unlock_money'], 8);
  132. UserMining::where('id', $vv['id'])->dec('lock_money', $vv['day_unlock_money'])->update();
  133. } else {
  134. $day_unlock = bcadd($day_unlock, $vv['lock_money'], 8);
  135. UserMining::where('id', $vv['id'])->dec('lock_money', $vv['lock_money'])->update();
  136. }
  137. }
  138. $day_get = bcmul($machine['day_get'] > 0 ? $machine['day_get'] : $day_gets[$machine['get_money_type']], $v['num'], 8);
  139. if ($v['get_money_type'] == 'XCH')
  140. $day_get = bcmul($day_get, 0.84, 8);
  141. if ($v['get_money_type'] == 'MASS')
  142. $day_get = bcmul($day_get, 1.35 - $p * 0.01, 8);
  143. $service_ratio = $machine['service_ratio'];
  144. $service_ratio = UserMiningService::where('uid', $v['uid'])->where('mid', $machine['id'])->value('ratio') ?: $service_ratio;
  145. $service_ratio = bcsub(1, bcdiv($service_ratio, 100, 4), 4);
  146. $day_service_get = bcmul($service_ratio, $day_get, 8);
  147. $second_step_get_ratio = bcdiv($machine['second_step_get_ratio'], 100, 4);
  148. $day_real_get = bcmul($second_step_get_ratio, $day_service_get, 8);
  149. $day_lock = bcsub($day_service_get, $day_real_get, 8);
  150. if ($day_real_get > 0) {
  151. $res = $res && UserMining::create([
  152. 'umid' => $v['id'],
  153. 'get_money' => bcadd($day_real_get, $day_unlock, 8),
  154. 'get_money_type' => $v['get_money_type'],
  155. 'add_time' => time(),
  156. 'add_date' => date('Y-m-d'),
  157. 'lock_money' => $day_lock,
  158. 'all_lock' => $day_lock,
  159. 'unlock' => $day_unlock,
  160. 'step' => 2,
  161. 'day_unlock_money' => bcdiv($day_lock, $machine['third_step_time'], 8),
  162. ]) && UserMoney::incomeMoney($v['uid'], $v['get_money_type'], bcadd($day_real_get, $day_unlock, 8), 'mining', '挖矿', '第二阶段每日释放' . $day_get . init_money_type()[$v['get_money_type']] . ',扣除技术服务费后实际到账' . $day_real_get . init_money_type()[$v['get_money_type']] . ($day_lock > 0 ? ',锁定' . $day_lock . init_money_type()[$v['get_money_type']] : '') . ($day_unlock > 0 ? ',解锁第二阶段锁定金' . $day_unlock . init_money_type()[$v['get_money_type']] : ''))
  163. && self::sendGroupAward($v['uid'], $v['mid'], $v['get_money_type'], bcadd($day_real_get, $day_unlock, 8))
  164. // && self::sendGroupAward($v['uid'], $v['mid'], $v['get_money_type'], $day_get)
  165. && self::sendSystemAward($v['uid'], $v['get_money_type'], bcadd($day_real_get, $day_unlock, 8));
  166. }
  167. } else {
  168. // var_dump($day_gets);
  169. $day_get = bcmul($machine['day_get'] > 0 ? $machine['day_get'] : $day_gets[$machine['get_money_type']], $v['num'], 8);
  170. // var_dump($day_get);
  171. if ($v['get_money_type'] == 'XCH')
  172. $day_get = bcmul($day_get, 0.84, 8);
  173. if ($v['get_money_type'] == 'MASS')
  174. $day_get = bcmul($day_get, 1.35 - $p * 0.01, 8);
  175. $service_ratio = $machine['service_ratio'];
  176. $service_ratio = UserMiningService::where('uid', $v['uid'])->where('mid', $machine['id'])->value('ratio') ?: $service_ratio;
  177. $service_ratio = bcsub(1, bcdiv($service_ratio, 100, 4), 4);
  178. $day_service_get = bcmul($service_ratio, $day_get, 8);
  179. $first_step_get_ratio = bcdiv($machine['first_step_get_ratio'], 100, 4);
  180. $day_real_get = bcmul($first_step_get_ratio, $day_service_get, 8);
  181. $day_stand = bcsub($day_service_get, $day_real_get, 8);
  182. // var_dump($day_real_get);
  183. // var_dump(compact('day_get', 'service_ratio', 'day_service_get', 'first_step_get_ratio', 'day_real_get', 'day_stand'));
  184. if ($day_real_get > 0) {
  185. $res = $res && UserMining::create([
  186. 'umid' => $v['id'],
  187. 'get_money' => $day_real_get,
  188. 'get_money_type' => $v['get_money_type'],
  189. 'add_time' => time(),
  190. 'step' => 1,
  191. 'add_date' => date('Y-m-d'),
  192. ]) && UserMoney::incomeMoney($v['uid'], $v['get_money_type'], bcadd($day_real_get, 0, 8), 'mining', '挖矿', '第一阶段每日释放' . $day_get . init_money_type()[$v['get_money_type']] . ',扣除技术服务费后实际到账' . $day_real_get . init_money_type()[$v['get_money_type']] . ($day_stand > 0 ? ',质押' . $day_stand . init_money_type()[$v['get_money_type']] : ''))
  193. && self::sendGroupAward($v['uid'], $v['mid'], $v['get_money_type'], bcadd($day_real_get, 0, 8))
  194. // && self::sendGroupAward($v['uid'], $v['mid'], $v['get_money_type'], $day_get)
  195. && self::sendSystemAward($v['uid'], $v['get_money_type'], bcadd($day_real_get, 0, 8));
  196. }
  197. if ($day_stand > 0) {
  198. $res = $res && self::where('id', $v['id'])->inc('stand_money', $day_stand)->update();
  199. }
  200. }
  201. MiningLog::create(['day' => date('Y-m-d'), 'mid' => $v['id'], 'mining' => $day_get]);
  202. }
  203. }
  204. // var_dump($res);
  205. if ($res) {
  206. BaseModel::commitTrans();
  207. return true;
  208. } else
  209. return self::setErrorInfo(self::getErrorInfo(), false);
  210. } catch (Exception $e) {
  211. return self::setErrorInfo($e->getMessage(), true);
  212. }
  213. }
  214. public static function sendGroupAward($uid, $mid, $money_type, $num)
  215. {
  216. $user = User::getUserInfo($uid);
  217. $spread = User::getUserInfo($user['spread_uid']);
  218. $ratio = UserMiningService::where('uid', $uid)->where('mid', $mid)->value('ratio');
  219. if (!$ratio) $ratio = MiningMachine::where('id', $mid)->value('service_ratio');
  220. $num = bcdiv($num, bcsub(1, bcdiv($ratio, 100, 4), 4), 8);
  221. $res = true;
  222. //var_dump($ratio);
  223. //var_dump($num);
  224. //var_dump($user);
  225. //var_dump($spread);
  226. // $send = 0;
  227. $v = 0;
  228. while ($spread) {
  229. $ratio2 = UserMiningService::where('uid', $spread['uid'])->where('mid', $mid)->value('ratio');
  230. if (!$ratio2) $ratio2 = MiningMachine::where('id', $mid)->value('service_ratio');
  231. //var_dump($ratio2);
  232. if ($ratio2 >= $ratio) {
  233. $spread = User::getUserInfo($spread['spread_uid']);
  234. } else {
  235. $brokerage = bcmul($num, bcdiv(bcsub($ratio, $ratio2), 100, 4), 8);
  236. if ($brokerage > 0) {
  237. //if (bcsub($brokerage, $send, 8) > 0) {
  238. $v++;
  239. //$v = UserLevel::getUserLevelInfo(UserLevel::getUserLevel($spread['uid']), 'level_id');
  240. $res = $res && UserMoney::incomeMoney($spread['uid'], $money_type, $brokerage, 'group_create_brokerage', '分享算力', '获得分享算力V' . $v . ':' . $user['nickname'] . '(' . $user['uid'] . ')' . '今日矿机收益分红(' . bcsub($ratio, $ratio2) . '%×' . $num . ')' . ',合计' . $brokerage . init_money_type()[$money_type]);
  241. // }
  242. // $send = $brokerage;
  243. $ratio = $ratio2;
  244. }
  245. $spread = User::getUserInfo($spread['spread_uid']);
  246. }
  247. }
  248. return $res;
  249. }
  250. public static function sendSystemAward($uid, $money_type, $num)
  251. {
  252. $user = User::getUserInfo($uid);
  253. $ratio = sys_config('system_create_award_ratio', 0);
  254. if (!$ratio) return true;
  255. $brokerage = bcmul($num, bcdiv($ratio, 100, 4), 8);
  256. if ($brokerage <= 0) return true;
  257. $users = UserLevel::valiWhere()->where(function ($query) {
  258. $query->where('is_forever', 1)->whereOr('valid_time', '>', time());
  259. })->where('system_create_award_ratio', 1)->column('uid');
  260. foreach ($users as &$v) {
  261. if (!UserLevel::getUserLevelInfo(UserLevel::getUserLevel($v), 'system_create_award_ratio')) {
  262. unset($v);
  263. }
  264. }
  265. $res = true;
  266. if (count($users) > 0)
  267. $brokerage = bcdiv($brokerage, count($users), 8);
  268. else
  269. $brokerage = 0;
  270. if ($brokerage > 0) {
  271. foreach ($users as $v) {
  272. $res = $res && UserMoney::incomeMoney($v, $money_type, $brokerage, 'system_create_brokerage', '系统产币分红', '获得系统用户' . $user['nickname'] . '(' . $user['uid'] . ')' . '今日矿机收益分红');
  273. }
  274. }
  275. return $res;
  276. }
  277. public static function getList($where)
  278. {
  279. $model = self::getOrderWhere($where, self::alias('a')
  280. ->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')
  281. ->field('a.*,r.nickname,r.phone,r.spread_uid');
  282. $model = $model->order('a.id desc');
  283. $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()->each(function ($item) use ($where) {
  284. $item['machine'] = MiningMachine::get($item['mid']);
  285. $item['user'] = User::getUserInfo($item['uid'])['nickname'] . '/' . $item['uid'];
  286. $item['_user'] = User::getUserInfo($item['uid']);
  287. $item['sum_get'] = UserMining::where('umid', $item['id'])->sum('get_money');
  288. $item['sum_lock'] = UserMining::where('umid', $item['id'])->sum('lock_money');
  289. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  290. $item['_stop_time'] = date('Y-m-d H:i:s', $item['mining_end_time']);
  291. $item['mining_end_time'] = $item['_stop_time'];
  292. $item['_cost_money'] = $item['cost_money'] * 1 . init_money_type()[$item['cost_money_type']];
  293. $item['_stand_money'] = $item['stand_money'] * 1 . init_money_type()[$item['get_money_type']];
  294. $item['mining_end_time'] = $item['_stop_time'];
  295. $item['_start_time'] = date('Y-m-d H:i:s', $item['mining_start_time']);
  296. $item['_pay_time'] = date('Y-m-d H:i:s', $item['pay_time']);
  297. if (isset($where['uid']) && $where['uid']) {
  298. $uids = User::where('spread_uid', $where['uid'])->column('uid');
  299. $item['award_ratio'] = UserMiningRatio::where('uid', $where['uid'])->where('mid', $item['mid'])->value('ratio') ?: 0;
  300. $item['lower_award_ratio'] = UserMiningRatio::where('mid', $item['id'])->where('uid', 'in', $uids)->max('ratio') ?: 0;
  301. $item['user_service_ratio'] = UserMiningService::where('uid', $where['uid'])->where('mid', $item['mid'])->value('ratio') ?: $item['machine']['service_ratio'];
  302. $item['lower_service_ratio'] = UserMiningService::where('mid', $item['mid'])->where('uid', 'in', $uids)->min('ratio') ?: $item['machine']['service_ratio'];
  303. }
  304. })) && count($data) ? $data->toArray() : [];
  305. $count = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->count();
  306. return compact('count', 'data');
  307. }
  308. public static function orderCount()
  309. {
  310. $data['wf'] = self::statusByWhere(-1, new self())->count();
  311. $data['dd'] = self::statusByWhere(0, new self())->count();
  312. $data['wk'] = self::statusByWhere(1, new self())->count();
  313. $data['js'] = self::statusByWhere(2, new self())->count();
  314. return $data;
  315. }
  316. public static function statusByWhere($status, $model = null, $alert = '')
  317. {
  318. if ($model == null) $model = new self;
  319. if ('' === $status)
  320. return $model;
  321. else if ($status == 0)//等待种
  322. return $model->where($alert . 'paid', 1)->where($alert . 'status', 0);
  323. else if ($status == 1)//已支付 未发货
  324. return $model->where($alert . 'paid', 1)->where($alert . 'status', 1);
  325. else if ($status == 2)//已支付 待收货
  326. return $model->where($alert . 'paid', 1)->where($alert . 'status', 2);
  327. else if ($status == -1)//未支付
  328. return $model->where($alert . 'paid', 0);
  329. else
  330. return $model;
  331. }
  332. public static function getBadge($where)
  333. {
  334. $price = self::getOrderPrice($where);
  335. $data = [
  336. [
  337. 'name' => '订单数量',
  338. 'field' => '件',
  339. 'count' => $price['count_sum'],
  340. 'background_color' => 'layui-bg-blue',
  341. 'col' => 2
  342. ],
  343. [
  344. 'name' => '算力总量',
  345. 'field' => 'T',
  346. 'count' => $price['total_num'],
  347. 'background_color' => 'layui-bg-blue',
  348. 'col' => 2
  349. ],
  350. ];
  351. $money_type = init_money_type();
  352. foreach ($money_type as $k => $v) {
  353. $data = array_merge(
  354. $data,
  355. [[
  356. 'name' => $v . '购买支付',
  357. 'field' => $k,
  358. 'count' => $price[$k . '_pay'] * 1,
  359. 'background_color' => 'layui-bg-blue',
  360. 'col' => 2
  361. ], [
  362. 'name' => $v . '质押',
  363. 'field' => $k,
  364. 'count' => $price[$k . '_stand'] * 1,
  365. 'background_color' => 'layui-bg-blue',
  366. 'col' => 2
  367. ]]
  368. );
  369. }
  370. return $data;
  371. }
  372. public static function getOrderWhere($where, $model, $aler = '', $join = '')
  373. {
  374. $model = self::statusByWhere($where['status'] ?? '', $model, $aler);
  375. if (isset($where['uid']) && $where['uid'] != '' && $where['uid'] != []) {
  376. if (is_array($where['uid']))
  377. $model = $model->where($aler . 'uid', 'in', $where['uid']);
  378. else
  379. $model = $model->where($aler . 'uid', $where['uid']);
  380. }
  381. if (isset($where['real_name']) && $where['real_name'] != '') {
  382. $model = $model->where($aler . 'id' . ($join ? '|' . $join . '.nickname|' . $join . '.uid|' . $join . '.phone' : ''), 'LIKE', "%$where[real_name]%");
  383. }
  384. if (isset($where['data']) && $where['data'] !== '') {
  385. $model = self::getModelTime($where, $model, $aler . 'add_time');
  386. }
  387. if (isset($where['cost_money_type']) && $where['cost_money_type'] !== '') {
  388. $model = $model->where($aler . 'cost_money_type', $where['cost_money_type']);
  389. }
  390. // var_dump($model);
  391. return $model;
  392. }
  393. /**
  394. * 处理订单金额
  395. * @param $where
  396. * @return array
  397. */
  398. public static function getOrderPrice($where)
  399. {
  400. $model = new self;
  401. $price = [];
  402. $price['count_sum'] = 0;//支付金额
  403. $price['total_num'] = 0;
  404. $money_type = init_money_type();
  405. foreach ($money_type as $k => $v) {
  406. $whereData['cost_money_type'] = $k;
  407. $sumNumber = self::getOrderWhere(array_merge($where, $whereData), $model)->field([
  408. 'sum(cost_money) as sum',
  409. 'sum(stand_money) as stand_sum',
  410. ])->find();
  411. // var_dump($sumNumber);
  412. // var_dump(self::getLastSql());
  413. $price[$k . '_pay'] = $sumNumber['sum'];
  414. $price[$k . '_stand'] = $sumNumber['stand_sum'];
  415. }
  416. // var_dump($where);
  417. $sumNumber = self::getOrderWhere($where, $model)->field([
  418. 'sum(num) as sum_total_num',
  419. 'count(id) as count_sum',
  420. ])->find();
  421. if ($sumNumber) {
  422. $price['count_sum'] = $sumNumber['count_sum'];
  423. $price['total_num'] = $sumNumber['sum_total_num'];
  424. }
  425. return $price;
  426. }
  427. }