ListController.class.php 3.9 KB
<?php
/**
 * Created by PhpStorm.
 * User: zhonglei
 * Date: 18/4/24
 * Time: 11:57
 */
namespace Rpc\Controller\OtherClass;

use Com\PackageValidate;
use Common\Service\BaseinfoService;
use Common\Service\ClassifyService;

class ListController extends AbstractController
{
    /**
     * List
     * @author tangxingguo
     * @desc 获取课程列表接口
     * @param Int page:1 页码
     * @param Int limit:20 每页记录数
     * @param Int class_id 分类ID
     * @param String keyword 标题关键字
     * @return array
                    array(
                        'page' => 1, // 页码
                        'limit' => 20, // 每页记录数
                        'total' => 200, // 记录总数
                        'list' => array(
                            array(
                                'app_data_id' => '8852ce5784c43e6dddd942ae12d78fad', // 数据ID
                                'title' => '冰糖雪梨', // 标题
                                'class_name' => '企业文化', // 分类名称
                                'author' => '张三', // 作者
                                'status' => '已发布', // 状态
                            ),
                        ),
                    )
     */
    public function Index()
    {
        $postData = $this->get_arguments();

        // 验证规则
        $rules = [
            'page' => 'integer',
            'limit' => 'integer',
            'class_id' => 'integer',
            'keyword' => 'max:64',
            'app_data_ids' => 'array'
        ];

        // 验证请求数据
        $validate = new PackageValidate();
        $validate->postData = $postData;
        $validate->validateParams($rules);

        // 分页
        $postData['page'] = isset($postData['page']) ? $postData['page'] : BaseinfoService::DEFAULT_PAGE_NUM;
        $postData['limit'] = isset($postData['limit']) ? $postData['limit'] : BaseinfoService::DEFAULT_PAGE_LIMIT;
        list($start, $perpage) = page_limit($postData['page'], $postData['limit']);

        // 条件
        $conds = [
            'qu_type' => BaseinfoService::OTHER_TYPE,
            'release_status' => BaseinfoService::PUBLISH_STATUS,
        ];
        if (strlen($postData['keyword']) > 0) {
            $conds['title like ?'] = '%' . $postData['keyword'] . '%';
        }
        if (isset($postData['class_id']) && $postData['class_id'] != 0) {
            $conds['qc_id'] = $postData['class_id'];
        }
        if (!empty($postData['app_data_ids'])) {
            $conds['qu_id NOT IN (?)'] = $postData['app_data_ids'];
        }

        // 排序条件
        $order_option = ['qu_id' => 'desc'];

        // 调研列表
        $list = [];
        $baseInfoService = new BaseinfoService();
        $baseInfoList = $baseInfoService->list_by_conds($conds, [$start, $perpage], $order_option);

        if (!empty($baseInfoList)) {
            // 分类数据
            $classIds = array_column($baseInfoList, 'qc_id');
            $classService = new ClassifyService();
            $classList = $classService->list_by_conds(['qc_id' => $classIds]);

            if (!empty($classList)) {
                $classList = array_combine_by_key($classList, 'qc_id');
            }

            foreach ($baseInfoList as $article) {
                $list[] = [
                    'app_data_id' => $article['qu_id'],
                    'title' => $article['title'],
                    'class_name' => isset($classList[$article['qc_id']]) ? $classList[$article['qc_id']]['qc_name'] : '',
                    'author' => '',
                    'status' => '已发布',
                ];
            }

            // 总数
            $total = $baseInfoService->count_by_conds($conds);
        }


        return [
            'page' => $postData['page'],
            'limit' => $postData['limit'],
            'total' => isset($total) ? $total : 0,
            'list' => $list,
        ];
    }
}