package com.xinelu.manage.controller.questioninfo;

import com.xinelu.common.annotation.Log;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.enums.BusinessType;
import com.xinelu.common.utils.poi.ExcelUtil;
import com.xinelu.manage.domain.questioninfo.QuestionInfo;
import com.xinelu.manage.dto.department.DepartmentCount;
import com.xinelu.manage.dto.questioninfo.QuestionInfoDto;
import com.xinelu.manage.service.questioninfo.IQuestionInfoService;
import com.xinelu.manage.vo.questionInfo.QuestionVO;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 问卷基本信息Controller
 *
 * @author xinelu
 * @date 2024-02-28
 */
@RestController
@RequestMapping("/system/question")
public class QuestionInfoController extends BaseController {
    @Resource
    private IQuestionInfoService questionInfoService;

    /**
     * 查询问卷基本信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(QuestionInfoDto questionInfo) {
        startPage();
        List<QuestionInfo> list = questionInfoService.selectQuestionInfoList(questionInfo);
        return getDataTable(list);
    }

    /**
     * 导出问卷基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:question:export')")
    @Log(title = "问卷基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QuestionInfoDto questionInfo) {
        List<QuestionInfo> list = questionInfoService.selectQuestionInfoList(questionInfo);
        ExcelUtil<QuestionInfo> util = new ExcelUtil<QuestionInfo>(QuestionInfo.class);
        util.exportExcel(response, list, "问卷基本信息数据");
    }

    /**
     * 获取问卷基本信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        if (Objects.isNull(id)) {
            return AjaxResult.success();
        }
        return AjaxResult.success(questionInfoService.selectQuestionInfoById(id));
    }

    /**
     * 新增问卷信息
     */
    @PreAuthorize("@ss.hasPermi('system:question:add')")
    @Log(title = "问卷基本信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody QuestionVO questionInfo) {
        if (Objects.isNull(questionInfo) || StringUtils.isBlank(questionInfo.getQuestionnaireName()) || StringUtils.isBlank(questionInfo.getQuestionType())) {
            return AjaxResult.error("请添加问卷信息！");
        }
        return questionInfoService.insertQuestionInfo(questionInfo);
    }

    /**
     * 修改问卷信息
     */
    @PreAuthorize("@ss.hasPermi('system:question:edit')")
    @Log(title = "问卷基本信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody QuestionVO question) {
        if (Objects.isNull(question) || StringUtils.isBlank(question.getQuestionnaireName()) || StringUtils.isBlank(question.getQuestionType())) {
            return AjaxResult.error("请添加问卷信息！");
        }
        return questionInfoService.updateQuestionInfo(question);
    }

    /**
     * 删除问卷基本信息
     */
    @PreAuthorize("@ss.hasPermi('system:question:remove')")
    @Log(title = "问卷基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long ids) {
        return toAjax(questionInfoService.deleteQuestionInfoById(ids));
    }

    /**
     * 修改问卷基本信息
     */
    @PostMapping("/updateQuestion")
    public AjaxResult updateQuestionByDepartment(@RequestBody QuestionInfo questionInfo) {
        return questionInfoService.updateQuestionByDepartment(questionInfo);
    }

    /**
     * 科室问卷数量
     */
    @GetMapping("/departmentQuestionCount")
    public AjaxResult departmentQuestionCount(DepartmentCount departmentCount) {
        return AjaxResult.success(questionInfoService.departmentQuestionCount(departmentCount));
    }

    /**
     * 查询问卷基本信息列表不分页
     */
    @GetMapping("/selectQuestionInfoList")
    public List<QuestionInfo> selectQuestionInfoList(QuestionInfoDto questionInfo) {
        return questionInfoService.selectQuestionInfoList(questionInfo);
    }
}
