<?php
namespace app\rongyu\controller;
use app\common\controller\Base;use app\rongyu\model\JsRongyuInfo as ryinfo;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
class JsRongyuInfo extends Base
{
public function index()
{
$list['count'] = ryinfo::count();
$list['title'] = '教师荣誉信息';
$this->assign('list', $list);
return $this->fetch();
}
public function rongyuList($id)
{
$list['id'] = $id;
$list['title'] = '教师荣誉信息';
$list['count'] = ryinfo::where('rongyuce',$id)->count();
$this->assign('list',$list);
return $this->fetch('');
}
public function ajaxData()
{
$getdt = request()->param();
$order = $getdt['order'][0]['dir'];
$order_column = $getdt['order'][0]['column'];
$order_field = $getdt['columns'][$order_column]['name'];
if($order_field=='')
{
$order_field = $getdt['columns'][$order_column]['data'];
}
$limit_start = $getdt['start'];
$limit_length = $getdt['length'];
$search = [
'hjschool'=>$getdt['hjschool'],
'fzschool'=>$getdt['fzschool'],
'category'=>$getdt['category'],
'rongyuce'=>$getdt['rongyuce'],
'search'=>$getdt['search']['value'],
'order'=>$order,
'order_field'=>$order_field
];
$ryinfo = new ryinfo;
$cnt = $ryinfo->select()->count();
$data = $ryinfo->search($search);
$datacnt = $data->count();
$data = $data->slice($limit_start,$limit_length);
$data = [
'draw'=> $getdt["draw"] , 'recordsTotal'=>$cnt, 'recordsFiltered'=>$datacnt, 'data'=>$data, ];
return json($data);
}
public function create($id = 0)
{
$list['title'] = '添加教师荣誉';
$list['id'] = $id;
$this->assign('list',$list);
return $this->fetch();
}
public function save()
{
$list = request()->only(['rongyuce','title','bianhao','category','hjschool','subject','hjshijian','jiangxiang','hjteachers','cyteachers','pic'],'put');
$validate = new \app\rongyu\validate\JsRongyuInfo;
$result = $validate->scene('add')->check($list);
$msg = $validate->getError();
if(!$result){
return json(['msg'=>$msg,'val'=>0]);
}
$data = ryinfo::create($list);
$teacherlist = [];
foreach ($list['hjteachers'] as $key => $value) {
$canyulist[] = [
'teacherid' => $value,
'category' => 1,
];
}
if(!empty($list['cyteachers'])){
foreach ($list['cyteachers'] as $key => $value) {
$canyulist[] = [
'teacherid' => $value,
'category' => 2,
];
}
}
$data->allJsry()->saveAll($canyulist);
$data ? $data=['msg'=>'添加成功','val'=>1] : $data=['msg'=>'数据处理错误','val'=>0];
return json($data);
}
public function createall($id)
{
$list['title'] = '添加教师荣誉册';
$list['rongyuce'] = $id;
$this->assign('list',$list);
return $this->fetch();
}
public function upload()
{
$rongyuce=input('post.rongyuce');
$file = request()->file('file');
$info = $file->validate(['size'=>2*1024*1024,'ext'=>'jpg,png,gif,jpeg'])->move('uploads\jiaoshirongyu');
if($info){
$list['url'] = $info->getSaveName();
$list['url'] = str_replace('\\','/',$list['url']);
$data = ryinfo::create(['pic'=>$list['url'],'rongyuce'=>$rongyuce]);
$id = $data->id;
$id ? $data = array('msg'=>'上传成功','val'=>true,'url'=>$list['url'],'ryid'=>$id) : $data = array('msg'=>'保存文件信息失败','val'=>false,'url'=>null);
}else{
$data = array('msg'=>$file->getError(),'val'=>false,'url'=>null);
}
return json($data);
}
public function read($id)
{
}
public function edit($id)
{
$list = ryinfo::where('id',$id)
->field('id,rongyuce,title,bianhao,hjschool,subject,jiangxiang,hjshijian,pic')
->with([
'hjJsry'=>function($query){
$query->field('rongyuid,teacherid')
->with(['teacher'=>function($query){
$query->field('id,xingming');
}]);
},
'cyJsry'=>function($query){
$query->field('rongyuid,teacherid')
->with(['teacher'=>function($query){
$query->field('id,xingming');
}]);
},
'ryTuce'=>function($query){
$query->field('id,fzshijian');
}
])
->find();
$this->assign('list',$list);
return $this->fetch();
}
public function update($id)
{
$list = request()->only(['bianhao','title','category','hjschool','subject','hjshijian','jiangxiang','hjteachers','cyteachers','pic'],'put');
$list['id'] = $id;
$validate = new \app\rongyu\validate\JsRongyuInfo;
$result = $validate->scene('edit')->check($list);
$msg = $validate->getError();
if(!$result){
return json(['msg'=>$msg,'val'=>0]);
}
$data = ryinfo::update($list);
$data->allJsry()->where('rongyuid',$list['id'])->delete(true);
$teacherlist = [];
foreach ($list['hjteachers'] as $key => $value) {
$canyulist[] = [
'teacherid' => $value,
'rongyuid' => $list['id'],
'category' => 1,
];
}
if(!empty($list['cyteachers'])){
foreach ($list['cyteachers'] as $key => $value) {
$canyulist[] = [
'teacherid' => $value,
'rongyuid' => $list['id'],
'category' => 2,
];
}
}
$data->allJsry()->saveAll($canyulist);
$data ? $data=['msg'=>'更新成功','val'=>1] : $data=['msg'=>'数据处理错误','val'=>0];
return json($data);
}
public function delete($id)
{
if($id == 'm')
{
$id = request()->delete('ids/a') }
$data = ryinfo::destroy($id);
$data ? $data=['msg'=>'删除成功','val'=>1] : $data=['msg'=>'数据处理错误','val'=>0];
return json($data);
}
public function setStatus()
{
$id = request()->post('id');
$value = request()->post('value');
$data = ryinfo::where('id',$id)->update(['status'=>$value]);
$data ? $data=['msg'=>'状态设置成功','val'=>1] : $data=['msg'=>'数据处理错误','val'=>0];
return json($data);
}
public function outXlsx($id)
{
$list = ryinfo::where('rongyuce',$id)
->field('id,rongyuce,title,bianhao,hjschool,subject,jiangxiang,hjshijian,pic')
->with([
'hjJsry'=>function($query){
$query->field('rongyuid,teacherid')
->with(['teacher'=>function($query){
$query->field('id,xingming');
}]);
},
'cyJsry'=>function($query){
$query->field('rongyuid,teacherid')
->with(['teacher'=>function($query){
$query->field('id,xingming');
}]);
},
'ryTuce'=>function($query){
$query->field('id,title,fzshijian');
}
])
->select();
if($list->isEmpty())
{
$this->error('兄弟,没有要下载的信息呀~');
return '';
}else{
$filename = $list[0]['ryTuce']['title'];
}
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load('jsRongyu.xlsx');
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->getCell('A2');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="fasdfas.xlsx"');
header('Cache-Control: max-age=0');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
ob_flush();
flush();
}
}