parent
8fc6aad85c
commit
8a92c94dc4
@ -0,0 +1,81 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace app\command; |
||||
|
||||
use think\console\Command; |
||||
use think\console\Output; |
||||
use think\console\Input; |
||||
use app\common\model\Order as OrderModel; |
||||
use app\common\model\MonthlySales as MonthlySalesModel; |
||||
|
||||
class MonthlySales extends Command |
||||
{ |
||||
protected function configure() |
||||
{ |
||||
// 指令配置 |
||||
$this->setName('MonthlySales')->setDescription('预售到期自动上架'); |
||||
$this->addArgument("month"); |
||||
} |
||||
|
||||
protected function execute(Input $input, Output $output) |
||||
{ |
||||
$order = new OrderModel(); |
||||
$monthlySales = new MonthlySalesModel(); |
||||
$month = $input->getArgument("month"); |
||||
var_dump($month); |
||||
// 获取自定义月份参数 |
||||
if(empty($month)){ |
||||
$month = date('Y-m', time()); |
||||
} |
||||
// $month = '2024-08'; // 指定为2024-08 |
||||
// 计算指定月份的第一天和最后一天 |
||||
$start_time = strtotime("{$month}-01 00:00:00"); |
||||
$end_time = strtotime(date("{$month}-t 23:59:59")); |
||||
// 获取指定月份,格式为 YYYY-MM |
||||
$last_month = date('Y-m', strtotime($month)); |
||||
|
||||
|
||||
// 查询每个用户的上个月销售额 |
||||
$monthConsumption = $order |
||||
->where('pay_status', 20) |
||||
->where('pay_time', '>=', $start_time) |
||||
->where('pay_time', '<=', $end_time) |
||||
->group('user_id') |
||||
->field("user_id, SUM(pay_price) as total_money, '{$last_month}' as month") |
||||
->select(); |
||||
if(!$monthConsumption){ |
||||
var_dump ('暂无数据'); |
||||
return; |
||||
} |
||||
|
||||
foreach ($monthConsumption as $item) { |
||||
$data = [ |
||||
'user_id' => $item['user_id'], |
||||
'total_money' => $item['total_money'], |
||||
'month' => $item['month'], |
||||
'create_time' => $item['pay_time'], |
||||
'update_time' => $item['receipt_time'], |
||||
]; |
||||
|
||||
// 查询是否存在该用户上个月的数据 |
||||
$existingRecord = $monthlySales |
||||
->where('month', $last_month) |
||||
->where('user_id', $item['user_id']) |
||||
->find(); |
||||
|
||||
if ($existingRecord) { |
||||
// 更新现有记录 |
||||
$updateData = [ |
||||
'total_money' => $data['total_money'], |
||||
'update_time' => $data['receipt_time'], |
||||
]; |
||||
$monthlySales->where('id', $existingRecord['id'])->update($updateData); |
||||
} else { |
||||
// 插入新记录 |
||||
$monthlySales->create($data); // 使用 create() 方法插入新记录 |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,61 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace app\common\model; |
||||
|
||||
use cores\BaseModel; |
||||
|
||||
class MonthlySales extends BaseModel |
||||
{ |
||||
// 定义表名 |
||||
protected $name = 'monthly_sales'; |
||||
|
||||
// 定义时间戳字段名 |
||||
protected $createTime = 'create_time'; |
||||
protected $updateTime = 'update_time'; |
||||
|
||||
public function getList(array $param = [],$userId): \think\Paginator |
||||
{ |
||||
// 检索查询条件 |
||||
$filter = $this->getFilter($param); |
||||
$pageSize = $param['pageSize'] ?? 15; |
||||
// 查询列表数据 |
||||
return MonthlySales::withoutGlobalScope()->where('user_id',$userId)->where($filter) |
||||
->order(['create_time' => 'desc']) |
||||
->paginate($pageSize); |
||||
} |
||||
|
||||
/** |
||||
* 设置查询条件 |
||||
* @param array $param |
||||
* @return array |
||||
*/ |
||||
private function getFilter(array $param): array |
||||
{ |
||||
// 设置默认的检索数据 |
||||
$params = $this->setQueryDefaultValue($param, [ |
||||
'userId' => 0, // 会员ID |
||||
'search' => '', // 搜索内容 |
||||
'betweenTime' => [], // 起止时间 |
||||
]); |
||||
// 检索查询条件 |
||||
$filter = []; |
||||
// 用户ID |
||||
$params['userId'] > 0 && $filter[] = ['user_id', '=', $params['userId']]; |
||||
// 搜索内容: 用户昵称 |
||||
!empty($params['search']) && $filter[] = ['total_money', 'like', "%{$params['search']}%"]; |
||||
// 起止时间 |
||||
if (!empty($params['betweenTime'])) { |
||||
if (isset($params['betweenTime']) && is_array($params['betweenTime'])) { |
||||
$params['betweenTime'] = implode(',', $params['betweenTime']); |
||||
} |
||||
$betweenTime = explode(',', $params['betweenTime']); |
||||
$times = between_time($betweenTime); |
||||
|
||||
$filter[] = ['month', '>=', date('Y-m', $times['start_time'])]; |
||||
$filter[] = ['month', '<', date('Y-m',$times['end_time'] + 86400)]; |
||||
} |
||||
return $filter; |
||||
} |
||||
} |
Loading…
Reference in new issue