Consult.php 3.65 KB
<?php

namespace Home;

use LibModels\Web\Home\ConsultData;
use LibModels\Web\Product\SearchData;
use WebPlugin\Helpers;
use WebPlugin\HelperSearch;
use WebPlugin\Images;


/**
 * 个人中心-我的咨询相关业务逻辑处理
 */
class ConsultModel
{

    public static function ConsultList($uid , $page, $limit = 10)
    {
        $result = array('consults' => array(), 'pager' => array());
        $skn = array();
        $consult = ConsultData::getConsult($uid, $page, $limit);
        if (isset($consult['code']) && $consult['code'] ==200 && isset($consult['data']['consult_list']) && !empty($consult['data']['consult_list'])) {
            foreach ($consult['data']['consult_list'] as $key => $val) {
                $skn[] = $val['skn'];
            }
            $goodsInfo = self::getProductGoodsInfo($skn);
            foreach ($consult['data']['consult_list'] as $key => $val) {
                if (!isset($goodsInfo[$val['skn']])) {
                    continue;
                }
                if ($val['answer']) {
                    $result['consults'][$key]['reply'] = $val['answer'];
                }
                $result['consults'][$key]['href'] = Helpers::getUrlBySkc($val['productId'], $goodsInfo[$val['skn']]['goods_id'], $goodsInfo[$val['skn']]['cn_alphabet']);;
                $result['consults'][$key]['thumb'] = Images::getImageUrl($goodsInfo[$val['skn']]['images_url'], 60, 60, 1);
                $result['consults'][$key]['name'] = $val['productName'];
                $result['consults'][$key]['question'] = $val['ask'] ? $val['ask'] : '';
                $result['consults'][$key]['consultTime'] = date('Y-m-d H:i:s', $val['askTime']);
            }
            //分页
            $result['pager']['hasCheckAll'] = false;
            $result['pager']['count'] = $consult['data']['total'];
            $result['pager']['curPage'] = $consult['data']['page'];
            $result['pager']['totalPages'] = $consult['data']['page_total'];
            $result['pager']['pagerHtml'] = HelperSearch::pager($consult['data']['total'], $limit);
        }
        if (empty($result['consults'])) {
            $result['consults']['empty'] = '您尚未咨询任何内容';
        }
        return $result;
    }

    /**
     * 查询多个skn的默认商品
     * @param $skn array
     * @return array
     */
    public static function getProductGoodsInfo($skn)
    {
        $result = array();
        $param['query'] = implode(',', $skn);
        $data = SearchData::searchAll($param);
        if (isset($data['data']['product_list']) && !empty($data['data']['product_list'])) {
            foreach ($data['data']['product_list'] as $key => $val) {
                $defaultGoods = array();
                $backGoods = array();
                foreach ($val['goods_list'] as $k => $v) {
                    do {
                        $backGoods['goods_id'] = $v['goods_id'];
                        $backGoods['images_url'] = $v['images_url'];
                        $backGoods['cn_alphabet'] = $val['cn_alphabet'];
                    } while(false);
                    if ($v['is_default'] == 'Y') {
                        $defaultGoods['goods_id'] = $v['goods_id'];
                        $defaultGoods['images_url'] = $v['images_url'];
                        $defaultGoods['cn_alphabet'] = $val['cn_alphabet'];

                    }
                    if (!empty($defaultGoods)) {
                        break;
                    }
                }
                $result[$val['product_skn']] = $defaultGoods ? $defaultGoods : $backGoods;
            }
        }
        return $result;
    }

}