<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//生肖促销Model
class Coupon_model extends CI_Model {
	
	function __construct()
    {
        parent::__construct();
        $this->HT = $this->load->database('HT', TRUE);
    }
	
	//获取订单列表
	function getlist($ctype=FALSE,$page=2,$offset=10)
	{
		if(!$ctype)
		{
			return array();	
		}
		$tmpcount=$page*$offset;
		$tmpcounts=($page-1)*$offset;
		/*
		if($page==1){
			$sql="SELECT TOP {$offset} * FROM zodiac_coupon id ORDER BY ID DESC";
		}else{
			$sql="SELECT * FROM(
					SELECT TOP {$offset} * FROM (
						SELECT TOP {$tmpcounts} * FROM zodiac_coupon ORDER BY ID DESC) 
							AS TEMP1 ORDER BY ID ASC) AS TEMP2
						";
		}
		*/
		$sql=" SELECT TEMP1.* FROM(
					SELECT TOP {$tmpcount} ROW_NUMBER() OVER(ORDER BY id DESC) AS ROWID,* 
					FROM zodiac_coupon WHERE site_code='CHT' AND ctype= '{$ctype}') AS TEMP1 
					WHERE ROWID>$tmpcounts ";	
		//$sql = "SELECT * FROM zodiac_coupon ORDER BY id DESC";
		$query = $this->HT->query($sql);
		return $query->result();
	}
	
	//订单统计
	function get_count($ctype = FALSE,$type='all')
	{
		if(!$ctype)
		{
			return 0;	
		}
		$where = " WHERE ctype = '{$ctype}'";
		switch($type)
		{
			//已使用
			case 'used':
				$where .= " AND is_use = 1 ";
				break;
			//已失效
			case 'invalid':
				$where .= " AND is_valid = 0";
				break;
			default:
				$where .= '';
		}
		$sql = "SELECT COUNT(*) as counts FROM zodiac_coupon ".$where;
		$query = $this->HT->query($sql);
		return $query->row()->counts;
	}
	
	//状态更新
	function zodiac_updatestatu($id=-1,$statu=0,$field){
		is_numeric($id) ? $id : -1;
		is_numeric($statu) ? $statu : 0;
		$data = array(
		   $field => $statu,
		);
		$this->HT->where('id', $id);
		$this->HT->update('zodiac_coupon', $data); 	
	}
	
	//删除
	function zodiac_dodel($id=-1)
	{
		is_numeric($id) ? $id : -1;
		$this->HT->where('id', $id);
		$this->HT->delete('zodiac_coupon'); 	
	}
	
	//搜索
	function get_zodiac_search($kwd='')
	{
		if($kwd=='')
		{
			$sql = "SELECT TOP 10 * FROM zodiac_coupon ORDER BY id DESC";
		}else{
			if(strpos($kwd,'@'))
			{
				//使用Email查询
				$sql = "SELECT * FROM zodiac_coupon WHERE useremail like '%{$kwd}%' "; 	
			}else{
				//使用姓名查询
				$sql = "SELECT * FROM zodiac_coupon WHERE username like '%{$kwd}%' ";	
			}
		}
		$query = $this->HT->query($sql);
		return $query->result();
	}
}