<?php
 /**
 *      [XCECP] (C) COLNET Inc.
 *      This is NOT a freeware, use is subject to license terms
 */
if(!defined("IN_QKWEB")){exit("Access Denied");}
class EventModel extends BaseModel{
	public $msg;
	function getGoodsInfo($id){
		if($id && intval($id)){
			$sql = "select * from qk_event_goods where id='$id'";
			$info = $this->db->getRow($sql);
			return $info;
		}
		return false;
	}
	function getInfo($id){
		if($id && intval($id)){
			$sql = "select * from qk_event where id='$id'";
			$info = $this->db->getRow($sql);
			return $info;
		}
		return false;
	}

	function editGoods($params){
		return $this->queryGoods($params);
	}
	function addGoods($params){
		return $this->queryGoods($params);
	}
	function queryGoods($params){
		$act = $params['act'];
		$id = $params['id'];
		$name = $params['name'];
		$eventId = $params['eventId'];
		$goodsId = $params['goodsId'];
		$iStatus = $params['iStatus'];
		$sortId = $params['sortId'];
		$thumbId = $params['thumbId'];
		if(!$eventId || !is_numeric($eventId)){$this->msg='活动ID不正确';return false;}
		if(!$goodsId || !is_numeric($goodsId)){$this->msg='商品ID不正确';return false;}
		$sql = "select id from qk_event where iStatus=1 and endtime<UNIX_TIMESTAMP() and id='$eventId'";
		if(!$this->db->getOne($sql)){$this->msg='活动不存在或已过期';return false;}
		$sql = "select id from qk_goods where iStatus=1 and inventory>0 and id='$goodsId'";
		if(!$this->db->getOne($sql)){$this->msg='商品不存在或无库存';return false;}

		if($act=="edit" && $id && intval($id)){
			$sql = "update qk_event_goods set ";
			$sqlEnd = " where id='$id'";
		}else if($act=="add"){
			$sql = "insert into qk_event_goods set ";
			$sqlEnd = ",posttime=UNIX_TIMESTAMP()";
		}
		$sql .= " name='$name',eventId='$eventId',goodsId='$goodsId',iStatus='$iStatus',sortId='$sortId',thumbId='$thumbId'";
		$sql .= $sqlEnd;
		if($this->db->query($sql)){
			if($act=='add'){$this->msg='添加成功';}else if($act=='edit'){$this->msg='修改成功';}
			return true;
		}
		$this->msg='操作失败';
		return false;
	}
	function edit($params){return $this->query($params);}
	function add($params){return $this->query($params);}
	function query($params){
		$act = $params['act'];
		$id = $params['id'];
		$name = $params['name'];
		$seoUrl = $params['seoUrl'];
		$starttime = $params['starttime'];
		$endtime = $params['endtime'];
		$template = $params['template'];
		$title = $params['title'];
		$keywords = $params['keywords'];
		$description = $params['description'];
		$starttime = strtotime($starttime);if(!$starttime){$starttime=0;}
		$endtime = strtotime($endtime);if(!$endtime){$endtime=0;}
		if($act=="edit" && $id && intval($id)){
			$sql = "update qk_event set ";
			$sqlEnd = " where id='$id'";
		}else if($act=="add"){
			$sql = "insert into qk_event set ";
			$sqlEnd = ",posttime=UNIX_TIMESTAMP()";
		}
		$sql .= " name='$name',template='$template',starttime='$starttime',endtime='$endtime',title='$title',keywords='$keywords',description='$description'";
		$sql .= $sqlEnd;
		if($this->db->query($sql)){
					$this->msg='活动修改成功';
					if($act=="add"){$this->msg='添加活动成功';$id = $this->db->insert_id();}$url = $GLOBALS['baseUrl']."event/view/id/".$id;
					if($seoUrl){load_class('seo');$sm=new SeoModel();if(!$sm->seoUrl($url,$seoUrl,$act)){$this->msg.= ', '.$sm->msg;}else{
						$this->db->query("update qk_event set seoUrl='$seoUrl' where id='$id'");
					}}
			return true;
		}
		return false;
	}

	function del($id){
		if($id && settype($id,"integer")){
			$sql = "delete from qk_event where id='$id'";
			if($this->db->query($sql)){
				$url = $GLOBALS['baseUrl']."event/view/id/".$id;$this->db->query("delete from qk_seo_url where url='$url'");
				$this->msg='删除成功';
				return true;
			}
		}
		$this->msg='删除失败';
		return false;
	}
	function delGoods($id){
		if($id && settype($id,"integer")){
			$sql = "delete from qk_event_goods where id='$id'";
			if($this->db->query($sql)){
				$this->msg='删除成功';
				return true;
			}
		}
		$this->msg='删除失败';
		return false;
	}
}
?>