123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace app\models\manage;
- use app\models\user\UserMoney;
- use crmeb\basic\BaseModel;
- use crmeb\traits\ModelTrait;
- use think\db\Query;
- use think\Exception;
- class ManageMoneyProduct extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'manage_money_product';
- protected static $unit = [
- 1 => ['chs' => '天', 'time' => 'day'],
- 2 => ['chs' => '月', 'time' => 'month'],
- 3 => ['chs' => '年', 'time' => 'year'],
- ];
- use ModelTrait;
- /**
- * @return ManageMoneyProduct
- */
- public static function valid()
- {
- return self::where('is_del', 0);
- }
- /**
- * @param int $page
- * @param int $limit
- * @param array $where
- * @return array
- */
- public static function getList(int $page = 1, int $limit = 10, array $where = []): array
- {
- $model = self::valid();
- $count = $model->count();
- $data = $model->page($page, $limit)->select()->each(function ($item) {
- $item['_unit'] = self::$unit[$item['unit']]['chs'];
- })->toArray();
- return compact('count', 'data');
- }
- public static function buyPoroduct($id, $uid, $num)
- {
- $info = self::valid()->where('id', $id)->find();
- if (!$info) {
- return self::setErrorInfo('产品已下架或不存在');
- }
- if ($info['personal_limit'] <
- UserManageMoney::where('mid', $id)
- ->where('uid', $uid)
- ->where('real_finish_time', null)
- ->where('end_time', '<', time())
- ->sum('money') + $num
- ) {
- return self::setErrorInfo('产品购入上限不足' . $num);
- }
- $money_type = init_money_type();
- $user_money = UserMoney::initialUserMoney($uid, $info['money_type']);
- if ($user_money['money'] < $num) {
- return self::setErrorInfo('购入理财产品所需的' . $money_type[$info['money_type']] . '不足');
- }
- BaseModel::beginTrans();
- try {
- $res1 = UserMoney::expendMoney($uid, $info['money_type'], $num, 'buy_manage_product', '购入理财产品', '购入理财产品' . $info['name'] . '*' . $num . $money_type[$info['money_type']]);
- if ($res1) {
- $res = UserManageMoney::create([
- 'uid' => $uid,
- 'mid' => $id,
- 'ratio' => $info['ratio'],
- 'money_type' => $info['money_type'],
- 'money' => $num,
- 'time' => $info['time'],
- 'unit' => $info['unit'],
- // 'stand_money_type' => $info['stand_money_type'],
- 'add_time' => time(),
- 'end_time' => $info['time'] > 0 ? strtotime('+' . $info['time'] . ' ' . self::$unit[$info['unit']]['en'] . ($info['time'] > 1 ? 's' : '')) : 9999999999,
- 'send_start_time' => bcadd(time(), $info['stand_time'] * 3600 * 24),
- 'status' => $info['stand_time'] == 0 ? 1 : 0,
- 'next_send_time' => $info['time'] > 0 ? date('Y-m-d', strtotime('+' . $info['time'] . ' ' . self::$unit[$info['unit']]['en'] . ($info['time'] > 1 ? 's' : ''))) : null,
- ]);
- if (!$res) {
- return BaseModel::setErrorInfo('购入失败', true);
- }
- } else {
- return BaseModel::setErrorInfo('支付失败', true);
- }
- BaseModel::commitTrans();
- return true;
- } catch (Exception $e) {
- return BaseModel::setErrorInfo($e->getMessage(), true);
- }
- }
- }
|