<?php

namespace App\Models;
/**
 *
 */
class Hit_code extends BaseModel {
	protected $table = "hit_code";
	public $timestamps = false;

	//获取打码量详情
	function getHitcodeDetails($value, $type = 2) {
		$key = $this->getFeild($type);
		$data = $this->where($key, $value)->first();
		if (!$data) {
			return -7012000102; //没有详情信息
		}
		return $data->toArray();
	}

	//字段对应值
	private function getFeild($num) {
		$data = array(
			'1' => 'id',
			'2' => 'account_identity',
			'3' => 'account_name',
			'4' => 'hitcode_game',
			'5' => 'hitcode_count',
			'6' => 'star_time',
			'7' => 'sort',
		);
		return $data[$num];
	}
	
	//插入数据
	function saveData($data) {
	    if(!$this->checkInfo($data['account_identity'])){
	        $res = $this->updateInfo($data,'account_identity',$data['account_identity']);
			return $res;
        }
		$this->account_name = $data['account_name'];
		$this->account_identity = $data['account_identity'];
		$this->star_time = $data['star_time'];
		$this->add_time = $data['add_time'];
		$res = $this->save();
		if (!$res) {
			return -7012000202; //更新失败
		}
		return $this->order_id;

	}
	
	
	/**
	 * 通过用户ID选择入款初始时间
	 * 'user description'
	 * @return 'type' 'description'
	 */
	function selectHitcodetimeById($value,$hitcode_time) {
		$account = new \App\Models\Account;
		$info = $account->getinfo($value,1);
		if($info<0){
			return $info;	//用户暂无记录
		}
		$data = array(
			'account_identity' => $info['identity'],
			'account_name' => $info['account'],
			'star_time' => $hitcode_time,
			'add_time' => date('Y-m-d H:i:s',time()),
		);
		
		$res = $this->saveData($data);
		return $res;
	}
	
	/**
	 * 通过用户identity选择入款初始时间
	 * 'user description'
	 * @return 'type' 'description'
	 */
	function selectHitcodetimeByIdentity($value,$hitcode_time) {
		$account = new \App\Models\Account;
		$info = $account->getinfo($value,2);
		if($info<0){
			return $info;	//用户暂无记录
		}
		$data = array(
			'account_identity' => $info['identity'],
			'account_name' => $info['account'],
			'star_time' => $hitcode_time,
			'add_time' => date('Y-m-d H:i:s',time()),
		);
		
		$res = $this->saveData($data);
		return $res;
	}
	
	//修改信息
	function updateInfo($data,$field,$value){
		$res = $this->where($field,$value)->update($data);
		if($res<0){
			return -7012000302; //操作失败
		}
		return 1;
	}
	
	//检测订单号
	function checkInfo($account_identity){
	    $res=$this->where('account_identity',$account_identity)->first();
	    if(!$res){
	        return true;
        }
        return false;
    }
}

?>