<?php
 /**
 *      [QKSHOP] (C) QIKEWEB Inc.
 *      This is NOT a freeware, use is subject to license terms
 */

class CartModel extends BaseModel{
	function getAmount(){
		load_class("goods");
		$gm = new GoodsModel();
		$userId = $_SESSION['userId'];
		$sessoinId = session_id();
		$sql = "select * from qk_shoppingcart where userId='$userId' or sessionId='$sessionId'";
		$res = $this->db->getAll($sql);
		foreach($res as $item){
			$price = $gm->getPrice($item['goodsId'])*$pnum;
		}
	}
	
	function add($params){
			$goodsId = $params['goodsId'];
			$pnum =  $params['pnum'];
			$prizeId =  $params['prizeId'];
			if(!$prizeId || !settype($prizeId,"integer")){$prizeId=0;}
			if($pnum && $goodsId && settype($pnum,"integer") && settype($goodsId,"integer")){
				$sql = "select id from qk_shoppingcart where (userId='$userId' or sessionId='".session_id()."') and goodsId='$goodsId'";
				$cartId = $this->db->getOne($sql);
				if($cartId){
					if($prizeId){
						$sql = "update qk_shoppingcart set pnum=pnum+".$pnum.",prizeId='$prizeId' where id='$cartId'";
						$this->db->query($sql);
					}
				}else{
					$sql = "insert into qk_shoppingcart set userId='$userId',sessionId='".session_id()."',pnum='$pnum',goodsId='$goodsId',prizeId='$prizeId',posttime=UNIX_TIMESTAMP()";
					$this->db->query($sql);
				}
				if($prizeId){
					$sql = "update qk_user_prize set iStatus=1 where id='$prizeId'";
					$this->db->query($sql);
				}
				$sql = "";
				return true;
			}
			return false;
	}
}

?>