UserMiningMachine.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. //今日已发放矿机
  49. BaseModel::beginTrans();
  50. self::dayMiningStatusEnd();
  51. self::dayMiningStatusStart();
  52. try {
  53. $res = true;
  54. $send_ids = UserMining::where('add_date', date('Y-m-d'))->column('umid');
  55. // var_dump(UserMining::getLastSql());
  56. // var_dump($send_ids);
  57. //今日需发放且未发放的矿机
  58. $list = self::where('status', 1)->where('mining_start_time', '<=', time() - (3600 * 24))
  59. ->where('mining_end_time', '>', time())->where('paid', 1)->whereNotIn('id', $send_ids)->select();
  60. // var_dump($list);
  61. // var_dump($list);
  62. if (count($list)) {
  63. // var_dump($list);
  64. $money_types = MiningMachine::group('get_money_type')->field('COUNT(id),get_money_type')->select();
  65. $day_gets = [];
  66. foreach ($money_types as $v) {
  67. if ($v['get_money_type'] == 'XCH') {
  68. $day_gets[$v['get_money_type']] = get_hpool_price($v['get_money_type']);
  69. } else if ($v['get_money_type'] == 'FIL') {
  70. $day_gets[$v['get_money_type']] = get_fil_price();
  71. } else {
  72. $day_gets[$v['get_money_type']] = get_hpool_price($v['get_money_type']);
  73. }
  74. }
  75. foreach ($list as $v) {
  76. $machine = MiningMachine::get($v['mid']);
  77. // var_dump(date('Y-m-d H:i:s',$v['third_step_start_time']));
  78. // exit;
  79. if (time() > $v['third_step_start_time']) {
  80. //第三阶段 释放质押和未发放完的部分
  81. $send_stand = bcdiv($v['stand_money'], $machine['third_step_time'], 8);
  82. $day_unlock = 0;
  83. $locks = UserMining::where('umid', $v['id'])->where('add_date', '<>', strtotime('Y-m-d'))->where('lock_money', '>', 0)->select();
  84. foreach ($locks as $vv) {
  85. if ($vv['lock_money'] >= $vv['day_unlock_money']) {
  86. $day_unlock = bcadd($day_unlock, $vv['day_unlock_money'], 8);
  87. UserMining::where('id', $vv['id'])->dec('lock_money', $vv['day_unlock_money'])->update();
  88. } else {
  89. $day_unlock = bcadd($day_unlock, $vv['lock_money'], 8);
  90. UserMining::where('id', $vv['id'])->dec('lock_money', $vv['lock_money'])->update();
  91. }
  92. }
  93. if (bcadd($send_stand, $day_unlock, 8)) {
  94. $res = $res && UserMining::create([
  95. 'umid' => $v['id'],
  96. 'get_money' => bcadd($send_stand, $day_unlock, 8),
  97. 'get_money_type' => $v['get_money_type'],
  98. 'add_time' => time(),
  99. 'add_date' => date('Y-m-d'),
  100. ]);
  101. }
  102. if ($day_unlock > 0) {
  103. $res = $res && UserMoney::incomeMoney($v['uid'], $v['get_money_type'], $day_unlock, 'mining', '挖矿', '第二阶段锁定金释放' . $day_unlock . init_money_type()[$v['get_money_type']])
  104. && self::sendGroupAward($v['uid'], $v['mid'], $v['get_money_type'], $day_unlock)
  105. && self::sendSystemAward($v['uid'], $v['get_money_type'], $day_unlock);
  106. }
  107. if ($send_stand > 0) {
  108. $res = $res && UserMoney::incomeMoney($v['uid'], $v['get_money_type'], $send_stand, 'mining', '挖矿', '第一阶段质押金释放' . $send_stand . init_money_type()[$v['get_money_type']]);
  109. }
  110. } elseif (time() > $v['second_step_start_time']) {
  111. $day_unlock = 0;
  112. $locks = UserMining::where('umid', $v['id'])->where('add_date', '<>', strtotime('Y-m-d'))->where('lock_money', '>', 0)->select();
  113. foreach ($locks as $vv) {
  114. if ($vv['lock_money'] >= $vv['day_unlock_money']) {
  115. $day_unlock = bcadd($day_unlock, $vv['day_unlock_money'], 8);
  116. UserMining::where('id', $vv['id'])->dec('lock_money', $vv['day_unlock_money'])->update();
  117. } else {
  118. $day_unlock = bcadd($day_unlock, $vv['lock_money'], 8);
  119. UserMining::where('id', $vv['id'])->dec('lock_money', $vv['lock_money'])->update();
  120. }
  121. }
  122. $day_get = bcmul($machine['day_get'] > 0 ? $machine['day_get'] : $day_gets[$machine['get_money_type']], $v['num'], 8);
  123. $day_get = bcmul($day_get, 0.84, 8);
  124. $service_ratio = bcsub(1, bcdiv($machine['service_ratio'], 100, 4), 4);
  125. $service_ratio = UserMiningService::where('uid', $v['uid'])->where('mid', $machine['id'])->value('ratio') ?: $service_ratio;
  126. $day_service_get = bcmul($service_ratio, $day_get, 8);
  127. $second_step_get_ratio = bcdiv($machine['second_step_get_ratio'], 100, 4);
  128. $day_real_get = bcmul($second_step_get_ratio, $day_service_get, 8);
  129. $day_lock = bcsub($day_service_get, $day_real_get, 8);
  130. if ($day_real_get > 0) {
  131. $res = $res && UserMining::create([
  132. 'umid' => $v['id'],
  133. 'get_money' => bcadd($day_real_get, $day_unlock, 8),
  134. 'get_money_type' => $v['get_money_type'],
  135. 'add_time' => time(),
  136. 'add_date' => date('Y-m-d'),
  137. 'lock_money' => $day_lock,
  138. 'day_unlock_money' => bcdiv($day_lock, $machine['third_step_time'], 8),
  139. ]) && 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']] : ''))
  140. && self::sendGroupAward($v['uid'], $v['mid'], $v['get_money_type'], bcadd($day_real_get, $day_unlock, 8))
  141. && self::sendSystemAward($v['uid'], $v['get_money_type'], bcadd($day_real_get, $day_unlock, 8));
  142. }
  143. } else {
  144. // var_dump($day_gets);
  145. $day_get = bcmul($machine['day_get'] > 0 ? $machine['day_get'] : $day_gets[$machine['get_money_type']], $v['num'], 8);
  146. // var_dump($day_get);
  147. $day_get = bcmul($day_get, 0.84, 8);
  148. $service_ratio = bcsub(1, bcdiv($machine['service_ratio'], 100, 4), 4);
  149. $service_ratio = UserMiningService::where('uid', $v['uid'])->where('mid', $machine['id'])->value('ratio') ?: $service_ratio;
  150. $day_service_get = bcmul($service_ratio, $day_get, 8);
  151. $first_step_get_ratio = bcdiv($machine['first_step_get_ratio'], 100, 4);
  152. $day_real_get = bcmul($first_step_get_ratio, $day_service_get, 8);
  153. $day_stand = bcsub($day_service_get, $day_real_get, 8);
  154. // var_dump($day_real_get);
  155. // var_dump(compact('day_get', 'service_ratio', 'day_service_get', 'first_step_get_ratio', 'day_real_get', 'day_stand'));
  156. if ($day_real_get > 0) {
  157. $res = $res && UserMining::create([
  158. 'umid' => $v['id'],
  159. 'get_money' => $day_real_get,
  160. 'get_money_type' => $v['get_money_type'],
  161. 'add_time' => time(),
  162. 'add_date' => date('Y-m-d'),
  163. ]) && 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']] : ''))
  164. && self::sendGroupAward($v['uid'], $v['mid'], $v['get_money_type'], bcadd($day_real_get, 0, 8))
  165. && self::sendSystemAward($v['uid'], $v['get_money_type'], bcadd($day_real_get, 0, 8));
  166. }
  167. if ($day_stand > 0) {
  168. $res = $res && self::where('id', $v['id'])->inc('stand_money', $day_stand)->update();
  169. }
  170. }
  171. }
  172. }
  173. // var_dump($res);
  174. if ($res) {
  175. BaseModel::commitTrans();
  176. return true;
  177. } else
  178. return self::setErrorInfo(self::getErrorInfo(), false);
  179. } catch (Exception $e) {
  180. return self::setErrorInfo($e->getMessage(), true);
  181. }
  182. }
  183. public static function sendGroupAward($uid, $mid, $money_type, $num)
  184. {
  185. $user = User::getUserInfo($uid);
  186. $spread = User::getUserInfo($user['spread_uid']);
  187. $res = true;
  188. $send = 0;
  189. $v = 0;
  190. while ($spread) {
  191. // $ratio = UserLevel::getUserLevelInfo(UserLevel::getUserLevel($spread['uid']), 'group_creat_award_ratio');
  192. $ratio = UserMiningRatio::where('uid', $uid)->where('mid', $mid)->value('ratio');
  193. if (!$ratio) $ratio = 0;
  194. if ($ratio <= 0) {
  195. $spread = User::getUserInfo($spread['spread_uid']);
  196. } else {
  197. $brokerage = bcmul($num, bcdiv($ratio, 100, 4), 8);
  198. if (bcsub($brokerage, $send, 8) > 0) {
  199. $v++;
  200. //$v = UserLevel::getUserLevelInfo(UserLevel::getUserLevel($spread['uid']), 'level_id');
  201. $res = $res && UserMoney::incomeMoney($spread['uid'], $money_type, bcsub($brokerage, $send, 8), 'group_create_brokerage', '分享算力', '获得分享算力V' . $v . ':' . $user['nickname'] . '(' . $user['uid'] . ')' . '今日矿机收益分红(' . $ratio . '%×' . $num . ')减去已发放级差' . $send . ',合计' . bcsub($brokerage, $send, 8) . init_money_type()[$money_type]);
  202. }
  203. $send = $brokerage;
  204. $spread = User::getUserInfo($spread['spread_uid']);
  205. }
  206. }
  207. return $res;
  208. }
  209. public static function sendSystemAward($uid, $money_type, $num)
  210. {
  211. $user = User::getUserInfo($uid);
  212. $ratio = sys_config('system_create_award_ratio', 0);
  213. if (!$ratio) return true;
  214. $brokerage = bcmul($num, bcdiv($ratio, 100, 4), 8);
  215. if ($brokerage <= 0) return true;
  216. $users = UserLevel::valiWhere()->where(function ($query) {
  217. $query->where('is_forever', 1)->whereOr('valid_time', '>', time());
  218. })->where('system_create_award_ratio', 1)->column('uid');
  219. foreach ($users as &$v) {
  220. if (!UserLevel::getUserLevelInfo(UserLevel::getUserLevel($v), 'system_create_award_ratio')) {
  221. unset($v);
  222. }
  223. }
  224. $res = true;
  225. if (count($users) > 0)
  226. $brokerage = bcdiv($brokerage, count($users), 8);
  227. else
  228. $brokerage = 0;
  229. if ($brokerage > 0) {
  230. foreach ($users as $v) {
  231. $res = $res && UserMoney::incomeMoney($v, $money_type, $brokerage, 'system_create_brokerage', '系统产币分红', '获得系统用户' . $user['nickname'] . '(' . $user['uid'] . ')' . '今日矿机收益分红');
  232. }
  233. }
  234. return $res;
  235. }
  236. public static function getList($where)
  237. {
  238. $model = self::getOrderWhere($where, self::alias('a')
  239. ->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')
  240. ->field('a.*,r.nickname,r.phone,r.spread_uid');
  241. $model = $model->order('a.id desc');
  242. $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()->each(function ($item) {
  243. $item['machine'] = MiningMachine::get($item['mid']);
  244. $item['user'] = User::getUserInfo($item['uid'])['nickname'] . '/' . $item['uid'];
  245. $item['_user'] = User::getUserInfo($item['uid']);
  246. $item['sum_get'] = UserMining::where('umid', $item['id'])->sum('get_money');
  247. $item['sum_lock'] = UserMining::where('umid', $item['id'])->sum('lock_money');
  248. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  249. $item['_stop_time'] = date('Y-m-d H:i:s', $item['mining_end_time']);
  250. $item['mining_end_time'] = $item['_stop_time'];
  251. $item['_cost_money'] = $item['cost_money'] * 1 . init_money_type()[$item['cost_money_type']];
  252. $item['_stand_money'] = $item['stand_money'] * 1 . init_money_type()[$item['get_money_type']];
  253. $item['mining_end_time'] = $item['_stop_time'];
  254. $item['_start_time'] = date('Y-m-d H:i:s', $item['mining_start_time']);
  255. $item['_pay_time'] = date('Y-m-d H:i:s', $item['pay_time']);
  256. })) && count($data) ? $data->toArray() : [];
  257. $count = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->count();
  258. return compact('count', 'data');
  259. }
  260. public static function orderCount()
  261. {
  262. $data['wf'] = self::statusByWhere(-1, new self())->count();
  263. $data['dd'] = self::statusByWhere(0, new self())->count();
  264. $data['wk'] = self::statusByWhere(1, new self())->count();
  265. $data['js'] = self::statusByWhere(2, new self())->count();
  266. return $data;
  267. }
  268. public static function statusByWhere($status, $model = null, $alert = '')
  269. {
  270. if ($model == null) $model = new self;
  271. if ('' === $status)
  272. return $model;
  273. else if ($status == 0)//等待种
  274. return $model->where($alert . 'paid', 1)->where($alert . 'status', 0);
  275. else if ($status == 1)//已支付 未发货
  276. return $model->where($alert . 'paid', 1)->where($alert . 'status', 1);
  277. else if ($status == 2)//已支付 待收货
  278. return $model->where($alert . 'paid', 1)->where($alert . 'status', 2);
  279. else if ($status == -1)//未支付
  280. return $model->where($alert . 'paid', 0);
  281. else
  282. return $model;
  283. }
  284. public static function getBadge($where)
  285. {
  286. $price = self::getOrderPrice($where);
  287. $data = [
  288. [
  289. 'name' => '订单数量',
  290. 'field' => '件',
  291. 'count' => $price['count_sum'],
  292. 'background_color' => 'layui-bg-blue',
  293. 'col' => 2
  294. ],
  295. [
  296. 'name' => '算力总量',
  297. 'field' => 'T',
  298. 'count' => $price['total_num'],
  299. 'background_color' => 'layui-bg-blue',
  300. 'col' => 2
  301. ],
  302. ];
  303. $money_type = init_money_type();
  304. foreach ($money_type as $k => $v) {
  305. $data = array_merge(
  306. $data,
  307. [[
  308. 'name' => $v . '购买支付',
  309. 'field' => $k,
  310. 'count' => $price[$k . '_pay'] * 1,
  311. 'background_color' => 'layui-bg-blue',
  312. 'col' => 2
  313. ], [
  314. 'name' => $v . '质押',
  315. 'field' => $k,
  316. 'count' => $price[$k . '_stand'] * 1,
  317. 'background_color' => 'layui-bg-blue',
  318. 'col' => 2
  319. ]]
  320. );
  321. }
  322. return $data;
  323. }
  324. public static function getOrderWhere($where, $model, $aler = '', $join = '')
  325. {
  326. $model = self::statusByWhere($where['status'] ?? '', $model, $aler);
  327. if (isset($where['uid']) && $where['uid'] != '' && $where['uid'] != []) {
  328. if (is_array($where['uid']))
  329. $model = $model->where($aler . 'uid', 'in', $where['uid']);
  330. else
  331. $model = $model->where($aler . 'uid', $where['uid']);
  332. }
  333. if (isset($where['real_name']) && $where['real_name'] != '') {
  334. $model = $model->where($aler . 'id' . ($join ? '|' . $join . '.nickname|' . $join . '.uid|' . $join . '.phone' : ''), 'LIKE', "%$where[real_name]%");
  335. }
  336. if (isset($where['data']) && $where['data'] !== '') {
  337. $model = self::getModelTime($where, $model, $aler . 'add_time');
  338. }
  339. if (isset($where['cost_money_type']) && $where['cost_money_type'] !== '') {
  340. $model = $model->where($aler . 'cost_money_type', $where['cost_money_type']);
  341. }
  342. // var_dump($model);
  343. return $model;
  344. }
  345. /**
  346. * 处理订单金额
  347. * @param $where
  348. * @return array
  349. */
  350. public static function getOrderPrice($where)
  351. {
  352. $model = new self;
  353. $price = [];
  354. $price['count_sum'] = 0;//支付金额
  355. $price['total_num'] = 0;
  356. $money_type = init_money_type();
  357. foreach ($money_type as $k => $v) {
  358. $whereData['cost_money_type'] = $k;
  359. $sumNumber = self::getOrderWhere(array_merge($where, $whereData), $model)->field([
  360. 'sum(cost_money) as sum',
  361. 'sum(stand_money) as stand_sum',
  362. ])->find();
  363. // var_dump($sumNumber);
  364. // var_dump(self::getLastSql());
  365. $price[$k . '_pay'] = $sumNumber['sum'];
  366. $price[$k . '_stand'] = $sumNumber['stand_sum'];
  367. }
  368. // var_dump($where);
  369. $sumNumber = self::getOrderWhere($where, $model)->field([
  370. 'sum(num) as sum_total_num',
  371. 'count(id) as count_sum',
  372. ])->find();
  373. if ($sumNumber) {
  374. $price['count_sum'] = $sumNumber['count_sum'];
  375. $price['total_num'] = $sumNumber['sum_total_num'];
  376. }
  377. return $price;
  378. }
  379. }