RedListController.class.php 2.89 KB
<?php
/**
 * 【业绩比拼-手机端】红包领取列表详情
 * RedListController.class.php
 *
 * User: daijun
 * Date: 2017-11-04
 */

namespace Api\Controller\Red;

use Common\Service\PacketrecordService;

class RedListController extends \Api\Controller\AbstractController
{

    public function Index_get()
    {

        // 获取参数
        $params = I('get.');
        // 红包记录ID
        $rid = intval($params['rid']);

        if (empty($this->uid)) {
            // 判断是否外部人员
            E('_EMPTY_USER_ID');
        }

        if (empty($rid)) {
            // 数据验证
            E('_EMPTY_RED_RECORD_ID');
        }

        // 实例化红包记录表
        $record_service = new PacketrecordService();
        // 红包记录详情
        $detail = $record_service->get_by_conds([
            'rid' => $rid, // 红包记录id
            'uid' => $this->uid, // 用户uid
            'packet_status' => PacketrecordService::PACKET_STATUS_SUCCESS // 红包状态:领取成功
        ]);

        if (empty($detail)) {
            // 红包记录不存在
            E('_ERR_RED_RECORD_DATA');
        }

        // 默认值
        $page = isset($params['page']) ? intval($params['page']) : PacketrecordService::DEFAULT_PAGE;
        $limit = isset($params['limit']) ? intval($params['limit']) : PacketrecordService::DEFAULT_LIMIT;

        if (PacketrecordService::RED_PACKET_RAND != $detail['p_type']) {
            // 如果不是随机比例红包,直接返回
            $this->_result = [
                'total' => 0,
                'limit' => intval($limit),
                'page' => intval($page),
                'list' => []
            ];

            return true;
        }

        // 分页
        list($start, $limit) = page_limit($page, $limit);

        // 组装查询条件
        $conds = [
            'ac_id' => $detail['ac_id'],// 同一个活动
            'p_type' => $detail['p_type'], // 相同的红包类型
            'packet_status' => PacketrecordService::PACKET_STATUS_SUCCESS, // 红包状态:领取成功
        ];

        // 查询当前活动领取成功的红包记录数
        $total = $record_service->count_by_conds($conds);

        $list = [];
        if ($total) {
            // 排序:成功领取时间倒序
            $order_option = ['receive_time' => 'DESC'];
            // 分页
            $page_option = [$start, $limit];
            // 查询字段
            $field = 'rand_money,rid,uid,receive_time';
            // 获取列表
            $record_list = $record_service->list_by_conds($conds, $page_option, $order_option, $field);

            $list = $record_service->format_records_list($record_list);
        }

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

        return true;
    }
}