<?php
/**
 * Created by PhpStorm.
 * User: xmy 280564871@qq.com
 * Date: 2017/5/3
 * Time: 17:57
 */

namespace Open\Model;

class ContractModel extends BaseModel{

	protected $_auto = [
		['create_time','time',self::MODEL_INSERT,'function'],
		['contract_number','generateContractNum',self::MODEL_INSERT,'callback'],
		['status','3',self::MODEL_INSERT],
	];

	public function generateContractNum(){
		$num = date("Ymd").uniqid(5);
		return $num;
	}

	/**
	 * 增加合同信息
	 * @param $game_id
	 * @param $develop_id
	 * @return bool|mixed
	 * author: xmy 280564871@qq.com
	 */
	public function addContract($game_id,$develop_id){
		$data['game_id'] = $game_id;
		$data['develop_id'] = $develop_id;
		$data = $this->create($data);
		if(empty($data)){
			return false;
		}
		return $this->add($data);
	}

	/**
	 * 获取数据列表
	 * @param $map
	 * @param $p
	 * @param string $order
	 * @param int $row
	 * @return mixed
	 * author: xmy 280564871@qq.com
	 */
	public function getDataLists($map,$p,$order="c.status asc,c.create_time desc",$row=10){
		$page = $this->dealPage($p);
		$data['data'] = $this->alias("c")->field("c.*,g.game_name,d.account")
			->join("left join tab_game g on g.id = c.game_id")
			->join("left join tab_developers d on d.id = c.develop_id")
			->where($map)
			->order($order)
			->page($page,$row)
			->select();
		$data['count'] = $this->alias("c")->where($map)->count();
		return $data;
	}


	/**
	 * 签署合同
	 * @param $map
	 * @return bool
	 * author: xmy 280564871@qq.com
	 */
	public function sign_contract($map){
		$data = $this->where($map)->find();
		if ($data['status'] != 2){
			return false;
		}else{
			$data['status'] = 1;
			return $res = $this->save($data);
		}
	}

	/**
	 * 确认合同
	 * @param $map
	 * @return bool
	 * author: xmy 280564871@qq.com
	 */
	public function sure_contract($map,$end_time){
		$data = $this->where($map)->find();
		if ($data['status'] != 1){
			return false;
		}else{
			$data['start_time'] = time();
			$data['end_time'] = strtotime($end_time);
			$data['status'] = 4;
			//确认合同后游戏上线
			$Game = new GameModel();
			$Game->setOnline($data['game_id']);
			return $this->save($data);
		}
	}
}