Comment.php 6.21 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016-3-23
 * Time: 14:48
 */

namespace Home;

use LibModels\Web\Home\CommentData;
use WebPlugin\Helpers;
use WebPlugin\HelperSearch;

/**
 * 个人中心-我的评论
 */
class CommentModel
{
    /**
     * 我的评论列表页
     * @param $uid
     * @param $isComment
     * @param $page
     * @param $limit
     * @return array
     */
    public static function getCommentList($uid, $isComment, $page, $limit)
    {
        //转string值为bool值
        if ($isComment == 'Y') {
            $isComment = true;
        }elseif($isComment == 'N') {
            $isComment = false;
        }

        //调用接口获得评论列表数据
        $result = CommentData::getCommentListData($uid);

        $commentList = array();
        $pager = array();
        $commentList['isComment'] = $isComment;//标记当前列表展示状态(已评论、未评论)
        $goodsNum = '0';//初始化商品数量
        $commentList['goodsNum'] = '0';
        //接口返回成功,则处理数据,否则返回空
        if (isset($result['code']) && $result['code'] == 200 && !empty($result['data'])) {
            foreach ($result['data'] as $key=>$value) {
                foreach ($value['orderGoods'] as $k=>$v) {
                    //处理已评论的数据
                    if ($isComment && array_key_exists('comment', $v)) {
                        $commentList['orders'][$key]['orderNum'] = $value['orderCode'];
                        $commentList['orders'][$key]['orderTime'] = $value['createTime'];
                        $commentList['orders'][$key]['orderId'] = $value['orderId'];
                        $cnAlphabet = '';
                        if (!empty($v['cnAlphabet'])) {
                            $cnAlphabet = $v['cnAlphabet'];
                        }
                        $commentList['orders'][$key]['goods'][$k]['href'] = Helpers::getUrlBySkc($v['productId'], $v['goodsId'], $cnAlphabet);
                        $commentList['orders'][$key]['goods'][$k]['thumb'] = Helpers::getImageUrl($v['imageUrl'],100,100);
                        $commentList['orders'][$key]['goods'][$k]['name'] = isset($v['productName']) ? $v['productName'] : '';
                        $commentList['orders'][$key]['goods'][$k]['remark'] = $v['comment'];
                        $commentList['orders'][$key]['goods'][$k]['productSkn'] = $v['productSkn'];
                        $commentList['orders'][$key]['goods'][$k]['productId'] = $v['productId'];
                        $commentList['orders'][$key]['goods'][$k]['goodsId'] = $v['goodsId'];
                        $commentList['orders'][$key]['goods'][$k]['erpSkuId'] = $v['erpSkuId'];
                        $commentList['orders'][$key]['goods'] = array_values($commentList['orders'][$key]['goods']);
                        $goodsNum ++;
                    }
                    //处理未评论的数据
                    elseif (!$isComment && !array_key_exists('comment', $v)) {
                        $commentList['orders'][$key]['orderNum'] = $value['orderCode'];
                        $commentList['orders'][$key]['orderTime'] = $value['createTime'];
                        $commentList['orders'][$key]['orderId'] = $value['orderId'];
                        $cnAlphabet = '';
                        if (!empty($v['cnAlphabet'])) {
                            $cnAlphabet = $v['cnAlphabet'];
                        }
                        $commentList['orders'][$key]['goods'][$k]['href'] = Helpers::getUrlBySkc($v['productId'], $v['goodsId'], $cnAlphabet);
                        $commentList['orders'][$key]['goods'][$k]['thumb'] = Helpers::getImageUrl($v['imageUrl'],100,100);
                        $commentList['orders'][$key]['goods'][$k]['name'] = isset($v['productName']) ? $v['productName'] : '';
                        $commentList['orders'][$key]['goods'][$k]['productSkn'] = $v['productSkn'];
                        $commentList['orders'][$key]['goods'][$k]['productId'] = $v['productId'];
                        $commentList['orders'][$key]['goods'][$k]['goodsId'] = $v['goodsId'];
                        $commentList['orders'][$key]['goods'][$k]['erpSkuId'] = $v['erpSkuId'];
                        $commentList['orders'][$key]['goods'] = array_values($commentList['orders'][$key]['goods']);
                        $goodsNum ++;
                    }
                }

            }
            //转关联数组为索引数组
            $commentList['orders'] = isset($commentList['orders']) ? array_values($commentList['orders']) : array();
            $commentList['goodsNum'] = $goodsNum;

            $total = count($commentList['orders']);
            $totalPage = ceil($total/$limit);
            $begin = ($page - 1) * $limit; //每页第一个商品键名
            $commentList['orders'] = array_slice($commentList['orders'], $begin, $limit); //截取该页显示的商品列表
            $pager = self::setPager($page, $total, $totalPage, $limit);
        }else{
            $commentList['orders']['empty'] = $isComment ? '您还没有已评论的商品' : '您还没有未评论的商品';//没有数据返回空
        }


        //面包屑-左侧导航
        $path = UserModel::getCenterCrumb('我的评论');
        $leftNav = UserModel::getCenterLeftNav('我的评论', $uid);
        $data = array(
            'path'=>$path,
            'homeNav'=>$leftNav,
            'comment' => $commentList,
            'userThumb' => UserModel::getUserHeadImg($uid),
            'pager' => $pager
        );

        return $data;

    }


    /**
     * 设置分页数据
     * @param $page
     * @param $total
     * @param $totalPage
     * @param $limit
     * @return array
     */
    public static function setPager($page, $total, $totalPage, $limit)
    {
        $result = array();
        if (isset($page) && isset($total) && isset($totalPage)) {
            $result['count'] = $total;
            $result['curPage'] = $page;
            $result['totalPages'] = $totalPage;
            $result['pagerHtml'] = HelperSearch::pager($total, $limit);
        }
        return $result;
    }
}