assign('breadcrumb1','积分管理');
$this->assign('breadcrumb2','打卡记录');
}
/**
* 列表页
*
* */
public function index(Model $Model,Custom $Custom,WeiBanFollow $WeiBanFollow,City $City){
// 接受参数
$code = request('custom_code','');
$phone = request('phone','');
$username = request('username','');
$activeName = request('active_name','');
$cityId = request('city_id',0);
$status = request('status');
$startTime = request('start_time','');
$endTime = request('end_time','');
// 编码转ID
$uid = $Custom->codeToId($code);
// 查询条件
$map = [];
// 编码ID
if( $uid ) $map[] = ['custom.uid','=',$uid];
if( $phone ) $map[] = ['custom.phone','=',$phone];
if( $username ) $map[] = ['custom.username','=',$username];
if( $cityId ) $map[] = ['custom.city_id','=',$cityId];
if( $activeName ) $map[] = ['score_clockin_active.name','=',$activeName];
if( $startTime ) $map[] = ['custom_clockin_record.insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
if( $endTime ) $map[] = ['custom_clockin_record.insert_time','<=',Carbon::createFromFormat('Y-m-d',$endTime)->endOfDay()->getTimestamp()];
if( !is_null($status) ) $map[] = ['custom_clockin_record.status','=',$status];
// 查询数据
$list = $Model->query()
->leftJoin('custom','custom.uid','=','custom_clockin_record.custom_uid')
->leftJoin('score_clockin_active','score_clockin_active.id','=','custom_clockin_record.active_id')
->where($map)
->select(['custom.*','score_clockin_active.name as active_name','custom_clockin_record.*'])
->orderByDesc('custom_clockin_record.id')
->paginate(config('page_num',10))
->appends(request()->all());
// 循环处理数据
foreach ($list as $key => $value) {
// 城市名
$value['city_name'] = $value['city_id'] ? $City->getOne($value['city_id'],'name') : '';
// id转编号
$value['custom_code'] = $Custom->idToCode($value['uid']);
// 如果不存在微伴ID
if( !$value['weiban_extid'] ) {
// 通过手机号查询注册的账号
$value['weiban_extid'] = (string) $WeiBanFollow->query()->where([['phone_number','=',$value['phone']]])->value('weiban_extid');
// 如果存在的话,修正
if( $value['weiban_extid'] ) $Model->edit($value['uid'],['weiban_extid'=>$value['weiban_extid']]);
}
// 重组
$list[$key] = $value;
}
// 获取列表
$cityList = $City->getCityList();
// 分配数据
$this->assign('cityList',$cityList);
// 分配数据
$this->assign('empty', '
~~暂无数据 |
');
$this->assign('list',$list);
// 加载模板
return $this->fetch();
}
/**
* 添加
*
* */
public function add(Request $request,Model $Model){
if( request()->isMethod('post') ){
// 验证参数
$request->scene('add')->validate();
// 接收数据
// 接收数据
$data['username'] = request('username','');
$data['phone'] = request('phone','');
// 写入数据表
$uid = $Model->add($data);
// 如果操作失败
if( !$uid ) return json_send(['code'=>'error','msg'=>'新增失败']);
// 记录行为
$this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,1,[],$data);
// 告知结果
return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
}
// 分配数据
$this->assign('crumbs','新增');
// 加载模板
return $this->fetch();
}
/**
* 修改
*
* */
public function edit(Request $request,Model $Model,City $City){
// 接收参数
$uid = request('uid',0);
// 查询用户
$oldData = $Model->where(['uid'=>$uid])->first();
// 修改
if(request()->isMethod('post')){
// 验证参数
$request->scene('edit')->validate();
// 如果用户不存在
if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
// 转数组
$oldData = $oldData->toArray();
// 接收数据
$data['username'] = request('username','');
$data['phone'] = request('phone','');
$data['city_id'] = request('city_id',0);
$data['weiban_extid'] = request('weiban_extid','');
// 写入数据表
$result = $Model->edit($uid,$data);
// 如果操作失败
if( !$result ) return json_send(['code'=>'error','msg'=>'新增失败']);
// 记录行为
$this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,2,$oldData,$data);
// 告知结果
return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
}
// 错误告知
if( !$oldData ) return $this->error('查无数据');
// 获取列表
$cityList = $City->getCityList();
// 分配数据
$this->assign('cityList',$cityList);
$this->assign('oldData',$oldData);
$this->assign('crumbs','修改');
// 加载模板
return $this->fetch();
}
/**
* 修改状态
*
* */
public function set_status(Request $request,Model $Model){
// 验证参数
$request->scene('set_status')->validate();
// 设置状态
$uid = request('uid',0);
$status = request('status',0);
// 查询用户
$oldData = $Model->where(['uid'=>$uid])->first();
// 如果用户不存在
if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
// 执行修改
$result = $Model->edit($uid,['status'=>$status]);
// 提示新增失败
if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
// 记录行为
$this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,2,$oldData,['status'=>$status]);
// 告知结果
return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
}
/**
* 导出表格
*
* */
public function down_excel(Model $Model,Custom $Custom,WeiBanFollow $WeiBanFollow,City $City){
// 接受参数
$code = request('custom_code','');
$phone = request('phone','');
$username = request('username','');
$activeName = request('active_name','');
$cityId = request('city_id',0);
$status = request('status');
$startTime = request('start_time','');
$endTime = request('end_time','');
// 编码转ID
$uid = $Custom->codeToId($code);
// 查询条件
$map = [];
// 编码ID
if( $uid ) $map[] = ['custom.uid','=',$uid];
if( $phone ) $map[] = ['custom.phone','=',$phone];
if( $username ) $map[] = ['custom.username','=',$username];
if( $cityId ) $map[] = ['custom.city_id','=',$cityId];
if( $activeName ) $map[] = ['score_clockin_active.name','=',$activeName];
if( $startTime ) $map[] = ['custom_clockin_record.insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
if( $endTime ) $map[] = ['custom_clockin_record.insert_time','<=',Carbon::createFromFormat('Y-m-d',$endTime)->endOfDay()->getTimestamp()];
if( !is_null($status) ) $map[] = ['custom_clockin_record.status','=',$status];
// 查询数据
$list = $Model->query()
->leftJoin('custom','custom.uid','=','custom_clockin_record.custom_uid')
->leftJoin('score_clockin_active','score_clockin_active.id','=','custom_clockin_record.active_id')
->where($map)
->select(['custom.*','score_clockin_active.name as active_name','custom_clockin_record.*'])
->orderByDesc('custom_clockin_record.id')
->get()
->toArray();
// 循环处理数据
foreach ($list as $key => $value) {
// 城市名
$value['city_name'] = $value['city_id'] ? $City->getOne($value['city_id'],'name') : '';
// id转编号
$value['custom_code'] = $Custom->idToCode($value['uid']);
// 如果不存在微伴ID
if( !$value['weiban_extid'] ) {
// 通过手机号查询注册的账号
$value['weiban_extid'] = (string) $WeiBanFollow->query()->where([['phone_number','=',$value['phone']]])->value('weiban_extid');
// 如果存在的话,修正
if( $value['weiban_extid'] ) $Model->edit($value['uid'],['weiban_extid'=>$value['weiban_extid']]);
}
// 重构数据
$value = [
'id'=>$value['id'],
'active_id'=>$value['active_id'],
'active_name'=>$value['active_name'],
'custom_code'=>$value['custom_code'],
'username'=>$value['username'],
'phone'=>(string)$value['phone'],
'clockin_day'=> $value['clockin_day'],
'score'=> $value['score'],
'clockin_time'=>date('Y-m-d H:i:s',$value['clockin_time']),
'city_name'=>$value['city_name'],
'weiban_extid'=>$value['weiban_extid'],
];
// 重组
$list[$key] = $value;
}
try {
// 去下载
$this->toDown($list);
} catch (\Throwable $th) {
echo $th->getMessage();
}
}
/**
* 去下载
*/
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);
}
}