ListController.class.php 4.79 KB
<?php
/**
 * 【手机端】01-培训列表接口
 * Created by PhpStorm.
 * @author:wanghuan
 * @date:2017-08-29
 */

namespace Api\Controller\Education;

use Common\Service\EducationService;
use Common\Service\CategoryService;
use Common\Service\RightUsersService;

class ListController extends \Api\Controller\AbstractController
{

    /** @var EducationService */
    protected $edu_s;
    /** @var CategoryService */
    protected $cate_s;
    /** @var RightUsersService */
    protected $right_users_s;

    public function before_action($action = '')
    {

        if (!parent::before_action($action)) {

            return false;
        }

        // 实例化培训service
        $this->edu_s = new EducationService();
        // 实例化培训分类service
        $this->cate_s = new CategoryService();
        // 实例化员工名单service
        $this->right_users_s = new RightUsersService();

        return true;
    }

    /**
     * 培训列表
     */
    public function Index_post()
    {

        // 接收post参数
        $params = I('post.');

        // 判断分页参数是否为空,为空赋默认值
        $page = !empty($params['page']) ? intval($params['page']) : self::DEFAULT_PAGE;
        $limit = !empty($params['limit']) ? intval($params['limit']) : self::DEFAULT_LIMIT;
        // 分页
        list($start, $limit) = page_limit($page, $limit);

        // 培训查询条件:培训分类开启、培训已发布或已终止
        $cond = [
            'uid' => $this->uid,
            'ca_status' => CategoryService::CATEGORY_OPEN,
            'ed_status>?' => EducationService::EDUCATION_PUBLISH_STATUS_DRAFT
        ];

        // 数据类型参数不为空:我的培训(个人中心中的我参加的培训)
        if ($params['type']) {
            // 查询条件追加已报名状态
            $cond['ru_sign_status'] = RightUsersService::SIGN_STATUS_SIGNED;
        }

        // 如果需要查询状态
        if (!empty($params['status'])) {
            $cond['status'] = $params['status'];
        }
        // 获取记录总数
        $total = $this->edu_s->user_education_count($cond);

        $list = [];
        // 记录总数不为空,查询列表数据
        if ($total) {
            // 分页参数
            $page_option = [$start, $limit];
            // 排序:培训开始时间倒序
            $order_option = [
                'ed_begin_time' => 'DESC'
            ];
            // 获取列表数据
            $list = $this->edu_s->user_education_list($cond, $page_option, $order_option);

        }

        if (!empty($list)) {
            // 数据格式化
            $list = $this->format_data($list);
        }

        // 返回结果
        $this->_result = [
            'total' => intval($total),
            'page' => intval($page),
            'limit' => intval($limit),
            'list' => $list
        ];
    }

    /**
     * 培训数据格式化
     *
     * @param array $data 待格式化数据
     *
     * @return array 格式化后数据
     */
    protected function format_data($data)
    {

        // 分类状态:启用
        $cate_cond['ca_status'] = CategoryService::CATEGORY_OPEN;
        // 获取分类数据
        $category_list = $this->cate_s->list_by_conds($cate_cond);
        $category_list = array_combine_by_key($category_list, 'ca_id');

        // 员工名单查询条件
        $right_cond['ru_uid'] = $this->uid;
        // 查询用户报名数据
        $right_users = $this->right_users_s->list_by_conds($right_cond);
        $right_users = array_combine_by_key($right_users, 'ed_id');

        $result = [];
        foreach ($data as $key => $val) {
            // 分类详情
            $category = $category_list[$val['ca_id']];

            // 分类名称
            $ca_name = '';
            if (!empty($category)) {

                $ca_name = $category['ca_name'];
            }

            // 培训进行状态
            $ed_show_status = $this->edu_s->get_ed_status($val);

            // 查询用户报名数据
            $right_users_info = $right_users[$val['ed_id']];
            // 用户报名状态
            $sign_up_status = $this->right_users_s->get_sign_up_status($val, $right_users_info);

            $result[$key] = [
                'ed_id' => intval($val['ed_id']),
                'ed_name' => $val['ed_name'],
                'ca_name' => $ca_name,
                'ed_cover_url' => CategoryService::format_cover($val['ed_cover_id']),
                'ed_begin_time' => $val['ed_begin_time'],
                'ed_status' => intval($ed_show_status),
                'ed_joined_count' => intval($val['ed_joined_count']),
                'ed_address' => $val['ed_address'],
                'sign_up_status' => $sign_up_status
            ];
        }

        return $result;
    }
}