123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977 |
- <?php
- namespace app\models\lala;
- use app\models\point_plan\PointPlan;
- use app\models\point_plan\UserPointPlan;
- use app\models\system\Message;
- use app\models\system\SystemMoney;
- use app\models\user\User;
- use app\models\user\UserBill;
- use app\models\user\UserLevel;
- use app\models\user\UserMoney;
- use crmeb\traits\ModelTrait;
- use crmeb\basic\BaseModel;
- use Exception;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\facade\Log;
- use think\model\relation\HasOne;
- /**
- * TODO 文章Model
- * Class Article
- * @package app\models\article
- */
- class LalaPinkJoin extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'lala_pink_join';
- use ModelTrait;
- /**
- * @return HasOne
- */
- public function activity(): HasOne
- {
- return $this->hasOne(LalaPink::class, 'id', 'lala_id')->field('id,name,background_image,join_number,bingo_number,sp_exchange_bingo_time');
- }
- /**
- * @return HasOne
- */
- public function user(): HasOne
- {
- return $this->hasOne(User::class, 'uid', 'uid')->field('uid,nickname,avatar,account,real_name');
- }
- /**
- * @param $uid
- * @param $id
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function join($uid, $id, $cost_type = ''): bool
- {
- $open = sys_config('lala_pink_open');
- $close = sys_config('lala_pink_close');
- if (time() < strtotime(date('Y-m-d') . $open) || time() >= strtotime(date('Y-m-d') . $close)) {
- return self::setErrorInfo('当前时间不在拼购时间段内');
- }
- $open_times = open_times();
- //上次开奖
- $last_log = LalaPinkLog::order('add_time', 'desc')->find();
- //本次开奖
- $this_time = [];
- if (!$last_log)
- foreach ($open_times as $v) {
- if ($v['value'] < time()) {
- $this_time = $v;
- }
- }
- else {
- foreach ($open_times as $v) {
- if ($v['value'] > $last_log['add_time'] && $v['value'] < time()) {
- $this_time = $v;
- break;
- }
- }
- }
- if ($this_time) {
- return self::setErrorInfo('预约已结束,等待上一场开奖');
- }
- //下次开奖
- $next_time = [];
- $next_group = 0;
- foreach ($open_times as $k => $v) {
- if ($v['value'] > time() && $v['value'] > $last_log['add_time']) {
- $next_time = $v;
- $next_group = $k + 1;
- break;
- }
- }
- if (time() + sys_config('lala_pink_close_join_before_open') * 60 > $next_time['value']) {
- return self::setErrorInfo('开奖前' . sys_config('lala_pink_close_join_before_open') . '分钟关闭参与通道!');
- }
- $activity = LalaPink::validWhere()->where('id', $id)->find();
- if (!$activity) return self::setErrorInfo('未找到有效活动');
- if ($cost_type == '') $cost_type = $activity['cost_money_type'];
- if ($cost_type != $activity['cost_money_type'] && $cost_type != $activity['cost_2_money_type']) return self::setErrorInfo('不支持该币种支付');
- if ($cost_type == $activity['cost_2_money_type'] && $cost_type != $activity['cost_money_type']) {
- $activity['cost_money_type'] = $activity['cost_2_money_type'];
- $activity['cost'] = $activity['cost_2'];
- }
- if ($activity['ticket_money_type'] != 'LALA') {
- $money_types = sys_data('money_type');
- $origin_ratio = 0;
- $target_ratio = get_lala_ratio();
- foreach ($money_types as $v) {
- if ($v['code'] == $activity['ticket_money_type']) {
- $origin_ratio = $v['usdt_price'];
- if ($origin_ratio == 0) {
- $origin_ratio = get_huobi_price($v['code']);
- }
- }
- }
- $ratio = bcdiv($origin_ratio, $target_ratio, 14);
- $activity['ticket'] = bcmul($activity['ticket'], $ratio, 8);
- $activity['ticket_money_type'] = 'LALA';
- }
- $user = User::getUserInfo($uid);
- if (!$user) return self::setErrorInfo('用户异常');
- if (self::where('paid', 1)->where('status', 0)->where('lala_id', $id)->where('uid', $uid)->find()) return self::setErrorInfo('已有参与中的拼购,请等待下一场!');
- if (self::where('paid', 1)->where('uid', $uid)->where('lala_id', $id)->whereTime('pay_time', 'today')->count() >= $activity['join_limit']) return self::setErrorInfo('今日参与次数已达上限,明天再来吧!');
- $user_cost_money = UserMoney::initialUserMoney($uid, $activity['cost_money_type']);
- $user_ticket_money = UserMoney::initialUserMoney($uid, $activity['ticket_money_type']);
- if ($user_cost_money['money'] < $activity['cost']) return self::setErrorInfo('参与拼购的' . $activity['cost_money_type'] . '不足' . $activity['cost']);
- if ($user_ticket_money['money'] < $activity['ticket']) return self::setErrorInfo('参与拼购的门票' . $activity['ticket_money_type'] . '不足' . $activity['ticket']);
- BaseModel::beginTrans();
- try {
- $bingo_count = LalaPinkJoin::where('uid', $uid)->where('lala_id', $id)->where('status', 1)->where('paid', 1)->count();
- $fail_count = LalaPinkJoin::where('uid', $uid)->where('lala_id', $id)->where('status', 'in', [2, 3])->where('paid', 1)->count();
- // var_dump($item['activity']);
- $flag = true;
- if (($bingo_count + 1) % 11 == 0) {
- $times = floor($bingo_count / 11) + 1;
- if ($fail_count + $bingo_count < $times * $activity['bingo_limit']) {
- $flag = false;
- }
- }
- if (in_array($uid, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 18, 20, 21, 30])) {
- if (rand(1, 100) < 80) {
- $flag = false;
- }
- }
- //生成订单
- $res0 = self::create([
- 'uid' => $uid,
- 'lala_id' => $id,
- 'cost' => $activity['cost'],
- 'cost_money_type' => $activity['cost_money_type'],
- 'ticket' => $activity['ticket'],
- 'ticket_money_type' => $activity['ticket_money_type'],
- 'bingo' => $activity['bingo'],
- 'bingo_money_type' => $activity['bingo_money_type'],
- 'join_award' => bcmul(bcdiv($activity['join_award_ratio'], 100, 8), $activity['cost'], 8),
- 'system_money' => bcmul(bcdiv($activity['system_money_ratio'], 100, 8), $activity['cost'], 8),
- 'add_time' => time(),
- 'site_id' => $user['site_id'],
- 'order_id' => self::getNewOrderId($uid),
- 'group_num' => $next_group,
- 'set_status' => $flag ? 0 : 2
- ]);
- if (!$res0) {
- return BaseModel::setErrorInfo('生成订单失败', true);
- }
- //支付门票
- $res1 = UserMoney::expendMoney($uid, $activity['ticket_money_type'], $activity['ticket'], 'lala_ticket', '拼购门票', '参与拼购支付门票费用', $res0->id, false);
- if (!$res1) {
- return BaseModel::setErrorInfo(UserMoney::getErrorInfo(), true);
- }
- //支付
- $res2 = UserMoney::expendMoney($uid, $activity['cost_money_type'], $activity['cost'], 'lala_cost', '参与拼购', '参与拼购支付', $res0->id, false);
- if (!$res2) {
- return BaseModel::setErrorInfo(UserMoney::getErrorInfo(), true);
- }
- $res3 = self::where('id', $res0->id)->update(['paid' => 1, 'pay_time' => time()]);
- if (!$res3) {
- return BaseModel::setErrorInfo('支付失败,请重试', true);
- }
- // $res4 = true;
- $res4 = self::sendPointAward($res0);
- BaseModel::checkTrans($res4);
- if ($res4)
- return true;
- else
- return self::setErrorInfo(self::getErrorInfo());
- } catch (Exception $e) {
- return BaseModel::setErrorInfo($e->getMessage(), true);
- }
- }
- /**
- * @param $order
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function sendJoinAward($order): bool
- {
- $lala = LalaPink::where('id', $order['lala_id'])->find();
- $order['cost_money_type'] = $lala['cost_money_type'];
- $res = UserMoney::incomeMoney($order['uid'], $order['cost_money_type'], $order['join_award'], 'join_award', '参与奖', '参与拼购获得参与奖', $order['id'], 0);
- return $res ?: self::setErrorInfo('发放参与奖失败');
- }
- /**
- * @param $order
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function addSystemAward($order): bool
- {
- $lala = LalaPink::where('id', $order['lala_id'])->find();
- $order['cost_money_type'] = $lala['cost_money_type'];
- $res = SystemMoney::incomeMoney($order['site_id'], $order['cost_money_type'], $order['system_money'], 'lala_join', '用户参与拼购', '用户参与拼购,奖池累加', $order['id']);
- return $res ?: self::setErrorInfo('奖池累加失败');
- }
- /**
- * @param $order
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function sendRecommendAward($order): bool
- {
- $lala = LalaPink::where('id', $order['lala_id'])->find();
- $order['cost_money_type'] = $lala['cost_money_type'];
- // return true;
- $userInfo = User::getUserInfo($order['uid']);
- if (!$userInfo || !$userInfo['spread_uid'] || $userInfo['spread_uid'] == $order['uid']) return true;
- if (!User::be(['uid' => $userInfo['spread_uid'], 'is_promoter' => 1])) return self::sendRecommendAwardTwo($order);
- if (!self::where('paid', 1)->whereTime('add_time', 'today')->where('uid', $userInfo['spread_uid'])->find()) return self::sendRecommendAwardTwo($order);
- $ratio = sys_config('store_brokerage_ratio');
- if ($ratio <= 0) {
- return true;
- }
- $brokerageRatio = bcdiv($ratio, 100, 4);
- $brokeragePrice = bcmul($order['cost'], $brokerageRatio, 8);
- if ($brokeragePrice <= 0) return true;
- $mark = '下级用户' . $userInfo['nickname'] . '(' . $order['uid'] . ')' . '参与拼购,获得分享奖励' . $brokeragePrice;
- $res = UserMoney::incomeMoney($userInfo['spread_uid'], $order['cost_money_type'], $brokeragePrice, 'lala_recommend_award', '分享奖励', $mark, $order['id'], 0);
- return $res && self::sendRecommendAwardTwo($order);
- }
- /**
- * @param $order
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function sendRecommendAwardTwo($order): bool
- {
- $lala = LalaPink::where('id', $order['lala_id'])->find();
- $order['cost_money_type'] = $lala['cost_money_type'];
- $userInfo = User::getUserInfo($order['uid']);
- $userInfoTwo = User::getUserInfo($userInfo['spread_uid']);
- if (!$userInfoTwo || !$userInfoTwo['spread_uid'] || $userInfoTwo['spread_uid'] == $order['uid']) return true;
- if (!User::be(['uid' => $userInfoTwo['spread_uid'], 'is_promoter' => 1])) return true;
- if (!self::where('paid', 1)->whereTime('add_time', 'today')->where('uid', $userInfoTwo['spread_uid'])->find()) return true;
- $ratio = sys_config('store_brokerage_two');
- if ($ratio <= 0) {
- return true;
- }
- $brokerageRatio = bcdiv($ratio, 100, 4);
- $brokeragePrice = bcmul($order['cost'], $brokerageRatio, 8);
- if ($brokeragePrice <= 0) return true;
- $mark = '下下级用户' . $userInfo['nickname'] . '(' . $order['uid'] . ')' . '参与拼购,获得分享奖励' . $brokeragePrice;
- return UserMoney::incomeMoney($userInfoTwo['spread_uid'], $order['cost_money_type'], $brokeragePrice, 'lala_recommend_award', '分享奖励', $mark, $order['id'], 0);
- }
- /**
- * @param $order
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function sendGroupAward($order): bool
- {
- $lala = LalaPink::where('id', $order['lala_id'])->find();
- $order['cost_money_type'] = $lala['cost_money_type'];
- $userInfo = User::getUserInfo($order['uid']);
- $spread = User::getUserInfo($userInfo['spread_uid']);
- $send = 0;
- $last_send = 0;
- $last_level = 0;
- $same = 0;
- $res = true;
- while ($spread) {
- if (!self::where('paid', 1)->whereTime('add_time', 'today')->where('uid', $spread['uid'])->find()) {
- $spread = User::getUserInfo($spread['spread_uid']);
- continue;
- }
- $level = UserLevel::getUserLevelInfo(UserLevel::getUserLevel($spread['uid']));
- if ($level['group_award_ratio'] <= 0) {
- $spread = User::getUserInfo($spread['spread_uid']);
- continue;
- }
- $brokerageRatio = bcdiv($level['group_award_ratio'], 100, 4);
- $brokeragePrice = bcmul($order['cost'], $brokerageRatio, 8);
- if ($brokeragePrice < $send) {
- $spread = User::getUserInfo($spread['spread_uid']);
- continue;
- }
- if ($brokeragePrice == $send) {
- if ($level['level_id'] == $last_level && $same == 0) {
- $brokeragePrice = bcmul($last_send, bcdiv($level['same_level_award_ratio'], 100, 4), 8);
- if ($brokeragePrice <= 0) {
- $spread = User::getUserInfo($spread['spread_uid']);
- continue;
- }
- $mark = '用户' . $userInfo['nickname'] . '(' . $order['uid'] . ')' . '参与拼购,获得团队平级奖励' . $brokeragePrice;
- $res = $res && UserMoney::incomeMoney($spread['uid'], $order['cost_money_type'], $brokeragePrice, 'lala_same_level_award', '平级奖励', $mark, $order['id'], 0);
- $same++;
- }
- $spread = User::getUserInfo($spread['spread_uid']);
- continue;
- }
- $realBrokeragePrice = bcsub($brokeragePrice, $send, 8);
- $mark = '用户' . $userInfo['nickname'] . '(' . $order['uid'] . ')' . '参与拼购,获得团队极差奖励' . $realBrokeragePrice;
- $res = $res && UserMoney::incomeMoney($spread['uid'], $order['cost_money_type'], $realBrokeragePrice, 'lala_group_award', '团队奖励', $mark, $order['id'], 0);
- $same = 0;
- $send = $brokeragePrice;
- $last_level = $level['level_id'];
- $last_send = $realBrokeragePrice;
- $spread = User::getUserInfo($spread['spread_uid']);
- }
- return $res;
- }
- /**
- * @param $order
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function sendPointAward($order): bool
- {
- return true;
- $userInfo = User::getUserInfo($order['uid']);
- // $spread = User::getUserInfo($userInfo['spread_uid']);
- $res = true;
- $points = UserPointPlan::sum('buy_num');
- if ($points <= 0) return true;
- $plan = PointPlan::get(1);
- if (!$plan) return true;
- $list = UserPointPlan::group('uid')->field('SUM(buy_num) as buys,uid')->select();
- // = UserPointPlan::select();
- $brokerageRatio = bcdiv($plan['ticket_ratio'], 100, 4);
- $brokeragePrice = bcmul($order['ticket'], $brokerageRatio, 8);
- $every_price = bcdiv($brokeragePrice, $points, 8);
- foreach ($list as $v) {
- $brokeragePrice = bcmul($v['buys'], $every_price, 8);
- if ($brokeragePrice > 0) {
- $mark = '用户' . $userInfo['nickname'] . '(' . $order['uid'] . ')' . '参与拼购,获得节点门票分红' . $brokeragePrice . $order['ticket_money_type'];
- $res = $res && UserMoney::incomeMoney($v['uid'], $order['ticket_money_type'], $brokeragePrice, 'lala_point_award', '节点分红', $mark, $order['id']);
- // break;
- }
- }
- return $res;
- }
- /**
- * @param $where
- * @param $page
- * @param $limit
- * @return array
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function getList($where, $page, $limit): array
- {
- $model = self::with(['user', 'activity'])->where('paid', 1)->order('add_time desc,id desc');
- if (isset($where['id']) && $where['id'] != '') {
- $model = $model->where('lala_id', $where['id']);
- }
- if (isset($where['uid']) && $where['uid'] != '') {
- $model = $model->where('uid', $where['uid']);
- }
- $list = $model->page((int)$page, (int)$limit)->select()->each(function ($item) {
- $item['cost_money_type'] = get_money_name($item['cost_money_type']);
- if ($item['status'] > 0) {
- $item['bingos'] = self::where('table_id', $item['table_id'])->where('group_id', $item['group_id'])->where('status', 1)->count();
- $item['fails'] = self::where('table_id', $item['table_id'])->where('group_id', $item['group_id'])->where('status', 'in', [2, 3])->count();
- $item['joins'] = self::with('user')->where('table_id', $item['table_id'])->where('group_id', $item['group_id'])->select();
- }
- });
- $count = $model->count();
- $yesterday = [];
- $month = [];
- $history = [];
- if (isset($where['uid']) && $where['uid'] != '') {
- $yesterday = [
- 'join' => self::where('uid', $where['uid'])->whereTime('add_time', 'yesterday')->where('paid', 1)->count(),
- 'bingo' => self::where('uid', $where['uid'])->whereTime('add_time', 'yesterday')->where('paid', 1)->where('status', 1)->count(),
- ];
- $month = [
- 'join' => self::where('uid', $where['uid'])->whereTime('add_time', 'month')->where('paid', 1)->count(),
- 'bingo' => self::where('uid', $where['uid'])->whereTime('add_time', 'month')->where('paid', 1)->where('status', 1)->count(),
- ];
- $history = [
- 'join' => self::where('uid', $where['uid'])->where('paid', 1)->count(),
- 'bingo' => self::where('uid', $where['uid'])->where('paid', 1)->where('status', 1)->count(),
- ];
- }
- return compact('list', 'count', 'yesterday', 'month', 'history');
- }
- /**
- * @param $where
- * @return array
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function getDetail($where): array
- {
- $model = self::with(['user', 'activity']);
- $model = $model->where('id', $where['id']);
- if (isset($where['uid']) && $where['uid'] != '') {
- $model = $model->where('uid', $where['uid']);
- }
- $item = $model->find()->toArray();
- if ($item['status'] > 0) {
- $item['bingos'] = self::where('table_id', $item['table_id'])->where('group_id', $item['group_id'])->where('status', 1)->count();
- $item['fails'] = self::where('table_id', $item['table_id'])->where('group_id', $item['group_id'])->where('status', 'in', [2, 3])->count();
- $item['joins'] = self::with('user')->where('table_id', $item['table_id'])->where('group_id', $item['group_id'])->select();
- }
- $item['cost_money_type'] = get_money_name($item['cost_money_type']);
- return $item;
- }
- /**
- * @param $where
- * @param $page
- * @param $limit
- * @return array
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function getListAdmin($where, $page, $limit): array
- {
- $model = self::with(['user', 'activity'])->where('paid', 1)->order('status asc,pay_time desc,id desc');
- if (isset($where['id']) && $where['id'] != '') {
- $model = $model->where('lala_id', $where['id']);
- }
- if (isset($where['uid']) && $where['uid'] != '') {
- $model = $model->where('uid', $where['uid']);
- }
- $count = $model->count();
- $data = $model->page((int)$page, (int)$limit)->select()->each(function ($item) {
- if ($item['status'] == 0) {
- $item['bingo_count'] = LalaPinkJoin::where('uid', $item['uid'])->where('lala_id', $item['lala_id'])->where('status', 1)->where('paid', 1)->count();
- // var_dump($item['activity']);
- if (($item['bingo_count'] + 1) % ($item['activity']['sp_exchange_bingo_time'] > 0 ? $item['activity']['sp_exchange_bingo_time'] : 1) == 0) {
- $item['bingo_count'] = "<font style='color: red'>" . $item['bingo_count'] . "</font>";
- }
- } else
- $item['bingo_count'] = '--';
- $item['_status'] = $item['status'] == 1 ? "拼中" : ($item['status'] == 2 ? "未拼中" : ($item['status'] == 3 ? "未成团" : "等待开团"));
- $item['_set_status'] = $item['set_status'] == 1 ? "必拼中" : ($item['set_status'] == 2 ? "必不中" : "未设置");
- $item['_open_time'] = time_format($item['open_time']);
- });
- return compact('data', 'count');
- }
- /**
- * @param $uid
- * @return string
- */
- public static function getNewOrderId($uid): string
- {
- do {
- $str = 'LA' . str_pad($uid, 8, 0, STR_PAD_LEFT) . time() . rand(1000, 9999);
- } while (self::be(['order_id' => $str]));
- return $str;
- }
- /**
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function openAward(): bool
- {
- $open_times = open_times();
- //上次开奖
- $last_log = LalaPinkLog::order('add_time', 'desc')->find();
- $this_time = [];
- $this_group = 0;
- if (!$last_log)
- foreach ($open_times as $k => $v) {
- if ($v['value'] < time()) {
- $this_time = $v;
- $this_group = $k + 1;
- }
- }
- else {
- foreach ($open_times as $k => $v) {
- if ($v['value'] > $last_log['add_time'] && $v['value'] < time()) {
- $this_time = $v;
- $this_group = $k + 1;
- break;
- }
- }
- }
- if (!$this_time) {
- return true;
- }
- $auto_success = sys_config('auto_success');
- // $auto_success = 1;
- $dec = 1;
- LalaPinkLog::create(['add_time' => time(), 'time' => $this_time['name']]);
- $activity = LalaPink::validWhere()->column('id');
- $sites = (new \app\models\system\Site)->where('status', 1)->column('id');
- foreach ($activity as $v) {
- foreach ($sites as $vv) {
- $res1 = true;
- $res2 = true;
- $res3 = true;
- BaseModel::beginTrans();
- try {
- $activity_info = LalaPink::get($v);
- $round = 1;
- $group_id = self::getNewGroupId($vv, $v);
- $list_success = self::order('pay_time asc,id asc')->where('status', 0)->where('paid', 1)->where('lala_id', $v)->where('site_id', $vv)->where('set_status', 1)->column('id', 'id');
- $list_fail = self::order('pay_time asc,id asc')->where('status', 0)->where('paid', 1)->where('lala_id', $v)->where('site_id', $vv)->where('set_status', 2)->column('id', 'id');
- $list = self::order('pay_time asc,id asc')->where('status', 0)->where('paid', 1)->where('lala_id', $v)->where('site_id', $vv)->where('set_status', 0)->column('id', 'id');
- // var_dump($list);
- // var_dump($list_fail);
- // var_dump($list_success);
- // exit();
- while ($list_success || $list_fail || $list) {
- //必中用户
- $bingo = [];
- foreach ($list_success as $k => $vvv) {
- if (count($bingo) >= $activity_info['bingo_number']) break;
- $bingo[] = $vvv;
- unset($list_success[$k]);
- }
- // var_dump($bingo);
- if (count($list) < $activity_info['bingo_number'] - count($bingo)) {
- //中奖人数不足,不做操作等待下一轮
- if (!$auto_success)
- break;
- else {
- $count = $activity_info['bingo_number'] - count($bingo) - count($list);
- for ($i = 0; $i < $count; $i++) {
- $list[-($dec + $i)] = -($dec + $i);
- }
- $dec += $i;
- }
- }
- // var_dump($list);
- // var_dump(count($list_fail) + count($list));
- // var_dump($activity_info['join_number'] - count($bingo));
- // var_dump($dec);
- if (count($list_fail) + count($list) < $activity_info['join_number'] - count($bingo)) {
- //人数不足,不做操作等待下一轮
- if (!$auto_success)
- break;
- else {
- $count = $activity_info['join_number'] - count($bingo) - count($list_fail) - count($list);
- for ($i = 0; $i < $count; $i++) {
- $list[-($dec + $i)] = -($dec + $i);
- }
- $dec += $i;
- }
- }
- if (count($bingo) < $activity_info['bingo_number']) {
- $count = $activity_info['bingo_number'] - count($bingo);
- $random = array_rand($list, $count);
- if (is_array($random))
- foreach ($random as $vvv) {
- $bingo[] = $list[$vvv];
- unset($list[$vvv]);
- }
- else {
- $bingo[] = $list[$random];
- unset($list[$random]);
- }
- }
- // var_dump($bingo);
- // var_dump($list);
- // var_dump($list_fail);
- // var_dump($list_success);
- //开奖
- $fail = [];
- foreach ($list_fail as $k => $vvv) {
- if (count($fail) >= $activity_info['join_number'] - count($bingo)) break;
- $fail[] = $vvv;
- unset($list_fail[$k]);
- }
- if (count($list) < $activity_info['join_number'] - count($bingo) - count($fail)) {
- //不中人数不足,不做操作等待下一轮
- break;
- }
- if (count($fail) < $activity_info['join_number'] - count($bingo)) {
- $count = $activity_info['join_number'] - count($bingo) - count($fail);
- $random = array_rand($list, $count);
- if (is_array($random))
- foreach ($random as $vvv) {
- $fail[] = $list[$vvv];
- unset($list[$vvv]);
- }
- else {
- $fail[] = $list[$random];
- unset($list[$random]);
- }
- }
- //发奖逻辑
- foreach ($bingo as $vvv) {
- $info = self::get($vvv);
- if (!$info) {
- $res1 = $res1 && self::create([
- 'uid' => 0,
- 'lala_id' => $v,
- 'cost' => $activity_info['cost'],
- 'cost_money_type' => $activity_info['cost_money_type'],
- 'ticket' => $activity_info['ticket'],
- 'ticket_money_type' => $activity_info['ticket_money_type'],
- 'bingo' => $activity_info['bingo'],
- 'bingo_money_type' => $activity_info['bingo_money_type'],
- 'join_award' => bcmul(bcdiv($activity_info['join_award_ratio'], 100, 8), $activity_info['cost'], 8),
- 'system_money' => bcmul(bcdiv($activity_info['system_money_ratio'], 100, 8), $activity_info['cost'], 8),
- 'add_time' => time(),
- 'pay_time' => time(),
- 'paid' => 1,
- 'site_id' => $vv,
- 'order_id' => self::getNewOrderId(0),
- 'group_num' => $this_group,
- 'status' => 1,
- 'open_time' => time(),
- 'group_id' => $group_id,
- 'table_id' => $round
- ]);
- } else {
- //设置状态
- $res1 = $res1 && self::where('id', $vvv)->update(['status' => 1, 'open_time' => time(), 'group_id' => $group_id, 'table_id' => $round]);
- //发奖
- $res1 = $res1 && UserMoney::incomeMoney($info['uid'], $info['bingo_money_type'], $info['bingo'], 'lala_bingo', '拼购中奖', '参与' . $activity_info['name'] . '第' . $group_id . '期第' . $round . '桌拼购中奖', $vvv, 0);
- $res1 = $res1 && self::addSystemAward($info);
- $res1 = $res1 && Message::addMessage($vvv, '您参与的' . $activity_info['name'] . '第' . $group_id . '期第' . $round . '桌拼购已拼中', 202);
- //$res1 = $res1 && self::sendJoinAward($info) && self::addSystemAward($info) && self::sendRecommendAward($info) && self::sendGroupAward($info);
- $uper = User::getUserInfo($info['uid']);
- while ($uper) {
- UserLevel::setLevelComplete($uper['uid']);
- $uper = User::getUserInfo($uper['spread_uid']);
- }
- }
- }
- foreach ($fail as $vvv) {
- $info = self::get($vvv);
- //设置状态
- if (!$info) {
- $res1 = $res1 && self::create([
- 'uid' => 0,
- 'lala_id' => $v,
- 'cost' => $activity_info['cost'],
- 'cost_money_type' => $activity_info['cost_money_type'],
- 'ticket' => $activity_info['ticket'],
- 'ticket_money_type' => $activity_info['ticket_money_type'],
- 'bingo' => $activity_info['bingo'],
- 'bingo_money_type' => $activity_info['bingo_money_type'],
- 'join_award' => bcmul(bcdiv($activity_info['join_award_ratio'], 100, 8), $activity_info['cost'], 8),
- 'system_money' => bcmul(bcdiv($activity_info['system_money_ratio'], 100, 8), $activity_info['cost'], 8),
- 'add_time' => time(),
- 'pay_time' => time(),
- 'paid' => 1,
- 'site_id' => $vv,
- 'order_id' => self::getNewOrderId(0),
- 'group_num' => $this_group,
- 'status' => 2,
- 'open_time' => time(),
- 'group_id' => $group_id,
- 'table_id' => $round
- ]);
- } else {
- $res2 = $res2 && self::where('id', $vvv)->update(['status' => 2, 'open_time' => time(), 'group_id' => $group_id, 'table_id' => $round]);
- //发奖
- $res2 = $res2 && UserMoney::incomeMoney($info['uid'], $info['cost_money_type'], $info['cost'], 'lala_bingo_fail', '拼购未中奖退回', '参与' . $activity_info['name'] . '第' . $group_id . '期第' . $round . '桌拼购未中奖', $vvv);
- $res2 = $res2 && self::sendJoinAward($info) && self::addSystemAward($info) && self::sendRecommendAward($info) && self::sendGroupAward($info);
- $res2 = $res2 && Message::addMessage($vvv, '您参与的' . $activity_info['name'] . '第' . $group_id . '期第' . $round . '桌拼购未拼中', 202);
- $uper = User::getUserInfo($info['uid']);
- while ($uper) {
- UserLevel::setLevelComplete($uper['uid']);
- $uper = User::getUserInfo($uper['spread_uid']);
- }
- }
- }
- $round++;
- }
- //剩余list用户设置为超时失败
- foreach ($list as $vvv) {
- $info = self::get($vvv);
- $res3 = $res3 && self::where('id', $vvv)->update(['status' => 3, 'open_time' => time(), 'group_id' => $group_id, 'table_id' => $round]);
- //发奖
- $res3 = $res3 && UserMoney::incomeMoney($info['uid'], $info['cost_money_type'], $info['cost'], 'lala_fail', '拼购人数不足退回', '参与' . $activity_info['name'] . '第' . $group_id . '期第' . $round . '桌拼购人数不足', $vvv, 0);
- $res3 = $res3 && Message::addMessage($vvv, '您参与的' . $activity_info['name'] . '第' . $group_id . '期第' . $round . '桌拼购人数不足未成团', 202);
- }
- //剩余list_fail用户设置为超时失败
- foreach ($list_fail as $vvv) {
- $info = self::get($vvv);
- $res3 = $res3 && self::where('id', $vvv)->update(['status' => 3, 'open_time' => time(), 'group_id' => $group_id, 'table_id' => $round]);
- //发奖
- $res3 = $res3 && UserMoney::incomeMoney($info['uid'], $info['cost_money_type'], $info['cost'], 'lala_fail', '拼购人数不足退回', '参与' . $activity_info['name'] . '第' . $group_id . '期第' . $round . '桌拼购人数不足', $vvv, 0);
- $res3 = $res3 && Message::addMessage($vvv, '您参与的' . $activity_info['name'] . '第' . $group_id . '期第' . $round . '桌拼购人数不足未成团', 202);
- }
- // BaseModel::rollbackTrans();
- BaseModel::checkTrans($res1 && $res2 && $res3);
- if (!($res1 && $res2 && $res3)) {
- return BaseModel::setErrorInfo($activity_info['name'] . '开奖失败');
- }
- } catch (Exception $e) {
- // dump($e->getTrace());
- Log::error($e->getMessage());
- return BaseModel::setErrorInfo($e->getMessage(), true);
- }
- }
- }
- return true;
- }
- /**
- * @param $site_id
- * @param $lala_id
- * @return string
- */
- public static function getNewGroupId($site_id, $lala_id): string
- {
- do {
- $str = date('Ymd') . str_pad($site_id, 3, 0, STR_PAD_LEFT) . str_pad($lala_id, 3, 0, STR_PAD_LEFT) . rand(1000, 9999);
- } while (self::be(['group_id' => $str]));
- return $str;
- }
- /**
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function sendMoney(): bool
- {
- $list = UserBill::where(['pm' => 1, 'status' => 0])
- ->whereTime('add_time', 'yesterday')
- ->where('type', 'in', ['lala_bingo', 'lala_bingo_fail', 'lala_fail', 'join_award', 'lala_recommend_award', 'lala_group_award', 'lala_same_level_award'])
- ->select();
- if ($list) {
- self::beginTrans();
- try {
- foreach ($list as $v) {
- UserMoney::sendMoney($v['id']);
- }
- self::commitTrans();
- return true;
- } catch (Exception $e) {
- self::rollbackTrans();
- return false;
- }
- }
- return true;
- }
- public static function autoPink()
- {
- $open_times = open_times();
- //上次开奖
- $last_log = LalaPinkLog::order('add_time', 'desc')->find();
- //本次开奖
- $this_time = [];
- if (!$last_log)
- foreach ($open_times as $v) {
- if ($v['value'] < time()) {
- $this_time = $v;
- }
- }
- else {
- foreach ($open_times as $v) {
- if ($v['value'] > $last_log['add_time'] && $v['value'] < time()) {
- $this_time = $v;
- break;
- }
- }
- }
- if ($this_time) {
- return true;
- }
- //下次开奖
- $next_time = [];
- $next_group = 0;
- foreach ($open_times as $k => $v) {
- if ($v['value'] > time() && $v['value'] > $last_log['add_time']) {
- $next_time = $v;
- $next_group = $k + 1;
- break;
- }
- }
- if (time() + sys_config('lala_pink_close_join_before_open', 15, true) * 60 > $next_time['value']) {
- return true;
- }
- $open = sys_config('lala_pink_open');
- $close = sys_config('lala_pink_close');
- if (time() < strtotime(date('Y-m-d') . $open) || time() >= strtotime(date('Y-m-d') . $close)) {
- return true;
- }
- $lala = LalaPink::validWhere()->order('cost', 'desc')->select();
- foreach ($lala as $v) {
- $cost = $v['cost'];
- $cost_money_type = $v['cost_money_type'];
- $uids = UserAutoPink::where("FIND_IN_SET('" . $v['id'] . "',lala_id)")->group('uid')->field('COUNT(uid) as cot,uid')->where('status', 1)->select();
- foreach ($uids as $vv) {
- $v['cost'] = $v['cost_2'];
- $v['cost_money_type'] = $v['cost_2_money_type'];
- $old = UserAutoPink::where('uid', $vv['uid'])->where("FIND_IN_SET('" . $v['id'] . "',lala_id)")->where('status', 1)->select();
- if (!$old) continue;
- $valid_time = 0;
- $is_forever = 0;
- foreach ($old as $vvv) {
- if ($vvv['valid_time'] > $valid_time) $valid_time = $vvv['valid_time'];
- if ($vvv['is_forever']) $is_forever = $vvv['is_forever'];
- }
- if (!$is_forever && $valid_time < time()) continue;
- $user = User::get($vv['uid']);
- if (!$user || $user['status'] == 0) continue;
- if (UserLevel::getUserLevelInfo(UserLevel::getUserLevel($vv['uid']), 'grade') < 4 && $next_group <= 1) continue;
- if (self::where('paid', 1)->where('status', 0)->where('lala_id', $v['id'])->where('uid', $vv['uid'])->find()) continue;
- if (self::where('paid', 1)->where('uid', $vv['uid'])->where('lala_id', $v['id'])->whereTime('pay_time', 'today')->count() >= $v['join_limit']) continue;
- if ($v['ticket_money_type'] != 'LALA') {
- $money_types = sys_data('money_type');
- $origin_ratio = 0;
- $target_ratio = get_lala_ratio();
- foreach ($money_types as $vvv) {
- if ($vvv['code'] == $v['ticket_money_type']) {
- $origin_ratio = $vvv['usdt_price'];
- if ($origin_ratio == 0) {
- $origin_ratio = get_huobi_price($vvv['code']);
- }
- }
- }
- $ratio = bcdiv($origin_ratio, $target_ratio, 14);
- $v['ticket'] = bcmul($v['ticket'], $ratio, 8);
- $v['ticket_money_type'] = 'LALA';
- }
- $user_cost_money = UserMoney::initialUserMoney($vv['uid'], $v['cost_money_type']);
- $user_ticket_money = UserMoney::initialUserMoney($vv['uid'], $v['ticket_money_type']);
- if ($user_cost_money['money'] < $v['cost']) {
- $v['cost'] = $cost;
- $v['cost_money_type'] = $cost_money_type;
- $user_cost_money = UserMoney::initialUserMoney($vv['uid'], $v['cost_money_type']);
- if ($user_cost_money['money'] < $v['cost']) {
- continue;
- }
- }
- // var_dump($user_cost_money);
- // var_dump($user_ticket_money);
- //var_dump(3);
- if ($user_ticket_money['money'] < $v['ticket']) continue;
- //var_dump(4);
- $bingo_count = LalaPinkJoin::where('uid', $vv['uid'])->where('lala_id', $v['id'])->where('status', 1)->where('paid', 1)->count();
- $fail_count = LalaPinkJoin::where('uid', $vv['uid'])->where('lala_id', $v['id'])->where('status', 'in', [2, 3])->where('paid', 1)->count();
- // var_dump($item['activity']);
- $flag = true;
- if (($bingo_count + 1) % 11 == 0) {
- $times = floor($bingo_count / 11) + 1;
- if ($fail_count + $bingo_count < $times * $v['bingo_limit']) {
- $flag = false;
- }
- }
- if (in_array($vv['uid'], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 18, 20, 21, 30])) {
- if (rand(1, 100) < 80) {
- $flag = false;
- }
- }
- BaseModel::beginTrans();
- try {
- //生成订单
- //var_dump(111);
- $res0 = self::create([
- 'uid' => $vv['uid'],
- 'lala_id' => $v['id'],
- 'cost' => $v['cost'],
- 'cost_money_type' => $v['cost_money_type'],
- 'ticket' => $v['ticket'],
- 'ticket_money_type' => $v['ticket_money_type'],
- 'bingo' => $v['bingo'],
- 'bingo_money_type' => $v['bingo_money_type'],
- 'join_award' => bcmul(bcdiv($v['join_award_ratio'], 100, 8), $v['cost'], 8),
- 'system_money' => bcmul(bcdiv($v['system_money_ratio'], 100, 8), $v['cost'], 8),
- 'add_time' => time(),
- 'site_id' => $user['site_id'],
- 'order_id' => self::getNewOrderId($vv['uid']),
- 'group_num' => $next_group,
- 'set_status' => $flag ? 0 : 2,
- ]);
- if (!$res0) {
- Log::error('生成订单失败1');
- BaseModel::rollbackTrans();
- continue;
- }
- //支付门票
- $res1 = UserMoney::expendMoney($vv['uid'], $v['ticket_money_type'], $v['ticket'], 'lala_ticket', '拼购门票', '参与拼购支付门票费用', $res0->id, false);
- if (!$res1) {
- Log::error('生成订单失败2');
- BaseModel::rollbackTrans();
- continue;
- }
- //支付
- $res2 = UserMoney::expendMoney($vv['uid'], $v['cost_money_type'], $v['cost'], 'lala_cost', '参与拼购', '参与拼购支付', $res0->id, false);
- if (!$res2) {
- Log::error('生成订单失败3');
- BaseModel::rollbackTrans();
- continue;
- }
- $res3 = self::where('id', $res0->id)->update(['paid' => 1, 'pay_time' => time()]);
- if (!$res3) {
- Log::error('生成订单失败4');
- BaseModel::rollbackTrans();
- continue;
- }
- $res4 = self::sendPointAward($res0);
- if (!$res4) {
- Log::error('生成订单失败5');
- BaseModel::rollbackTrans();
- continue;
- }
- //BaseModel::rollbackTrans();
- BaseModel::commitTrans();
- } catch (Exception $e) {
- Log::error($e->getMessage());
- BaseModel::rollbackTrans();
- continue;
- }
- }
- }
- return true;
- }
- }
|