PraiseListController.class.php 3.54 KB
<?php
/**
 * 【销售活动-后台】获取点赞列表
 * User: WJY
 * Date: 2017-11-02
 */

namespace Apicp\Controller\Activity;

use Common\Common\User;
use Common\Service\ActivityService;
use Common\Service\LikeService;

class PraiseListController extends \Apicp\Controller\AbstractController
{

    // 类型为活动
    const TYPE_ACTIVITY = 1;

    /** @var LikeService */
    protected $like_s;
    /** @var User */
    protected $user_s;

    public function before_action($action = '')
    {

        if (!parent::before_action($action)) {
            return false;
        }
        // 实例化活动信息表
        $this->like_s = new LikeService();
        // 实例化用户表
        $this->user_s = new User();

        return true;
    }

    public function Index_post()
    {
        $params = array(
            'obj_id' => I('ac_id', 0, 'intval'),
            'page' => I('page', ActivityService::DEFAULT_PAGE, 'intval'),
            'limit' => I('limit', ActivityService::DEFAULT_LIMIT, 'intval'),
            'type' => self::TYPE_ACTIVITY,
        );

        $list = $this->get_like_list($params);

        $this->_result = $list;

        return true;
    }

    /**
     * 获取点赞列表
     *
     * @param array $params 查询参数
     *
     * @return array
     */
    public function get_like_list($params = [])
    {
        // 每页条数
        $limit = !isset($params['limit']) ? LikeService::DEFAULT_LIMIT : intval($params['limit']);
        $page = !isset($params['page']) ? LikeService::DEFAULT_PAGE : intval($params['page']);

        // 参数校验
        $obj_id = intval($params['obj_id']);
        $type = intval($params['type']);

        if (!in_array($type, LikeService::$levels)) {
            // 点赞类型不正确
            E('_ERR_LIKE_TYPE');
        }
        if (!$obj_id) {
            // 数据id不能为空
            E('_EMPTY_DATA_ID');
        }

        list($start, $limit, $page) = page_limit($page, $limit);

        // 分页参数
        $page_option = [$start, $limit];

        // 排序按照发布时间参数
        $order_option = ['created' => 'DESC'];

        // 组装搜索条件
        $conds = ['obj_id' => $obj_id, 'type' => $type];

        // 查询总条数
        $total = $this->like_s->count_by_conds($conds);

        $list = [];
        $user_list = [];

        if ($total > 0) {
            // 返回参数
            $fields = 'like_id,uid,created';

            // 列表和总数
            $list = $this->like_s->list_by_conds($conds, $page_option, $order_option, $fields);

            $uids = array_column($list, 'uid');

            // 查询用户集合
            $users = $this->user_s->listByUid($uids);

            $user_list = array_combine_by_key($users, 'memUid');
        }

        // 循环格式化列表数据
        $res_list = [];
        foreach ($list as $val) {
            $value = [];
            $user_info = $user_list[$val['uid']];
            if (empty($user_info)) {
                // 如果用户被删除
                $user_info = $this->user_s->getByUid($val['uid']);
            }

            $value['like_id'] = $val['like_id'];
            $value['uid'] = $val['uid'];
            $value['username'] = strval($user_info['memUsername']);
            $value['avatar'] = strval($user_info['memFace']);
            $value['created'] = $val['created'];

            $res_list[] = $value;
        }

        return [
            'page' => $page,
            'limit' => $limit,
            'total' => intval($total),
            'list' => $res_list,
        ];
    }
}