<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');


class Workflow_model extends CI_Model {
        
    function __construct() 
    {
        parent::__construct();
        $this->HT = $this->load->database('HT', TRUE);
    }

    //获取树结构列表
    public function get_structure_list($wt_refuse=1){
        $sql = "SELECT   is1.is_id AS id,
                         is1.is_parent_id AS pId,
                         wt_name as name,
                         1 AS status,
                         wt_sn
                FROM     work_type 
                         INNER JOIN infoStructures is1 ON wt_parentid = is1.is_id 
                WHERE    wt_refuse = ? and wt_isdelete=0
                ORDER BY is1.is_level ASC,is1.is_sort ASC,is1.is_path ASC";
        $query = $this->HT->query($sql,array($wt_refuse));
        return $query->result();
    }
    public function get_department_list($wt_refuse=1){
        $sql = "SELECT   *
                FROM     work_type 
                         INNER JOIN infoStructures is1 ON wt_parentid = is1.is_id 
                WHERE    wt_refuse = ? and wt_isdelete=0  
                ORDER BY is1.is_level DESC,is1.is_path ASC,is1.is_sort ASC";
        $query = $this->HT->query($sql,array($wt_refuse));
        return $query->result();
    }

    public function get_parent_admin($wt_sn='',$top='1')
    {
        if (empty($wt_sn)) {
            $mapsql=" and is_level<7 ";
        }else{
            $mapsql=" and is_level<$wt_sn ";
        }
        $sql="SELECT TOP $top *
                FROM     work_type 
                         INNER JOIN infoStructures is1 ON wt_parentid = is1.is_id 
                WHERE    wt_refuse = 2 and wt_isdelete=0 and (wt_form=0 or is_level<4) and is_parent_id!=0 $mapsql
                ORDER BY is1.is_level desc";
        $query = $this->HT->query($sql);
        return $query->result();
    }

    public function get_user_top_role($wt_sn_str='')
    {
        $sql="SELECT top 1 wt_sn,is_level
                FROM     work_type 
                         INNER JOIN infoStructures is1 ON wt_parentid = is1.is_id 
                WHERE    wt_refuse = 2 and wt_isdelete=0 and (wt_form=0 or is_level<4)  and wt_sn in($wt_sn_str) and is_parent_id!=0
                ORDER BY is1.is_level asc";
        $query = $this->HT->query($sql);
        $result=$query->result();
        return $result[0];
    }

    public function get_verify_by_filed($wv_wf_sn,$filed='work_verify.*')
    {
        $sql=" SELECT $filed,whu_uname as OPI_Name FROM work_verify LEFT JOIN webht_user ON wv_admin=whu_uid WHERE wv_wf_sn =? AND wv_status=1 AND wv_isrole=0";
        $query = $this->HT->query($sql,array($wv_wf_sn));
        $result=$query->result();
        return $result;
    }

    public function get_verify($wv_wf_sn,$wv_sort=false,$wv_status=false,$top=1)
    {
        $mapsql='';
        if ($wv_status!=false) {
            $mapsql=" AND wv_status=$wv_status ";
        }
        if ($wv_sort) {
            $mapsql.=" AND wv_sort=$wv_sort ";
        }
        $sql=" SELECT TOP $top * FROM work_verify WHERE wv_wf_sn =? $mapsql order by wv_sort asc";
        $query = $this->HT->query($sql,array($wv_wf_sn));
        $result=$query->result();
        return $result;
    }

    public function get_work_detail($wf_sn)
    {
        $sql=" SELECT * 
                        FROM work_flow 
                        left join work_type on wf_wt_sn=wt_sn 
                        left join OperatorInfo on wf_user=OPI_SN
                WHERE wf_sn=?";
        $query = $this->HT->query($sql, array($wf_sn));
        $result=$query->result();
        return $result;
    }

    public function get_work_list($wv_admin,$wv_status,$wf_sn=false,$page_flag=false,$wv_actived=-1)
    {// 
        $map=" AND wt_form!='rota.php' AND (wv_admin=258 or wv_admin='$wv_admin')";
        if (!$wv_actived) {
            $map.=" AND wv_actived!=-1 ";
        }else{
            $map.=" AND wv_actived!=$wv_actived ";
        }
        if ($wf_sn) {
            $map=" AND wf_sn= $wf_sn ";
        }
        $sql=" SELECT top 30 *,(select top 1 wv_status from work_verify where wv_wf_sn=wf_sn and wv_status=1) as isverify  ";
        $page_flag?$sql='SELECT wv_sn ':'';
    	$sql.=" FROM work_verify 
    					left join work_flow on wv_wf_sn=wf_sn 
    					left join work_type on wf_wt_sn=wt_sn 
                        left join OperatorInfo on wf_user=OPI_SN
    			WHERE wv_status=? $map
                ORDER BY wf_time desc";	
    	$query = $this->HT->query($sql, array($wv_status));
        $result=$query->result();
        return $result;
    }

    public function get_work_list_by_sn($task_id_str)
    {
        $sql="SELECT *,(select top 1 wv_status from work_verify where wv_wf_sn=wf_sn and wv_status=1) as isverify FROM work_verify 
                        left join work_flow on wv_wf_sn=wf_sn 
                        left join work_type on wf_wt_sn=wt_sn 
                        left join OperatorInfo on wf_user=OPI_SN
                WHERE wv_sn in ($task_id_str) ORDER BY wf_time desc,wf_sn desc";
        $query = $this->HT->query($sql);
        $result=$query->result();
        return $result;
    }

    public function get_rata_list($datetime=false,$limit=false,$order='DESC')
    {
        $map='';
        if ($datetime) {
            $map=" AND wf_time> $datetime ";
        }
        $top='';
        if ($limit) {
            $top=" TOP $limit ";
        }
        $sql=" SELECT $top *,(select top 1 wv_status from work_verify where wv_wf_sn=wf_sn and wv_status=1) as isverify 
                        FROM work_flow 
                        left join work_type on wf_wt_sn=wt_sn 
                        left join OperatorInfo on wf_user=OPI_SN
                WHERE wt_form='rota.php' $map
                ORDER BY wf_time $order,wf_sn $order";
        $query = $this->HT->query($sql);
        $result=$query->result();
        return $result;
    }

    public function get_list_by_namestring($namestring,$datetime)
    {
        $sql=" SELECT * FROM work_verify 
                        left join work_flow on wv_wf_sn=wf_sn 
                        left join work_type on wf_wt_sn=wt_sn 
                        left join OperatorInfo on wf_user=OPI_SN
                WHERE OPI_Name in($namestring) AND (wf_time>? or wf_time=?) AND wt_form!='rota.php'";
        $query = $this->HT->query($sql,array($datetime,$datetime));
        $result=$query->result();
        return $result;
    }

    public function get_user_work_list($wf_user,$wt_sn=false,$page_flag=false,$task_id_str=0)
    {
        $mapsql='';
        if ($wt_sn) {
           $mapsql=" AND (wt_parentid='$wt_sn' OR wf_wt_sn='$wt_sn') ";
        }
        $sql=" SELECT *,(select top 1 wv_status from work_verify where wv_wf_sn=wf_sn and wv_status=1) as isverify ";
        if ($task_id_str===0) {
            $mapsql="wf_user=$wf_user and wt_form!='rota.php'".$mapsql;
            $page_flag?$sql='SELECT wf_sn ':'';
        }else{
            $mapsql="wf_sn in ($task_id_str) ";
        }

        $sql.=" FROM work_flow 
                        left join work_type on wf_wt_sn=wt_sn 
                        left join OperatorInfo on wf_user=OPI_SN
                WHERE $mapsql
                ORDER BY wf_sn desc";
        $query = $this->HT->query($sql);
        $result=$query->result();
        return $result;
    }

    public function get_work_flow_data($wf_sn_string)
    {
        $sql=" SELECT * FROM work_flow_data WHERE wfd_wf_sn in ($wf_sn_string)";
        $query = $this->HT->query($sql);
        $result=$query->result();
        return $result;
    }

    public function get_wfd_by_wfd_key($wfd_key,$wf_sn)
    {
        $sql=" SELECT * FROM work_flow_data WHERE wfd_key=? AND wfd_wf_sn=? ";
        $query = $this->HT->query($sql,array($wfd_key,$wf_sn));
        $result=$query->result();
        return $result;
    }

    public function get_work_flow_data_by_key($wf_sn)
    {
        $rota='rota'.trim($wf_sn);
        $sql=" SELECT * FROM work_flow_data WHERE wfd_wf_sn=0 AND wfd_value like '1' AND wfd_key=?";
        $query = $this->HT->query($sql,array($rota));
        $result=$query->result();
        return $result;
    }

    public function get_type_list($wt_parentid=0,$wt_sn=false,$wt_isdelete=0,$wt_refuse=0)
    {
        
        $map_sql=" WHERE wt_parentid=$wt_parentid ";
        if ($wt_parentid==='all') {
            $map_sql=" WHERE 1=1 ";
        }
        if ($wt_isdelete!=='all') {
            $map_sql.=" AND wt_isdelete=$wt_isdelete ";
        }
        if ($wt_sn!=false) { 
            $map_sql=" WHERE wt_sn=$wt_sn ";
        }
        $map_sql.= " AND wt_refuse = $wt_refuse ";

    	$sql=" SELECT * FROM work_type $map_sql ORDER BY wt_sn ASC,wt_isdelete ASC ";
    	$query = $this->HT->query($sql);
        $result=$query->result();
        return $result;
    }

    public function get_user_list($whu_uid=false)
    {
        $mapsql='';
        if ($whu_uid!==false) {
            $mapsql=" and whu_uid in ($whu_uid) ";
        }
        $sql="SELECT * from webht_user where 1=1 $mapsql order by department asc";
        $query = $this->HT->query($sql);
        $result=$query->result();
        return $result;
    }

    public function get_user_by_role($role_wt_sn,$department=false)
    {
        $role_wt_sn=','.$role_wt_sn.',';
        $mapsql='';
        if ($department!=false) {
            $mapsql=" and department in ($department) ";
        }
        $sql="SELECT * FROM webht_user WHERE ','+role+',' like '%$role_wt_sn%' $mapsql ";
        $query = $this->HT->query($sql);
        $result=$query->result();
        return $result;
    }

    public function get_department_parent($department)
    {
        $sql="SELECT is_path FROM work_type inner join infoStructures on wt_parentid=is_id where wt_refuse=1 and wt_sn in ($department)";
        $query = $this->HT->query($sql);
        $result=$query->result();
        return $result;
    }

    public function get_user_by_wtparentid($role_wt_sn,$wt_parentids)
    {
        $sql="SELECT *  
                FROM webht_user 
                     inner join work_type on department=wt_sn 
               where ','+role+',' like '%,$role_wt_sn,%' and wt_refuse=1 and wt_parentid in($wt_parentids)";
        $query = $this->HT->query($sql);
        $result=$query->result();
        return $result;
    }

    public function get_department_by_name($uname_array)
    {
        $mapsql='';
        foreach ($uname_array as $n) {
            $mapsql.="whu_uname like'%$n%' or ";
        }
        $mapsql.='1=2';
        $sql="SELECT * FROM webht_user WHERE $mapsql ";
        $query = $this->HT->query($sql);
        $result=$query->result();
        return $result;
    }

    public function get_email_by_wtsn($wt_msg_user){
        $sql="SELECT whu_email FROM OperatorInfo
                       LEFT JOIN webht_user ON whu_uname like OPI_Name+'%'
               WHERE   OPI_SN in ($wt_msg_user)";
        $query = $this->HT->query($sql);
        $result=$query->result();
        return $result;
    }

    public function add_type($wt_name,$wt_form,$wt_step,$wt_msg_user,$wt_parentid,$wt_time,$wt_refuse=0,$wt_isneedkaoqin=0,$wt_description='',$wt_isrest=0,$wt_userdefined=0,$wt_needmail=0,$wt_linear=0)
    {
    	$sql="INSERT INTO work_type (
            wt_name,
            wt_form,
            wt_step,
            wt_msg_user,
            wt_parentid,
            wt_time,
            wt_refuse,
            wt_isneedkaoqin,
            wt_description,
            wt_isrest,
            wt_userdefined,
            wt_needmail,
            wt_linear
            ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)";
        $this->HT->query($sql, array($wt_name,$wt_form,$wt_step,$wt_msg_user,$wt_parentid,$wt_time,$wt_refuse,$wt_isneedkaoqin,$wt_description,$wt_isrest,$wt_userdefined,$wt_needmail,$wt_linear));
        return $this->HT->last_id('work_type');
    }

    public function add_work($wf_wt_sn,$wf_user,$wf_time)
    {
    	$sql="INSERT INTO work_flow (
            wf_wt_sn,
            wf_user,
            wf_time
            ) VALUES (?,?,?)";
        $this->HT->query($sql, array($wf_wt_sn,$wf_user,$wf_time));
        return $this->HT->last_id('work_flow');
    }

    public function add_work_data($col_val)
    {
    	$sql="INSERT INTO work_flow_data (
            wfd_key,
            wfd_value,
            wfd_wf_sn
            ) VALUES $col_val";
        $this->HT->query($sql);
        return $this->HT->last_id('work_flow_data');
    }

    public function add_work_verify($wv_admin,$wv_time,$wv_wf_sn,$wv_sort=0,$wv_actived='')
    {
    	$sql="INSERT INTO work_verify (
            wv_admin,
            wv_time,
            wv_wf_sn,
            wv_sort,
            wv_actived
            ) VALUES (?,?,?,?,?)";
        $this->HT->query($sql, array($wv_admin,$wv_time,$wv_wf_sn,$wv_sort,$wv_actived));
        return $this->HT->last_id('work_verify');
    }

    public function update_wt($wt_name,$wt_form,$wt_step,$wt_msg_user,$wt_parentid,$wt_sn,$wt_isneedkaoqin=0,$wt_description='',$wt_isrest=0,$wt_userdefined=0,$wt_needmail=0,$wt_linear=0)
    {
        $sql="UPDATE work_type SET wt_name=?,wt_form=?,wt_step=?,wt_msg_user=?,wt_parentid=?,wt_isneedkaoqin=?,wt_description=?,wt_isrest=?,wt_userdefined=?,wt_needmail=?,wt_linear=? WHERE wt_sn=?";
        $query=$this->HT->query($sql, array($wt_name,$wt_form,$wt_step,$wt_msg_user,$wt_parentid,$wt_isneedkaoqin,$wt_description,$wt_isrest,$wt_userdefined,$wt_needmail,$wt_linear,$wt_sn));
        return $query;
    }

    public function update_wv($wv_wf_sn,$wv_comment,$wv_status,$wv_admin=false,$wv_sort=false){
        $mapsql='';
        if ($wv_admin!=false) {
            $mapsql=" AND wv_admin=$wv_admin ";
        }
        if ($wv_sort!==false) {
            $mapsql.=" and wv_sort=$wv_sort ";
        }
        $sql="UPDATE work_verify SET wv_comment=?,wv_status=? WHERE wv_wf_sn=? $mapsql ";
        $query=$this->HT->query($sql, array($wv_comment,$wv_status,$wv_wf_sn));
        return $query;
    }

    public function active_verify($wv_actived,$wv_wf_sn,$wv_sort)
    {
        $sql="UPDATE work_verify SET wv_actived=? WHERE wv_wf_sn=? AND wv_sort=?";
        $query=$this->HT->query($sql, array($wv_actived,$wv_wf_sn,$wv_sort));
        return $query;
    }

    public function update_wf($wf_sn,$filed_array)
    {
        $set_col='';
        foreach ($filed_array as $key => $value) {
            $set_col.="$key=$value".',';
        }
        $set_col=rtrim($set_col,',');
        $sql="UPDATE work_flow SET $set_col WHERE wf_sn=?";
        $query=$this->HT->query($sql, array($wf_sn));
        return $query;
    }

    public function update_wfd($wfd_sn,$wfd_value){
        $sql="UPDATE work_flow_data SET wfd_value=? WHERE wfd_sn=?";
        $query=$this->HT->query($sql, array($wfd_value,$wfd_sn));
        return $query;
    }

    public function update_webuser($whu_uname,$whu_email,$whu_ip,$department,$role,$whu_uid)
    {
        $sql="UPDATE webht_user set whu_uname=?,whu_email=?,whu_ip=?,department=?,role=? where whu_uid=? ";
        $query = $this->HT->query($sql,array($whu_uname,$whu_email,$whu_ip,$department,$role,$whu_uid));
        return $query;
    }
	
	public function add_webuser($whu_uname,$whu_email,$whu_ip,$department,$role)
    {
		$status = 1;
        $sql="INSERT INTO webht_user 
					(whu_uname,
					 whu_email,
					 whu_ip,
					 whu_status,
					 department,
					 role) 
				VALUES
					(?,?,?,?,?,?)";
        $query = $this->HT->query($sql,array($whu_uname,$whu_email,$whu_ip,$status,$department,$role));
        return $query;
    }
	
	public function delete_webuser($whu_uname,$whu_uid)
    {
        $sql="DELETE FROM 
						webht_user 
					WHERE 
						whu_uname=?
					and
						whu_uid=?";
        $query = $this->HT->query($sql,array($whu_uname,$whu_uid));
        return $query;
    }

    public function delete_wt($wt_sn,$wt_isdelete=1)
    {
        $sql="UPDATE work_type SET wt_isdelete=? WHERE wt_sn=?";
        $query=$this->HT->query($sql, array($wt_isdelete,$wt_sn));
        return $query;
    }

    public function delete_wf($wf_sn){
        $sql="DELETE FROM work_flow WHERE wf_sn=?";
        $query=$this->HT->query($sql, array($wf_sn));
        return $query;
    }

    public function delete_wfd($wfd_wf_sn)
    {
        $sql="DELETE FROM work_flow_data WHERE wfd_wf_sn=?";
        $query=$this->HT->query($sql, array($wfd_wf_sn));
        return $query;
    }

    public function delete_wv($wv_wf_sn){
        $sql="DELETE FROM work_verify WHERE wv_wf_sn=?";
        $query=$this->HT->query($sql, array($wv_wf_sn));
        return $query;
    }
	
	public function delete_test(){
		$sql = "delete from work_flow_data where wfd_key like '%201905%'";
		$query=$this->HT->query($sql);
	}

}