<?php
class CouponModel extends BaseModel{
	public $msg = '';
	function getInfo($id){
		if(!$id||!is_numeric($id)){return false;}
		$info = $this->db->getRow("select * from qk_coupon where id='$id'");
		if(!$info){return false;}
		$info['typeInfo'] = $this->db->getRow("select * from qk_coupon_type where id='".$info['typeId']."'");
		$info['url'] = "/coupon/view/id/".$info['id'];
		load_class("attachment");
		$am = new AttachmentModel();
		$info['thumbnail']['url'] = $am->getUrl($info['thumbId']);
		return $info;
	}
	function add($params){
		return $this->query($params);
	}
	function edit($params){
		return $this->query($params);
	}
	function query($params){
		global $baseUrl;
			$act = $params['act'];
			$id = $params['id'];
			$name = $params['name'];
			$comId = $params['comId'];
			$typeId = $params['typeId'];
			$money = $params['money'];
			$discount = $params['discount'];
			$starttime = $params['starttime'];
			$endtime = $params['endtime'];
			$thumbId = $params['thumbId'];
			$content = $params['content'];
			$starttime = strtotime($starttime);
			$endtime = strtotime($endtime);
			if(!$name){$this->msg = '优惠券名称不能为空';return false;}
			if($seoUrl){if(substr($seoUrl,0,1)!="/"){$seoUrl = $baseUrl.$seoUrl;}}
			if(settype($comId,"integer")===false){$comId=0;}
			if(settype($thumbId,"integer")===false){$thumbId=0;}
			if(settype($typeId,"integer")===false){$typeId=0;}
			if(settype($sortId,"integer")===false){$sortId=50;}
			
			if($name){
				if($act=="edit"){
					$sql = "update qk_coupon set ";
					$sqlEnd = " where id='".$id."'";
				}else if($act=="add"){
					$sql = "select id from qk_coupon where name='$name' and comId='$comId' and starttime='$starttime' and endtime='$endtime' and money='$money' and discount='$discount' ";
					if($this->db->getOne($sql)){
						$this->msg='优惠券重复';
						return false;
					}
					$sql = "insert into qk_coupon set ";
					$sqlEnd = ",posttime=UNIX_TIMESTAMP()";
				}
				$sql .= " name='$name',comId='$comId',typeId='$typeId',thumbId='$thumbId',money='$money',content='$content',discount='$discount',starttime='$starttime',endtime='$endtime'";
				//$sql .= ",templateView='$templateView'";
				$sql .= $sqlEnd;
				if($this->db->query($sql)){
						if($act=="add"){$id = $this->db->insert_id();$this->msg='添加成功';}else{$this->msg='修改成功';}
						if($seoUrl){
						if(substr($seoUrl,0,1)!="/"){$seoUrl = $baseUrl.$seoUrl;}
						$url = $baseUrl."coupon/view/id/".$id;
						$sql = "select count(id) from qk_seo_url where seoUrl='".$seoUrl."'";
						if($this->db->getOne($sql)==0){
							$sql = "insert into qk_seo_url set url='$url',seoUrl='$seoUrl',posttime=UNIX_TIMESTAMP()";
						}else{
							$sql = "update qk_seo_url set seoUrl='$seoUrl' where url='$url'";
						}
						$this->db->query($sql);
					}
					if($act=="add"){return $id;}
					return true;
				}
			}
		return false;
	}

	function del($id){if($id && is_numeric($id)){return $this->db->query("delete from `qk_coupon` where id='$id'");}return false;}
	function delUserCoupon($id){if($id && is_numeric($id)){return $this->db->query("delete from `qk_user_coupon` where id='$id'");}return false;}
	function ajaxEdit($params){
		$id = $params['id'];
		$act = $params['act'];
		$col = $params['col'];
		$val = $params['val'];
		$oldval = $params['oldval'];
		$type = $params['type'];
		if($type=="date"){
			$val = strtotime($val);
		}
		if($id && is_numeric($id) && $col){
			$sql = "update `qk_coupon` set  $col='$val' where id='$id'";
			if($this->db->query($sql)){
				$this->msg = '修改成功';
				return true;
			}
		}
		$this->msg = '修改失败：参数有误';
		return false;
	}
	function ajaxColEditUserCoupon($param){
				$col = $param['col'];
				$val = $param['val'];
				$id = $param['id'];
				if($col && settype($id,"integer")){
					if($col=='iStatus' && $val!='0'){
						$info = $this->db->getRow("select * from qk_user_coupon where id='$id'");
						if(!$info){$this->msg='优惠券不存在';return false;}else if($info['useoftime']){$this->msg='该优惠券已经被用户使用';return false;}else if($info['endtime']>0 && $info['endtime']<time()){ $this->msg='该优惠券码已经过期，不能被启用';return false; }
					}
					$sql = "update `qk_user_coupon` set `$col`='$val' where id='".$id."'";
					if($this->db->query($sql)){
						return true;
					}
				}else{
					$this->msg = '参数不完整';
				}
		return false;
	}


	function addUserCoupon($param){$param['act']='add';return $this->queryUserCoupon($param);}
	function queryUserCoupon($param){
		$act = $param['act'];
		$number = $param['number'];
		$userId = $param['userId'];
		$endtime = $param['endtime'];
		$amount = $param['amount'];
		if(!$number || !is_numeric($number) || $number<=0){ $this->msg='发放数量需要为数字且为正数';return false; }
		if(!$userId){ $userId=0; }
		if($userId && !is_numeric($userId)){ $this->msg='用户ID为数字';return false; }
		if(!$endtime){$endtime=0;}
		if($endtime!==0){
			if(!($endtime=strtotime($endtime))){ $this->msg='过期日期格式不正确';return false; }
			if($endtime<=time()){ $this->msg='过期日期不得早于当前时间';return false; }
		}
		$couponId = $param['couponId'];
		if(!$couponId || !is_numeric($couponId) || $couponId<=0){ $this->msg='请选择优惠券';return false; }
		$money = $this->db->getOne("select money from qk_coupon where id='$couponId'");
		if(!$money){ $this->msg='优惠券不存在';return false;}
		if($userId!==0){
		$userId = $this->db->getOne("select id from qk_user where id='$userId'");
		if(!$userId){ $this->msg='用户不存在';return false;}
		}
		for($i==0;$i<$number;$i++){
			$code = randCoupon();
			$this->db->query("insert into qk_user_coupon set iStatus=1,endtime='$endtime',userId='$userId',number='$code',money='$money',couponId='$couponId',posttime=UNIX_TIMESTAMP()");
		}
			$this->msg='添加成功';return true;
	}

	function addType($param){
		$name = $param['name']; if(!$name){$this->msg='类型名不得为空';return false;}
		if($this->db->getOne("select id from qk_coupon_type where name='$name'")){ $this->msg='类型名已存在';return false;}
		$this->db->query("insert into qk_coupon_type set iStatus=1,name='$name',posttime=UNIX_TIMESTAMP()");
		$id = $this->db->insert_id();
		$this->msg='添加类型成功';
		return $id;
	}

}
?>