AdminLightController.php 9.6 KB
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author:kane < chengjin005@163.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\AdminBaseController;
use think\Db;

/**
 * Class AdminLightController
 * @package app\portal\controller
 * @adminMenuRoot(
 *     'name'   =>'设备管理',
 *     'action' =>'default',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 30,
 *     'icon'   =>'th',
 *     'remark' =>'设备管理'
 * )
 */
class AdminLightController extends AdminBaseController
{
    /**
     * 设备列表
     * @adminMenu(
     *     'name'   => '设备列表',
     *     'parent' => 'portal/AdminLight/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '设备列表',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        cmf_get_current_admin_id();
        //接收搜索参数
        $param = $this->request->param();
        //添加搜索条件
        $where = [];
        $keyword = empty($param['number']) ? '' : $param['number'];
        if (!empty($keyword)) {
            $where['l.number'] = ['like', "%$keyword%"];
        }
        $status = empty($param['status']) ? '' : $param['status'];
        if (!empty($status)) {
            $where['l.status'] = $status;
        }
        $data = Db::name('light')
            ->alias('l')
            ->join('temple t', 'l.temple_id=t.id','left')
            ->join('temple_area ta', 'l.area_id=ta.id','left')
            ->where($where)
            ->field('l.*,t.address,t.name as tname,ta.name as taname,ta.light')
            ->order('l.create_time', 'desc')
            ->paginate('10');
        //向地址传参
        $data->appends($param);

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

    public function index2()
    {
        $id = $this->request->param('id');
        $data = Db::name('light')
            ->alias('l')
            ->join('temple t', 'l.temple_id=t.id','left')
            ->join('temple_area ta', 'l.area_id=ta.id','left')
            ->where('l.temple_id', $id)
            ->field('l.*,t.address,t.name as tname,ta.name as taname,ta.light')
            ->order('l.create_time', 'desc')
            ->paginate('10');
        $this->assign('page', $data->render());
        $this->assign('list', $data);
        return $this->fetch();
    }

    /**
     * 添加设备
     * @adminMenu(
     *     'name'   => '添加设备',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加设备',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        $address = Db::name('temple')->field('address')->distinct('true')->select();
        $this->assign('address', $address);
        return $this->fetch();
    }


    //获取寺庙
    public function getTemple()
    {
        $address = $this->request->param('address');
        $temple = Db::name('temple')->where('address', $address)->select()->toJson();
        return $temple;
    }

    //获取寺庙下级区域
    public function getTempleArea()
    {
        $id = $this->request->param('id');
        $temple = Db::name('temple_area')->where('temple_id', $id)->select()->toJson();
        return $temple;
    }

    //获取区域下所有位置
    public function getLocation($id="",$lightId="")
    {
        if (empty($id)) {
            $id = $this->request->param('id');
        }
        $data = Db::name('temple_area')->where('id', $id)->find();
        $arr = [];
        for ($r = 1; $r <= $data['rows']; $r++) {
            for ($c = 1; $c <= $data['columns']; $c++) {
                array_push($arr, [$r,$c]);
            }
        }
        if (empty($lightId)){
            $lightId=$this->request->param('lightId');
        }
        $is_set = Db::name('light')->where(['area_id'=> $id,'id'=>['neq',$lightId]])->field('row,column')->select()->toArray();
        foreach ($arr as $k=>$v){
            foreach ($is_set as $k1=>$v1){
            if (array_values($v)==array_values($v1)){
                unset($arr[$k]);
//                $arr = array_values($arr);
            }
            }
        }
        return json_encode($arr);
    }

    //获取Add区域下所有位置
    public function getAddLocation()
    {
        $id = $this->request->param('id');
        $data = Db::name('temple_area')->where('id', $id)->find();
        $arr = [];
        for ($r = 1; $r <= $data['rows']; $r++) {
            for ($c = 1; $c <= $data['columns']; $c++) {
                array_push($arr, [$r,$c]);
            }
        }
        $is_set = Db::name('light')->where(['area_id'=>$id])->field('row,column')->select()->toArray();
        foreach ($arr as $k=>$v){
            foreach ($is_set as $k1=>$v1){
                if (array_values($v)==array_values($v1)){
                    unset($arr[$k]);
//                $arr = array_values($arr);
                }
            }
        }
        return json_encode($arr);
    }

    /**
     * 添加设备提交
     * @adminMenu(
     *     'name'   => '添加设备提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加设备提交',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {
        $param = $this->request->param();
        $param['location'] = explode(",", $param['location']);
        $param['create_time'] = time();
        $param['row']=$param['location'][0];
        $param['column']=$param['location'][1];
        unset($param['location']);
        $re = Db::name('light')->insert($param);
        if ($re) {
            $this->success('添加成功', 'AdminLight/index');
        } else {
            $this->error('添加失败', 'AdminLight/index');
        }
    }

    /**
     * 编辑设备
     * @adminMenu(
     *     'name'   => '编辑设备',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑设备',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
        $id = $this->request->param('id');
        $data = Db::name('light')
            ->alias('l')
            ->join('temple t', 't.id=l.temple_id','left')
            ->where('l.id', $id)
            ->field('l.*,t.name as tname,t.id as tid')
            ->find();
        $data['location']=[$data['row'],$data['column']];
        $address = Db::name('temple')->field('address')->distinct('true')->select();
        $this->assign('address',$address);
        if (!empty($data['address'])) {
            $temple = Db::name('temple')->where('address', $data['address'])->select();
            $this->assign('temple', $temple);
        }
        if(!empty($data['temple_id'])) {
            $area = Db::name('temple_area')->where('temple_id', $data['temple_id'])->select();
            $this->assign('area', $area);
        }
        if(!empty($data['area_id'])) {
            $location = json_decode($this->getLocation($id = $data['area_id'], $locationId = $data['id']), true);
            $this->assign('location', $location);
        }
        $this->assign('list', $data);
        return $this->fetch();
    }

    /**
     * 编辑设备提交
     * @adminMenu(
     *     'name'   => '编辑设备提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑设备提交',
     *     'param'  => ''
     * )
     */
    public function editPost()
    {
        $param = $this->request->param();
        $param['location'] = explode(",", $param['location']);
        $param['create_time'] = time();
        $param['row']=$param['location'][0];
        $param['column']=$param['location'][1];
        unset($param['location']);
        Db::name('light')->where('id', $param['id'])->update($param);
        $this->success('编辑成功');
    }


    /**
     * 删除设备
     * @adminMenu(
     *     'name'   => '删除设备',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除设备',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
        $id = $this->request->param('id');
        $re = Db::name('light_order')
            ->alias('lo')
            ->join('light l', 'lo.light_id=l.id')
            ->where(['l.id' => $id, 'lo.end_time' => ['>', time()]])
            ->find();
        if ($re) {
            $this->error('该设备有进行中的订单,不允许删除');
        }
        Db::name('light')->where('id', $id)->delete();
        $this->success('删除成功');
    }

}