where('id', $id)->find(); $money = UserMoney::initialUserMoney($uid, $info['buy_money_type']); $money_num = bcmul($num, $info['buy_price'], 8); if ($money['money'] < $money_num) return self::setErrorInfo('账户余额不足!'); BaseModel::beginTrans(); try { $res = UserMoney::expendMoney($uid, $info['buy_money_type'], $money_num, 'join_point_plan', '认购节点', '认购' . $info['plan_name'] . $num . '股'); if (!$res) { return self::setErrorInfo(UserMoney::getErrorInfo('支付失败'), true); } $res = UserPointPlan::create([ 'buy_num' => $num, 'buy_all_price' => $money_num, 'release_money' => $info['release_money'], 'release_money_type' => $info['release_money_type'], 'release_days' => $info['release_days'], 'buy_money_type' => $info['buy_money_type'], 'ticket_ratio' => $info['ticket_ratio'], 'service_ratio' => $info['service_ratio'], 'add_time' => time(), 'uid' => $uid, 'release_time_all' => $info['release_days'], 'plan_id' => $info['id'], ]); $res = $res && self::giveLevel($uid, $info) && self::giveMachine($uid, $info, $num) && self::decStock($info, $num); if ($res) { BaseModel::commitTrans(); return $res; } else { return self::setErrorInfo('认购失败,' . self::getErrorInfo(), true); } } catch (Exception $e) { return self::setErrorInfo($e->getMessage(), true); } } /** * @param $plan * @param $num * @return bool * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public static function decStock($plan, $num): bool { $update = [ 'stock' => $plan['stock'] - $num, ]; if ($plan['stock'] == $num) { $update['status'] = 2; } $res = self::where('id', $plan['id'])->update($update); if ($res) { if ($plan['stock'] == $num) { if ($next = self::where('status', 0)->order('add_time asc,id asc')->find()) { $res = $res && self::where('id', $next['id'])->update(['status' => 1, 'start_time' => time()]); } } } return $res; } /** * @param $uid * @param $plan * @return bool */ public static function giveLevel($uid, $plan): bool { if (!$uid || !$plan['give_level']) { return true; } //查询当前选择的会员等级 $systemLevel = SystemUserLevel::where(['is_show' => 1, 'is_del' => 0, 'id' => $plan['give_level']])->find(); if (!$systemLevel) return true; //检查是否拥有此会员等级 $level = UserLevel::where(['uid' => $uid, 'level_id' => $plan['give_level'], 'is_del' => 0])->field('valid_time,is_forever')->find(); if ($level) if (!(!$level['is_forever'] && time() < $level['valid_time'])) return true; //设置会员过期时间 $add_valid_time = (int)$systemLevel->valid_date * 86400; $res = UserLevel::create([ 'is_forever' => $systemLevel->is_forever, 'status' => 1, 'is_del' => 0, 'grade' => $systemLevel->grade, 'uid' => $uid, 'add_time' => time(), 'level_id' => $plan['give_level'], 'discount' => $systemLevel->discount, 'valid_time' => $systemLevel->is_forever ? $add_valid_time + time() : 0, 'mark' => '尊敬的用户【' . User::where('uid', $uid)->value('nickname') . '】在' . date('Y-m-d H:i:s', time()) . '购买节点股份赠送会员等级成为' . $systemLevel['name'] . '会员', ]); //提取等级任务并记录完成情况 $levelIds = [$plan['give_level']]; $lowGradeLevelIds = SystemUserLevel::where('grade', '<', $systemLevel->grade)->where(['is_show' => 1, 'is_del' => 0])->column('id', 'id'); if (count($lowGradeLevelIds)) $levelIds = array_merge($levelIds, $lowGradeLevelIds); $taskIds = SystemUserTask::where('level_id', 'in', $levelIds)->column('id', 'id'); $inserValue = []; foreach ($taskIds as $id) { $inserValue[] = ['uid' => $uid, 'task_id' => $id, 'status' => 1, 'add_time' => time()]; } $res = $res && User::where('uid', $uid)->update(['level' => $plan['give_level']]); if ($inserValue) $res && UserTaskFinish::insertAll($inserValue); if ($res) { return true; } else { return self::setErrorInfo('赠送等级失败'); } } /** * @param $uid * @param $plan * @return bool */ public static function giveMachine($uid, $plan, $num): bool { if (!$uid || !$plan['give_mining_machine'] || $plan['give_mining_machine_num'] <= 0) { return true; } $info = MiningMachine::valid()->where('id', $plan['give_mining_machine'])->find(); if (!$info) { return true; } if ($info['stock'] < $plan['give_mining_machine_num']) { return true; } $info['cost_money'] = bcmul($info['cost_money'], $plan['give_mining_machine_num']); $info['stand_money'] = bcmul($info['stand_money'], $plan['give_mining_machine_num']); $res = UserMiningMachine::create([ 'uid' => $uid, 'mid' => $plan['give_mining_machine'], 'day_get' => $info['day_get'], 'get_money_type' => $info['get_money_type'], 'cost_money' => 0, 'cost_money_type' => $info['cost_money_type'], 'mortgage_money' => $info['mortgage_money'], 'mortgage_money_type' => $info['mortgage_money_type'], 'redeem_money' => $info['redeem_money'], 'redeem_money_type' => $info['redeem_money_type'], 'stand_money' => 0, 'add_time' => time(), 'pay_time' => time(), 'paid' => 1, 'num' => $plan['give_mining_machine_num'] * $num, 'cost_times' => 0, 'lala_id' => 0, 'mining_end_time' => bcadd(bcadd(time(), bcmul($info['first_step_time'] + $info['third_step_time'] + $info['second_step_time'], 3600 * 24)), bcmul($info['stand_time'], 3600 * 24)), 'mining_start_time' => bcadd(time(), bcmul($info['stand_time'], 3600 * 24)), 'second_step_start_time' => bcadd(bcadd(time(), bcmul($info['first_step_time'], 3600 * 24)), bcmul($info['stand_time'], 3600 * 24)), 'third_step_start_time' => bcadd(bcadd(time(), bcmul($info['first_step_time'] + $info['second_step_time'], 3600 * 24)), bcmul($info['stand_time'], 3600 * 24)), ]); if ($res) return true; else return self::setErrorInfo('赠送矿机失败'); } /** * @param $where * @return array */ public static function getList($where) { $model = new self(); if (isset($where['title']) && $where['title'] != '') { $model->where('id|plan_name', 'like', "%{$where['title']}%"); } if (isset($where['status']) && $where['status'] != '') { $model->where('status', $where['status']); } $count = $model->count(); $data = $model->page((int)$where['page'], (int)$where['limit'])->select()->each(function ($item) { $item['join_number'] = UserPointPlan::where('plan_id', $item['id'])->sum('buy_num'); $item['_start_time'] = $item['start_time'] ? date('Y-m-d H:i:s', $item['start_time']) : '--'; $item['_status'] = $item['status'] == 0 ? '等待中' : ($item['status'] == 1 ? '认购中' : '已结束'); }); return compact('count', 'data'); } }