AdminLightOrderController.php 8.5 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;
use app\admin\service;

/**
 * Class AdminLightOrderController
 * @package app\portal\controller
 * @adminMenuRoot(
 *     'name'   =>'订单管理',
 *     'action' =>'default',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 30,
 *     'icon'   =>'th',
 *     'remark' =>'订单管理'
 * )
 */
class AdminLightOrderController extends AdminBaseController
{
    /**
     * 订单列表
     * @adminMenu(
     *     'name'   => '订单列表',
     *     'parent' => 'portal/AdminLightOrder/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '订单列表',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        //接收搜索参数
        $param = $this->request->param();
        //添加搜索条件
        $where=[];
        $where1=[];
        if (cmf_get_current_admin_id()!=1){
            $where['t.user_id']=cmf_get_current_admin_id();
            $where1['user_id']= $where['t.user_id'];
        };
        $user_name = empty($param['user_name']) ? '' : $param['user_name'];
        if (!empty($user_name)) {
            $where['u.user_name'] = ['like', "%$user_name%"];
        }
        $mobile=empty($param['mobile']) ? '' : $param['mobile'];
        if (!empty($mobile)) {
            $where['u.mobile'] = ['like', "%$mobile%"];
        }
        $temple=empty($param['temple']) ? '' : $param['temple'];
        if (!empty($temple)) {
            $where['l.temple_id'] = $temple;
        }
        $startTime = empty($param['start_time']) ? 0 : strtotime($param['start_time']);
        $endTime   = empty($param['end_time']) ? 0 : strtotime($param['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['lo.pay_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['lo.pay_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['lo.pay_time'] = ['<= time', $endTime];
            }
        }

        //所有寺庙
        $temples=Db::name('temple')->where($where1)->select();
        $this->assign('temples',$temples);

        //数据提取
        $data = Db::name('light_order')
            ->alias('lo')
            ->join('light l','lo.light_id=l.id')
            ->join('users u','lo.users_id=u.id')
            ->join('temple t','l.temple_id=t.id')
            ->join('temple_area ta','l.area_id=ta.id')
            ->where($where)
            ->field('lo.*,l.*,u.*,t.name as tname,ta.name as taname,lo.name as loname,lo.id as loid')
            ->order('lo.create_time','desc')
            ->paginate('10');
        //向地址传参
        $data->appends($param);

        $this->assign('page',$data->render());
        $this->assign('start_time', isset($param['start_time']) ? $param['start_time'] : '');
        $this->assign('end_time', isset($param['end_time']) ? $param['end_time'] : '');
        $this->assign('mobile', isset($param['mobile']) ? $param['mobile'] : '');
        $this->assign('temple', isset($param['temple']) ? $param['temple'] : '');
        $this->assign('user_name', isset($param['user_name']) ? $param['user_name'] : '');
        $this->assign('list', $data);
        return $this->fetch();
    }

    public function index2(){
        $id=$this->request->param('id');
        $data = Db::name('light_order')
            ->alias('lo')
            ->join('light l','lo.light_id=l.id')
            ->join('users u','lo.users_id=u.id')
            ->join('temple t','l.temple_id=t.id')
            ->join('temple_area ta','l.area_id=ta.id')
            ->where('lo.users_id',$id)
            ->field('lo.*,l.*,u.*,t.name as tname,ta.name as taname,lo.name as loname,lo.id as loid ')
            ->order('lo.create_time','desc')
            ->paginate('10');
        $this->assign('page',$data->render());
        $this->assign('list',$data);
        return $this->fetch();
    }
    public function index3(){
        $id=$this->request->param('id');
        $data = Db::name('light_order')
            ->alias('lo')
            ->join('light l','lo.light_id=l.id')
            ->join('users u','lo.users_id=u.id')
            ->join('temple t','l.temple_id=t.id')
            ->where('t.id',$id)
            ->field('lo.*,u.*,lo.id as loid')
            ->order('lo.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 edit()
    {
        $id = $this->request->param('id');
        $where=[];
        if (cmf_get_current_admin_id()!=1){
            $where['user_id']=cmf_get_current_admin_id();
        };
        $data = Db::name('light_order')
            ->alias('lo')
            ->join('light l','lo.light_id=l.id')
            ->join('users u','lo.users_id=u.id')
            ->join('temple t','l.temple_id=t.id')
            ->join('temple_area ta','l.area_id=ta.id')
            ->where('lo.id',$id)
            ->field('lo.*,l.*,u.mobile,u.user_name,t.name as tname,ta.name as taname,lo.name as loname')
            ->find();

        $address = Db::name('temple')->where($where)->field('address')->distinct('true')->select();
        $this->assign('address',$address);
        $temple = Db::name('temple')->where('address',$data['address'])->where($where)->select();
        $this->assign('temple', $temple);
        $area = Db::name('temple_area')->where('temple_id', $data['temple_id'])->select();
        $this->assign('area', $area);
        $location=json_decode($this->getLocation($id=$data['area_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['create_time'] = time();
        Db::name('light_order')->where('id', $param['id'])->update($param);
       $lightService= new service\LightService();
       $lightService->lightChange($param['light_id'],'01','01',$param['name']);
        $this->success('编辑成功');
    }


    /**
     * 删除订单
     * @adminMenu(
     *     'name'   => '删除订单',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除订单',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
        $id = $this->request->param('id');
        Db::name('light_order')->where('id', $id)->delete();
        $this->success('删除成功');
    }

    //获取区域下所有位置
    public function getLocation($id="")
    {
        if (empty($id)) {
            $id = $this->request->param('id');
        }
       $re=Db::name('light_order')->where(['end_time'=>['>',time()],'status'=>'1||2'])->distinct('light_order')->field('light_id')->select()->toArray();
       $arr=[];
        foreach ($re as $k=>$v){
            array_push($arr,$re[$k]['light_id']);
        }
        $data=Db::name('light')->where('area_id',$id)->whereNotIn('id',$arr)->order(['row'=>'asc','column'=>'asc'])->select()->toJson();
        return $data;
    }
}