UserMedalListController.class.php 4.88 KB
<?php
/**
 * Created by IntelliJ IDEA.
 * 勋章列表
 * User: zhoutao
 * Reader: zhoutao 2017-05-31 10:07:35
 * Date: 2017-05-24 15:43:49
 */

namespace Apicp\Controller\Medal;

use Com\PackageValidate;
use Common\Common\Attach;
use Common\Common\Constant;
use Common\Common\Integral;
use Common\Common\User;
use Common\Model\MedalLogModel;
use Common\Model\MedalModel;
use Common\Service\MedalLogService;
use Common\Service\MedalService;
use Common\Service\MemberMedalService;

class UserMedalListController extends AbstractController
{
    /**
     * UserMedalList
     * @author tangxingguo
     * @desc 列表接口
     * @param String uid:true 人员ID
     * @param String start_time 开始时间(毫秒级时间戳)
     * @param String end_time 结束时间(毫秒级时间戳)
     * @param Int get_type 获得类型(1=自主获得;2=手动增加)
     * @return Array
                array(
                    'page' => 1, // 当前页
                    'limit' => 20, // 当前页条数
                    'total' => 100, // 总条数
                    'list' => array(
                        array(
                            'created' => 122321313123, // 勋章发放时间
                            'get_type' => 1, // 获得类型(1=自主获得;2=手动增加)
                            'medal_name' => '学习楷模', // 勋章名称
                            'icon_type' => 1, // 图标类型(1=用户上传;2=系统图标)用户上传的图标会有icon_url,系统图标前端至icon库查找
                            'icon' => 'medal03', // 图标
                            'icon_url' => 'http://baidu.com', // 图标
                            'ea_name' => '辣鸡', // 操作人
                        )
                    )
                );
     */
    public function index()
    {
        $rules = [
            'uid' => 'require',
            'start_time' => 'integer',
            'end_time' => 'integer',
            'get_type' => 'integer',
            'page' => 'integer',
            'limit' => 'integer',
        ];

        // 验证请求数据
        $validate = new PackageValidate($rules, [], array_keys($rules));
        $postData = $validate->postData;
        $uid = $postData['uid'];

        // 分页
        $page = $postData['page'] ?? Constant::PAGING_DEFAULT_PAGE;
        $limit = $postData['limit'] ?? Constant::PAGING_DEFAULT_LIMIT;
        list($start, $perpage) = page_limit($page, $limit);

        // 条件
        $conds = ['mem_uid' => $uid];
        if (isset($postData['start_time'])) {
            $conds['created >= ?'] = $postData['start_time'];
        }
        if (isset($postData['end_time'])) {
            $conds['created <= ?'] = $postData['end_time'];
        }
        if (isset($postData['get_type'])) {
            $conds['get_type'] = $postData['get_type'];
        }

        // 排序
        $orderOption = ['created' => 'DESC'];

        // 勋章发放记录
        $medalLogServ = new MedalLogService();
        $logList = $medalLogServ->list_by_conds($conds, [$start, $perpage], $orderOption);

        // 勋章列表
        if (!empty($logList)) {
            $medalServ = new MedalService();
            $imIds = array_column($logList, 'im_id');
            $medalList = $medalServ->list_by_conds(['im_id' => $imIds]);
            if (!empty($medalList)) {
                $medalList = array_combine_by_key($medalList, 'im_id');

                // 获取自定义用户icon连接
                foreach ($medalList as $v) {
                    if ($v['icon_type'] == MedalModel::ICON_TYPE_USER_UPLOAD) {
                        $customIconIds[] = $v['icon'];
                    }
                }
                if (!empty($customIconIds)) {
                    $attachUtil = new Attach();
                    $attArrs = $attachUtil->listAttachUrl($customIconIds);
                }
            }


            // 拼接数据
            foreach ($logList as &$v) {
                $v['get_type'] = MedalLogModel::GET_TYPE_HM == $v['get_type'] ?
                    MedalLogModel::GET_TYPE_HM : MedalLogModel::GET_TYPE_AUTO;
                // 勋章名称
                $v['medal_name'] = $medalList[$v['im_id']]['name'] ?? '';
                // 图标类型 1: 用户上传 2: 系统图标
                if (isset($medalList[$v['im_id']])) {
                    $v['icon'] = $medalList[$v['im_id']]['icon'];
                    $v['icon_type'] = $medalList[$v['im_id']]['icon_type'];
                }
                // 自定义上次拼接附件连接
                $v['icon_url'] = $attArrs[$medalList[$v['im_id']]['icon']]['atAttachment'] ?? '';
            }
        }

        $total = $medalLogServ->count_by_conds($conds);

        $this->_result = [
            'page' => $page,
            'limit' => $limit,
            'total' => $total,
            'list' => array_values($logList)
        ];

        return true;
    }
}