HEX
Server: Apache
System: Linux 4485441ca2e2 6.8.0-1039-aws #41~22.04.1-Ubuntu SMP Thu Sep 11 11:03:07 UTC 2025 aarch64
User: (1000)
PHP: 8.2.24
Disabled: NONE
Upload Files
File: /var/www/html/wp-content/plugins/ganryu_shop/model/Item.php
<?php

require_once dirname(__FILE__).'/Abstract.php';

class ganryu_shop_model_Item extends ganryu_shop_model_Abstract{

	function addItem($data){
		global $wpdb;
		
		$tbl = $this->tablename('item');
		
		$sql = "
			insert into $tbl(
				itemcode,
				itemname,
				itemdetail,

				sozai,
				gensan,
				sizetabledata,
				image,
				images,

				point_rate,
				relation_item,
				price_min,
				price_max,
				subitem_ct,
				is_close,
				deleted,
				create_date,
				modify_date,
				
				deliv_cost_type,
				price_custom_text
			)
			values(
				:itemcode,
				:itemname,
				:itemdetail,

				:sozai,
				:gensan,
				:sizetabledata,
				:image,
				:images,

				:point_rate,
				:relation_item,
				0,
				0,
				0,
				0,
				0,
				now(),
				now(),

				:deliv_cost_type,
				:price_custom_text
			)
		";
		$bind = array(
			'itemcode'=>$data['itemcode'],
			'itemname'=>$data['itemname'],
			'itemdetail'=>$data['itemdetail'],
			'sozai'=>$data['sozai'],
			'gensan'=>$data['gensan'],
			'sizetabledata'=>$data['sizetabledata'],
			'image'=>$data['image']?$data['image']:null,
			'images'=>$data['images']?$data['images']:null,
			'point_rate'=>strlen($data['point_rate']>0)?$data['point_rate']:null,
			'relation_item'=>$data['relation_item'],
			'deliv_cost_type'=>$data['deliv_cost_type'],
			'price_custom_text'=>$data['price_custom_text'],
		);
		$res = $this->query($sql, $bind);
		if (!$res){
			return false;
		}
		
		return $this->lastId();
	}

	function updateItem($itemid, $data){
		global $wpdb;
		
		$tbl = $this->tablename('item');
		
		$sql = "
			update $tbl set
				itemcode = :itemcode,
				itemname = :itemname,
				itemdetail = :itemdetail,
				sozai = :sozai,
				gensan = :gensan,
				sizetabledata = :sizetabledata,
				image = :image,
				images = :images,
				point_rate = :point_rate,
				relation_item = :relation_item,
				modify_date = now(),
				deliv_cost_type = :deliv_cost_type,
				price_custom_text = :price_custom_text
			where
				itemid = :itemid
		";
		$bind = array(
			'itemcode'=>$data['itemcode'],
			'itemname'=>$data['itemname'],
			'itemdetail'=>$data['itemdetail'],
			'sozai'=>$data['sozai'],
			'gensan'=>$data['gensan'],
			'sizetabledata'=>$data['sizetabledata'],
			'image'=>$data['image']?$data['image']:null,
			'images'=>$data['images']?$data['images']:null,
			'point_rate'=>strlen($data['point_rate']>0)?$data['point_rate']:null,
			'relation_item'=>$data['relation_item'],
			'deliv_cost_type'=>$data['deliv_cost_type'],
			'price_custom_text'=>$data['price_custom_text'],
			'itemid'=>$itemid
		);
		$res = $this->query($sql, $bind);
		if (!$res){
			return false;
		}
		
		return true;
	}
	
	
	function updateItemPriceRange($itemid){
		$tbl = $this->tablename('item');
		$stbl = $this->tablename('subitem');
		
		$sql = "
			select
				min(subprice) min,
				max(subprice) max,
				count(*) ct
			from
				$stbl
			where
				itemid = :itemid
			and
				deleted = 0
		";
		$bind = array('itemid'=>$itemid);
		$res = $this->selectRow($sql, $bind);
		if ($res === false){
			return false;
		}
		$min = 0;
		$max = 0;
		$ct = 0;
		if ($res){
			$min = $res['min']+0;
			$max = $res['max']+0;
			$ct = $res['ct']+0;
		}
		
		$sql = "
			update $tbl set
				price_min = :min,
				price_max = :max,
				subitem_ct = :ct
			where
				itemid = :itemid
		";
		$bind = array('min'=>$min, 'max'=>$max, 'ct'=>$ct, 'itemid'=>$itemid);
		$res = $this->query($sql, $bind);
		if (!$res){
			return false;
		}
		
		return true;
		
	}

	
	function getAdminList($pg, $num, $data){
		global $wpdb;
		
		$w = array('deleted=0');
		$b = array();
		if (isset($data['wd']) && $data['wd']){
			$w[] = "itemname like :wd or itemdetail like :wd or keywords like :wd";
			$b['wd'] = '%'.$data['wd'].'%';
		}
		if (isset($data['iid']) && $data['iid']){
			$w[] = 'itemid = :iid';
			$b['iid'] = $data['iid'];
		}
		if (isset($data['icd']) && $data['icd']){
			$w[] = 'itemcode = :icd';
			$b['icd'] = $data['icd'];
		}
		if (isset($data['cls']) && preg_match('/^cl(\d+)$/', $data['cls'], $m)){
			$w[] = 'is_close = :is_close';
			$b['is_close'] = $m[1];
		}
		$ord = "iid_asc";
		if (isset($data['ord'])){
			$ord = $data['ord'];
		}
		$ords = "";
		switch($ord){
			case "iid_dsc":
				$ords = "itemid desc"; break;
			case "crd_asc":
				$ords = "create_date asc, itemid asc"; break;
			case "crd_dsc":
				$ords = "create_date desc, itemid desc"; break;
			case "mod_asc":
				$ords = "modify_date asc, itemid asc"; break;
			case "mod_dsc":
				$ords = "modify_date desc, itemid desc"; break;
			default:
				$ords = "itemid asc";
		}
	
	
		$wsql = "";
		if ($w){
			foreach ($w as $ww){
				if ($wsql) $wsql .= " and ";
				$wsql .= '('.$ww.')';
			}
			$wsql = " where ".$wsql;
		}
		
		
		$tbl = $this->tablename('item');
		$sql = "
			select
				count(*) ct
			from
				$tbl
			$wsql
		";
		$bind = $b;
		$res = $this->selectRow($sql, $bind);
		if ($res === false){
			return false;
		}
		$ct = $res['ct'];
		if ($ct == 0){
			return $this->searchResult0();
		}
		
		$sql = "
			select
				itemid,
				itemcode,
				itemname,
				price_min,
				price_max,
				is_close,
				image
			from
				$tbl
			$wsql
			order by
			$ords
		";
		$sql .= $this->searchLimit($pg, $num);
		
		$res = $this->selectAll($sql, $bind);
		if ($res === false){
			return false;
		}
		return $this->searchResult($res, $pg, $num, $ct);
	}


	function getItem($itemid){
		$tbl = $this->tablename('item');
		$sql = "
			select
				itemid,
				itemcode,
				itemname,
				itemdetail,
				price_min,
				price_max,
				sozai,
				gensan,
				sizetabledata,
				image,
				images,
				subitem_ct,
				point_rate,
				deleted,
				is_close,
				relation_item,
				deliv_cost_type,
				price_custom_text
			from
				$tbl
			where
				itemid = :itemid
		";
		$bind = array('itemid'=>$itemid);
		$res = $this->selectRow($sql, $bind);
		if ($res === false){
			return false;
		}
		return $res;
	}
	
	
	function getItemType($itemid, $ashash=false){
		$tbl = $this->tablename('item_type');
		
		$sql = "
			select
				itemtype
			from
				$tbl
			where
				itemid = :itemid
			and
				valid = 1
			order by itemtype asc
		";
		$bind = array('itemid'=>$itemid);
		$res = $this->selectAll($sql, $bind);
		if ($res === false){
			return false;
		}
		if ($ashash){
			return $res;
		}
		$typs = array();
		foreach ($res as $rd){
			$typs[] = $rd['itemtype'];
		}
		return $typs;
	}
	
	function updateItemType($itemid, $typelist){
		$tbl = $this->tablename('item_type');
		
		$sql = "
			select
				id,
				itemtype
			from
				$tbl
			where
				itemid = :itemid
		";
		$bind = array('itemid'=>$itemid);
		$res = $this->selectAll($sql, $bind);
		if ($res === false){
			return false;
		}
		$cur = $res;
		
		foreach ($typelist as $type){
			$cidx = -1;
			for($i = 0; $i < count($cur); $i++){
				if ($cur['itemtype'] == $type){
					$cidx = $i;
					break;
				}
			}
		
			if ($cidx == -1){
				$sql = "
					insert into $tbl(
						itemid,
						itemtype,
						valid
					)
					values(
						:itemid,
						:type,
						1
					)
				";
				$bind = array('itemid'=>$itemid, 'type'=>$type);
				$res = $this->query($sql, $bind);
				if (!$res){
					return false;
				}
			}
			else{
				$id = $cur[$cidx]['id'];
				$sql = "
					update $tbl set
						valid = 1
					where
						id = :id
				";
				$bind = array('id'=>$id);
				$res = $this->query($sql, $bind);
				if (!$res){
					return false;
				}
				
				array_splice($cur, $cidx, 1);
			}
		}
		
		if ($cur){
			$w = "";
			$b = array();
			$idx = 0;
			foreach ($cur as $cd){
				if ($w) $w .= ",";
				$w .= ":id$idx";
				$b["id$idx"] = $cd['id'];
				$idx++;
			}
			$sql = "
				update $tbl set
					valid = 0
				where
					id in ($w)
			";
			$bind = $b;
			$res = $this->query($sql, $bind);
			if (!$res){
				return false;
			}
			
		}
		return true;
	}
	
	
	function addItemImage($ss, $size, $file, $temporary=false){
		$tbl = $this->tablename('item_image');
		$sql = "
			select
				imageid
			from
				$tbl
			where
				valid = 0
			or
				(expire_date < now() and expire_date is not null)
		";
		$res = $this->selectRow($sql);
		if ($res === false){
			return false;
		}
		
		$expire = "null";
		if ($temporary){
			$expire = "adddate(now(), interval 1 hour)";
		}

		if (!$res){
			$sql = "
				insert into $tbl(
					session,
					size,
					data,
					expire_date,
					valid
				)
				values(
					:ss,
					:size,
					:data,
					$expire,
					1
				)
			";
			$bind = array('ss'=>$ss, 'size'=>$size, 'data'=>$file->binary());
			$res = $this->query($sql, $bind);
			if (!$res){
				return false;
			}
		}
		else{
			$imageid = $res['imageid'];
			$sql = "
				update $tbl set
					session = :ss,
					size = :size,
					data = :data,
					expire_date = $expire,
					valid = 1
				where
					imageid = :imageid
			";
			$bind = array('ss'=>$ss, 'size'=>$size, 'data'=>$file->binary(), 'imageid'=>$imageid);
			$res = $this->query($sql, $bind);
			if (!$res){
				return false;
			}
		}
		
		return true;
	}
	
	
	function addItemImageTemporary($file, $size){
		$ss = md5(uniqid(rand(10000,99999)));
		$res = $this->addItemImage($ss, $size, $file, true);
		if (!$res){
			return false;
		}
		
		return $ss;
	}
	
	function imageRelease($ss, $size){
		$tbl = $this->tablename('item_image');
		
		$sql = "
			update $tbl set
				expire_date = null
			where
				session = :ss
			and
				size = :size
		";
		$bind = array('ss'=>$ss, 'size'=>$size);
		$res = $this->query($sql, $bind);
		if (!$res){
			return false;
		}
		
		return true;
	}
	
	
	
	function deleteItemImage($ss, $size=null){
		$tbl = $this->tablename('item_image');
		$sql = "
			update $tbl set
				data = null,
				opt = null,
				expire_date = null,
				valid = 0
			where
				session = :ss
		";
		$bind = array('ss'=>$ss);
		if ($size){
			$sql .= "and size = :size";
			$bind['size'] = $size;
		}
		
		$res = $this->query($sql, $bind);
		if (!$res){
			return false;
		}
		
		return true;
	}
	
	
	function getItemImage($ss, $size, $withdata=false){
		$ssql = "";
		if ($withdata){
			$ssql = "data,";
		}
		$tbl = $this->tablename('item_image');
		$sql = "
			select
				$ssql
				opt,
				date_format(expire_date, '%Y%m%d%H%i%s') as expire_date
			from
				$tbl
			where
				session = :ss
			and
				size = :size
			and 
				(expire_date is null or expire_date >= now() )
			and
				valid = 1
		";
		$bind = array('ss'=>$ss, 'size'=>$size);
		$res = $this->selectRow($sql, $bind);
		if ($res === false){
			return false;
		}
		return $res;
	}
	
	
	function deleteItem($itemid){
		$tbl = $this->tablename('item');
		$sql = "
			update $tbl set
				deleted = 1
			where
				itemid = :itemid
		";
		$bind = array('itemid'=>$itemid);
		$res = $this->query($sql, $bind);
		if (!$res){
			return false;
		}
		return true;
	
	}


	function openItem($itemid, $isclose){
		$tbl = $this->tablename('item');
		$sql = "
			update $tbl set
				is_close = :is_close
			where
				itemid = :itemid
		";
		$bind = array('is_close'=>$isclose, 'itemid'=>$itemid);
		$res = $this->query($sql, $bind);
		if (!$res){
			return false;
		}
		return true;
	
	}
	
	
	function searchItem($pg, $num, $data=array()){
		$tbl = $this->tablename('item');
		$tptbl = $this->tablename('item_type');
		$wsql = "
			where
				is_close = 0 and deleted = 0
		";
		$b = array();
		$ord = "order by modify_date desc";
		if ($data['itp']){
			$wsql .= "and exists (select * from $tptbl tp where i.itemid = tp.itemid and itemtype = :itp and valid = 1)";
			$b['itp'] = $data['itp'];
		}
	
		$sql = "
			select count(*) ct from $tbl i $wsql
		";
		$bind = array_merge($b, array());
		$res = $this->selectRow($sql, $bind);
		if ($res === false){
			return false;
		}
		$ct = $res['ct'];
		if ($ct == 0){
			return $this->searchResult0();
		}
		$sql = "
			select
				itemid,
				itemcode,
				itemtype,
				itemname,
				itemdetail,
				image,
				price_max,
				price_min
			from
				$tbl i
			$wsql
			$ord
		";
		if ($num > 0){
			$sql .= $this->searchLimit($pg, $num);
		}
		$res = $this->selectAll($sql, $bind);
		if ($res === false){
			return false;
		}
		return $this->searchResult($res, $pg, $num, $ct);
	}
	
	function getCategorySetupLabels($itemid){
		$tbl = $this->tablename('item');
		$sql = "
			select
				category_setup
			from
				$tbl
			where
				itemid = :itemid
		";
		$bind = array('itemid'=>$itemid);
		$res = $this->selectRow($sql, $bind);
		if ($res === false){
			return false;
		}
		if (!$res){
			return null;
		}
		$labar = json_decode($res['category_setup'], true);
		return $labar;
	}	
	function getCategorySetup($itemid){
		
		$res = $this->getCategorySetupLabels($itemid);
		if ($res === false){
			return false;
		}
		$labar = $res;
		
		$tbl = $this->tablename('item_category');

		$reslist = array();

		$sql = "
			select
				cateid,
				catepos,
				ord,
				catelabel,
				image
			from
				$tbl
			where
				itemid = :itemid
			and
				valid = 1
			order by catepos, ord
		";
		$bind = array('itemid'=>$itemid);
		$res = $this->selectAll($sql, $bind);
		if ($res === false){
			return false;
		}
		
		foreach ($res as $rd){
			if (!array_key_exists($rd['catepos']-1, $reslist)){
				$reslist[$rd['catepos']-1] = array('datas'=>array(), 'lab'=>$labar[$rd['catepos']-1]);
			}
			$reslist[$rd['catepos']-1]['datas'][] = array(
				'id'=>$rd['cateid'],
				'label'=>$rd['catelabel'],
				'image'=>$rd['image']
			);
		}
		
		
		return $reslist;
	}
	
	function updateCategorySetup($itemid, $conflist){
		$tbl = $this->tablename('item_category');
		
		$sql = "
			select
				cateid,
				image
			from
				$tbl
			where
				itemid = :itemid
			and
				valid = 1
		";
		$bind = array('itemid'=>$itemid);
		$res = $this->selectAll($sql, $bind);
		if ($res === false){
			return false;
		}
		$ids = array();
		$imgs = array();
		foreach ($res as $rd){
			$ids[] = $rd['cateid'];
			if ($rd['image']){
				$imgs[] = $rd['image'];
			}
		}
		$stayids = array();
		$labs = array();
		
		$catepos = 1;

		foreach ($conflist as $cn){
			$labs[] = $cn['lab'];
			$ord = 1;
			foreach ($cn['datas'] as $dt){
				if ($dt['id']){
					if (!in_array($dt['id'], $ids)){
						$dt['id'] = null;
					}
				}
				if (!$dt['id']){
					$sql = "
						insert into $tbl(
							itemid,
							catepos,
							ord,
							catelabel,
							image,
							valid
						)
						values(
							:itemid,
							:catepos,
							:ord,
							:catelabel,
							:image,
							1
						)
					";
					$bind = array(
						'itemid'=>$itemid, 
						'catepos'=>$catepos, 
						'ord'=>$ord, 
						'catelabel'=>$dt['label'], 
						'image'=>$dt['image']
					);
					$res = $this->query($sql, $bind);
					if (!$res){
						return false;
					}
				}
				else{
					$sql = "
						update $tbl set
							catepos = :catepos,
							ord = :ord,
							catelabel = :catelabel,
							image = :image
						where
							cateid = :cateid
					";
					$bind = array(
						'catepos'=>$catepos, 
						'ord'=>$ord, 
						'catelabel'=>$dt['label'], 
						'image'=>$dt['image'],
						'cateid'=>$dt['id']
					);
					$res = $this->query($sql, $bind);
					if (!$res){
						return false;
					}
					$stayids[] = $dt['id'];
				}
				$ord++;
				if ($dt['image']){
					$pos = array_search($dt['image'], $imgs);
					if($pos !== false){
						array_splice($imgs, $pos, 1);
					}
				}
			}
			$catepos++;
			
		}
		
		$delids = array();
		foreach ($ids as $idval){
			if (in_array($idval, $stayids)) continue;
			$delids[] = $idval;
		}
		if ($delids){
			$sql ="
				update $tbl set
					valid = 0
				where
					cateid in (".join(',', $delids).")
			";
			$res = $this->query($sql);
			if (!$res){
				return false;
			}
		}
		
		$tbl = $this->tablename("item");
		$sql ="
			update $tbl set
				category_setup = :setup
			where
				itemid = :itemid
		";
		$bind = array('setup'=>json_encode($labs), 'itemid'=>$itemid);
		$res = $this->query($sql, $bind);
		if (!$res){
			return false;
		}
		
		return $imgs;
	
	}
	
	function getItemCategory($cateid){
		$tbl = $this->tablename('item_category');
		
		$sql = "
			select
				cateid,
				itemid,
				catepos,
				ord,
				catelabel,
				image
			from
				$tbl
			where
				cateid = :cateid
			and
				valid = 1
		";
		$bind = array('cateid'=>$cateid);
		$res = $this->selectRow($sql, $bind);
		if ($res === false){
			return false;
		}
		return $res;
	}
	
	
	function getSubitemList($itemid){
		$tbl = $this->tablename('subitem');
		$tblc = $this->tablename('item_category');
		$sql = "
			select
				subitemid,
				itemid,
				category1,
				category2,
				category3,
				subprice,
				stock_count,
				stock_nolimit,
				limit_count,
				is_close
			from
				$tbl
			where
				itemid = :itemid
			order by category1, category2, category3
		";
		$bind = array('itemid'=>$itemid);
		$res = $this->selectAll($sql, $bind);
		if ($res === false){
			return false;
		}
		return $res;
	}
	
	function generateCategoryConfig($catelist, $subitemlist){
		$reslist = array();
		if (count($catelist) == 0){
			$reslist[] = array('idtxt'=>'0_0', 'labels'=>array(), 'image'=>'',  'ids'=>array(0,0));
		}
		else if (count($catelist) == 1){
			foreach ($catelist[0]['datas'] as $cate){
				$reslist[] = array('idtxt'=>$cate['id'].'_0', 'labels'=>array($cate['label']), 'image'=>$cate['image'], 'ids'=>array($cate['id'],0));
			}
		}
		else {
			foreach ($catelist[0]['datas'] as $cate0){
				foreach ($catelist[1]['datas'] as $cate1){
					$reslist[] = array('idtxt'=>$cate0['id'].'_'.$cate1['id'], 'labels'=>array($cate0['label'],$cate1['label']), 'image'=>$cate0['image'],'ids'=>array($cate0['id'],$cate1['id']));
				}
			}
		}
		
		for($i = 0; $i < count($reslist); $i++){
			$itm = null;
			foreach ($subitemlist as $subitm){
				if (
					$subitm['category1'] == $reslist[$i]['ids'][0] &&
					$subitm['category2'] == $reslist[$i]['ids'][1]
				){
					$itm = $subitm;
					break;
				}
			}
			$reslist[$i]['subitem'] = $itm;
		}
		
		return $reslist;	
	}
	
	
	function updateSubitemList($itemid, $subitemlist){
		$tbl = $this->tablename('subitem');
		
		$sql = "
			select
				subitemid,
				category1,
				category2
			from
				$tbl
			where
				itemid = :itemid
			and
				deleted = 0
		";
		$bind = array('itemid'=>$itemid);
		$res=  $this->selectAll($sql, $bind);
		if ($res === false){
			return false;
		}
		$curlist = $res;
		
		$stayids = array();
		
		foreach ($subitemlist as $subitem){
			$baseitem = null;
			foreach ($curlist as $cur){
				if (
					$cur['category1'] == $subitem['conf']['ids'][0] &&
					$cur['category2'] == $subitem['conf']['ids'][1]
				){
					$baseitem = $cur;
					break;
				}
			}
			if (!$baseitem){
				$sql = "
					insert into $tbl(
						itemid,
						category1,
						category2,
						category3,
						subprice,
						stock_count,
						stock_nolimit,
						limit_count,
						is_close,
						deleted,
						create_date,
						modify_date
					)
					values(
						:itemid,
						:category1,
						:category2,
						0,
						:subprice,
						:stock_count,
						:stock_nolimit,
						:limit_count,
						0,
						0,
						now(),
						now()
					)
				";
				$bind = array(
					'itemid'=>$itemid,
					'category1'=>$subitem['conf']['ids'][0],
					'category2'=>$subitem['conf']['ids'][1],
					'subprice'=>$subitem['subprice'],
					'stock_count'=>$subitem['stock_count'],
					'stock_nolimit'=>$subitem['stock_nolimit'],
					'limit_count'=>$subitem['limit_count']?$subitem['limit_count']:null
				);
				$res = $this->query($sql, $bind);
				if (!$res){
					return false;
				}
			}
			else{
				$sql = "
					update $tbl set
						subprice = :subprice,
						stock_count = :stock_count,
						stock_nolimit = :stock_nolimit,
						limit_count = :limit_count,
						modify_date = now()
					where
						subitemid = :id
				";
				$bind = array(
					'subprice'=>$subitem['subprice'],
					'stock_count'=>$subitem['stock_count'],
					'stock_nolimit'=>$subitem['stock_nolimit'],
					'limit_count'=>$subitem['limit_count']?$subitem['limit_count']:null,
					'id'=>$baseitem['subitemid']
				);
				$res = $this->query($sql, $bind);
				if (!$res){
					return false;
				}
				$stayids[] = $baseitem['subitemid'];
			}
		}
		
		
		$delids = array();
		foreach ($curlist as $cur){
			if (in_array($cur['subitemid'], $stayids)){
				continue;
			}
			$delids[] = $cur['subitemid'];
		}
		
		
		if ($delids){
			$sql = "
				update $tbl set
					deleted = 1
				where
					subitemid in (".join(',', $delids).")
			";
			$res = $this->query($sql);
			if (!$res){
				return false;
			}
		}
		
		$res = $this->updateItemPriceRange($itemid);
		if (!$res){
			return;
		}
		
		return true;
	}
	
	
	function getSubitemCount($itemid){
		$tbl = $this->tablename('subitem');
		$sql = "
			select
				count(*) ct
			from
				$tbl
			where
				itemid = :itemid
			and
				deleted = 0
		";
		$bind = array('itemid'=>$itemid);
		$res = $this->selectRow($sql, $bind);
		if ($res === false){
			return false;
		}
		return $res['ct'];
	}
	
	
	function getSubitem($subitemid){
		$tbl = $this->tablename('subitem');
		$sql = "
			select
				subitemid,
				itemid,
				category1,
				category2,
				category3,
				subprice,
				stock_count,
				stock_nolimit,
				limit_count,
				is_close,
				deleted
			from
				$tbl
			where
				subitemid = :id
			and
				deleted = 0
		";
		$bind = array('id'=>$subitemid);
		$res = $this->selectRow($sql, $bind);
		if ($res === false){
			return false;
		}
		return $res;
	}
	
	
	function getItemFav($userid, $itemid){
		$tbl = $this->tablename('user_item_fav');
		$sql = "
			select
				id
			from
				$tbl
			where
				userid = :userid
			and
				itemid = :itemid
			and
				valid = 1
		";
		$bind = array('userid'=>$userid, 'itemid'=>$itemid);
		$res = $this->selectRow($sql, $bind);
		if ($res === false){
			return false;
		}
		return $res?1:0;
	}
	
	function setItemFav($userid, $itemid, $fav){
		$tbl = $this->tablename('user_item_fav');
		
		$sql = "
			select
				id
			from
				$tbl
			where
				userid = :userid
			and
				itemid = :itemid
		";
		$bind = array('userid'=>$userid, 'itemid'=>$itemid);
		$res = $this->selectRow($sql, $bind);
		if ($res === false){
			return false;
		}
		
		if (!$res){
			if (!$fav){
				return true;
			}
			$sql = "
				insert into $tbl(
					userid,
					itemid,
					valid,
					create_date
				)
				values(
					:userid,
					:itemid,
					1,
					now()
				)
			";
			$bind = array('userid'=>$userid, 'itemid'=>$itemid);
			$res = $this->query($sql, $bind);
			if ($res === false){
				return false;
			}
		}
		else{
			$updid = $res['id'];
			$sql = "
				update $tbl set
					valid = :valid,
					create_date = now()
				where
					id = :id
			";
			$bind = array('id'=>$updid, 'valid'=>$fav?1:0);
			$res = $this->query($sql, $bind);
			if (!$res){
				return false;
			}
		}

		return true;
	}
	
	
	function getFavList($userid, $pg, $num){
		$ftbl = $this->tablename('user_item_fav');
		$itbl = $this->tablename('item');
		
		$sql = "
			select
				count(*) ct
			from
				$ftbl f, $itbl i
			where
				f.itemid = i.itemid
			and
				f.userid = :userid
			and
				f.valid = 1
		";
		$bind = array('userid'=>$userid);
		$res = $this->selectRow($sql, $bind);
		if ($res === false){
			return false;
		}
		$ct = 0;
		if ($res) $ct = $res['ct']+0;
		if ($ct == 0){
			return $this->searchResult0();
		}
		
		$sql = "
			select
				i.itemid,
				i.itemname,
				i.price_min,
				i.price_max,
				i.image
			from
				$ftbl f, $itbl i
			where
				f.itemid = i.itemid
			and
				f.userid = :userid
			and
				f.valid = 1
			order by 
				f.create_date desc
		";
		$sql .= $this->searchLimit($pg, $num);
		$bind = array('userid'=>$userid);
		$res = $this->selectAll($sql, $bind);
		if ($res === false){
			return false;
		}
		return $this->searchResult($res, $pg, $num, $ct);
	}

	function getItemCountWithType($type){
		$tbl = $this->tablename('item_type');
		$sql = "
			select
				count(*) ct
			from
				$tbl
			where
				itemtype = :type
			and
				valid = 1
		";
		$bind = array('type'=>$type);
		$res = $this->selectRow($sql, $bind);
		if ($res === false){
			return false;
		}
		return $res?$res['ct']+0:0;
	}
	
	
	function minusStock($subitemid, $num){
		$tbl = $this->tablename('subitem');
		$res = $this->getSubitem($subitemid);
		if (!$res){
			return false;
		}
		$sql = "
			update $tbl set
				stock_count = stock_count - :num
			where
				subitemid = :subitemid
		";
		$bind = array('subitemid'=>$subitemid, 'num'=>$num);
		$res = $this->query($sql, $bind);
		if (!$res){
			return false;
		}
		return true;
	}
}