123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace app\models\lala;
- use app\models\mining\UserMiningMachine;
- use app\models\system\SystemMoney;
- use app\models\user\User;
- use app\models\user\UserMoney;
- use Exception;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use crmeb\traits\ModelTrait;
- use crmeb\basic\BaseModel;
- /**
- * TODO 文章Model
- * Class Article
- * @package app\models\article
- */
- class LalaPink extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'lala_pink';
- use ModelTrait;
- /**
- * @return LalaPink
- */
- public static function validWhere()
- {
- return self::where('status', 1);
- }
- /**
- * @param $where
- * @param $page
- * @param $limit
- * @return array
- */
- public static function getList($where, $page, $limit): array
- {
- $model = self::validWhere();
- $data = $model->page((int)$page, (int)$limit)->select()->each(function ($item) {
- $item['_cost'] = ($item['cost'] * 1) . get_money_name($item['cost_money_type']);
- $item['_bingo'] = ($item['bingo'] * 1) . get_money_name($item['bingo_money_type']);
- if ($item['ticket_money_type'] != 'LALA') {
- $money_types = sys_data('money_type');
- $origin_ratio = 0;
- $target_ratio = 0;
- foreach ($money_types as $v) {
- if ($v['code'] == $item['ticket_money_type']) {
- $origin_ratio = $v['usdt_price'];
- if ($origin_ratio == 0) {
- $origin_ratio = get_huobi_price($v['code']);
- }
- }
- if ($v['code'] == 'LALA') {
- $target_ratio = get_lala_ratio();
- }
- }
- $ratio = bcdiv($origin_ratio, $target_ratio, 14);
- $item['ticket'] = bcmul($item['ticket'], $ratio, 8);
- $item['ticket_money_type'] = 'LALA';
- }
- $item['ticket_money_type'] = get_money_name($item['ticket_money_type']);
- $item['cost_money_type'] = get_money_name($item['cost_money_type']);
- $item['cost_2_money_type'] = get_money_name($item['cost_2_money_type']);
- $item['bingo_money_type'] = get_money_name($item['bingo_money_type']);
- $item['_ticket'] = ($item['ticket'] * 1) . $item['ticket_money_type'];
- $item['_status'] = $item['status'] ? "正常" : "已关闭";
- });
- $count = $model->count();
- return compact('data', 'count');
- }
- /**
- * @param $where
- * @param $page
- * @param $limit
- * @return array
- */
- public static function getExchangeList($id, $uid, $page, $limit): array
- {
- $model = LalaSpExchange::where('lala_id', $id)->where('uid', $uid);
- $data = $model->order('add_time desc,id desc')->page((int)$page, (int)$limit)->select()->each(function ($item) {
- $item['origin_money_type'] = get_money_name($item['origin_money_type']);
- $item['target_money_type'] = get_money_name($item['target_money_type']);
- });
- $count = $model->count();
- return compact('data', 'count');
- }
- /**
- * @param $uid
- * @param $id
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function spExchange($uid, $id): bool
- {
- $user_times = LalaPinkJoin::where('uid', $uid)->where('lala_id', $id)->where('status', 1)->where('paid', 1)->count();
- $used_times = LalaSpExchange::where('uid', $uid)->where('lala_id', $id)->sum('cost_time');
- $used_times += UserMiningMachine::where('uid', $uid)->where('paid', 1)->where('lala_id', $id)->sum('cost_times');
- $lala_info = self::validWhere()->where('id', $id)->find();
- $user_info = User::getUserInfo($uid);
- if (!$user_info) return self::setErrorInfo('用户异常');
- if (!$lala_info) return self::setErrorInfo('找不到拼购活动');
- if (bcsub($user_times, $used_times) < $lala_info['sp_exchange_bingo_time']) {
- return self::setErrorInfo('剩余幸运值不足,无法兑换');
- }
- BaseModel::beginTrans();
- try {
- $res1 = UserMoney::expendMoney($uid, $lala_info['sp_exchange_origin'], $lala_info['sp_exchange_origin_cost'], 'sp_exchange', '特殊兑换', $lala_info['name'] . '特殊兑换');
- if (!$res1) {
- BaseModel::rollbackTrans();
- return self::setErrorInfo(UserMoney::getErrorInfo());
- }
- $res2 = UserMoney::incomeMoney($uid, $lala_info['sp_exchange_target'], $lala_info['sp_exchange_target_get'], 'sp_exchange', '特殊兑换', $lala_info['name'] . '特殊兑换');
- // $res2 = SystemMoney::tradeMoney($user_info['site_id'], $uid, $lala_info['sp_exchange_target'], $lala_info['sp_exchange_target_get']);
- if (!$res2) {
- BaseModel::rollbackTrans();
- return self::setErrorInfo(UserMoney::getErrorInfo());
- }
- $res3 = LalaSpExchange::create([
- 'uid' => $uid,
- 'lala_id' => $id,
- 'cost_time' => $lala_info['sp_exchange_bingo_time'],
- 'add_time' => time(),
- 'origin' => $lala_info['sp_exchange_origin_cost'],
- 'origin_money_type' => $lala_info['sp_exchange_origin'],
- 'target' => $lala_info['sp_exchange_target_get'],
- 'target_money_type' => $lala_info['sp_exchange_target'],
- ]);
- if ($res3) {
- BaseModel::commitTrans();
- return true;
- } else {
- BaseModel::rollbackTrans();
- return self::setErrorInfo('创建记录失败');
- }
- } catch (Exception $e) {
- BaseModel::rollbackTrans();
- return self::setErrorInfo($e->getMessage());
- }
- }
- }
|