DetailController.class.php 4.83 KB
<?php
/**
 * 【手机端】02-培训详情接口
 * Created by PhpStorm.
 * @author:wanghuan
 * @date:2017-08-29
 */

namespace Api\Controller\Education;

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

class DetailController extends \Api\Controller\AbstractController
{

    /** @var EducationService */
    protected $edu_s;
    /** @var CategoryService */
    protected $cate_s;
    /** @var PlanService */
    protected $plan_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->plan_s = new PlanService();
        // 实例化员工名单service
        $this->right_users_s = new RightUsersService();

        return true;
    }

    /**
     * 培训详情
     */
    public function Index_post()
    {

        // 培训id
        $ed_id = I('post.ed_id', 0, 'intval');
        // 培训id为空
        if (!$ed_id) {

            E('_EMPTY_ED_ID');
        }

        // 培训详情
        $education = $this->get_education($ed_id);

        // 适用范围查询条件
        $right_cond = [
            'ru_uid' => $this->uid,
            'ed_id' => $ed_id
        ];
        // 查询当前员工适用范围数据
        $user_right = $this->right_users_s->get_by_conds($right_cond);
        // 当前员工不在适用范围
        if (empty($user_right)) {

            E('_ERR_ED_NO_RIGHT');
        }

        // 培训分类为禁用状态
        if (CategoryService::CATEGORY_CLOSE == $education['ca_status']) {

            E('_ERR_CATE_CLOSE');
        }

        // 数据格式化
        $education = $this->format_data($education);

        // 培训安排查询条件
        $cond['ed_id'] = $ed_id;
        // 分页
        $page_option = null;
        // 排序
        $order_option['plan_order'] = 'ASC';
        // 返回字段
        $fields = 'plan_id, plan_name, plan_order';
        // 培训安排列表
        $plan_list = $this->plan_s->list_by_conds($cond, $page_option, $order_option, $fields);
        $plans = [];
        if (!empty($plan_list)) {
            // 培训安排数据格式化
            foreach ($plan_list as $key => $val) {
                $plans[$key] = [
                    'plan_id' => intval($val['plan_id']),
                    'plan_name' => $val['plan_name']
                ];
            }
        }

        // 返回结果追加培训安排列表数据
        if ($education) {

            $education['plans'] = $plans;
        }

        // 返回结果
        $this->_result = $education;
    }

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

        // 培训分类id
        $ca_id = $education['ca_id'];
        // 分类详情
        $category = $this->cate_s->get($ca_id);

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

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

        // 用户报名状态
        $sign_up_status = $this->right_users_s->get_sign_up_status($education, $right_users);
        // 培训进行状态
        $ed_show_status = $this->edu_s->get_ed_status($education);

        // 返回格式化数据
        return [
            'ed_id' => intval($education['ed_id']),
            'ed_name' => $education['ed_name'],
            'ca_name' => $ca_name,
            'ed_cover_url' => CategoryService::format_cover($education['ed_cover_id']),
            'ed_begin_time' => $education['ed_begin_time'],
            'ed_end_time' => $education['ed_end_time'],
            'ed_address' => $education['ed_address'],
            'ed_introductions' => $education['ed_introductions'],
            'ed_is_sign_up' => intval($education['ed_is_sign_up']),
            'ed_sign_deadline' => $education['ed_sign_deadline'],
            'ed_is_charge' => intval($education['ed_is_charge']),
            'ed_charge_type' => intval($education['ed_charge_type']),
            'ed_charge_score' => intval($education['ed_charge_score']),
            'ed_is_check' => intval($education['ed_is_check']),
            'ed_status' => intval($ed_show_status),
            'sign_up_status' => $sign_up_status
        ];
    }
}