baji/app/common/model/ProfitExpenses.php
2025-03-06 13:24:40 +08:00

116 lines
3.4 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\common\model;
use app\common\model\Base;
use think\Model;
/**
* 账单支出
*/
class ProfitExpenses extends Base
{
// 设置当前模型对应的完整数据表名称
protected $table = 'profit_expenses';
/**
* 获取列表
*/
public static function getList($where = [], $field = '*', $order = '', $pageSize = "15")
{
$list = self::where($where)
->field($field)
->order($order)
->paginate(['list_rows' => $pageSize, 'query' => request()->param()]);
$page = $list->render();
$data['list'] = $list->toArray()['data'];
$data['count'] = $list->total();
$data['last_page'] = $list->toArray()['last_page'];
$data['page'] = $page;
return $data;
}
/**
* 插入方法
* @param string $date 日期 (格式: YYYY-MM-DD)
* @param int $expense_type 支出类型1 推广 ,2活动,3 发货
* @param float $amount 金额
* @param string $description 备注
* @return bool|int 插入成功返回主键ID失败返回false
*/
public static function insertDatas($date, $expense_type, $amount, $description = '')
{
$data = [
'profit_date' => $date,
'amount' => $amount,
'expense_type' => $expense_type,
'description' => $description,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
];
return self::insertGetId($data);
}
/**
* 查询当天总金额
* @param string $date 查询日期 (格式: YYYY-MM-DD)
* @param string|null $startDate 开始日期 (可选)
* @param string|null $endDate 结束日期 (可选)
* @return float 返回总金额
*/
public static function getTotalAmountByDate($date, $startDate = null, $endDate = null)
{
$query = self::where('profit_date', $date);
if ($startDate && $endDate) {
$query->whereBetween('profit_date', [$startDate, $endDate]);
}
return $query->sum('amount');
}
public static function getTotalAmountByDates($startDate, $endDate)
{
$query = self::whereBetween('profit_date', [$startDate, $endDate]);
return $query->sum('amount');
}
/**
* 查询每天的总金额,按日期分组排序
* @return array 返回每天的总金额,按日期分组
*/
public static function getDailyTotalAmount()
{
return self::field('profit_date, SUM(amount) as total_amount')
->group('profit_date')
->order('profit_date', 'asc')
->select()
->toArray();
}
/**
* 删除方法
* @param int $id 记录ID
* @return bool 删除成功返回true失败返回false
*/
public static function deleteData($id)
{
return self::where('id', $id)->delete();
}
/**
* 修改方法
* @param int $id 记录ID
* @param float $amount 修改后的金额
* @param string $description 修改后的备注
* @return bool 修改成功返回true失败返回false
*/
public static function updateDatas($id, $amount, $description = '')
{
$data = [
'amount' => $amount,
'description' => $description,
'updated_at' => date('Y-m-d H:i:s'),
];
return self::where('id', $id)->update($data);
}
}