<?php


namespace app\admin\controller;
use app\BaseController;
use think\App;
use think\facade\Db;

//流程单
class Process extends BaseController
{
    public $post="";
 public function __construct(App $app)
 {

     parent::__construct($app);
     $this->post = $this->request->post();
 }

    /**
     * @return \think\response\Json|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
  public function list(){
     $page =isset($this->post['page']) &&$this->post['page']!="" ? intval($this->post['page']) : 1;
     $size =isset($this->post['size']) &&$this->post['size']!="" ? intval($this->post['size']) : 10;

     $count = Db::name("process")->where('is_del',0)->count();
     $total = ceil($count/$size);
     $page = $page>$total ? $total:$page;
      $list = Db::name("process")->where('is_del', 0)->page($page, $size)->select();
     return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  }

    /**
     * @return \think\response\Json|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
  public  function info(){
    $id =isset($this->post['id']) &&$this->post['id']!="" ? intval($this->post['id']) : "";
    if($id==""){
        return error_show(1004,"参数id 不能为空");
    }
    $info = Db::name("process")->where(['id'=>$id,"is_del"=>0])->find();
    if(empty($info)){
        return error_show(1004,"流程信息未找到");
    }
    $list=Db::name("action_process")->where(["pid"=>$id,"is_del"=>0])->order("weight,id")->select();
    $info['item'] = empty($list) ? []:$list;
    return app_show(0,"获取成功",$info);
  }

    /**
     * @return \think\response\Json|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
  public function process(){
    $process_type = isset($this->post['type'])&&$this->post['type']!="" ? $this->post['type']:"";
    if($process_type==""){
        return error_show(1004,"参数type 不能为空");
    }
      $orderCode = isset($this->post['orderCode'])&&$this->post['orderCode']!="" ? $this->post['orderCode']:"";
      if($orderCode==""){
          return error_show(1004,"参数orderCode不能为空");
      }
    $list = Db::name("action_process")
        ->withoutField('updatetime')//排除更新时间
        ->where(['order_type'=>$process_type,"is_del"=>0,"status"=>1])
        ->order("weight desc,id desc")
        ->select()
        ->toArray();

      //根据所有的状态,查询对应的操作
      $order_process_s = array_column($list,'order_process');

      $node= Db::name("process_order")
          ->where(["order_type"=>$process_type,"order_code"=>$orderCode])
          ->whereIn('action_process',$order_process_s)
          ->column('id,action_uid,action_name,addtime','action_process');

    $data=[];
    foreach ($list as $value){
//        $node = Db::name("process_order")
//            ->where([
//                "order_type"=>$value['order_type'],
//                "action_process"=>$value['order_process'],
//                "order_code"=>$orderCode])
//            ->find();
        $value['action_uid']= isset($node[$value['order_process']]['action_uid']) ? $node[$value['order_process']]['action_uid']:'';
        $value['action_name']= isset($node[$value['order_process']]['action_name']) ? $node[$value['order_process']]['action_name']:'';
        $value['addtime']= isset($node[$value['order_process']]['addtime']) ? $node[$value['order_process']]['addtime']:'';
        $data[]=$value;
    }
    return app_show(0,"获取成功",$data);
  }  

}