UserAnswerController.php 7.6 KB
<?php
// +----------------------------------------------------------------------
// | 用户答题结果模块
// +----------------------------------------------------------------------
// | Author: 魏强
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use func\func;

/**
 * Class UserAnswerController
 * @package app\admin\controller
 * @adminMenuRoot(
 *     'name'   => '用户答题列表',
 *     'action' => 'index',
 *     'parent' => '',
 *     'display'=> true,
 *     'order'  => 100,
 *     'icon'   => 'pencil',
 *     'remark' => '用户答题列表'
 * )
 */
class UserAnswerController extends AdminBaseController
{

    public function index()
    {
        $request = $this->request->param();
        $where = [];
        if(!empty($request['mobile'])) {
            $where['u.mobile'] = ['like', "%$request[mobile]%"];
        }
        !empty($request['start_time']) ? $start_time = $request['start_time'] : $start_time = 0;
        !empty($request['end_time']) ? $end_time = $request['end_time'] : $end_time = time();
        $list = Db::name('user_answer')
            ->alias('ua')
            ->field('ua.* ,u.user_nickname ,u.mobile')
            ->join('user u','ua.user_id = u.id')
            ->where($where)
            ->whereTime('ua.create_time','between',[$start_time,$end_time])
            ->order('ua.create_time','DESC')
            ->paginate(2,false,['query'=>$request])
            ->each(function ($item){
                //用户当前优惠券情况
                $user_coupon = Db::name('user_coupon')
                    ->alias('uc')
                    ->field('c.title ,uc.type')
                    ->join('coupon c','uc.coupon_id = c.id')
                    ->where('uc.user_id',$item['user_id'])
                    ->where('uc.delete_time',0)
                    ->select();
                count($user_coupon) != 0 ? $item['user_coupon_text'] = '' : $item['user_coupon_text'] = '无优惠券';
                foreach($user_coupon as $uc){
                    $uc['type'] == 0 ? $user_coupon_type_text = '未使用' : $user_coupon_type_text = '已使用';
                    $item['user_coupon_text'] .= $uc['title']."($user_coupon_type_text) ";
                }
                //获取题目信息
                $str = trim(trim(htmlspecialchars_decode($item['str']),'['),']');
                $arr_str = explode(',',$str);
                $item['question_text'] = [];
                $all_score = 0;
                //循环查询每道题目的内容
                for($i=0;$i<count($arr_str);$i++) {
                    $arr_str[$i] = trim($arr_str[$i], '"');
                    $arr_q = explode('-', $arr_str[$i]);
                    if (!empty($arr_q[0]) && !empty($arr_q[1])) {
                        //题目
                        $question_title = Db::name('question')->where('id', $arr_q[0])->find();
                        //选项
                        $question_choose = Db::name('question')->where('id', $arr_q[1])->find();
                        $all_score +=$question_choose['score'];
                        $item['question_text'][] = [
                                'order'=>$question_title['order_id'],
                                'title'=>$question_choose['title'],
                                'score'=>$question_choose['score']
                        ];

                    }
                }
                $item['all_score'] = $all_score;
                return $item;
            });
        $this->assign('page',$list->render());
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 导出答题结果
     * @adminMenu(
     *     'name'   => '导出答题结果',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 1000,
     *     'icon'   => '',
     *     'remark' => '导出答题结果',
     *     'param'  => ''
     * )
     */
    public function Exl(){
        $request = $this->request->param();
        $where = [];
        if(!empty($request['mobile'])) {
            $where['u.mobile'] = ['like', "%$request[mobile]%"];
        }
        !empty($request['start_time']) ? $start_time = $request['start_time'] : $start_time = 0;
        !empty($request['end_time']) ? $end_time = $request['end_time'] : $end_time = time();
        $list = Db::name('user_answer')
            ->alias('ua')
            ->field('ua.* ,u.user_nickname ,u.mobile')
            ->join('user u','ua.user_id = u.id')
            ->where($where)
            ->whereTime('ua.create_time','between',[$start_time,$end_time])
            ->order('u.create_time','DESC')
            ->select()
            ->each(function ($item){
                //用户当前优惠券情况
                $user_coupon = Db::name('user_coupon')
                    ->alias('uc')
                    ->field('c.title ,uc.type')
                    ->join('coupon c','uc.coupon_id = c.id')
                    ->where('uc.user_id',$item['user_id'])
                    ->where('uc.delete_time',0)
                    ->select();
                count($user_coupon) != 0 ? $item['user_coupon_text'] = '' : $item['user_coupon_text'] = '无优惠券';
                foreach($user_coupon as $uc){
                    $uc['type'] == 0 ? $user_coupon_type_text = '未使用' : $user_coupon_type_text = '已使用';
                    $item['user_coupon_text'] .= $uc['title']."($user_coupon_type_text) ";
                }
                //获取题目信息
                $str = trim(trim(htmlspecialchars_decode($item['str']),'['),']');
                $arr_str = explode(',',$str);
                $item['question_text'] = [];
                $all_score = 0;
                for($i=0;$i<count($arr_str);$i++) {
                    $arr_str[$i] = trim($arr_str[$i], '"');
                    $arr_q = explode('-', $arr_str[$i]);
                    if (!empty($arr_q[0]) && !empty($arr_q[1])) {
                        //题目
                        $question_title = Db::name('question')->where('id', $arr_q[0])->find();
                        //选项
                        $question_choose = Db::name('question')->where('id', $arr_q[1])->find();
                        $all_score +=$question_choose['score'];
                        $item['question_text'][] = [
                            'order'=>$question_title['order_id'],
                            'title'=>$question_choose['title'],
                            'score'=>$question_choose['score']
                        ];

                    }
                }
                $item['all_score'] = $all_score;
                return $item;
            });
        $exl = new func();
        $exl_title = ['用户昵称','用户电话','题目编号','用户答案','答题分数','答题总分数','当前优惠券信息','答题时间'];
        $exl_dis   = [];
        foreach ($list as $li){
            foreach ($li['question_text'] as $qt){
                $exl_dis[] = [
                    $exl->filterEmoji($li['user_nickname']),
                    $li['mobile'],
                    $qt['order'],
                    $qt['title'],
                    $qt['score'],
                    $li['all_score'],
                    $li['user_coupon_text'],
                    date('Y-m-d H:i',$li['create_time'])
                ];
            }
        }
        $exl->exportExcel($exl_title, $exl_dis, '用户答题列表', './', true);
    }




}