ActionLogService.class.php 7.41 KB
<?php
/**
 * Created by PhpStorm.
 * User: zhonglei
 * Date: 2017/6/21
 * Time: 17:14
 */

namespace Common\Service;

use Common\Common\Constant;
use Common\Common\Notice;
use Common\Model\ActionLogModel;
use Common\Model\MedalModel;
use Common\Model\MedalRecordModel;
use Common\Model\MedalRelationModel;
use Common\Common\Integral;
use Common\Common\User;

class ActionLogService extends AbstractService
{
    /** @var MedalModel */
    protected $_medal_model;
    /** @var MedalRelationModel */
    protected $_medal_relation_model;
    /** @var MedalRecordModel */
    protected $_medal_record_model;

    // 构造方法
    public function __construct()
    {
        $this->_medal_model = new MedalModel();
        $this->_medal_relation_model = new MedalRelationModel();
        $this->_medal_record_model = new MedalRecordModel();
        $this->_d = new ActionLogModel();
        parent::__construct();

    }

    /**
     * 问答式中心勋章处理
     *
     * @author: 蔡建华
     * @param int $class_id 问答类型ID
     * @param string $uid 回答问题用户ID
     * @param string $operator_uid 操作用户UID
     * @param array &$award 返回数据
     *
     * @return bool
     */
    public function medal($class_id = '', $uid = '', $operator_uid = '', &$award = [])
    {
        // 获取用户信息
        $userServ = &User::instance();
        $user = $userServ->getByUid($uid);

        // 如果没有用户信息
        if (empty($user['memUsername'])) {
            return true;
        }

        // 获取用户权限
        $rightServ = new RightService();
        $right = $rightServ->get_by_right($user);

        $data['right'] = $right;
        $medal = $this->_medal_model->fetch_all_medal($data);

        // 查询出相关激励
        if (empty($medal)) {

            return true;
        }

        // 获取符合条件的激励IDS
        $em_ids = array_column($medal, 'em_id');
        // 查询激励关系列表
        $list = $this->_medal_relation_model->list_by_conds(['em_id' => $em_ids]);

        // 如果存在符合条件激励
        if (empty($list)) {

            return true;
        }

        // 获取激励列表IDS
        $em_id_arr = array_column($list, 'em_id');
        // 如果查询出激励IDS
        if (empty($em_id_arr)) {

            return true;
        }

        // 初始化勋章
        $integral = new Integral();

        // 获取勋章列表
        $integral_list = $integral->listMedal();
        // 获取激励列表
        $integral_key_list = array_combine_by_key($integral_list, 'im_id');
        // 获取激励列表
        $result = $this->_medal_model->list_by_conds(['em_id' => $em_id_arr]);
        // 实例化积分勋章
        $integralUtil = &Integral::instance();

        // 遍历激励列表
        foreach ($result as $val) {

            $select_uid = $uid;
            // 如果类型是采纳问题
            if (Constant::LOG_ANSWERED_BEST_TYPE == $val['answer_type'] && !empty($operator_uid)) {
                $select_uid = $operator_uid;
            }

            // 获取当前用户是否已经领取过当期激励
            $count = $this->_medal_record_model->count_by_conds(['uid' => $select_uid, 'em_id' => $val['em_id']]);

            // 如果已领取积分或者勋章
            if ($count) {

                continue;
            }

            // 查询勋章对应关系
            $relate_total = $this->_medal_relation_model->count_by_conds(['class_id' => $class_id, 'em_id' => $val['em_id']]);

            // 如果没有对应关系
            if (!$relate_total) {

                continue;
            }

            // 查出对应ep_list
            $em_list = $this->_medal_relation_model->list_by_conds(['em_id' => $val['em_id']]);
            $class_ids = array_column($em_list, 'class_id');

            $answered_total = 0;

            // 回答问题
            if (Constant::LOG_ANSWERED_TYPE == $val['answer_type']) {
                // 日志表查询当前用户是的回答问题次数
                $answered_total = $this->_d->count_by_conds([
                    'class_id' => $class_ids,
                    'type' => Constant::LOG_ANSWERED_TYPE,
                    'answer_uid' => $uid
                ]);
            }

            // 问题被采纳
            if (Constant::LOG_BEST_ANSWERED_TYPE == $val['answer_type']) {
                // 日志表查询当前用户是的回答问题次数
                $answered_total = $this->_d->count_by_conds([
                    'class_id' => $class_ids,
                    'type' => Constant::LOG_BEST_ANSWERED_TYPE,
                    'answer_uid' => $uid
                ]);
            }

            // 采纳问题
            if (Constant::LOG_ANSWERED_BEST_TYPE == $val['answer_type'] && !empty($operator_uid)) {
                // 日志表查询当前用户是的回答问题次数
                $answered_total = $this->_d->count_by_conds([
                    'class_id' => $class_ids,
                    'type' => Constant::LOG_BEST_ANSWERED_TYPE,
                    'uid' => $operator_uid
                ]);
            }

            // 判断满足条件的次数
            if ($val['em_number'] <= $answered_total) {

                // 获取勋章
                if (Constant::EC_MEDAL_TYPE_MEDAL == $val['em_type']) {

                    $integralUtil->endowMedal($val['im_id'], $select_uid, $user['memUsername']);

                    // 发送勋章消息
                    Notice::instance()->NoticeTIntegralMsg([$select_uid], strval($integral_key_list[$val['im_id']]['name']), $val['title']);

                    // 激励信息
                    $award[] = [
                        'award_id' => intval($val['em_id']), // 激励ID
                        'uid' => $select_uid,
                        'award_action' => strval($val['title']), // 激励行为
                        'description' => strval($val['em_desc']), // 描述
                        'award_type' => intval($val['em_type']), // 激励类型(1=勋章;2=积分)
                        'medals' => [
                            'im_id' => intval($val['im_id']), // 勋章ID
                            'icon' => strval($integral_key_list[$val['im_id']]['icon']), // 勋章图标URL或者前端路径
                            'icon_type' => intval($integral_key_list[$val['im_id']]['icon_type']), // 勋章图标来源(1=用户上传;2=系统预设)
                            'name' => strval($integral_key_list[$val['im_id']]['name']), // 勋章名称
                            'desc' => strval($integral_key_list[$val['im_id']]['desc']), // 勋章描述
                        ],
                        'integral' => intval($val['em_integral']), // 积分
                    ];

                    if (Constant::LOG_ANSWERED_BEST_TYPE == $val['answer_type'] && !empty($operator_uid)) {
                        // 满足次数加入记录
                        $this->_medal_record_model->insert([
                            'uid' => $operator_uid,
                            'em_id' => $val['em_id']
                        ]);
                    } else {
                        // 满足次数加入记录
                        $this->_medal_record_model->insert([
                            'uid' => $uid,
                            'em_id' => $val['em_id']
                        ]);
                    }

                }

            }
        }

        return true;
    }
}