123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- <?php
- namespace app\admin\controller;
- use think\App;
- use think\Db;
- class WasteApply extends Auth {
- public function __construct(App $app = null) {
- parent::__construct($app);
- $this->table = 'waste_apply';
- }
- public $s = [
- '申请中',
- '审核通过',
- '审核拒绝',
- '撤回',
- ];
- public function index() {
- if (request()->isAjax()) {
- //分页参数
- $length = input('rows', 10, 'intval'); //每页条数
- $page = input('page', 1, 'intval'); //第几页
- $start = ($page - 1) * $length; //分页开始位置
- //排序
- $sortRow = input('sidx', 'id', 'trim'); //排序列
- $sort = input('sord', 'desc', 'trim'); //排序方式
- $order = $sortRow . ' ' . $sort;
- $cateid = input('cateid', '', 'trim');
- if ($cateid) {
- $map[] = ['cateid', 'like', '%' . $cateid . '%'];
- }
- $addrId= input('addrId','');
- if ($addrId) {
- $map[] = ['waste_device_id','=',$addrId];
- }
- $start_time = input('start', '', 'trim');
- $end_time = input('end', '', 'trim');
- if ($start_time !== '' && $end_time !== '') {
- if ($start_time <= $end_time) {
- $start_time = date('Y-m-d 00:00:00', strtotime($start_time));
- $end_time = date('Y-m-d 23:59:59', strtotime($end_time));
- $map[] = ['create_time', 'between', [$start_time, $end_time]];
- }
- }
- if ($start_time !== '' && $end_time == '') {
- $start_time = date('Y-m-d 00:00:00', strtotime($start_time));
- $map[] = ['create_time', '>=', $start_time];
- }
- if ($start_time == '' && $end_time !== '') {
- $end_time = date('Y-m-d 23:59:59', strtotime($end_time));
- $map[] = ['create_time', '<=', $end_time];
- }
- $status = input('status', '', 'trim');
- // $post = request()->post();
- // if (!isset($post['status'])) {
- // $map[] = ['status', 'in', [0, 1]];
- // }
- if($status != ''){
- $map[] = ['status', '=', $status];
- }
- $map[] = ['del', '=', 0];
- $map[] = ['org_id', '=', $this->orgId];
- $map = empty($map) ? true : $map;
- //数据查询
- $lists = db($this->table)
- ->where($map)
- ->limit($start, $length)
- ->order('id desc')->select();
- foreach ($lists as $k => $v) {
- $lists[$k]['waste_device'] = db('address')
- ->where('id', $v['waste_device_id'])
- ->value('title');
- $lists[$k]['real_name'] = db('user')
- ->where('id', $v['user_id'])
- ->value('real_name');
- $lists[$k]['cate'] = db('waste_type')
- ->where('id', $v['cateid'])
- ->value('title');
- $lists[$k]['weight'] = round($v['weight'] / 1000, 2);
- $lists[$k]['statusName'] = isset($this->s[$v['status']])?$this->s[$v['status']]:'';
- }
- //数据返回
- $totalCount = db($this->table)->where($map)->count();
- $totalPage = ceil($totalCount / $length);
- $result['page'] = $page;
- $result['total'] = $totalPage;
- $result['records'] = $totalCount;
- $result['rows'] = $lists;
- return json($result);
- }
- else {
- $type = (new \app\common\model\WasteType())->getList($this->orgId);
- $addr = model('Address')->getListByType(7,$this->orgId);
- $this->assign('addr', $addr);
- $this->assign('m_name', '修改申请');
- $this->assign('type', $type);
- $this->assign('status', $this->s);
- return $this->fetch();
- }
- }
- public function info($id){
- if (!$id) {
- $this->error('参数错误');
- }
- $info = db($this->table)->where('id', $id)
- ->find();
- if(empty($info)){
- $this->error('记录不存在');
- }
- $old = $info['old_data']?json_decode($info['old_data'],true):[];
- $scan = Db::name('waste_scale')
- ->where('enable',1)
- ->where('del',0)
- ->where('org_id',cur_org_id())
- ->select();
- $uList = [];
- if(!empty($scan)){
- $uList = Db::name('user')
- ->where('del',0)
- ->where('id','in',array_column($scan,'user_id'))
- ->field('id,real_name as title')
- ->select();
- }
- $type = (new \app\common\model\WasteType())->getList($this->orgId);
- $addr = model('Address')->getListByType(7,$this->orgId);
- $this->assign('addr', $addr);
- $this->assign('type', $type);
- $this->assign('uList',$uList);
- $this->assign('old',$old);
- $this->assign('info',$info);
- $org = Db::name('org')
- ->where('id',$info['org_id'])
- ->value('name');
- $this->assign('org',$org);
- return $this->fetch();
- }
- public function edit($id) {
- if (empty($id)) {
- $this->error('未选择记录');
- }
- $info = Db::name($this->table)->where('id',$id)->find();
- if (!$info) {
- $this->error('申请记录不存在');
- }
- if (request()->isPost()) {
- $data = request()->post();
- $check = Db::name('waste_apply')
- ->where('record_id',$id)
- ->where('del',0)
- ->where('status',0)
- ->find();
- if ($check) {
- $this->error('当前记录正在申请中');
- }
- $data['record_id'] = $data['id'];
- $data['org_id'] =$this->orgId;
- $data['create_time'] = getTime();
- unset($data['id']);
- $result = validate('WasteApply')->check($data,[],'');
- if(true !== $result){
- $this->error(validate('WasteApply')->getError());
- }
- $res = Db::name('waste_apply')
- ->where('id',$id)
- ->update($data);
- $res?$this->success('操作成功'):$this->error('操作失败');
- }else{
- $scan = Db::name('waste_scale')
- ->where('enable',1)
- ->where('del',0)
- ->where('org_id',cur_org_id())
- ->select();
- $uList = [];
- if(!empty($scan)){
- $uList = Db::name('user')
- ->where('del',0)
- ->where('id','in',array_column($scan,'user_id'))
- ->field('id,real_name as title')
- ->select();
- }
- $type = (new \app\common\model\WasteType())->getList($this->orgId);
- $addr = model('Address')->getListByType(7,$this->orgId);
- $this->assign('addr', $addr);
- $this->assign('type', $type);
- $this->assign('uList',$uList);
- $this->assign('info',$info);
- $uInfo = Db::name('user')
- ->where('id',$this->userId)
- ->find();
- $this->assign('uInfo',$uInfo);
- return $this->fetch();
- }
- }
- public function withdraw($id = 0) {
- if (!$id) {
- $this->error('参数错误');
- }
- $info = db($this->table)->where('id', $id)
- ->find();
- if(empty($info)){
- $this->error('记录不存在');
- }
- if($info['status']!=0){
- $this->error('当前记录状态不可撤回');
- }
- $res = db($this->table)->where('id', $id)->update([
- 'status' => 3,
- 'update_time' => getTime(),
- ]);
- if ($res) {
- $this->success('操作成功');
- }
- else {
- $this->error('操作失败');
- }
- }
- public function option($id = 0) {
- if (!$id) {
- $this->error('参数错误');
- }
- $info = db($this->table)->where('id', $id)
- ->find();
- if(empty($info)){
- $this->error('记录不存在');
- }
- if($info['status']!=0){
- $this->error('当前记录状态不可审核');
- }
- if (request()->isPost()) {
- $data = request()->post();
- $res = db($this->table)->where('id', $id)->update([
- 'status' => 3,
- 'update_time' => getTime(),
- ]);
- if ($res) {
- $this->success('操作成功');
- }
- else {
- $this->error('操作失败');
- }
- }else{
- $old = Db::name('waste_record')
- ->where('id',$info['record_id'])
- ->find();
- $scan = Db::name('waste_scale')
- ->where('enable',1)
- ->where('del',0)
- ->where('org_id',cur_org_id())
- ->select();
- $uList = [];
- if(!empty($scan)){
- $uList = Db::name('user')
- ->where('del',0)
- ->where('id','in',array_column($scan,'user_id'))
- ->field('id,real_name as title')
- ->select();
- }
- $type = (new \app\common\model\WasteType())->getList($this->orgId);
- $addr = model('Address')->getListByType(7,$this->orgId);
- $this->assign('addr', $addr);
- $this->assign('type', $type);
- $this->assign('uList',$uList);
- $this->assign('old',$old);
- $this->assign('info',$info);
- return $this->fetch();
- }
- }
- //excel导出
- public function export() {
- $meta_title = '医废修改';
- if (request()->isGet()) {
- $cateid = input('cateid', '', 'trim');
- if ($cateid) {
- $map[] = ['cateid', 'like', '%' . $cateid . '%'];
- }
- $addrId= input('addrId','');
- if ($addrId) {
- $map[] = ['waste_device_id','=',$addrId];
- }
- $start_time = input('start', '', 'trim');
- $end_time = input('end', '', 'trim');
- if ($start_time !== '' && $end_time !== '') {
- if ($start_time <= $end_time) {
- $start_time = date('Y-m-d 00:00:00', strtotime($start_time));
- $end_time = date('Y-m-d 23:59:59', strtotime($end_time));
- $map[] = ['create_time', 'between', [$start_time, $end_time]];
- }
- }
- if ($start_time !== '' && $end_time == '') {
- $start_time = date('Y-m-d 00:00:00', strtotime($start_time));
- $map[] = ['create_time', '>=', $start_time];
- }
- if ($start_time == '' && $end_time !== '') {
- $end_time = date('Y-m-d 23:59:59', strtotime($end_time));
- $map[] = ['create_time', '<=', $end_time];
- }
- $status = input('status', '', 'trim');
- // $post = request()->post();
- // if (!isset($post['status'])) {
- // $map[] = ['status', 'in', [0, 1]];
- // }
- if($status != ''){
- $map[] = ['status', '=', $status];
- }
- $map[] = ['del', '=', 0];
- $map[] = ['org_id', '=', $this->orgId];
- $map = empty($map) ? true : $map;
- //数据查询
- $lists = db($this->table)
- ->where($map)
- ->order('id desc')->select();
- foreach ($lists as $k => $v) {
- $lists[$k]['waste_device'] = db('address')
- ->where('id', $v['waste_device_id'])
- ->value('title');
- $lists[$k]['real_name'] = db('user')
- ->where('id', $v['user_id'])
- ->value('real_name');
- $lists[$k]['cate'] = db('waste_type')
- ->where('id', $v['cateid'])
- ->value('title');
- $lists[$k]['weight'] = round($v['weight'] / 1000, 2);
- $lists[$k]['statusName'] = isset($this->s[$v['status']])?$this->s[$v['status']]:'';
- }
- include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
- //实例化PHPExcel类
- $objPHPExcel = new \PHPExcel();
- //激活当前的sheet表
- $objPHPExcel->setActiveSheetIndex(0);
- //设置表格头(即excel表格的第一行)
- $objPHPExcel->setActiveSheetIndex(0)
- ->setCellValue('A1', '编号')
- ->setCellValue('B1', '收取地点')
- ->setCellValue('C1', '收取人')
- ->setCellValue('D1', '类别')
- ->setCellValue('E1', '重量(kg)')
- ->setCellValue('F1', '时间')
- ->setCellValue('G1', '状态');
- // 设置表格头水平居中
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- //设置列水平居中
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- //设置单元格宽度
- $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
- $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
- $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
- $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
- $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
- $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(50);
- $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
- $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20);
- //循环刚取出来的数组,将数据逐一添加到excel表格。
- for ($i = 0; $i < count($lists); $i++) {
- $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $lists[$i]['id']);
- $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $lists[$i]['waste_device']);
- $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $lists[$i]['real_name']);
- $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $lists[$i]['cate']);
- $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $lists[$i]['weight']);
- $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $lists[$i]['create_time']);
- $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $lists[$i]['statusName']);
- }
- //设置保存的Excel表格名称
- $filename = $meta_title.'_' . date('YmdHis', time()) . '.xls';
- //设置当前激活的sheet表格名称
- $objPHPExcel->getActiveSheet()->setTitle($meta_title);
- //设置浏览器窗口下载表格
- ob_end_clean();
- header("Content-Type: application/force-download");
- header("Content-Type: application/octet-stream");
- header("Content-Type: application/download");
- header('Content-Disposition:inline;filename="' . $filename);
- //生成excel文件
- $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
- //下载文件在浏览器窗口
- return $objWriter->save('php://output');
- }
- }
- }
|