package com.ruoyi.web.controller.back;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ie.domain.Questions;
import com.ruoyi.ie.service.IQuestionsService;
import com.ruoyi.ie.service.ISubquestionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 试题Controller
 *
 * @author mingxue
 * @date 2021-07-24
 */
@RestController
@RequestMapping("/back/questions")
public class BackQuestionsController extends BaseController
{
    @Autowired
    private IQuestionsService questionsService;
    @Autowired
    private ISubquestionsService subquestionsService;

    /**
     * 查询试题列表
     */
    @PreAuthorize("@ss.hasPermi('back:questions:list')")
    @GetMapping("/list")
    public TableDataInfo list(Questions questions)
    {
        startPage();
        List<Questions> list = questionsService.selectQuestionsList(questions);
        questionsService.replaceImage(list);

//        list = questionsService.changeQuestionList(list);
        return getDataTable(list);
    }

    /**
     * 导出试题列表
     */
    @PreAuthorize("@ss.hasPermi('back:questions:export')")
    @Log(title = "试题", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Questions questions)
    {
        List<Questions> list = questionsService.selectQuestionsList(questions);
        ExcelUtil<Questions> util = new ExcelUtil<Questions>(Questions.class);
        return util.exportExcel(list, "试题数据");
    }

    /**
     * 获取试题详细信息
     */
    @PreAuthorize("@ss.hasPermi('back:questions:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(questionsService.selectQuestionsById(id));
    }
    /**
     * 获取试题详细信息
     */
    @PreAuthorize("@ss.hasPermi('back:questions:query')")
    @GetMapping(value = "/getQuestionInfo/{id}")
    public AjaxResult getQuestionInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(questionsService.getQuestionInfo(id));
    }


    /**
     * 新增试题
     */
    @PreAuthorize("@ss.hasPermi('back:questions:add')")
    @Log(title = "试题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Questions questions)
    {
        if(StringUtils.isBlank(questions.getTitle())){
            return AjaxResult.error("题目标题不能为空");
        }
        return toAjax(questionsService.insertQuestions(questions));
    }

    /**
     * 修改试题
     */
    @PreAuthorize("@ss.hasPermi('back:questions:edit')")
    @Log(title = "试题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Questions questions)
    {
        return toAjax(questionsService.updateQuestions(questions));
    }

    /**
     * 删除试题
     */
    @PreAuthorize("@ss.hasPermi('back:questions:remove')")
    @Log(title = "试题", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        int result = questionsService.deleteQuestionsByIds(ids) ;
        for(int i= 0;i<ids.length;i++){
            if(StringUtils.isNotNull(ids[i])){
                subquestionsService.deleteSubquestionsByQuestionId(ids[i].intValue());
            }
        }
        return toAjax(result);
    }

    /**
     * 批量更新试题类型
     * @param dto
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('back:questions:edit')")
//    @Log(title = "试题", businessType = BusinessType.UPDATE)
//    @PostMapping("/updateBatchQtpyeByIds")
//    public AjaxResult updateBatchQtpyeByIds(QuestionBackDto dto)
//    {
//        return toAjax(questionsService.updateBatchQtpyeByIds(dto.getIds(),dto.getQtpye()));
//    }

    /**
     * 批量更新试题类型
     * @param ids
     * @return
     */
    @PreAuthorize("@ss.hasPermi('back:questions:edit')")
    @Log(title = "试题", businessType = BusinessType.UPDATE)
    @PostMapping("/handleUpdateBatchQtpyeByIds")
    public AjaxResult handleUpdateBatchQtpyeByIds(String ids, String qtpye)
    {
        for(Long id:Convert.toLongArray(ids)){
            Questions question =new Questions();
            question.setId(id);
            question.setQtpye(qtpye);
            questionsService.updateQuestions(question);
        }
        return AjaxResult.success("更新成功");
    }
}
