123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?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 BorrowMoneyProduct extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'borrow_money_product';
- use ModelTrait;
- /**
- * @return BorrowMoneyProduct
- */
- 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();
- $list = $model->page($page, $limit)->select()->toArray();
- return compact('count', 'list');
- }
- public static function buyPoroduct($id, $uid, $num)
- {
- $info = self::valid()->where('id', $id)->find();
- if (!$info) {
- return self::setErrorInfo('产品已下架或不存在');
- }
- if ($info['personal_limit'] <
- UserBorrowMoney::where('mid', $id)
- ->where('uid', $uid)
- ->where('finish_time', null)
- ->sum('money') + $num
- ) {
- return self::setErrorInfo('借贷上限不足' . $num);
- }
- $money_type = init_money_type();
- BaseModel::beginTrans();
- try {
- $res1 = UserMoney::incomeMoney($uid, $info['money_type'], $num, 'borrow_money', '借贷', '借贷' . $info['name'] . '*' . $num . $money_type[$info['money_type']]);
- if ($res1) {
- $res = UserBorrowMoney::create([
- 'uid' => $uid,
- 'mid' => $id,
- 'ratio' => $info['ratio'],
- 'money_type' => $info['money_type'],
- 'money' => $num,
- 'add_time' => time(),
- '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('+1day')) : 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);
- }
- }
- }
|