EducationModel.class.php 9.31 KB
<?php
/**
 * 培训基础表Model
 * Created by PhpStorm.
 */

namespace Common\Model;

use Common\Service\EducationService;

class EducationModel extends AbstractModel
{
    // 库培训发布状态:草稿
    const EDUCATION_PUBLISH_STATUS_DRAFT = 1;
    // 库培训发布状态:已发布
    const EDUCATION_PUBLISH_STATUS_PUBLISHED = 2;
    // 库培训发布状态:已终止
    const EDUCATION_PUBLISH_STATUS_STOP = 3;

    // 培训进行状态:未开始
    const EDUCATION_PROCESS_STATUS_UN_BEGIN = 1;
    // 培训进行状态:进行中
    const EDUCATION_PROCESS_ON_GOING = 2;
    // 培训进行状态:已结束
    const EDUCATION_PROCESS_END = 3;
    // 培训进行状态:已终止
    const EDUCATION_PROCESS_STOP = 4;
    // 培训进行状态:草稿
    const EDUCATION_PROCESS_DRAFT = 5;

    // 搜索发布状态
    const SEARCH_PUBLISH = 1;
    // 搜索草稿
    const SEARCH_DRAFT = 2;

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 更新已参加人数
     * @author wanghuan
     *
     * @param int $ed_id 培训id
     * @param int $type 更新类型:0=加1,1=减1
     *
     * @return bool
     */
    public function update_joined($ed_id, $type)
    {
        // 减1
        if ($type) {
            return $this->_m->where('ed_id=' . $ed_id)->setDec('ed_joined_count', 1);
        } else { // 加1
            return $this->_m->where('ed_id=' . $ed_id)->setInc('ed_joined_count', 1);
        }
    }

    /**
     * 根据条件获取用户权限范围的培训总数
     * @author wanghuan
     *
     * @param array $cond 查询条件
     *
     * @return array|bool
     */
    public function user_education_count($cond = [])
    {
        // 条件
        $where = [];
        // 参数
        $params = [];
        if (!$this->parse_where_user_education($where, $params, $cond)) {

            return false;
        }

        $sql = 'SELECT COUNT(e.ed_id) FROM __TABLE__ e LEFT JOIN oa_education_right_users u ON e.ed_id=u.ed_id WHERE ' . implode(' AND ',
                $where);

        return $this->_m->result($sql, $params);
    }

    /**
     * 根据条件获取用户权限范围的培训列表
     * @author wanghuan
     *
     * @param array $cond 查询条件
     * @param null $page_option 分页
     * @param array $order_option 排序
     *
     * @return array|bool
     */
    public function user_education_list($cond = [], $page_option = null, $order_option = [])
    {
        // 条件
        $where = [];
        // 参数
        $params = [];
        // 参数解析
        $this->parse_where_user_education($where, $params, $cond);

        // 排序
        $order_by = '';
        if (!$this->_order_by($order_by, $order_option)) {

            return false;
        }

        // 分页参数
        $limit = '';
        if (!$this->_limit($limit, $page_option)) {

            return false;
        }

        $sql = 'SELECT e.* FROM __TABLE__ e LEFT JOIN oa_education_right_users u ON e.ed_id=u.ed_id WHERE ' . implode(' AND ',
                $where) . $order_by . $limit;
        
        return $this->_m->fetch_array($sql, $params);
    }

    /**
     * 查询用户权限范围的培训列表sql参数解析
     * @author wanghuan
     *
     * @param array $where sql查询条件
     * @param array $params sql参数
     * @param array $condition 传入参数
     *
     * @return bool
     */
    protected function parse_where_user_education(&$where, &$params, $condition = [])
    {

        // 用户id
        if ($condition['uid']) {
            $where[] = 'u.ru_uid=?';
            $params[] = $condition['uid'];
        }

        // 分类状态
        if ($condition['ca_status']) {
            $where[] = 'e.ca_status=?';
            $params[] = $condition['ca_status'];
        }

        // 报名状态
        if ($condition['ru_sign_status']) {
            $where[] = 'u.ru_sign_status=?';
            $params[] = $condition['ru_sign_status'];
        }

        // 报名状态
        if ($condition['status']) {

            // 未开始
            if (EducationService::EDUCATION_PROCESS_STATUS_UN_BEGIN == $condition['status']) {
                // 开始时间大于当前时间
                $where[] = 'e.ed_begin_time >?';
                $params[] = MILLI_TIME;
            }

            // 进行中
            if (EducationService::EDUCATION_PROCESS_ON_GOING == $condition['status']) {
                // 开始时间小于等于当前时间
                $where[] = 'e.ed_begin_time <= ?';
                $params[] = MILLI_TIME;

                // 结束时间大于等于当前时间
                $where[] = 'e.ed_end_time >= ?';
                $params[] = MILLI_TIME;
            }

            // 已结束
            if (EducationService::EDUCATION_PROCESS_END == $condition['status']) {
                $where[] = 'e.ed_end_time < ?';
                $params[] = MILLI_TIME;
            }
        }

        // 企业标识
        $where[] = 'e.domain=?';
        $params[] = QY_DOMAIN;
        // 状态
        $where[] = 'e.status<?';
        $params[] = self::ST_DELETE;

        // 企业标识
        $where[] = 'u.domain=?';
        $params[] = QY_DOMAIN;
        // 状态
        $where[] = 'u.status<?';
        $params[] = self::ST_DELETE;

        return true;
    }


    /**
     * 拼装 培训列表条件组装 where语句
     *
     * @author: 蔡建华
     *
     * @param array $params 查询条件参数
     *
     * @return string
     */
    public function get_search_where($params = [])
    {
        // 组装查询语句
        $where = ' status<' . self::ST_DELETE . " AND domain= '" . QY_DOMAIN . "'";
        $search_key = trim($params['search_key']);
        // 如果标题不为空
        if (!empty($search_key)) {
            $search_key = str_replace("%", '\%', $search_key);
            $where .= " AND ed_name like '%" . $search_key . "%' ";
        }

        // 培训时间范围
        if (!empty($params['begin_time']) && $params['begin_time'] != 'NaN' && $params['end_time'] != 'NaN') {

            $where .= ' AND ((ed_end_time > ' . $params['begin_time'] . ' AND ed_begin_time < ' . $params['end_time'] . ') OR (ed_begin_time<' . $params['end_time'] . ' AND ed_end_time = 0 ))';
        }

        // 分类不为空
        if (!empty($params['ca_id'])) {

            $where .= ' AND ca_id=' . $params['ca_id'];
        }

        // 培训状态不为空
        if (self::SEARCH_DRAFT == $params['type']) {

            // 如果是草稿的时候执行
            $where .= ' AND ed_status =' . self::EDUCATION_PUBLISH_STATUS_DRAFT;
        } else {
            // 不是草稿的时候执行
            if (!empty($params['ed_status'])) {
                switch ($params['ed_status']) {

                    // 草稿5
                    case self::EDUCATION_PROCESS_DRAFT:
                        $where .= ' AND ed_status =' . self::EDUCATION_PUBLISH_STATUS_DRAFT;
                        break;
                    // 未开始1
                    case self::EDUCATION_PROCESS_STATUS_UN_BEGIN:
                        $where .= ' AND ed_begin_time >' . MILLI_TIME . ' AND ed_status =' . self::EDUCATION_PUBLISH_STATUS_PUBLISHED;
                        break;
                    // 已开始2
                    case self::EDUCATION_PROCESS_ON_GOING:
                        $where .= ' AND ed_status =' . self::EDUCATION_PUBLISH_STATUS_PUBLISHED . ' AND ed_begin_time<' . MILLI_TIME . ' AND ed_end_time>=' . MILLI_TIME;
                        break;
                    // 已结束3
                    case self::EDUCATION_PROCESS_END:
                        $where .= ' AND ((ed_status =' . self::EDUCATION_PUBLISH_STATUS_PUBLISHED . ' AND ed_end_time> 0 AND  ed_end_time<' . MILLI_TIME . ') OR ed_status =' . self::EDUCATION_PUBLISH_STATUS_STOP . ')';
                        break;

                    default:
                }
            }
            if (self::SEARCH_PUBLISH == $params['type']) {
                $where .= ' AND ed_status !=' . self::EDUCATION_PUBLISH_STATUS_DRAFT;
            }
        }

        return $where;
    }

    /**
     * 根据条件查询培训总数
     *
     * @author: 蔡建华
     *
     * @param $data array 查询条件
     *
     * @return int|mixed
     */
    public function count_by_education($data = [])
    {
        $where = $this->get_search_where($data);

        $sql = 'SELECT COUNT(*) FROM __TABLE__ WHERE ' . $where;

        return $this->_m->result($sql, []);
    }

    /**
     * 根据条件查询培训列表
     *
     * @author: 蔡建华
     *
     * @param $data array 查询条件
     * @param null $page_option 分页参数
     * @param array $order_option 排序参数
     * @param string $fields 查询的字段
     *
     * @return array|bool
     */
    public function list_by_education($data, $page_option = null, $order_option = [], $fields = ' * ')
    {
        $where = $this->get_search_where($data);
        // 排序
        $orderby = '';
        if (!$this->_order_by($orderby, $order_option)) {
            return false;
        }
        // 分页参数
        $limit = '';
        if (!$this->_limit($limit, $page_option)) {
            return false;
        }

        $sql = "SELECT {$fields} FROM __TABLE__ WHERE " . $where . " {$orderby}{$limit}";

        // 读取记录
        return $this->_m->fetch_array($sql, []);
    }

}