<?php
namespace Admin\Model;

use Think\Model;


/**
 * 订单模型
 */
class OrderModel extends Model{
    /* 自动验证规则 */
    protected $_validate = array(
       
		);

    /* 自动完成规则 */
    protected $_auto = array(
        array('create_time',       'getCreateTime',         self::MODEL_INSERT,  'callback'),
    );

    /**
     * 构造函数
     * @param string $name 模型名称
     * @param string $tablePrefix 表前缀
     * @param mixed $connection 数据库连接信息
     */
    public function __construct($name = '', $tablePrefix = '', $connection = '') {
        /* 设置默认的表前缀 */
        $this->tablePrefix ='tab_';
        /* 执行构造方法 */
        parent::__construct($name, $tablePrefix, $connection);
    }

    /**
     * 订单列表
     * @param 	integer  	$p  		页码
     * @param 	array  		$map  	条件数组
     * @return 	array    	结果数据集
     * @author 	鹿文学 
     */
		public function lists($p=1,$map=array()) {
			$page = intval($p);
      $page = $page ? $page : 1; //默认显示第一页数据
						
						
			$data = $this->field('count(merchandise_id) as mid,GROUP_CONCAT(id) as ids')				
			
			->where(['is_refund'=>0,'pay_time'=>array('gt',0),'send_time'=>0])				
			
			->group('merchandise_id')->having('mid>1')->select();
									
									
			foreach($data as $k=>$v) {								
			
			$tempids = explode(',',$v['ids']);
			
			asort($tempids);
			
			$ids = array_slice($tempids,1);								
			
			$this->where(['id'=>array('in',$ids)])->setField('is_refund',2);						

			}			
			
			if(isset($_REQUEST['row'])) {$row = $_REQUEST['row'];}else{$row = 10;}
			
			$lists = $this->alias('o')->field('o.id,o.is_refund,o.order_number,o.seller_account,o.buyer_account,o.order_price,o.poundage,if(o.pay_status = 0 and o.pay_time<1 and timestampdiff(MINUTE,FROM_UNIXTIME(o.order_time,"%Y-%m-%d %H:%i"),now())>30,2,o.pay_status) as pay_status,o.order_time,o.pay_time,o.send_time,m.game_name,m.server_name,m.small_account,m.title,m.phone')
					->join('tab_merchandise as m on (m.id = o.merchandise_id)')
					->where($map)->page($page,$row)->order('o.id desc')->select();
			
			$count = $this->alias('o')->join('tab_merchandise as m on (m.id = o.merchandise_id)')->where($map)->count();
			
			$data['data'] = $lists;
			
			$page = set_pagination($count,$row);
			if($page) {$data['page']=$page;}
			
			return $data;
		}

    /**
     * 新增或更新一个订单
     * @param array  $data 手动传入的数据
     * @return boolean fasle 失败 , int  成功 返回完整的数据
     * @author 王贺 
     */
    public function update($data = null){
        /* 获取数据对象 */
        $data = $this->token(false)->create($data);
        if(empty($data)){
            return false;
        }     
        /* 添加或新增基础内容 */
        if(empty($data['id'])){ //新增数据
            $id = $this->add($data); //添加基础内容

            if(!$id){
                $this->error = '新增基础内容出错!';
                return false;
            }else{
                if(!isset($data['relation_game_id'])){
                    $relation=M('Game','tab_')->where(array('id'=>$id))->save(array('relation_game_id'=>$id));
                    if(!$relation){
                        $this->error('关联id添加失败');//游戏添加完成
                    }
                }
            }
        } else { //更新数据
            $status = $this->save(); //更新基础内容
            if(false === $status){
                $this->error = '更新基础内容出错!';
                return false;
            }
        }
         // 添加或新增扩展内容
        $logic = $this->logic('Set');
        $logic->checkModelAttr(5);
        if(!$logic->update($id)){
            if(isset($id)){ //新增失败,删除基础数据
                $this->delete($id);
            }
            $this->error = $logic->getError();
            return false;
        }
        return $data;
    }
    

    /**
     * 创建时间不写则取当前时间
     * @return int 时间戳
     * @author huajie <banhuajie@163.com>
     */
    protected function getCreateTime(){
        $create_time    =   I('post.create_time');
        return $create_time?strtotime($create_time):NOW_TIME;
    }
    protected function ratio_c(){
        $ratio=I('post.ratio');
        if((0<=$ratio)&&(100>=$ratio)){
            return true;
        }else{
            
            return false;
        }
    }
	
	
	
	
	
	
}