<?php
namespace app\bug\model;

use think\helper\Arr;use think\model\concern\SoftDelete;
class WorkAction extends Base
{
//设置字段信息
	use SoftDelete;
    protected $schema = [
        'id'       =>'int',//
        'action_name'       =>'varchar',//功能名称
        'menu_id'       =>'int',//归属菜单页面id
        'belong'       =>'tinyint',//系统归属1采销2结算3数据统计
        'belong_action'       =>'text',//功能集合
        'belong_process'       =>'text',//功能集合
        'belong_private'       =>'text',//功能集合
        'apply_id'       =>'int',//申请人id
        'apply_name'       =>'varchar',//申请人名称
        'createTime'       =>'datetime',//
        'updateTime'       =>'datetime',//
        'delete_time'       =>'datetime',//
    ];
    protected $updateTime='updateTime';
    protected $createTime='createTime';
    protected $deleteTime = 'delete_time';
    
    public function GetBelongActionAttr($v){
    	return json_decode($v,true);
    }
    
    public function SetBelongActionAttr($v){
    	return json_encode($v,JSON_UNESCAPED_UNICODE);
    }
    public function GetBelongPrivateAttr($v){
        	return json_decode($v,true);
        }

    public function SetBelongPrivateAttr($v){
        	return json_encode($v,JSON_UNESCAPED_UNICODE);
        }
    public function GetBelongProcessAttr($v){
    	return json_decode($v,true);
    }
    
    public function SetBelongProcessAttr($v){
    	return json_encode($v,JSON_UNESCAPED_UNICODE);
    }
    
    public function GetTreeActionByIdArr($idArr,$belong=0){
    	$list = $this->whereIn("id",$idArr)->field("id,action_name,menu_id,belong_action,belong_private,belong_process")->select();
    	$temp=[];
    	foreach ($list as $item){
    		$meun = $this->GetMenuList($item->menu_id,$belong);
    		$item->belong_action_info = $this->GetActionList($item->belong_action,$belong);
    		$item->belong_private_info = $this->GetActionList($item->belong_private,$belong);
    		$item->belong_process_info = $this->GetProcessList($item->belong_process,$belong);
		    $temp[]=array_merge($item->toArray(),$meun);
    	}
    	return $temp;
    }
    
    public function GetMenuList($menuid,$belong){
    	$menu=[];
        switch ($belong){
        	case 1:
        		$menu=\app\admin\model\AdminMenu::GetMenu($menuid);
        		break;
        		case 2:
        		$menu=\app\cxinv\model\AdminMenu::GetMenu($menuid);
        		break;
        		case 3:
        		$menu=\app\cxinv\model\AdminMenu::GetMenu($menuid);
        		break;
        }
        return$menu;
    }
    
    public function GetActionList($action,$belong){
        	$act=[];
            switch ($belong){
            	case 1:
            		$act=\app\admin\model\Action::GetAction($action);
            		break;
            		case 2:
            		$act=\app\cxinv\model\Action::GetAction($action);
            		break;
            		case 3:
            		$act=\app\cxinv\model\Action::GetAction($action);
            		break;
            }
            return$act;
    }
  
    public function GetProcessList($action,$belong){
        	$act=[];
            switch ($belong){
            	case 1:
            		$act=\app\admin\model\ActionProcess::GetProcess($action);
            		break;
            		case 2:
            		$act=\app\admin\model\ActionProcess::GetProcess($action);
            		break;
            		case 3:
            		$act=\app\admin\model\ActionProcess::GetProcess($action);
            		break;
            }
            return$act;
    }
    public function GetInfoById($id,$belong=0){
            $list = $this->where('id',$id)->field('id,action_name,menu_id,belong_action')->findOrEmpty();
    		$meun = $this->GetMenuList($list->menu_id,$belong);
    		$list->belong_action_info = $this->GetActionList($list->belong_action,$belong);
		    $temp=array_merge($list->toArray(),$meun);
    	return $temp;
    }

    public function CreateByArray($array){
        $ave = $this->saveAll($array);
        if($ave->isEmpty())throw new \Exception("功能创建失败");
        return array_column($ave->toArray(),"id");
    }

    public function GetBelongActionByIdArrs($IdArrs){
         $list = $this->whereIn('id',$IdArrs)->field('belong_action,belong_private,belong_process')->select();
         if($list->isEmpty())return [];
         $action = array_column($list->toArray(),"belong_action");
         $private = array_column($list->toArray(),"belong_private");
         $process = array_column($list->toArray(),"belong_process");
          return ["action"=>array_values(array_unique(Arr::flatten($action))),"private"=>array_values(array_unique(Arr::flatten
          ($private))),"process"=>array_values(array_unique(Arr::flatten($process)))];
    }
}