<?php
class ComModel extends BaseModel{
	public $msg = '';
	function getInfo($id){
		if(is_array($id)){
			$param = $id;
			if(!($userId = $param['userId']) && !($id = $param['id'])){ return false; }
			if($userId){
				$id = $this->db->getOne("select id from qk_com where userId='$userId'");
			}
		}
		$sql = "select * from qk_com where id='$id'";
		$info = $this->db->getRow($sql);
		load_class("user");
		$um = new UserModel();
		$info['province'] = $this->db->getOne("select name from qk_district where id='".$info['provinceId']."'");
		$info['city'] = $this->db->getOne("select name from qk_district where id='".$info['cityId']."'");
		$info['url'] = "/com/view/id/".$info['id'];
		$info['url'] = "/com/".$info['id'].".html";
		$info['userInfo'] = $um->getInfo($info['userId']);
		load_class("attachment");
		$am = new AttachmentModel();
		$info['thumbnail']['url'] = $am->getUrl($info['thumbId']);
		$info['tags'] = unserialize($info['tag']);
		return $info;
	}
	function add($param){
		$param['act']="add";
		return $this->query($param);
	}

	function edit($param){
		$param['act']="edit";
		return $this->query($param);
	}

	function query($param){
		$id = $param['id'];
		$act = $param['act'];
		$name = $param['name'];
		$comName = $param['comName'];
		$comAddr = $param['comAddr'];
		$comZip = $param['comZip'];
		$comTel = $param['comTel'];
		$comFax = $param['comFax'];
		$comEmail = $param['comEmail'];
		$comQQ = $param['comQQ'];
		$comWebsite = $param['comWebsite'];
		$content = $param['content'];
		$comService = $param['comService'];
		$provinceId = $param['provinceId'];
		$cityId = $param['cityId'];
		$countyId = $param['countyId'];
		$thumbId = $param['thumbId'];
		$loginName = $param['loginName'];
		$userId = $param['userId'];
		if(!$userId || !is_numeric($userId)){
			if($loginName){
				$userId = $this->db->getOne("select id from qk_user where loginName='$loginName'");
			}
			if(!$userId){$userId=0;$this->msg = '会员账号不存在';return false;}
		}
		if($act=="add"){
			$sql = "insert into qk_com set ";
			$sqlEnd = ",posttime=UNIX_TIMESTAMP()";
		}else if($act=="edit"){
			if(!$id){$this->msg='商家不存在'; return false;}
			$sql = "update qk_com set ";
			$sqlEnd = " where id='$id'";
		}
		$sql .= " name='$name',comName='$comName',comAddr='$comAddr',comZip='$comZip',comTel='$comTel',comFax='$comFax',comEmail='$comEmail',comQQ='$comQQ',comWebsite='$comWebsite'";
		$sql .= ",content='$content',comService='$comService',provinceId='$provinceId',cityId='$cityId',countyId='$countyId',thumbId='$thumbId',userId='$userId'";
		$sql .= $sqlEnd;
		if($this->db->query($sql)){
			if($act=="add"){$this->msg = '添加成功';return $this->db->insert_id();}else if($act=="edit"){$this->msg = '修改成功';}
			return true;
		}

		return false;
	}
}
?>