LalaPink.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace app\models\lala;
  3. use app\models\mining\UserMiningMachine;
  4. use app\models\system\SystemMoney;
  5. use app\models\user\User;
  6. use app\models\user\UserMoney;
  7. use Exception;
  8. use think\db\exception\DataNotFoundException;
  9. use think\db\exception\DbException;
  10. use think\db\exception\ModelNotFoundException;
  11. use crmeb\traits\ModelTrait;
  12. use crmeb\basic\BaseModel;
  13. /**
  14. * TODO 文章Model
  15. * Class Article
  16. * @package app\models\article
  17. */
  18. class LalaPink extends BaseModel
  19. {
  20. /**
  21. * 数据表主键
  22. * @var string
  23. */
  24. protected $pk = 'id';
  25. /**
  26. * 模型名称
  27. * @var string
  28. */
  29. protected $name = 'lala_pink';
  30. use ModelTrait;
  31. /**
  32. * @return LalaPink
  33. */
  34. public static function validWhere()
  35. {
  36. return self::where('status', 1);
  37. }
  38. /**
  39. * @param $where
  40. * @param $page
  41. * @param $limit
  42. * @return array
  43. */
  44. public static function getList($where, $page, $limit): array
  45. {
  46. $model = self::validWhere();
  47. $data = $model->page((int)$page, (int)$limit)->select()->each(function ($item) {
  48. $item['_cost'] = ($item['cost'] * 1) . get_money_name($item['cost_money_type']);
  49. $item['_bingo'] = ($item['bingo'] * 1) . get_money_name($item['bingo_money_type']);
  50. if ($item['ticket_money_type'] != 'LALA') {
  51. $money_types = sys_data('money_type');
  52. $origin_ratio = 0;
  53. $target_ratio = 0;
  54. foreach ($money_types as $v) {
  55. if ($v['code'] == $item['ticket_money_type']) {
  56. $origin_ratio = $v['usdt_price'];
  57. if ($origin_ratio == 0) {
  58. $origin_ratio = get_huobi_price($v['code']);
  59. }
  60. }
  61. if ($v['code'] == 'LALA') {
  62. $target_ratio = get_lala_ratio();
  63. }
  64. }
  65. $ratio = bcdiv($origin_ratio, $target_ratio, 14);
  66. $item['ticket'] = bcmul($item['ticket'], $ratio, 8);
  67. $item['ticket_money_type'] = 'LALA';
  68. }
  69. $item['ticket_money_type'] = get_money_name($item['ticket_money_type']);
  70. $item['cost_money_type'] = get_money_name($item['cost_money_type']);
  71. $item['cost_2_money_type'] = get_money_name($item['cost_2_money_type']);
  72. $item['bingo_money_type'] = get_money_name($item['bingo_money_type']);
  73. $item['_ticket'] = ($item['ticket'] * 1) . $item['ticket_money_type'];
  74. $item['_status'] = $item['status'] ? "正常" : "已关闭";
  75. });
  76. $count = $model->count();
  77. return compact('data', 'count');
  78. }
  79. /**
  80. * @param $where
  81. * @param $page
  82. * @param $limit
  83. * @return array
  84. */
  85. public static function getExchangeList($id, $uid, $page, $limit): array
  86. {
  87. $model = LalaSpExchange::where('lala_id', $id)->where('uid', $uid);
  88. $data = $model->order('add_time desc,id desc')->page((int)$page, (int)$limit)->select()->each(function ($item) {
  89. $item['origin_money_type'] = get_money_name($item['origin_money_type']);
  90. $item['target_money_type'] = get_money_name($item['target_money_type']);
  91. });
  92. $count = $model->count();
  93. return compact('data', 'count');
  94. }
  95. /**
  96. * @param $uid
  97. * @param $id
  98. * @return bool
  99. * @throws DataNotFoundException
  100. * @throws DbException
  101. * @throws ModelNotFoundException
  102. */
  103. public static function spExchange($uid, $id): bool
  104. {
  105. $user_times = LalaPinkJoin::where('uid', $uid)->where('lala_id', $id)->where('status', 1)->where('paid', 1)->count();
  106. $used_times = LalaSpExchange::where('uid', $uid)->where('lala_id', $id)->sum('cost_time');
  107. $used_times += UserMiningMachine::where('uid', $uid)->where('paid', 1)->where('lala_id', $id)->sum('cost_times');
  108. $lala_info = self::validWhere()->where('id', $id)->find();
  109. $user_info = User::getUserInfo($uid);
  110. if (!$user_info) return self::setErrorInfo('用户异常');
  111. if (!$lala_info) return self::setErrorInfo('找不到拼购活动');
  112. if (bcsub($user_times, $used_times) < $lala_info['sp_exchange_bingo_time']) {
  113. return self::setErrorInfo('剩余幸运值不足,无法兑换');
  114. }
  115. BaseModel::beginTrans();
  116. try {
  117. $res1 = UserMoney::expendMoney($uid, $lala_info['sp_exchange_origin'], $lala_info['sp_exchange_origin_cost'], 'sp_exchange', '特殊兑换', $lala_info['name'] . '特殊兑换');
  118. if (!$res1) {
  119. BaseModel::rollbackTrans();
  120. return self::setErrorInfo(UserMoney::getErrorInfo());
  121. }
  122. $res2 = UserMoney::incomeMoney($uid, $lala_info['sp_exchange_target'], $lala_info['sp_exchange_target_get'], 'sp_exchange', '特殊兑换', $lala_info['name'] . '特殊兑换');
  123. // $res2 = SystemMoney::tradeMoney($user_info['site_id'], $uid, $lala_info['sp_exchange_target'], $lala_info['sp_exchange_target_get']);
  124. if (!$res2) {
  125. BaseModel::rollbackTrans();
  126. return self::setErrorInfo(UserMoney::getErrorInfo());
  127. }
  128. $res3 = LalaSpExchange::create([
  129. 'uid' => $uid,
  130. 'lala_id' => $id,
  131. 'cost_time' => $lala_info['sp_exchange_bingo_time'],
  132. 'add_time' => time(),
  133. 'origin' => $lala_info['sp_exchange_origin_cost'],
  134. 'origin_money_type' => $lala_info['sp_exchange_origin'],
  135. 'target' => $lala_info['sp_exchange_target_get'],
  136. 'target_money_type' => $lala_info['sp_exchange_target'],
  137. ]);
  138. if ($res3) {
  139. BaseModel::commitTrans();
  140. return true;
  141. } else {
  142. BaseModel::rollbackTrans();
  143. return self::setErrorInfo('创建记录失败');
  144. }
  145. } catch (Exception $e) {
  146. BaseModel::rollbackTrans();
  147. return self::setErrorInfo($e->getMessage());
  148. }
  149. }
  150. }