<?php
/** 点评配置
 * @Copyright (C) 2017 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author chang <13331202938@126.com>
 */
namespace Common\Model;
use Think\Model;
class LocationDpModel extends Model{
    protected $tableName = 'dp_group';
	public function _initialize(){
		parent::_initialize ();
    }
    /**获取点评配置
     * @param $param 不同分类的ID
     * @return array 返回状态、信息
     */ 
    public function get_dp_cfg($param=array("type"=>0,'goods_id'=>0)){
        $type = intval($param['type']);//类型
        $goods_id = intval($param['goods_id']);
        if($goods_id>0){
	        $goods_info=idtoname($goods_id,'Goods','*','id',false);
	        $dp_data=$this->get_dp_config($type);
        }
        return $dp_data;
    }
    /**获取点评标签数据
    * @param $type 类型
    * @return array 返回信息
    */
	public function get_dp_config($type){
		$map=array();
		$map['id'] = $type;
		
		//lxl 2018.7.21
		// $dp_data['point_group']=M('PointGroup')->where($map)->field(true)->find();
		
		$dp_data['dp_group']=M('DpGroup')->where($map)->field(true)->find();
		$dp_data['dp_group']['preset_list'] = preg_split("/[, ]/", $dp_data['dp_group']['preset']);
		foreach($dp_data['dp_group']['preset_list'] as $kk=>$vv){
			if(trim($vv)=="")unset($dp_data['dp_group']['preset_list'][$kk]);
		}
		return $dp_data;
	}
	
	/**
	 * 提交保存点评
	 * @param  $user_id 提交点评的会员
	 * @param  $param 参数
	 * @param  $content 点评文字内容
	 * @param  $dp_point 总评分
	 * @param  $dp_image 点评的图片数组 array("./public/...","./public/.....");
	 * @param  $tag_group 点评标签(二维数组)，格式如下
	 */
	public function save_review($uid,$param=array()){
		$return=array('status'=>0,'info'=>'');
		$score=$param['scores'];
		foreach($score as $k=>$v){if(!$v||$v==0){$score[$k]=5;}}
		$dp_img=$param['dp_image'];
		$content=$param['content'];
		//1,活动 2商品 3,门店 4,外卖 5团购 6,优惠劵 7,服务
		if($param['event_id']>0){//活动
			$type_id=$param['event_id'];
			$type=1;
			$model='Event';
		}elseif($param['goods_id']>0){//商品
			$type_id=$param['goods_id'];
			if($param['type'] == 1){
				$type = 4;
			}elseif($param['type'] == 2){
				$type = 7;
			}
			$model='Goods';
		}else{
			$return['info']='参数错误';return $return;
		}
		//添加评论表中的数据
		$data=array();
		$data['dp_point']=$score?$score:5;//总评
		$data['uid']=$uid;
		$data['type_id']=$type_id;
		$data['type']=$type;
		$data['order_id']=$param['order_id'];
		$data['shop_id']=idtoname($param['order_id'],'OrderInfo','shop_id');
		$data['content']=$content?$content:'系统默认评论';
		$data['status']=1;
		$data['dp_img']=$param['goods_img']?serialize($param['goods_img']):'';
		$data['addtime']=NOW_TIME;
		$dp_id=M("MemberDpList")->add($data);
		if($dp_id){
			$map=array();
			$map['id']=$type_id;

			// 更新点评数据
			M($model)->where($map)->setInc('dp_count',1);
			if($score[0]==1)M($model)->where($map)->setInc('dp_count_1',1);
			if($score[0]==2)M($model)->where($map)->setInc('dp_count_2',1);
			if($score[0]==3)M($model)->where($map)->setInc('dp_count_3',1);
			if($score[0]==4)M($model)->where($map)->setInc('dp_count_4',1);
			if($score[0]==5)M($model)->where($map)->setInc('dp_count_5',1);
			$return['status']=1;
			$return['info']='点评成功';
		}else{
			$return['info']='点评失败';return $return;
		}

			if($type>0 && $dp_id>0){
				$a = 'score_'.$param['type'];
				$data_point=array();
				$data_point['dp_id']=$dp_id;
				$data_point['uid']=$uid;
				$data_point['group_id']=$param['type'];
				$data_point['point']=$param[$a];
				$data_point['type_id']=$type_id;
				$data_point['type']=$type;
				M('MemberPointLink')->add($data_point);
			}

$tag_g = array();
$a = 'tag_'.$param['type'];
$tag_g = $param[$a];
		//更新标签表
		// foreach($tag_g as $k=>$v){
			// $group_id=$k;
			foreach($tag_g as $kk=>$vv){
				$data_tag=array();
				$data_tag['dp_id']=$dp_id;
				$data_tag['uid']=$uid;
				$data_tag['tag_name']=$vv;
				$data_tag['type_id']=$type_id;
				$data_tag['group_id']=$param['type'];
				$data_tag['type']=$type;
				M('MemberTagLink')->add($data_tag);
			}
		// }
		return $return;
	}
	//格式化点评数据
	public function format_dp($data){
		if(MODULE_NAME=='Shop'||MODULE_NAME=='Admin')$mode_name='Home';else $mode_name=MODULE_NAME;
		if($data['type']==1){//活动
			$data['name']=idtoname($data['type_id'],'Event','name');
			$data['url']=U($mode_name.'/Event/cont',array('id'=>$data['type_id']));
			$table_id='event_id';
		}if($data['type']==4){//外卖
			$data['name']=idtoname($data['type_id'],'Goods','goods_name');
			$type = 1;
			$table_id='goods_id';
		}if($data['type']==7){//服务
			$data['name']=idtoname($data['type_id'],'Goods','goods_name');
			$type = 2;
			$table_id='goods_id';
		}
		$param=array();
		$param[$table_id]=$data['type_id'];
		$param['type'] = $type;
		$cfg=$this->get_dp_cfg($param);

		//lxl 2018.7.21
		/*$map_p=array();
		$map_p['dp_id']=$data['id'];
		$map_p['group_id']=$cfg['point_group']['id'];
		$info=M('MemberPointLink')->where($map_p)->field(true)->find();
		$cfg['point_group']['dp_point']=$info['point'];
		$data['point_group']=$cfg['point_group'];*/

		foreach($cfg['dp_group']['preset_list'] as $kk=>$vv){
			$map_t=array();
			$map_t['dp_id']=$data['id'];
			$map_t['group_id']=$cfg['dp_group']['id'];
			$map_t['tag_name']=$vv;
			$is_check=M('MemberTagLink')->where($map_t)->field(true)->find();
			if($is_check){
				if($cfg['dp_group']['check_id'])
					$cfg['dp_group']['check_id'].=','.$kk;
				else
					$cfg['dp_group']['check_id'].=','.$kk;
			}
		}
		$data['dp_group']=$cfg['dp_group'];
		return $data;
	}
}
?>