<?php
namespace app\index\logic;
use app\index\model\daycheckinfo;
use app\index\model\gworkermodel;
use app\index\model\pinfomodel;
use app\index\model\prjlocationmodel;
use app\index\model\pwrelationmodel;
use app\index\model\ginfomodel;
use app\index\model\wheadmodel;
use app\index\model\contactmodel;
use app\index\model\gcmodel;
use app\index\model\questionanswerrecordmodel;
use app\index\model\questionbankmodel;
use app\index\model\questionbankoptionsmodel;
use app\index\model\questionusermodel;
use app\index\model\transfermodel;
use think\Db;

/**
 * 答题
 *
 * @author wj
 * @date 2025-07-25
 */
class questionlogic {
    public function getquestionlist($arr) {
        $m_qb = new questionbankmodel();
        $m_qbo = new questionbankoptionsmodel();
        $m_qu = new questionusermodel();

        $userid = $arr['user_id'];
        $quinfo = $m_qu->isactivebyuseid($userid);
        if (empty($quinfo)) {
            return backarr(0, "用户不可答题");
        }

        $qb_list = $m_qb->getlistall();
        foreach ($qb_list as $key => $value) {
            $qbid = $value['id'];
            $options = $m_qbo->getoptionsbyseqbid($qbid);
            $value['options'] = $options;
            $qb_list[$key] = $value;
        }
        return backarr(1, "操作成功", $qb_list);
    }
    public function saveanswer($arr) {
        $m_qb = new questionbankmodel();
        $m_qbo = new questionbankoptionsmodel();
        $m_qar = new questionanswerrecordmodel();
        $m_qu = new questionusermodel();

        $userid = $arr['user_id'];
        $quinfo = $m_qu->isactivebyuseid($userid);
        if (empty($quinfo)) {
            return backarr(0, "用户不可答题");
        }
        $answertime = $quinfo['id'];

        $answers = $arr['answers'];
        Db::startTrans();
        try {
            foreach ($answers as $key => $value) {
                $qbid = $value['qb_id'];
                $qbinfo = $m_qb->getinfobyid($qbid);
                if (empty($qbinfo)) {
                    throw new \Exception("无问题信息");
                }
                $type = $qbinfo['type'];
                if (in_array($type, [1, 2])) {
                    $answerid = $value['answer_id'];
                    $answerid_arr = array_filter(explode(",", $answerid));
                    $label_arr = [];
                    foreach ($answerid_arr as $key => $aid) {
                        $qboinfo = $m_qbo->getinfobywhere($qbid, $aid);
                        if (empty($qboinfo)) {
                            continue;
                        }
                        $label_arr[] = $qboinfo['label'];
                    }
                    $label = implode(" ", $label_arr);
                    $insertData = [
                        'user_id' => $userid,
                        'seqb_id' => $qbid,
                        'answer' => $answerid,
                        'label' => $label,
                        'createtime' => date("Y-m-d H:i:s"),
                        'answer_time' => $answertime,
                    ];
                    $m_qar->addinfo($insertData);
                }
                if (in_array($type, [3])) {
                    $answer_str = !isset($value['answer_str']) || empty($value['answer_str']) ? "" : $value['answer_str'];
                    $insertData = [
                        'user_id' => $userid,
                        'seqb_id' => $qbid,
                        'answer' => $answer_str,
                        'createtime' => date("Y-m-d H:i:s"),
                    ];
                    $m_qar->addinfo($insertData);
                }
            }

            $quid = $quinfo['id'];
            $m_qu->updinfobyid($quid, ['is_active' => 0, 'is_answer' => 1, 'answer_time' => date("Y-m-d H:i:s")]);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $msg = $e->getMessage();
            return backarr(0, $msg);
        }
        return backarr(1, "操作成功");
    }
}