121 lines
3.9 KiB
PHP
121 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace app\api\model;
|
|
|
|
use app\common\library\DatabaseRoute;
|
|
use app\common\model\BaseModel;
|
|
use app\common\model\SysUser;
|
|
use ba\Exception;
|
|
use ba\Random;
|
|
use think\facade\Db;
|
|
|
|
class UserMoney extends BaseModel
|
|
{
|
|
|
|
public static function selectUserMoney($user_id)
|
|
{
|
|
$where = $sale = ['user_id' => $user_id];
|
|
$money = DatabaseRoute::getDb('user_money', $user_id)->where($where)->find();
|
|
if(!$money) {
|
|
$database_name = DatabaseRoute::getConnection('user_money', $sale, true);
|
|
$money = [
|
|
'user_id' => $user_id,
|
|
'money' => 0.00,
|
|
'amount' => 0.00,
|
|
];
|
|
Db::connect($database_name)->name('user_money')->insert($money);
|
|
}
|
|
if($money['money'] == 0) {
|
|
$money['money'] = 0;
|
|
}
|
|
if($money['amount'] == 0) {
|
|
$money['amount'] = 0;
|
|
}
|
|
$money['amount'] = sprintf('%.4f', $money['amount']);
|
|
$money['money'] = sprintf('%.4f', $money['money']);
|
|
$money = apiconvertToCamelCase($money);
|
|
return returnSuccessData($money);
|
|
}
|
|
public static function selectUserMoneyfind($user_id)
|
|
{
|
|
$db = DatabaseRoute::getDb('user_money', $user_id);
|
|
$data = $db->where([ 'user_id' => $user_id])->find();
|
|
return $data;
|
|
}
|
|
|
|
|
|
public static function queryUserMoneyDetails($user_id, $get)
|
|
{
|
|
$user_where = $sale = ['user_id' => $user_id];
|
|
$where = [];
|
|
if(!empty($get['classify'])) {
|
|
$where['classify'] = $get['classify'];
|
|
}
|
|
if(!empty($get['type'])) {
|
|
$where['type'] = $get['type'];
|
|
}
|
|
if(!empty($get['moneyType'])) {
|
|
$where['money_type'] = $get['moneyType'];
|
|
}
|
|
if(!empty($get['classify'])) {
|
|
$where['classify'] = $get['classify'];
|
|
}
|
|
$money = DatabaseRoute::getDb('user_money_details', $user_id)->where($user_where)->where($where);
|
|
if(!empty($get['viewType']) && $get['viewType'] == 1) {
|
|
$money = $money->whereIn('classify', [1,6]);
|
|
}
|
|
$count = $money->count();
|
|
$money = $money->order('create_time','desc')->limit(page($get['page'], $get['limit']), $get['limit'])->select()->toArray();
|
|
foreach ($money as $k => &$v) {
|
|
$v['money'] = sprintf('%.2f', $v['money']);
|
|
}
|
|
return returnSuccessData([
|
|
'currPage' => $get['page'],
|
|
'pageSize' => $get['limit'],
|
|
'records' => apiconvertToCamelCase($money),
|
|
'totalCount' => $count,
|
|
'totalPage' => ceil($count / $get['limit']),
|
|
]);
|
|
}
|
|
|
|
public static function updateAmount($userId, $money, $isIncr=true)
|
|
{
|
|
$userMoney = self::selectUserMoneyfind($userId);
|
|
if (!$userMoney) {
|
|
DatabaseRoute::getDb('user_money', $userId, true)->insert([
|
|
'user_id' => $userId,
|
|
'money' => 0,
|
|
'amount' => 0
|
|
]);
|
|
}
|
|
$money = floatval($money);
|
|
$model = DatabaseRoute::getDb('user_money', $userId, true, true);
|
|
if ($isIncr) {
|
|
$model->inc('amount', $money);
|
|
}else{
|
|
$model->dec('amount', $money);
|
|
}
|
|
$model->update();
|
|
}
|
|
|
|
public static function updateMoney($userId, $money, $isIncr=true)
|
|
{
|
|
$userMoney = self::selectUserMoneyfind($userId);
|
|
if (!$userMoney) {
|
|
DatabaseRoute::getDb('user_money', $userId, true)->insert([
|
|
'user_id' => $userId,
|
|
'money' => 0,
|
|
'amount' => 0
|
|
]);
|
|
}
|
|
$money = floatval($money);
|
|
$model = DatabaseRoute::getDb('user_money', $userId, true, true);
|
|
if ($isIncr) {
|
|
$model->inc('money', $money);
|
|
}else{
|
|
$model->dec('money', $money);
|
|
}
|
|
$model->update();
|
|
}
|
|
|
|
} |