UserMiningMachine.php 16 KB

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