<?php
namespace model;
use \model;
/**
 * Class history
 * @package model
 */
class history extends model
{
    public $table = 'history'; 
    public $key = 'h_id'; 
	public function __construct()
    {
		parent::__construct();
	}

	public function test()
    {
        $where = [
            'p_state[>]' => raw('5 ^ p_state')
        ];
        $result = $this->db->select('pro',['p_id','p_state'],$where);
        dd($this->db->last(),$result);
	}

	public function getList($mod,$id)
    {
        $where = [
            'h_mod'=>$mod,
            'h_mod_id'=>$id,
            'ORDER'=>['h_id'=>'DESC']
        ];
		return $this->db->select($this->table,'*',$where);
	}


	public function getOne($mod,$action,$id)
    {
        $where = [
            'h_mod'=>$mod,
            'h_action'=>$action,
            'h_mod_id'=>$id,
            'ORDER'=>['h_id'=>'DESC']
        ];
		return $this->db->get($this->table,'*',$where);
	}

    /**
     * addRow
     *
     * @param bool $repeat 是否允许重复记录
     * @return void
     */
    public function addRow($mod, $ids, $action, $remark = '', $repeat = true)
    {
        if (!is_array($ids)) $ids = explode(',', $ids);
        $result = false;
        $opuser = getOpuser();
        $datetime = getNowTime();
        foreach ($ids as $id) {
            $data = [
                'h_mod' => $mod,
                'h_action' => $action,
                'h_remark' => $remark,
                'h_mod_id' => $id,
                'opuser' => $opuser,
                'create_time' => $datetime,
            ];
            /*
            $h_id = 0;
            if($repeat==false){
                $h_id = $this->db->get($this->table,'h_id',['opuser'=>$opuser,'h_mod'=>$mod,'h_action'=>$action,'h_mod_id'=>$id]);
            }
            if($h_id){
                $result = $this->db->update($this->table,$data,['h_id'=>$h_id]);
            }else{
                $result = $this->db->insert($this->table,$data);
            }*/
            $result = $this->db->insert($this->table, $data);
        }
        return $result;
    }

    /**
     * 添加删除记录 
     *
     * @return void
     */
    public function addRowForDelete($mod, $ids, $action, $remark)
    {
        if (!is_array($ids)) $ids = explode(',', $ids);
        $result = false;
        $opuser = getOpuser();
        $datetime = getNowTime();
        foreach ($ids as $id) {
            $plmseq = $this->db->get('sample', 's_plmseq', ['s_id' => $id]);
            $data = [
                'h_mod' => $mod,
                'h_action' => $action,
                'h_remark' => $remark . ':' . $plmseq,
                'h_mod_id' => $id,
                'opuser' => $opuser,
                'create_time' => $datetime,
            ];
            $result = $this->db->insert($this->table, $data);
        }
        return $result;
    }

    /**
    * deleteRowsWhere
    *
    * @return void
    */
	public function deleteRowsWhere($mod,$ids,$action)
    {
        if(!is_array($ids)) $ids = explode(',',$ids);
        $result = false;
        $opuser = getOpuser();
        foreach($ids as $id){
            $where = [
                'opuser'=>$opuser,
                'h_mod'=>$mod,
                'h_action'=>$action,
                'h_mod_id'=>$id
            ];
            $result = $this->db->delete($this->table,$where);
        }
        return $result;
	}
}