<?php
namespace app\rongyu\controller;
use app\base\controller\AdminBase;use app\rongyu\model\DwRongyu as DW;use app\tools\controller\File;
class Danwei extends AdminBase
{
public function index()
{
$list['webtitle'] = '单位荣誉列表';
$list['dataurl'] = '/rongyu/danwei/data';
$list['status'] = '/rongyu/danwei/status';
$this->view->assign('list', $list);
return $this->view->fetch();
}
public function ajaxData()
{
$src = $this->request
->only([
'page' => '1'
,'limit' => '10'
,'field' => 'update_time'
,'order' => 'desc'
,'fzschool_id' => array()
,'hjschool_id' => array()
,'category_id' => array()
,'searchval' => ''
],'POST');
$DW = new DW;
$data = $DW->search($src);
$data = reSetObject($data, $src);
return json($data);
}
public function create()
{
$list['set'] = array(
'webtitle' => '添加单位荣誉'
,'butname' => '添加'
,'formpost' => 'POST'
,'url' => 'save'
);
$this->view->assign('list',$list);
return $this->view->fetch('create');
}
public function save()
{
$list = request()->only([
'url'
,'project'
,'title'
,'teachers' => array()
,'hjschool_id'
,'category_id'
,'fzshijian'
,'fzschool_id'
,'jiangxiang_id'
], 'post');
$validate = new \app\rongyu\validate\DwRongyu;
$result = $validate->scene('create')->check($list);
$msg = $validate->getError();
if(!$result){
return json(['msg'=>$msg,'val'=>0]);
}
$data = DW::create($list);
$teachers = array();
foreach ($list['teachers'] as $key => $value) {
$teachers[]['teacher_id'] = $value;
}
$data->cyDwry()->saveAll($teachers);
$data ? $data = ['msg' => '添加成功', 'val' => 1]
: $data = ['msg' => '数据处理错误', 'val' => 0];
return json($data);
}
public function createAll()
{
$list['set'] = array(
'webtitle' => '批量上传荣誉图片'
,'butname' => '批传'
,'formpost' => 'POST'
,'url' => 'saveall'
);
$this->view->assign('list', $list);
return $this->view->fetch();
}
public function saveAll()
{
$list = request()->only([
'text'
,'serurl'
], 'post');
$file = request()->file('file');
$data = \app\facade\File::saveFileInfo($file, $list, false);
if($data['val'] != true)
{
$data=['msg' => '添加失败', 'val' => 0];
return json($data);
}
$createInfo = [
'url' => $data['url']
,'title' => '批传单位荣誉图片'
,'project' => '无'
];
$validate = new \app\rongyu\validate\DwRongyu;
$result = $validate->scene('createall')->check($createInfo);
$msg = $validate->getError();
if(!$result){
return json(['msg'=>$msg,'val'=>0]);;
}
$data = DW::create($createInfo);
$data ? $data = ['msg' => '批传成功','val' => 1] :
$data = ['msg' => '数据处理错误','val' => 0];
return json($data);
}
public function upload()
{
$list = request()->only([
'text'
,'serurl'
], 'post');
$file = request()->file('file');
$data = \app\facade\File::saveFileInfo($file, $list, false);
return json($data);
}
public function edit($id)
{
$list['data'] = DW::where('id', $id)
->field('id, title, project, category_id, hjschool_id, fzshijian, fzschool_id, jiangxiang_id, url')
->with([
'cyDwry' => function($query){
$query->field('rongyu_id, teacher_id')
->with([
'teacher' => function($query){
$query->field('id, xingming');
}
]);
},
])
->find();
$list['set'] = array(
'webtitle'=>'编辑单位荣誉'
,'butname'=>'修改'
,'formpost'=>'PUT'
,'url'=>'/rongyu/danwei/update/' . $id
);
$this->view->assign('list', $list);
return $this->view->fetch('create');
}
public function update($id)
{
$list = request()->only([
'title'
,'project'
,'category_id'
,'hjschool_id'
,'fzshijian'
,'fzschool_id'
,'jiangxiang_id'
,'teachers' => array()
,'url'
], 'put');
$list['id'] = $id;
$validate = new \app\rongyu\validate\DwRongyu;
$result = $validate->scene('edit')->check($list);
$msg = $validate->getError();
if(!$result){
return json(['msg' => $msg, 'val' => 0]);;
}
$DW = new DW();
$data = DW::update($list);
$data->cyDwry->delete(true);
$canyulist = [];
foreach ($list['teachers'] as $key => $value) {
$canyulist[] = [
'teacher_id' => $value,
];
}
$data->cyDwry()->saveAll($canyulist);
$data ? $data = ['msg' => '更新成功', 'val' => 1] :
$data = ['msg' => '数据处理错误', 'val' => 0];
return json($data);
}
public function delete($id)
{
$id = request()->delete('id');
$id = explode(',', $id);
$data = DW::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 = DW::where('id', $id)->update(['status' => $value]);
$data ? $data = ['msg' => '状态设置成功', 'val' => 1]
: $data = ['msg' => '数据处理错误', 'val' => 0];
return json($data);
}
}