房间内流水详情
This commit is contained in:
98
application/api/model/RoomDailyIncome.php
Normal file
98
application/api/model/RoomDailyIncome.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
class RoomDailyIncome extends Model
|
||||
{
|
||||
// 定义表名(如果表名和模型名一致可省略,这里显式定义更清晰)
|
||||
protected $name = 'room_daily_income';
|
||||
|
||||
// 开启自动时间戳(TP5.1默认用时间戳格式,对应create_time/update_time)
|
||||
protected $autoWriteTimestamp = true;
|
||||
|
||||
// 定义字段类型转换(确保金额为浮点型)
|
||||
protected $type = [
|
||||
'income' => 'float',
|
||||
'user_id' => 'integer',
|
||||
];
|
||||
|
||||
/**
|
||||
* 累加用户当日收益(核心方法)
|
||||
* @param int $userId 用户ID
|
||||
* @param float $amount 新增收益金额(正数)
|
||||
* @return bool
|
||||
*/
|
||||
public function addDailyIncome($roomId, $amount)
|
||||
{
|
||||
$today = date('Y-m-d'); // 今日日期
|
||||
|
||||
$today_res = $this->where([
|
||||
'room_id' => $roomId,
|
||||
'date' => $today
|
||||
])->find();
|
||||
if($today_res){
|
||||
$res = $this->where([
|
||||
'room_id' => $roomId,
|
||||
'date' => $today
|
||||
])->update([
|
||||
'income' => $amount,
|
||||
'update_time'=> time(),
|
||||
]);
|
||||
if($res){return true;}
|
||||
}else{
|
||||
$res = $this->insert([
|
||||
'room_id' => $roomId,
|
||||
'income' => $amount,
|
||||
'date' => $today,
|
||||
'create_time'=> time(),
|
||||
'update_time'=> time(),
|
||||
]);
|
||||
if($res){return true;}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询房间某一天收益
|
||||
* @param int $roomId 房间ID
|
||||
* @param string $today 日期(格式:YYYY-MM-DD)
|
||||
* @return float 收益(无收益则返回0)
|
||||
*/
|
||||
public function getTodayIncome($roomId,$today = 0)
|
||||
{
|
||||
if (!$today) {
|
||||
$today = date('Y-m-d');
|
||||
}
|
||||
$result = $this->where([
|
||||
'room_id' => $roomId,
|
||||
'date' => $today
|
||||
])->value('income');
|
||||
|
||||
return $result ? floatval($result) : 0.00;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询用户时间段内的收益总和
|
||||
* @param int $userId 用户ID
|
||||
* @param string $startDate 开始日期(格式:YYYY-MM-DD)
|
||||
* @param string $endDate 结束日期(格式:YYYY-MM-DD)
|
||||
* @return float 时间段总收益(无收益则返回0)
|
||||
*/
|
||||
public function getIncomeByPeriod($roomId, $startDate, $endDate)
|
||||
{
|
||||
// 校验日期格式(可选,增强健壮性)
|
||||
if (!strtotime($startDate) || !strtotime($endDate)) {
|
||||
return 0.00;
|
||||
}
|
||||
|
||||
$total = $this->where([
|
||||
'room_id' => $roomId,
|
||||
'date' => ['between', [$startDate, $endDate]]
|
||||
])->sum('income');
|
||||
|
||||
return $total ? floatval($total) : 0.00;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user