<?php
/**
 * 分类
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaol <1094186793@sina.cn>
 */
namespace Mobile\Model;
use Think\Model;
class DpModel extends Model{
    protected $tableName = 'member_dp_list';
	public function get_dp_goods($shop_id){
		$return['status'] = 0;$return['msg'] = '';
		if(empty($shop_id)){$return['msg'] = '参数获取错误';}
		//获取全部评论标签
		$dp_data['dp_group']=M('DpGroup')->field(true)->select();
		if($dp_data){
			foreach($dp_data['dp_group'] as $k=>$v){
				$dp_data['dp_group'][$k]['preset_list'] = preg_split("/[, ]/", $v['preset']);
				foreach($dp_data['dp_group'][$k]['preset_list'] as $kk=>$vv){
					if(trim($vv)=="")unset($dp_data['dp_group'][$k]['preset_list'][$kk]);
				}
			}
			$return['status'] = 1;
			$return['dp_group'] = $dp_data['dp_group'];
		}
		
		// 获取全部评论
		$dp_id = $g_map = array();
		$g_map['shop_id'] = $shop_id;
		$g_map['status'] = 1;
		$dp_list = M('MemberDpList')->where($g_map)->field(true)->select();
		if($dp_list){
			foreach($dp_list as $key => $value){
				$dp_id[] = $value['id'];
				$dp_list[$key]['img'] = idtoname($value['uid'],'Member','header_img');
				$dp_list[$key]['pl_img'] = unserialize($value['dp_img']);
				$dp_list[$key]['width'] = $value['dp_point']/5*100;
				$dp_list[$key]['username'] = idtoname($value['uid'],'Member','username');
			}
			$dp_sum = M('MemberDpList')->where($g_map)->sum('dp_point');
			$count_dp = count($dp_list);
			$avg_dp = round($dp_sum/($count_dp*5)*5,1);
			$sc_dp = round(($avg_dp/5)*100,1);
			$return['avg_dp'] = $avg_dp;
			$return['sc_dp'] = $sc_dp;
			$return['dp_id'] = $dp_id;
			$return['count_dp'] = $count_dp;
			$return['dp_list'] = $dp_list;
			$return['status'] = 2;
		}
		return $return;
	}
	public function get_dp_service($postdata,$goods_id,$is_all=1){
		$return['status'] = 0;$return['msg'] = '';
		if(empty($goods_id)){$return['msg'] = '参数获取错误';}
		//服务标签
		$param['type'] = 2;
		$param['goods_id'] = $goods_id;
		$data_pl = D('Common/LocationDp')->get_dp_cfg($param);
		if($data_pl){
			$return['status'] = 1;
			$return['dp_group'] = $data_pl['dp_group'];
		}
		
        //服务评论
		$dp_list = array();
		$return['show'] = 0;
        //商品评论列表
		$g_map = array();$g_map['type_id'] = $goods_id;$g_map['status'] = 1;
		$order = 'id desc';
		if($is_all == 1){
			$page_num = 10;
			$page=empty($postdata['page'])?1:$postdata['page'];
			$dp_list_arr = page_list('MemberDpList',$g_map,$order,true,$page_num,'',$page);
			if($dp_list_arr['list']){
				$dp_list = $dp_list_arr['list'];
				if($dp_list_arr['count'] > $page_num*$page){
					$return['show'] = 1;
				}
			}
		}else{
			$dp_list = M('MemberDpList')->where($g_map)->field(true)->order($order)->limit(1)->select();
		}
		if($dp_list){
			foreach($dp_list as $key => $value){
				$dp_list[$key]['img'] = idtoname($value['uid'],'Member','header_img');
                if($value['dp_img']){
                    $dp_list[$key]['pl_img'] = unserialize($value['dp_img']);
                }
				$dp_list[$key]['width'] = $value['dp_point']/5*100;
			}
			$dp_sum = M('MemberDpList')->where($g_map)->sum('dp_point');
			$count_dp = M('MemberDpList')->where($g_map)->count();
			$avg_dp = round($dp_sum/($count_dp*5)*5,1);
			$sc_dp = round(($avg_dp/5)*100,1);
			$return['avg_dp'] = $avg_dp;
			$return['sc_dp'] = $sc_dp;
			$return['count_dp'] = $count_dp;
			$return['status'] = 2;
		}else{
			$return['count_dp'] = 0;
		}
		$return['dp_list'] = $dp_list;
		return $return;
	}
	//获取单个商品的评论
	public function get_goods($postdata,$goods_id,$is_all=1){
		$return['status'] = 0;$return['msg'] = '';
		if(empty($goods_id)){$return['msg'] = '参数获取错误';}
		//商品标签
		$param['type'] = 1;
		$param['goods_id'] = $goods_id;
		$data_pl = D('Common/LocationDp')->get_dp_cfg($param);
		if($data_pl){
			$return['status'] = 1;
			$return['dp_group'] = $data_pl['dp_group'];
		}
		$dp_list = array();
		$return['show'] = 0;
        //商品评论列表
		$g_map = array();$g_map['type_id'] = $goods_id;$g_map['status'] = 1;
		$order = 'id desc';
		if($is_all == 1){
			$page_num = 10;
			$page=empty($postdata['page'])?1:$postdata['page'];
			if($postdata['type']==1)$g_map['dp_point']=array('in',array(4,5));
			else if($postdata['type']==2)$g_map['dp_point']=array('in',array(2,3));
			else if($postdata['type']==3)$g_map['dp_point']=array('in',array(0,1));
			$dp_list_arr = page_list('MemberDpList',$g_map,$order,true,$page_num,'',$page);
			if($dp_list_arr['list']){
				$dp_list = $dp_list_arr['list'];
				if($dp_list_arr['count'] > $page_num*$page){
					$return['show'] = 1;
				}
			}
		}else{
			$dp_list = M('MemberDpList')->where($g_map)->field(true)->order($order)->limit(2)->select();
		}
		if($dp_list){
			foreach($dp_list as $key => $value){
				// $dp_id[] = $value['id'];
				$dp_list[$key]['img'] = idtoname($value['uid'],'Member','header_img');
                if($value['dp_img']){
                    $dp_list[$key]['pl_img'] = unserialize($value['dp_img']);
                }
				$dp_list[$key]['width'] = $value['dp_point']/5*100;
			}
			$dp_sum = M('MemberDpList')->where($g_map)->sum('dp_point');
			$count_dp = M('MemberDpList')->where($g_map)->count();
			$avg_dp = round($dp_sum/($count_dp*5)*5,1);
			$sc_dp = round(($avg_dp/5)*100,1);
			$return['avg_dp'] = $avg_dp;
			$return['sc_dp'] = $sc_dp;
			// $return['dp_id'] = $dp_id;
			$return['count_dp'] = $count_dp;
			$return['dp_list'] = $dp_list;
			$return['status'] = 2;
		}
		return $return;
	}
	// 根据评论内容筛选
	// uid 会员id
	// type_id 商品id
	// tag_name 评论名字
	// group_id 1商品，2服务
	// type 4商品，7服务
	public function get_tag_link_list($postdata,$tag_name,$goods_id,$is_sp=1){
		$return = array();$return['status'] = 0;
		if(!$tag_name || !$goods_id){
			$return['msg'] = '参数错误';
			return $return;
		}
		$map=array();
		$map['a.tag_name']=$tag_name;
		$map['a.type_id']=$goods_id;
		if($is_sp == 1){
			$map['a.group_id']=1;
			$map['a.type']=4;
		}else{
			$map['a.group_id']=2;
			$map['a.type']=7;
		}
		$page_num = 10;
		$page=empty($postdata['page'])?1:$postdata['page'];
		$join = '__MEMBER_DP_LIST__ mdl on mdl.id = a.dp_id';
		$order = 'mdl.id desc';
		$field = 'mdl.*,a.tag_name';
		$list=page_list('MemberTagLink',$map,$order,$field,$page_num,$join,$page);
		$return['dp_list'] = array();
		$return['show'] = 0;
		if($list['list']){
			foreach($list['list'] as $key => $value){
				$dp_id[] = $value['id'];
				$list['list'][$key]['img'] = idtoname($value['uid'],'Member','header_img');
                if($value['dp_img']){
                    $list['list'][$key]['pl_img'] = unserialize($value['dp_img']);
                }
				$list['list'][$key]['width'] = $value['dp_point']/5*100;
			}
			if($list['count'] > $page_num*$page){
				$return['show'] = 1;
			}else{
				$return['show'] = 0;
			}
			$return['status'] = 1;
			$return['dp_list'] = $list['list'];
		}
		return $return;
	}
}