assign('breadcrumb1','拉新抽奖');
$this->assign('breadcrumb2','抽奖记录');
}
/**
* 列表页
*
* */
public function index(Model $Model,LotteryRecruitment $LotteryRecruitment,Custom $Custom){
// 接受参数
$lotteryId = request('lottery_id',0);
$customCode = request('custom_code','');
$startTime = request('start_time','');
$endTime = request('end_time','');
$status = request('status');
// 查询条件
$map = [['lottery_recruitment_record.reward_id','>',0]];
// 编码转ID
$customUid = $customCode ? $Custom->codeToId($customCode) : 0;
if( $customUid ) $map[] = ['lottery_recruitment_record.custom_uid','=',$customUid];
if( $lotteryId ) $map[] = ['lottery_recruitment_record.lottery_id','=',$lotteryId];
if( $startTime ) $map[] = ['lottery_recruitment_record.insert_time','>=',strtotime($startTime)];
if( $endTime ) $map[] = ['lottery_recruitment_record.insert_time','<=',strtotime($endTime)];
if( !is_null($status) ) $map[] = ['lottery_recruitment_record.status','=',$status];
// 查询数据
$list = $Model->query()
->join('lottery_recruitment','lottery_recruitment.id','=','lottery_recruitment_record.lottery_id')
->join('custom','custom.uid','=','lottery_recruitment_record.custom_uid')
->where($map)
->select(['lottery_recruitment_record.*','custom.weiban_extid','custom.username','lottery_recruitment.name as active_name'])
->orderByDesc('id')
->paginate(config('page_num',10));
// 循环处理数据
foreach ($list as $key => $value) {
// 组合数据
$value['state'] = $Model->getRecordState($value['status'],'name');
// id转编号
$value['custom_code'] = $Custom->idToCode($value['custom_uid']);
// 重组
$list[$key] = $value;
}
// 获取列表
$statusList = $Model->getRecordStateList();
// 获取列表
$lotteryList = $LotteryRecruitment->query()->get(['id','name'])->toArray();
// 分配数据
$this->assign('empty', '
~~暂无数据 |
');
$this->assign('list',$list);
$this->assign('statusList',$statusList);
$this->assign('lotteryList',$lotteryList);
// 加载模板
return $this->fetch();
}
/**
* 修改状态
*
* */
public function set_status(Request $request,Model $Model){
// 验证参数
$request->scene('set_status')->validate();
// 设置状态
$id = request('id',0);
$status = request('status',0);
// 查询用户
$oldData = $Model->where(['id'=>$id])->first();
// 如果用户不存在
if( !$oldData ) return json_send(['code'=>'error','msg'=>'数据不存在']);
// 执行修改
$result = $Model->edit($id,['status'=>$status]);
// 提示新增失败
if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
// 记录行为
$this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
// 告知结果
return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
}
/**
* 导出表格
*
* */
public function down_excel(Model $Model,Custom $Custom){
// 接受参数
$lotteryId = request('lottery_id',0);
$customCode = request('custom_code','');
$startTime = request('start_time','');
$endTime = request('end_time','');
$status = request('status');
// 查询条件
$map = [['lottery_recruitment_record.reward_id','>',0]];
// 编码转ID
$customUid = $customCode ? $Custom->codeToId($customCode) : 0;
if( $customUid ) $map[] = ['lottery_recruitment_record.custom_uid','=',$customUid];
if( $lotteryId ) $map[] = ['lottery_recruitment_record.lottery_id','=',$lotteryId];
if( $startTime ) $map[] = ['lottery_recruitment_record.insert_time','>=',strtotime($startTime)];
if( $endTime ) $map[] = ['lottery_recruitment_record.insert_time','<=',strtotime($endTime)];
if( !is_null($status) ) $map[] = ['lottery_recruitment_record.status','=',$status];
// 查询数据
$list = $Model->query()
->join('lottery_recruitment','lottery_recruitment.id','=','lottery_recruitment_record.lottery_id')
->join('custom','custom.uid','=','lottery_recruitment_record.custom_uid')
->where($map)
->select([
'lottery_recruitment_record.id',
'lottery_recruitment.id as active_id',
'lottery_recruitment.name as active_name',
'custom.uid as custom_uid',
'custom.username',
'lottery_recruitment_record.reward_name',
'lottery_recruitment_record.status',
'lottery_recruitment_record.insert_time',
'custom.weiban_extid',
])
->orderByDesc('id')->get()->toArray();
// 循环处理数据
foreach ($list as $key => $value) {
// 组合数据
$value['status'] = $Model->getRecordState($value['status'],'name');
$value['custom_uid'] = $Custom->idToCode($value['custom_uid']);
$value['username'] = hide_phone($value['username']);
$value['insert_time'] = date('Y-m-d H:i:s',$value['insert_time']);
// 重组
$list[$key] = $value;
}
// 去下载
$this->toDown($list);
}
/**
* 去下载
*/
private function toDown($data){
// xlsx文件保存路径
$excel = new \Vtiful\Kernel\Excel(['path' =>public_path().'/uploads/']);
$filePath = $excel->fileName(uniqid().'.xlsx', 'sheet1')->header(['记录ID','活动ID','活动名称','客户编码','客户昵称','奖品名称','中奖状态','中奖时间','微伴ID'])->data($data)->output();
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Disposition: attachment;filename="灯谜抽奖中奖记录'.date('Y-m-d His').'.xlsx"');
header('Content-Length: ' . filesize($filePath));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Cache-Control: max-age=0');
header('Pragma: public');
ob_clean();
flush();
// 输出文件,成功删除文件路径
if ( copy($filePath, 'php://output') ) @unlink($filePath);
}
}