123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- <?php
- namespace app\models\point_plan;
- use app\admin\model\system\SystemUserLevel;
- use app\models\mining\MiningMachine;
- use app\models\mining\UserMiningMachine;
- use app\models\system\SystemUserTask;
- use app\models\user\User;
- use app\models\user\UserLevel;
- use app\models\user\UserMoney;
- use app\models\user\UserTaskFinish;
- use crmeb\basic\BaseModel;
- use crmeb\traits\ModelTrait;
- use think\Collection;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\Exception;
- class PointPlan extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'point_plan';
- use ModelTrait;
- /**
- * @param $id
- * @param $uid
- * @param $num
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function joinPlan($id, $uid, $num): bool
- {
- $info = self::where('status', 1)->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');
- }
- }
|