package com.ruoyi.web.controller.learn;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.learn.domain.LearnKnowledgeTree;
import com.ruoyi.learn.domain.LearnQuestions;
import com.ruoyi.learn.service.ILearnKnowledgeTreeService;
import com.ruoyi.learn.service.ILearnQuestionsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.learn.domain.LearnKnowledgeQuestion;
import com.ruoyi.learn.service.ILearnKnowledgeQuestionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 知识点题关系Controller
 *
 * @author ruoyi
 * @date 2025-09-25
 */
@RestController
@RequestMapping("/learn/knowledgeQuestion")
public class LearnKnowledgeQuestionController extends BaseController
{
    @Autowired
    private ILearnKnowledgeQuestionService learnKnowledgeQuestionService;
    @Autowired
    private ILearnKnowledgeTreeService knowledgeTreeService;
    @Autowired
    private ILearnQuestionsService questionsService;

    /**
     * 查询知识点题关系列表
     */
    @PreAuthorize("@ss.hasPermi('learn:knowledgeQuestion:list')")
    @GetMapping("/list")
    public TableDataInfo list(LearnKnowledgeQuestion learnKnowledgeQuestion)
    {
        startPage();
        List<LearnKnowledgeQuestion> list = learnKnowledgeQuestionService.selectLearnKnowledgeQuestionList(learnKnowledgeQuestion);
        return getDataTable(list);
    }

    /**
     * 导出知识点题关系列表
     */
    @PreAuthorize("@ss.hasPermi('learn:knowledgeQuestion:export')")
    @Log(title = "知识点题关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LearnKnowledgeQuestion learnKnowledgeQuestion)
    {
        List<LearnKnowledgeQuestion> list = learnKnowledgeQuestionService.selectLearnKnowledgeQuestionList(learnKnowledgeQuestion);
        ExcelUtil<LearnKnowledgeQuestion> util = new ExcelUtil<LearnKnowledgeQuestion>(LearnKnowledgeQuestion.class);
        util.exportExcel(response, list, "知识点题关系数据");
    }

    /**
     * 获取知识点题关系详细信息
     */
    @PreAuthorize("@ss.hasPermi('learn:knowledgeQuestion:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(learnKnowledgeQuestionService.selectLearnKnowledgeQuestionById(id));
    }

    /**
     * 新增知识点题关系
     */
    @PreAuthorize("@ss.hasPermi('learn:knowledgeQuestion:add')")
    @Log(title = "知识点题关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LearnKnowledgeQuestion learnKnowledgeQuestion)
    {
        return toAjax(learnKnowledgeQuestionService.insertLearnKnowledgeQuestion(learnKnowledgeQuestion));
    }

    /**
     * 修改知识点题关系
     */
    @PreAuthorize("@ss.hasPermi('learn:knowledgeQuestion:edit')")
    @Log(title = "知识点题关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LearnKnowledgeQuestion learnKnowledgeQuestion)
    {
//        //处理科目关联（是否有修改）
//        LearnKnowledgeTree knowledgeTree = knowledgeTreeService.selectLearnKnowledgeTreeById(learnKnowledgeQuestion.getKnowledgeId());
//        if (knowledgeTree != null ) {
//            LearnQuestions q = questionsService.selectLearnQuestionsById(learnKnowledgeQuestion.getQuestionId());
//            if (q != null && !q.getSubjectId().equals(knowledgeTree.getSubjectId())) {
//                q.setSubjectId(knowledgeTree.getSubjectId());
//                questionsService.updateLearnQuestions(q);
//            }
//        }
        return toAjax(learnKnowledgeQuestionService.updateLearnKnowledgeQuestion(learnKnowledgeQuestion));
    }

    /**
     * 删除知识点题关系
     */
    @PreAuthorize("@ss.hasPermi('learn:knowledgeQuestion:remove')")
    @Log(title = "知识点题关系", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(learnKnowledgeQuestionService.deleteLearnKnowledgeQuestionByIds(ids));
    }
}
