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

use app\portal\model\StoreModel;
use cmf\controller\AdminBaseController;
use think\Db;
use think\Loader;

/**
 * Class AdminStoreController
 * @package app\portal\controller
 * @adminMenuRoot(
 *     'name'   =>'门店管理',
 *     'action' =>'default',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 30,
 *     'icon'   =>'th',
 *     'remark' =>'门店管理'
 * )
 */
class AdminStoreController extends AdminBaseController
{
    /**
     * 店铺列表
     * @adminMenu(
     *     'name'   => '店铺列表',
     *     'parent' => 'portal/AdminStore/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '店铺列表',
     *     'param'  => ''
     * )
     */
    public function index(){
        $data = $this->request->param();
        $final = $this->adminIndex($data);
        $where_coo = $final['where_arr'];
        $where_coo['status'] = 1;
        $coo_list = Db::name('Store')->where($where_coo)->order('create_time desc')
        ->paginate(10,false,['query'=>$final['page_arr']]);
        $page = $coo_list->render();
        $this->assign('page',$page);
        $this->assign('list',$coo_list);
        return $this->fetch();
    }


    /**
     * 添加店铺
     * @adminMenu(
     *     'name'   => '添加店铺',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加店铺',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        $data = $this->request->param();
        if($data){
//            添加数据
            $model = new StoreModel();
            $validate = Loader::validate('Store');
            if(!$validate->check($data)){
                $mes = $validate->getError();
                $this->error("$mes");
            }
            $final = $model->isUpdate(false)->save($data);
            if($final){
                $this->success('成功',url('index'));
            }else{
                $this->error('失败');
            }
        }else{
            return $this->fetch();
        }
    }


    /**
     * 查看店铺
     * @adminMenu(
     *     'name'   => '查看店铺',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '查看店铺',
     *     'param'  => ''
     * )
     */
    public function view()
    {
        $where_find['id'] = $this->request->param('id');
        $list = Db::name('Store')->where($where_find)->find();
        $this->assign('list',$list);
        return $this->fetch();
    }


    /**
     * 编辑店铺
     * @adminMenu(
     *     'name'   => '编辑店铺',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑店铺',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
//            添加数据
        $data = $this->request->post();
        $validate = Loader::validate('Store');
        if(!$validate->check($data)){
            $mes = $validate->getError();
            $this->error("$mes");
        }
        $model = new StoreModel();
        $final = $model->isUpdate(true)->save($data);
        if($final){
            $this->success('成功',url('index'));
        }else{
            $this->error('失败');
        }
    }


    /**
     * 删除店铺
     * @adminMenu(
     *     'name'   => '删除店铺',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除店铺',
     *     'param'  => ''
     * )
     */
    public function del(){
        $ids = $this->request->post();
        $id = $this->request->param('id');
        if($ids){
            $add_del['id'] = array('in',$ids['ids']);
        }else if($id){
            $add_del['id'] = $id;
        }else{
            $this->error('删除失败');
        }
        $add_del['status'] = 9;
        $model = new StoreModel();
        $del = $model->isUpdate(true)->allowField(true)->save($add_del);
        if($del){
            $this->success('删除成功',url('index'));
        }else{
            $this->error('删除失败');
        }
    }
    /**
     * 门店登录列表
     * @adminMenu(
     *     'name'   => '门店登录列表',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '门店登录列表',
     *     'param'  => ''
     * )
     */
    public function choose(){
        $data = $this->request->param();
        $where_login['l.store_id'] = $data['id'];
        $where_login['l.status'] = array('neq',9);
        $list = Db::name('StoreLogin')->alias('l')
            ->where($where_login)
            ->join("Member m",'m.id = l.member_id')
            ->field('l.id,m.name,l.create_time,l.update_time,m.type,m.id as member_id')
            ->paginate(10);
        $page = $list->render();
        $this->assign('page',$page);
        $this->assign('list',$list);
        return $this->fetch();
    }
    /**
     * 绑定店长
     * @adminMenu(
     *     'name'   => '选择店长',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '选择店长',
     *     'param'  => ''
     * )
     */
    public function one(){
        $data = $this->request->param();
        if(empty($data['id'])){
            $this->apiResponse('0','id不能为空');
        }
        $where_login['id'] = $data['id'];
        $where_login['status'] = array('neq',9);
        $login = Db::name('StoreLogin')->where($where_login)->find();
        if($login){
            $where_member['id'] = $login['member_id'];
            $where_member['status'] = array('neq',9);
            $member = Db::name('Member')->where($where_member)->field('id,openid')->find();
            if($member){
                $where_store['id'] = $login['store_id'];
                $where_store['status'] = array('neq',9);
                $update['openid'] = $member['openid'];
                $update['member_id'] = $member['id'];
                $update['update_time'] = time();
                $store_save = Db::name('Store')->where($where_store)->update($update);
                if($store_save){
                    $update_mem['type'] = 2;
                    $update_mem['update_time'] = time();
                    $update_member = Db::name('Member')->where($where_member)->update($update_mem);
                    if($update_member){
                        $this->apiResponse('1','绑定成功');
                    }else{
                        $this->apiResponse('0','绑定失败');
                    }
                }else{
                    $this->apiResponse('0','绑定失败');
                }
            }else{
                $this->apiResponse('0','用户信息错误');
            }
        }else{
            $this->apiResponse('0','绑定失败');
        }
        return $this->fetch();
    }

    /**
     * 取消绑定店长
     * @adminMenu(
     *     'name'   => '选择店长',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '选择店长',
     *     'param'  => ''
     * )
     */
    public function unchoose()
    {
        $data = $this->request->param();
        if (empty($data['id'])) {
            $this->apiResponse('0', 'id不能为空');
        }
        $where_login['id'] = $data['id'];
        $where_login['status'] = array('neq', 9);
        $login = Db::name('StoreLogin')->where($where_login)->find();

        $where_store['id'] = $login['store_id'];
        $where_store['status'] = array('neq', 9);
        $store_save = Db::name('Store')->where($where_store)->field('member_id')->find();
        if ($store_save['member_id'] == $data['member_id']) {
            $update['openid'] = '';
            $update['member_id'] = 0;
            $update['update_time'] = time();
            $save = Db::name('Store')->where($where_store)->update($update);
            if ($save) {
                $where_member['id'] = $login['member_id'];
                $where_member['status'] = array('neq', 9);
                $update_mem['type'] = 1;
                $update_mem['update_time'] = time();
                $update_member = Db::name('Member')->where($where_member)->update($update_mem);
                if ($update_member) {
                    $this->apiResponse('1', '解除绑定成功');
                } else {
                    $this->apiResponse('0', '解除绑定失败');
                }
            } else {
                $this->apiResponse('0', '解除绑定失败');
            }
        } else {
            $this->apiResponse('0', '解除绑定失败');
        }

    }
}