package com.fs.survey;

import com.fs.common.annotation.Log;
import com.fs.common.config.LoginContextManager;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.survey.service.ISurveyDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 问卷数据Controller
 *
 */
@RestController
@RequestMapping("/survey/data")
public class SurveyDataController extends BaseController {

    @Autowired
    private ISurveyDataService surveyDataService;

    /**
     * 查询问卷数据列表
     */
    @PreAuthorize("@ss.hasPermi('survey:data:list')")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam Map<String, Object> params) {
        startPage();

        List<Map<String, Object>> list = surveyDataService.selectSurveyDataList(params);
        return getDataTable(list);
    }

    /**
     * 获取问卷数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('survey:data:query')")
    @GetMapping(value = "/{versionId}/{id}")
    public AjaxResult getInfo(@PathVariable("versionId") Long versionId, @PathVariable("id") Long id) {
        return AjaxResult.success(surveyDataService.selectSurveyDataById(versionId, id));
    }

    /**
     * 提交问卷数据
     */
    @Log(title = "提交问卷", businessType = BusinessType.INSERT)
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody Map<String, Object> data) {
        return toAjax(surveyDataService.submitSurveyData(data));
    }

    /**
     * 修改问卷数据
     */
    @PreAuthorize("@ss.hasPermi('survey:data:edit')")
    @Log(title = "问卷数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Map<String, Object> data) {
        return toAjax(surveyDataService.updateSurveyData(data));
    }

    /**
     * 删除问卷数据
     */
    @PreAuthorize("@ss.hasPermi('survey:data:remove')")
    @Log(title = "问卷数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{versionId}/{ids}")
    public AjaxResult remove(@PathVariable Long versionId, @PathVariable Long[] ids) {
        return toAjax(surveyDataService.deleteSurveyDataByIds(versionId, ids));
    }

    /**
     * 导出问卷数据
     */
    @PreAuthorize("@ss.hasPermi('survey:data:export')")
    @Log(title = "问卷数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestParam Map<String, Object> params) throws IOException {
//        List<Map<String, Object>> list = surveyDataService.selectSurveyDataList(params);
//        ExcelUtil<Map> util = new ExcelUtil<>(Map.class);
//        util.exportExcel(response, list, "问卷数据");
    }

    /**
     * 数据统计
     */
    @PreAuthorize("@ss.hasPermi('survey:data:statistics')")
    @GetMapping("/statistics/{versionId}")
    public AjaxResult statistics(@PathVariable Long versionId) {
        return AjaxResult.success(surveyDataService.getStatistics(versionId));
    }
}
