You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

475 lines
13 KiB
PHTML

5 years ago
<?php
namespace Mobile\Model;
use Think\Model;
/**
* 商品模型
*/
class MerchandiseModel extends Model{
/* 自动验证规则 */
protected $_validate = array(
array('title','require','标题不能为空',self::MUST_VALIDATE,'regex', self::MODEL_BOTH),
array('title', '6,20', '标题不能超过20个字符', self::MUST_VALIDATE, 'length', self::MODEL_BOTH),
array('content', '10,100', '描述不能超过100个字符', self::VALUE_VALIDATE, 'length', self::MODEL_BOTH),
//array('screenshot','require','游戏截图不能为空',self::MUST_VALIDATE,'regex', self::MODEL_BOTH),
array('price','require','售价不能为空',self::MUST_VALIDATE,'regex', self::MODEL_BOTH),
array('game_id','require','游戏不能为空',self::MUST_VALIDATE,'regex', self::MODEL_BOTH),
array('game_name','require','游戏不能为空',self::MUST_VALIDATE,'regex', self::MODEL_BOTH),
array('server_id','require','区服不能为空',self::MUST_VALIDATE,'regex', self::MODEL_BOTH),
array('server_name','require','区服不能为空',self::MUST_VALIDATE,'regex', self::MODEL_BOTH),
array('small_id','require','小号不能为空',self::MUST_VALIDATE,'regex', self::MODEL_BOTH),
array('small_account','require','小号名称不能为空',self::MUST_VALIDATE,'regex', self::MODEL_BOTH),
array('phone','require','电话不能为空',self::MUST_VALIDATE,'regex', self::MODEL_BOTH),
);
/* 自动完成规则 */
protected $_auto = array(
array('create_time', 'getCreateTime', self::MODEL_INSERT, 'callback'),
);
/**
* 构造函数
* @param string $name 模型名称
* @param string $tablePrefix 表前缀
* @param mixed $connection 数据库连接信息
*/
public function __construct($name = '', $tablePrefix = '', $connection = '') {
/* 设置默认的表前缀 */
$this->tablePrefix ='tab_';
/* 执行构造方法 */
parent::__construct($name, $tablePrefix, $connection);
}
/**
* 商品列表
* @param integer $p 页码
* @param array $map 条件数组
* @param string $fields 字段列表
* @param string $order 排序
* @param integer $row 每页数量
* @return array 结果数据集
* @author 鹿文学
*/
public function lists($p=1,$map=array(),$fields=true,$order='online_time desc',$row=10) {
$page = intval($p);
$page = $page ? $page : 1; //默认显示第一页数据
$lists = $this->alias('m')->field($fields)
->where($map)->page($page,$row)->order($order)->select();
$count = $this->alias('m')->where($map)->count();
if(is_array($lists)) {
$data['lists'] = $lists;$data['status']=1;
if($count > $row){
$data['total'] = ceil($count/$row);
} else {
$data['total']=1;
}
} else {
$data['lists']='';$data['total'] = 1;$data['status'] = 0;
}
$data['current'] = $page;
return $data;
}
/**
* 商品订单列表
* @param integer $p 页码
* @param array $map 条件数组
* @param string $fields 字段列表
* @param string $order 排序
* @param integer $row 每页数量
* @return array 结果数据集
* @author 鹿文学
*/
public function lists_and_order($p=1,$map=array(),$fields=true,$order='pay_time desc',$row=10) {
$page = intval($p);
$page = $page ? $page : 1; //默认显示第一页数据
$sql = $this->alias('m')->field($fields)
->join('tab_order as o on(o.merchandise_id=m.id) ','left')
->where($map)->order($order)->group('m.id')->select(false);
$lists = $this->table(' ('.$sql.') as a')->order('a.time desc')->select();
$result = $this->alias('m')->field('count(DISTINCT m.id) as count')->where($map)->join('tab_order as o on(o.merchandise_id=m.id) ','left')
->group('m.id')->select();
$count = $result['count'];
if(is_array($lists)) {
$data['lists'] = $lists;$data['status']=1;
if($count > $row){
$data['total'] = ceil($count/$row);
} else {
$data['total']=1;
}
} else {
$data['lists']='';$data['total'] = 1;$data['status'] = 0;
}
$data['current'] = $page;
return $data;
}
/**
* 新增或更新一个商品
* @param array $data 手动传入的数据
* @return boolean fasle 失败 int 成功 返回完整的数据
* @author 王贺
*/
public function update($data = null){
/* 获取数据对象 */
$data = $this->token(false)->create($data);
if(empty($data)){
return false;
}
/* 添加或新增基础内容 */
if(empty($data['id'])){ //新增数据
$id = $this->add($data); //添加基础内容
if(!$id){
$this->error = '新增基础内容出错!';
return false;
}else{
$data['id']=$id;
}
} else { //更新数据
$status = $this->save($data); //更新基础内容
if(false === $status){
$this->error = '更新基础内容出错!';
return false;
}
}
return $data;
}
/**
* 创建时间不写则取当前时间
* @return int 时间戳
* @author huajie <banhuajie@163.com>
*/
protected function getCreateTime(){
$create_time = I('post.create_time');
return $create_time?strtotime($create_time):NOW_TIME;
}
protected function ratio_c(){
$ratio=I('post.ratio');
if((0<=$ratio)&&(100>=$ratio)){
return true;
}else{
return false;
}
}
/**
* 获取商品总数
* @param array $map 条件数组
* @return integer 总数
* @author 鹿文学
*/
public function total($map) {
return $this->where($map)->count();
}
/**
* 商品详情
* @param integer $id 商品编号
* @return array 结果数据集
* @author 鹿文学
*/
public function detail($id=0) {
if(is_numeric($id) && $id > 0) {
$map['m.id'] = $id;
$field = 'm.id,m.title,m.content,m.phone,m.status,m.screenshot,m.online_time,m.server_id,m.server_name,m.price,m.day,m.accumulation,m.small_id,m.small_account,m.game_id,g.relation_game_id,g.game_name,g.game_size,g.game_type_name,g.sdk_version,g.down_port,g.add_game_address,g.ios_game_address';
$data = $this->merchandise_info($map,$field);
}
return $data;
}
/**
* 商品详情
* @param integer $id 商品编号
* @return array 结果数据集
* @author 鹿文学
*/
public function sale_detail($id=0) {
if(is_numeric($id) && $id > 0) {
$order = D('Order')->get_info_by_good($id);
if(is_array($order)) {
if($order['pay_time']>0) {$this->where(['id'=>$id])->setField(['status'=>4]);}
}
$map['m.id'] = $id;
$map['m.status']=['in',[1,3,4]];
$map['m.online_time'] = ['gt',0];
$data = $this->merchandise_info($map);
}
return $data;
}
/**
* 商品详情
* @param integer $id 商品编号
* @return array 结果数据集
* @author 鹿文学
*/
public function deal_detail($id=0) {
if(is_numeric($id) && $id > 0) {
$map['m.id'] = $id;
$map['m.status']=array('in',[1,4]);
$data = $this->merchandise_info($map);
}
return $data;
}
private function merchandise_info($map,$field='') {
if(empty($field)) {
$field = 'm.id,m.title,m.content,m.status,m.screenshot,m.online_time,m.server_name,m.price,m.day,m.accumulation,m.small_account,g.relation_game_id,m.game_id,g.game_name,g.game_size,g.game_type_name,g.sdk_version,g.down_port,g.add_game_address,g.ios_game_address';
}
$data = $this->alias('m')->field($field)
->join('tab_game as g on(g.id = m.game_id) ')
->where($map)->find();
if($data['price']) {
$price=$data['price'];
$data['price'] = preg_match('/^[1-9]\d*(\.(00)?)?$/',$price)?intval($price):$price;
}
return $data;
}
/**
* 商品信息
* @param integer $id 商品编号
* @return array 结果数据集
* @author 鹿文学
*/
public function info($id=0) {
if(is_numeric($id) && $id > 0) {
$map['m.id'] = $id;
$map['m.status'] = 3;
$map['m.online_time'] = ['gt',0];
$data = $this->alias('m')->field('m.id,m.title,m.price,m.game_name,m.screenshot')
->where($map)->find();
}
return $data;
}
/**
* 商品购买需要的信息
* @param integer $id 商品编号
* @return array 结果数据集
* @author 鹿文学
*/
public function buy_info($id=0) {
if(is_numeric($id) && $id > 0) {
$map['m.id'] = $id;
$map['m.status'] = 3;
$data = $this->alias('m')->field('m.id,m.price,m.small_id,m.small_account,m.user_id as seller_id,m.user_account as seller_account,m.game_id')
->where($map)->find();
}
return $data;
}
/**
* 商品信息
* @param integer $id 商品编号
* @return array 结果数据集
* @author 鹿文学
*/
public function record_info($id=0) {
if(is_numeric($id) && $id > 0) {
$map['m.id'] = $id;
$user = D('User')->getLoginInfo();
$field = 'if(m.status=4,1,m.status) as status,m.id,m.user_id,m.title,m.game_id,m.content,m.screenshot,m.online_time,m.over_time,m.server_name,m.price,m.day,m.accumulation,m.small_id,m.small_account,g.relation_game_id,g.game_name,g.game_size,g.game_type_name,g.sdk_version,g.down_port,g.add_game_address,g.ios_game_address';
$data = $this->merchandise_info($map,$field);
if($user['user_id'] != $data['user_id']) {$data['status']=6;}
$data['accumulation'] = M('Spend','tab_')->where(['pay_status'=>1,'small_id'=>$data['small_id']])->sum('pay_amount');
}
return $data;
}
/**
* 删除
* @param integer $id 商品编号
* @param integer $buyerstatus 是否买家状态
* @author 鹿文学
*/
public function del_record($id='',$buyerstatus='') {
if(is_numeric($id) && $id>0) {
if(!empty($buyerstatus)) {$data['buyer_status']=1;} else {$data['seller_status'] = 1;}
$res = $this->where(['id'=>$id])->save($data);
if($res) {
return true;
} else {
$this->error='删除失败';
return false;
}
} else {
$this->error='参数错误';
return false;
}
}
/**
* 下架
* @param integer $id 商品编号
* @author 鹿文学
*/
public function offline_record($id='') {
if(is_numeric($id) && $id>0) {
$res = $this->where(['id'=>$id])->save(['status'=>-1]);
if($res) {
$mer = $this->field('small_id,user_id')->where(['id'=>$id])->find();
M('User','tab_')->where(['id'=>$mer['small_id']])->save(['lock_status'=>1, 'puid'=>$mer['user_id'],'source_puid'=>0]);
$this->error='下架成功';
return true;
} else {
$this->error='下架失败';
return false;
}
} else {
$this->error='参数错误';
return false;
}
}
/**
* 改价
* @param integer $id 商品编号
* @param float $price 新价格
* @author 鹿文学
*/
public function change_price($id='',$price='') {
if(is_numeric($id) && $id>0 && is_numeric($price) && $price>0) {
$res = $this->where(['id'=>$id,'status'=>3])->save(['status'=>0,'price'=>$price]);
if($res) {
$this->error='改价成功';
return true;
} else {
$this->error='改价失败';
return false;
}
} else {
$this->error='参数错误';
return false;
}
}
/**
* 游戏相关商品
* @param integer $game_id 游戏编号
* @param integer $id 不包含的商品编号
* @param integer $row 限制条数
* @author 鹿文学
*/
public function relevant($game_id='',$id='',$row=1) {
if(is_numeric($game_id) && $game_id>0) {
$user = D('User')->getLoginInfo();
if(!is_array($user)) {return '';}
$map['status'] = 3;
$map['game_id']=$game_id;
empty($id) || $map['id']=array('not in',$id);
$lists = $this->field('id,title,game_name,user_id,online_time,price,screenshot')->where($map)->limit($row)->select();
$data = '';
foreach($lists as $k => $v) {
$data[$k]= array('title'=>$v['title'],'game_name'=>$v['game_name'],'online_time'=>date('Y-m-d H:i:s',$v['online_time']),'price'=>preg_match('/^[1-9]\d*(\.(00)?)?$/',$v['price'])?intval($v['price']):$v['price']);
$data[$k]['icon'] = '';
if(!empty($v['screenshot'])) {
$icon = explode(',',$v['screenshot']);
$data[$k]['icon'] = get_cover($icon[0],'path');
}
if($user['user_id']==$v['user_id']) {
$data[$k]['url'] = U('Trade/record_info',array('id'=>$v['id']));
} else {
$data[$k]['url'] = U('Trade/detail',array('id'=>$v['id']));
}
}
return $data;
} else {
return '';
}
}
}