123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- namespace app\sys\controller;
- use app\model\CustomerVisitLog;
- use think\facade\View;
- use app\model\Shop as ShopModel;
- class Shop
- {
- /**
- * 列表页面
- */
- public function index()
- {
- if (!request()->isAjax()){
- return View::fetch();
- } else {
- $keyword = input('keyword', '', 'trim');
- if ($keyword){
- $where[] = ['name', 'like', '%' . $keyword . '%'];
- }
- $page = input('page', 1, 'intval');
- $limit = input('limit', 10, 'intval');
- $where[] = ['root_id', '=', request()->employee->root_id];
- $count = ShopModel::where($where)->count();
- $list = ShopModel::where($where)->page($page, $limit)->order('addtime desc')->select()->toArray();
- return json(['code'=> 0, 'data'=> $list, 'count'=> $count]);
- }
- }
- /**
- * 添加
- */
- public function add()
- {
- if (request()->isPost()) {
- $root_id = request()->employee->root_id;
- $param = request()->only(['name', 'address'=> '']);
- $param['root_id'] = $root_id;
- $find = ShopModel::where([['root_id', '=', $root_id], ['name', '=', $param['name']]])->findOrEmpty();
- if (!$find->isEmpty()){
- return json(['code'=> 1, 'msg'=> '店面已存在']);
- }
- $result = ShopModel::create($param);
- if ($result !== false) {
- return json(['code'=> 0, 'msg'=> '添加成功']);
- } else {
- return json(['code'=> 1, 'msg'=> '添加失败']);
- }
- } else {
- return View::fetch();
- }
- }
- /**
- * 修改
- */
- public function edit()
- {
- $id = input('id', '', 'intval');
- if (request()->isPost()){
- $info = ShopModel::find($id);
- if ($info['root_id'] !== request()->employee->root_id){
- return json(['code'=> 1, 'msg'=> '编辑失败']);
- }
- $name = input('name', '', 'trim');
- $root_id = request()->employee->root_id;
- $find = ShopModel::where([['root_id', '=', $root_id], ['name', '=', $name]])->findOrEmpty();
- if (!$find->isEmpty() && $find['id'] != $id){
- return json(['code'=> 1, 'msg'=> '店面已存在']);
- }
- $address = input('address', '', 'trim');
- $info->name = $name;
- $info->address = $address;
- $result = $info->save();
- if ($result !== false) {
- return json(['code'=> 0, 'msg'=> '操作成功']);
- } else {
- return json(['code'=> 1, 'msg'=> '操作失败']);
- }
- } else {
- $info = ShopModel::find($id);
- View::assign('data', $info);
- return View::fetch();
- }
- }
- /**
- * 删除
- */
- public function delete()
- {
- $id = input('id', '', 'intval');
- $info = ShopModel::find($id);
- if ($info['root_id'] !== request()->employee->root_id){
- return json(['code'=> 1, 'msg'=> '删除失败']);
- }
- $use = CustomerVisitLog::where('shop_id', '=', $id)->findOrEmpty();
- if (!$use->isEmpty()){
- return json(['code'=> 1, 'msg'=> '已有员工报备客户到店,无法删除']);
- }
- $result = $info->delete();
- if ($result !== false) {
- return json(['code'=> 0, 'msg'=> '操作成功']);
- } else {
- return json(['code'=> 1, 'msg'=> '操作失败']);
- }
- }
- }
|