<?php
/**
 * 微信小程序支付模块
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <570035208@qq.com>
 */

namespace Wxsmall\Model;
use Think\Model;
class DpModel extends Model{
    protected $tableName = 'Goods';
    /**
     * //点评详情页
     * @param array $param 参数信息
     * @return array 点评列表
     */
    public function index($param){
        $return = array('status'=>3,'msg'=>'参数错误');
        $map = array();
        $param=I('param.');
        $order_id = $param['order_id'];
		$map['order_id'] = $order_id;
		$field='og.id,og.order_id,og.goods_id,og.goods_name,og.goods_number,og.goods_price,og.goods_attr,g.img,g.dp_count,g.avg_score,g.avg_point';
		$goods_info=M('OrderGoods')->alias('og')->where($map)->field($field)->join('LEFT JOIN __GOODS__ g on og.goods_id=g.id')->select();
		foreach($goods_info as $k=>$v){
			$goods_attr=unserialize($v['goods_attr']);
			$goods_info[$k]['goods_attr']=$goods_attr['name'];
			$goods_info[$k]['avg_point']=$v['avg_point']*100;
        }
        $return['data']['list'] = $goods_info;
	
		if($order_id){
            $map = array();
			$map['id'] = $order_id;
			$order_info = M('OrderInfo')->where($map)->field(true)->find();
			if($order_info['is_comment'] == 1){
                $return['msg'] = '已经评论过了！';
                return $return;
			}
            $map=array();
            $map['order_id']=$order_id;
			$order_goods = M('OrderGoods')->where($map)->getField('goods_id');
			if($order_info['type'] == 14){
				$param['type'] = 1;
			}elseif($order_info['type'] == 11){
				$param['type'] = 2;
			}
			$param['goods_id'] = $order_goods;
			
		}
        $data=D('Common/LocationDp')->get_dp_cfg($param);
        $return['data']['dp_group'] = $data['dp_group'];
        unset($return['msg']);
        $return['status'] = 1;
        return $return;
    }
    public function add_dp($param){
        $return = array('status'=>3,'msg'=>'参数错误');
        $content = json_decode(htmlspecialchars_decode($param['content']),true);
        $dp_group = json_decode(htmlspecialchars_decode($param['dp_group']),true);
        $scores = json_decode(htmlspecialchars_decode($param['scores']),true);
        $dp_img = json_decode(htmlspecialchars_decode($param['dp_img']),true);
        $return['dp_img']=$dp_img;
        $goods_id = json_decode(htmlspecialchars_decode($param['goods_id']),true);
        $order_id = $param['order_id'];
        $uid = $param['uid'];
        if(!empty($goods_id)){
            $return['status'] = 1;
            $return['msg'] = '点评成功！';
            foreach($goods_id as $k=>$v){
                $new_param = array();
                $new_param['goods_id'] = $v;
                $new_param['order_id'] = $order_id;
                $new_param['type'] = 1;//外卖商品类型
                foreach($dp_group as $kk=>$vv){
                    if($k==$kk){
                        $new_param['tag_'.$new_param['type']] = array_keys($vv);//评论标签
                    }
                }
                foreach($content as $kk=>$vv){
                    if($k==$kk){
                        $new_param['content'] = $vv=='undefined'?'':$vv;
                    }
                }
                foreach($dp_img as $kk=>$vv){
                    if($k==$kk){
                        $new_param['goods_img'] = $vv;
                    }
                }
                foreach($scores as $kk=>$vv){
                    if($k==$kk){
                        $new_param['scores'] = $vv;
                        $new_param['score_'.$new_param['type']] = $vv;continue;
                    }else{
                        $new_param['scores'] = 1;//最低总评为1
                    }
                }
                $data=D('Common/LocationDp')->save_review($uid,$new_param);
                if($data['status'] == 1){
                    $save = $map = array();
                    $save['is_comment'] = 1;
                    $map['id'] = $param['order_id'];
                    M('OrderInfo')->where($map)->save($save);
                }
            }
        }
        return $return;
	}
}