CategoryService.class.php 3.54 KB
<?php
/**
 * 培训分类表Service
 * Created by PhpStorm.
 */

namespace Common\Service;

use Common\Model\CategoryModel;
use Common\Model\EducationModel;

class CategoryService extends AbstractService
{

    // 分类状态:禁用
    const CATEGORY_CLOSE = 0;
    // 分类状态:启用
    const CATEGORY_OPEN = 1;

    /** @var CategoryModel */
    protected $_d;

    public function __construct()
    {

        // 实例化培训分类model
        $this->_d = new CategoryModel();

        parent::__construct();
    }


    /**
     * 检查新增分类数据
     * @author daijun
     *
     * @param array $data 传入参数
     *
     * @return bool
     */
    public function check_add_data($data = [])
    {

        $ca_name = $data['ca_name'];
        // 检查分类名称是否为空
        if (empty($ca_name)) {

            E('_EMPTY_CATEGORY_NAME');
        }

        // 检查分类名称长度
        if (get_str_len($ca_name) > 20) {

            E('_ERR_CATEGORY_NAME_LEN');
        }

        // 如果分类描述不为空,则检查描述长度
        if (!empty($data['ca_desc']) && get_str_len($data['ca_desc']) > 120) {

            E('_ERR_CATEGORY_DESC_LEN');
        }

        // 检查该分类名称是否存在
        $cate_data = $this->_d->get_by_conds(['ca_name' => $ca_name]);
        if (!empty($cate_data)) {

            E('_ERR_CATEGORY_NAME_REPEAT');
        }

        return true;
    }


    public function check_edit_data($data = [])
    {
        // 分类ID不能为空
        if (empty($data['ca_id'])) {

            E('_EMPTY_CATEGORY_ID');
        }

        $ca_name = $data['ca_name'];
        // 检查分类名称是否为空
        if (empty($ca_name)) {

            E('_EMPTY_CATEGORY_NAME');
        }

        // 检查分类名称长度
        if (get_str_len($ca_name) > 20) {

            E('_ERR_CATEGORY_NAME_LEN');
        }

        // 如果分类描述不为空,则检查描述长度
        if (!empty($data['ca_desc']) && get_str_len($data['ca_desc']) > 120) {

            E('_ERR_CATEGORY_DESC_LEN');
        }

        // 检查该分类名称是否重复
        $cate_data = $this->_d->get_by_conds(['ca_name' => $ca_name]);
        if (!empty($cate_data) && $cate_data['ca_id'] != $data['ca_id']) {

            E('_ERR_CATEGORY_NAME_REPEAT');
        }

        // 检查分类状态
        if (!is_numeric($data['ca_status'])) {

            E('_EMPTY_CATEGORY_STATUS');
        }

        return true;
    }


    /**
     * 格式化分类列表返回数据
     * @author:daijun
     *
     * @param array $list 分类列表
     *
     * @return array
     */
    public function format_list_data($list = [])
    {
        $education = new EducationModel();

        // 获取分类ID集合
        $ca_ids = array_column($list, 'ca_id');

        // 查询分类集合下的所有培训
        $edu_list = $education->list_by_conds(['ca_id' => $ca_ids], null, [], 'ed_id,ca_id,ed_name');

        $edu_data = array_combine_by_key($edu_list, 'ca_id');

        $res_list = [];
        foreach ($list as $v) {
            // 循环格式化数据
            $arr['ca_id'] = intval($v['ca_id']);
            $arr['ca_name'] = strval($v['ca_name']);
            $arr['ca_desc'] = strval($v['ca_desc']);
            $arr['ca_status'] = intval($v['ca_status']);
            $arr['ca_order'] = intval($v['ca_order']);
            // 判断该分类下是否含有培训
            $arr['cd_num'] = empty($edu_data[$v['ca_id']]) ? 0 : 1;

            $res_list[] = $arr;
        }

        return $res_list;
    }
}