UserMiningMachine.php 18 KB

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