<?php
namespace App\Services\Admin;
use Illuminate\Support\Facades\DB;
class GroupService
{
    //获取全部组
    public function GetList($arr){
        $result=array();
        $sql='';
        if($arr['type']=='enable'){
            $sql=" where status =1";
        }
        $query=DB::select("select * from `group` ".$sql);
        if(count($query)){
            $result['list']=$query;
            $result['status']='ok';

        }else{
            $result['status']='no';
            $result['msg']='无有效数据';
        }
        return $result;
    }
    //修改组权限
    public function ChangeMenu($arr){
        $result=array();
        DB::beginTransaction();
        try {
            $d= DB::table('group_menu')->where('group_id', '=', $arr['groupid'])->delete();
            foreach ($arr['menuList'] as $v){
                // dd($v);
                $i=DB::table('group_menu')->insert([
                    'group_id' => $arr['groupid'],
                    'menu_id' => $v['id'],
                ]);
            }
            DB::commit(); // 手动提交事务
            $result['status']='ok';
        } catch (\Exception $e) {
            DB::rollback(); // 发生异常时手动回滚事务
            $result['status']='no';
            $result['msg']='操作失败';
        }
        return $result;
    }
    public function Save($arr){
        $result=array();
        if($arr['type']=='save'){
            $query=DB::table('group')->insert([
                'group_name' => $arr['info']['name'],
                'status' => 1,
            ]);
        }else{
            $query=DB::table('group')
                ->where('id', $arr['info']['id'])
                ->update([
                'pid' => $arr['info']['pid'],
                'name' => $arr['info']['name'],
                'url' => $arr['info']['url'],
                'icon' => $arr['info']['icon']
            ]);
        }
        if($query){
            $result['status']='ok';
        }else{
            $result['status']='no';
            $result['msg']='操作失败';
        }
        return $result;

    }
}
