文件
mashangyoujiu/Application/Retail/Controller/RiderRewardController.class.php

435 行
15 KiB
PHP

<?php
namespace Retail\Controller;
use Think\Controller;
/**
* 骑手奖励活动管理控制器
*/
class RiderRewardController extends Controller {
/**
* 奖励活动管理首页
*/
public function index() {
$this->display();
}
/**
* 获取奖励活动列表
*/
public function getRewardList() {
$page = I('page', 1, 'intval');
$pageSize = I('pageSize', 20, 'intval');
$keyword = I('keyword', '', 'trim');
$status = I('status', '', 'trim');
$type = I('type', '', 'trim');
$start_time = I('start_time', '', 'trim');
$end_time = I('end_time', '', 'trim');
$where = array();
// 关键词搜索
if (!empty($keyword)) {
$where['activity_name'] = array('like', '%' . $keyword . '%');
}
// 状态筛选
if ($status !== '') {
$where['status'] = $status;
}
// 类型筛选
if ($type !== '') {
$where['reward_type'] = $type;
}
// 时间范围筛选
if (!empty($start_time)) {
$where['start_time'] = array('egt', $start_time . ' 00:00:00');
}
if (!empty($end_time)) {
$where['end_time'] = array('elt', $end_time . ' 23:59:59');
}
$model = M('rider_reward_activity');
$total = $model->where($where)->count();
$list = $model->where($where)
->order('create_time desc')
->page($page, $pageSize)
->select();
// 格式化数据
foreach ($list as &$item) {
$item['status_text'] = $this->getStatusText($item['status']);
$item['reward_type_text'] = $this->getRewardTypeText($item['reward_type']);
$item['create_time_format'] = date('Y-m-d H:i:s', $item['create_time']);
$item['start_time_format'] = $item['start_time'];
$item['end_time_format'] = $item['end_time'];
$item['reward_amount_format'] = number_format($item['reward_amount'], 2);
// 计算参与人数
$item['participant_count'] = M('rider_reward_record')
->where(array('activity_id' => $item['id']))
->count('DISTINCT rider_id');
}
$this->ajaxReturn(array(
'code' => 0,
'msg' => 'success',
'data' => array(
'list' => $list,
'total' => $total
)
));
}
/**
* 获取奖励活动详情
*/
public function getRewardDetail() {
$id = I('id', 0, 'intval');
if (empty($id)) {
$this->ajaxReturn(array('code' => 1, 'msg' => '参数错误'));
}
$detail = M('rider_reward_activity')->where(array('id' => $id))->find();
if (empty($detail)) {
$this->ajaxReturn(array('code' => 1, 'msg' => '活动不存在'));
}
// 格式化数据
$detail['status_text'] = $this->getStatusText($detail['status']);
$detail['reward_type_text'] = $this->getRewardTypeText($detail['reward_type']);
$detail['create_time_format'] = date('Y-m-d H:i:s', $detail['create_time']);
$detail['start_time_format'] = $detail['start_time'];
$detail['end_time_format'] = $detail['end_time'];
$detail['reward_amount_format'] = number_format($detail['reward_amount'], 2);
// 获取参与统计
$stats = $this->getActivityStats($id);
$detail['stats'] = $stats;
$this->ajaxReturn(array(
'code' => 0,
'msg' => 'success',
'data' => $detail
));
}
/**
* 添加/编辑奖励活动
*/
public function saveReward() {
$id = I('id', 0, 'intval');
$data = array(
'activity_name' => I('activity_name', '', 'trim'),
'reward_type' => I('reward_type', '', 'trim'),
'reward_amount' => I('reward_amount', 0, 'floatval'),
'condition_type' => I('condition_type', '', 'trim'),
'condition_value' => I('condition_value', '', 'trim'),
'start_time' => I('start_time', '', 'trim'),
'end_time' => I('end_time', '', 'trim'),
'description' => I('description', '', 'trim'),
'status' => I('status', 1, 'intval')
);
// 数据验证
if (empty($data['activity_name'])) {
$this->ajaxReturn(array('code' => 1, 'msg' => '活动名称不能为空'));
}
if (empty($data['reward_type'])) {
$this->ajaxReturn(array('code' => 1, 'msg' => '奖励类型不能为空'));
}
if ($data['reward_amount'] <= 0) {
$this->ajaxReturn(array('code' => 1, 'msg' => '奖励金额必须大于0'));
}
if (empty($data['start_time']) || empty($data['end_time'])) {
$this->ajaxReturn(array('code' => 1, 'msg' => '活动时间不能为空'));
}
$model = M('rider_reward_activity');
if ($id > 0) {
// 编辑
$data['update_time'] = time();
$result = $model->where(array('id' => $id))->save($data);
} else {
// 添加
$data['create_time'] = time();
$data['update_time'] = time();
$result = $model->add($data);
}
if ($result !== false) {
$this->ajaxReturn(array('code' => 0, 'msg' => '操作成功'));
} else {
$this->ajaxReturn(array('code' => 1, 'msg' => '操作失败'));
}
}
/**
* 删除奖励活动
*/
public function deleteReward() {
$id = I('id', 0, 'intval');
if (empty($id)) {
$this->ajaxReturn(array('code' => 1, 'msg' => '参数错误'));
}
// 检查是否有参与记录
$recordCount = M('rider_reward_record')->where(array('activity_id' => $id))->count();
if ($recordCount > 0) {
$this->ajaxReturn(array('code' => 1, 'msg' => '该活动已有参与记录,无法删除'));
}
$result = M('rider_reward_activity')->where(array('id' => $id))->delete();
if ($result) {
$this->ajaxReturn(array('code' => 0, 'msg' => '删除成功'));
} else {
$this->ajaxReturn(array('code' => 1, 'msg' => '删除失败'));
}
}
/**
* 启用/禁用活动
*/
public function toggleStatus() {
$id = I('id', 0, 'intval');
$status = I('status', 0, 'intval');
if (empty($id)) {
$this->ajaxReturn(array('code' => 1, 'msg' => '参数错误'));
}
$result = M('rider_reward_activity')->where(array('id' => $id))->save(array(
'status' => $status,
'update_time' => time()
));
if ($result !== false) {
$this->ajaxReturn(array('code' => 0, 'msg' => '操作成功'));
} else {
$this->ajaxReturn(array('code' => 1, 'msg' => '操作失败'));
}
}
/**
* 获取参与记录列表
*/
public function getParticipantList() {
$activity_id = I('activity_id', 0, 'intval');
$page = I('page', 1, 'intval');
$pageSize = I('pageSize', 20, 'intval');
$keyword = I('keyword', '', 'trim');
if (empty($activity_id)) {
$this->ajaxReturn(array('code' => 1, 'msg' => '参数错误'));
}
$where = array('r.activity_id' => $activity_id);
// 关键词搜索
if (!empty($keyword)) {
$where['u.nickname|u.phone'] = array('like', '%' . $keyword . '%');
}
$model = M('rider_reward_record');
$total = $model->alias('r')
->join('ls_users u ON r.rider_id = u.id')
->where($where)
->count();
$list = $model->alias('r')
->field('r.*, u.nickname, u.phone, u.avatar')
->join('ls_users u ON r.rider_id = u.id')
->where($where)
->order('r.create_time desc')
->page($page, $pageSize)
->select();
// 格式化数据
foreach ($list as &$item) {
$item['create_time_format'] = date('Y-m-d H:i:s', $item['create_time']);
$item['reward_amount_format'] = number_format($item['reward_amount'], 2);
}
$this->ajaxReturn(array(
'code' => 0,
'msg' => 'success',
'data' => array(
'list' => $list,
'total' => $total
)
));
}
/**
* 导出奖励活动数据
*/
public function exportReward() {
$keyword = I('keyword', '', 'trim');
$status = I('status', '', 'trim');
$type = I('type', '', 'trim');
$start_time = I('start_time', '', 'trim');
$end_time = I('end_time', '', 'trim');
$where = array();
if (!empty($keyword)) {
$where['activity_name'] = array('like', '%' . $keyword . '%');
}
if ($status !== '') {
$where['status'] = $status;
}
if ($type !== '') {
$where['reward_type'] = $type;
}
if (!empty($start_time)) {
$where['start_time'] = array('egt', $start_time . ' 00:00:00');
}
if (!empty($end_time)) {
$where['end_time'] = array('elt', $end_time . ' 23:59:59');
}
$list = M('rider_reward_activity')->where($where)->order('create_time desc')->select();
// 导出Excel
vendor('PHPExcel.PHPExcel');
$objPHPExcel = new \PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objActSheet = $objPHPExcel->getActiveSheet();
// 设置表头
$objActSheet->setCellValue('A1', '活动ID');
$objActSheet->setCellValue('B1', '活动名称');
$objActSheet->setCellValue('C1', '奖励类型');
$objActSheet->setCellValue('D1', '奖励金额');
$objActSheet->setCellValue('E1', '触发条件');
$objActSheet->setCellValue('F1', '开始时间');
$objActSheet->setCellValue('G1', '结束时间');
$objActSheet->setCellValue('H1', '状态');
$objActSheet->setCellValue('I1', '参与人数');
$objActSheet->setCellValue('J1', '创建时间');
// 填充数据
$row = 2;
foreach ($list as $item) {
$participant_count = M('rider_reward_record')
->where(array('activity_id' => $item['id']))
->count('DISTINCT rider_id');
$objActSheet->setCellValue('A' . $row, $item['id']);
$objActSheet->setCellValue('B' . $row, $item['activity_name']);
$objActSheet->setCellValue('C' . $row, $this->getRewardTypeText($item['reward_type']));
$objActSheet->setCellValue('D' . $row, $item['reward_amount']);
$objActSheet->setCellValue('E' . $row, $item['condition_type'] . ':' . $item['condition_value']);
$objActSheet->setCellValue('F' . $row, $item['start_time']);
$objActSheet->setCellValue('G' . $row, $item['end_time']);
$objActSheet->setCellValue('H' . $row, $this->getStatusText($item['status']));
$objActSheet->setCellValue('I' . $row, $participant_count);
$objActSheet->setCellValue('J' . $row, date('Y-m-d H:i:s', $item['create_time']));
$row++;
}
$filename = '骑手奖励活动_' . date('YmdHis') . '.xlsx';
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $filename . '"');
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
}
/**
* 获取奖励活动统计
*/
public function getRewardStats() {
$start_time = I('start_time', '', 'trim');
$end_time = I('end_time', '', 'trim');
$where = array();
if (!empty($start_time)) {
$where['start_time'] = array('egt', $start_time . ' 00:00:00');
}
if (!empty($end_time)) {
$where['end_time'] = array('elt', $end_time . ' 23:59:59');
}
$model = M('rider_reward_activity');
$stats = array(
'total_count' => $model->where($where)->count(),
'active_count' => $model->where(array_merge($where, array('status' => 1)))->count(),
'inactive_count' => $model->where(array_merge($where, array('status' => 0)))->count(),
'peak_hour_count' => $model->where(array_merge($where, array('reward_type' => 'peak_hour')))->count(),
'weather_count' => $model->where(array_merge($where, array('reward_type' => 'weather')))->count(),
'order_count' => $model->where(array_merge($where, array('reward_type' => 'order_count')))->count()
);
// 计算总奖励金额
$total_amount = M('rider_reward_record')
->where($where)
->sum('reward_amount');
$stats['total_amount'] = number_format($total_amount ?: 0, 2);
$this->ajaxReturn(array(
'code' => 0,
'msg' => 'success',
'data' => $stats
));
}
/**
* 获取活动统计数据
*/
private function getActivityStats($activity_id) {
$stats = array();
// 参与人数
$stats['participant_count'] = M('rider_reward_record')
->where(array('activity_id' => $activity_id))
->count('DISTINCT rider_id');
// 总奖励金额
$stats['total_reward'] = M('rider_reward_record')
->where(array('activity_id' => $activity_id))
->sum('reward_amount') ?: 0;
// 触发次数
$stats['trigger_count'] = M('rider_reward_record')
->where(array('activity_id' => $activity_id))
->count();
return $stats;
}
/**
* 获取状态文本
*/
private function getStatusText($status) {
$statusMap = array(
0 => '已禁用',
1 => '已启用'
);
return isset($statusMap[$status]) ? $statusMap[$status] : '未知';
}
/**
* 获取奖励类型文本
*/
private function getRewardTypeText($type) {
$typeMap = array(
'peak_hour' => '高峰时段奖励',
'weather' => '恶劣天气补贴',
'order_count' => '满单奖励',
'distance' => '长距离奖励',
'rating' => '好评奖励',
'punctuality' => '准时奖励',
'special' => '特殊活动奖励'
);
return isset($typeMap[$type]) ? $typeMap[$type] : '其他';
}
}
?>