2026-01-08 11:59:42 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace app\api\model;
|
|
|
|
|
|
|
|
|
|
|
|
use think\Model;
|
|
|
|
|
|
|
|
|
|
|
|
class RoomDailyIncome extends Model
|
|
|
|
|
|
{
|
|
|
|
|
|
// 定义表名(如果表名和模型名一致可省略,这里显式定义更清晰)
|
|
|
|
|
|
protected $name = 'room_daily_income';
|
|
|
|
|
|
|
|
|
|
|
|
// 开启自动时间戳(TP5.1默认用时间戳格式,对应create_time/update_time)
|
2026-01-16 17:10:05 +08:00
|
|
|
|
// protected $autoWriteTimestamp = true;
|
|
|
|
|
|
|
2026-01-08 11:59:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 累加用户当日收益(核心方法)
|
|
|
|
|
|
* @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([
|
2026-01-09 11:28:20 +08:00
|
|
|
|
'income' => $today_res['income'] +$amount,
|
2026-01-08 11:59:42 +08:00
|
|
|
|
'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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|