AdminFaultAuditController.php 3.9 KB
<?php
/**
 * Created by PhpStorm.
 * User: ruidiudiu
 * Date: 2018/11/23
 * Time: 11:57
 */

namespace app\portal\controller;


use app\portal\model\EquipmentModel;
use cmf\controller\AdminBaseController;
use think\Db;

/**
 * Class AdminFaultAuditController
 * @package app\portal\controller
 * @adminMenuRoot(
 *     'name'   =>'故障审核',
 *     'action' =>'index',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 1,
 *     'icon'   =>'th',
 *     'remark' =>'故障审核'
 * )
 */
class AdminFaultAuditController extends AdminBaseController{

    public function index(){
        $param=$this->request->param();
        $where=array();
        if (!empty($param['fault_type'])){
            $where['fault_type']=$param['fault_type'];
        }
        $data=Db::name('fault')
            ->alias('a')
            ->field('a.*,b.serial_number,b.hospital,u.phone')
            ->join('equipment b','a.eq_name=b.name')
            ->join('users u','a.users_id=u.id')
            ->where($where)
            ->order('a.state','ase')
            ->order('a.create_time','desc')
            ->paginate(10);

        $this->assign('page',$data->render());
        $this->assign('data',$data);
        $this->assign('fault_type', isset($param['fault_type']) ? $param['fault_type'] : '');
        return $this->fetch();
    }

    //保修
    public function warranty(){
        $id=$this->request->param('id');

        $fault=Db::name('fault')->where('id',$id)->find();

        try{
            Db::startTrans();
            Db::name('equipment')->where('name',$fault['eq_name'])->update(['state'=>2]);
            Db::name('fault')->where('id',$id)->update(['state'=>2]);
        }catch (\Exception $exception){
            Db::rollback();
            $this->error('错误:'.$exception->getMessage());
        }
        Db::commit();
        $this->success('处理完成');

    }

    //恢复设备
    public function reply(){
        $id=$this->request->param('id');

        $fault=Db::name('fault')->where('id',$id)->find();

        try{
            Db::startTrans();
            Db::name('equipment')->where('name',$fault['eq_name'])->update(['state'=>1]);
            Db::name('fault')->where('id',$id)->update(['state'=>3]);
        }catch (\Exception $exception){
            Db::rollback();
            $this->error('错误:'.$exception->getMessage());
        }
        Db::commit();
        $this->success('处理完成');

    }


    //结束计费
    public function bill_error(){
        $id=$this->request->param('id');

        $fault=Db::name('fault')->where('id',$id)->find();

        $order=Db::name('order')->where(['users_id'=>$fault['users_id'],'state'=>1])->find();
        if (empty($order)){
            $this->error('没有未完成订单');
        }else{
            $time=ceil(($fault['create_time']-$order['start_time'])/3600);
            $getPrice=new OrderController();
            $price=$getPrice->getPrice($fault['users_id'],$order['start_time'],$fault['create_time']);
            $data=[
                'end_time'=>$fault['create_time'],
                'time'=>$time,
                'price'=>$price,
                'state'=>2
            ];
            try{
                Db::startTrans();
                Db::name('users')->where('id',$fault['users_id'])->update(['is_use'=>0]);
                Db::name('order')->where('id',$order['id'])->update($data);
                Db::name('fault')->where('id',$id)->update(['state'=>3]);
                if (!Db::name('order')->where(['eq_name'=>$order['eq_name'],'state'=>1])->find()){
                Db::name('equipment')->where('name',$order['eq_name'])->update(['use'=>0]);
                }
            }catch (\Exception $exception){
                Db::rollback();
                $this->error('错误:'.$exception->getMessage());
            }
            Db::commit();
            $this->success('处理完成');
        }
    }






}