package com.jojubanking.boot.module.system.controller.admin.dzquestion;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.jojubanking.boot.framework.common.pojo.PageResult;
import com.jojubanking.boot.framework.common.pojo.CommonResult;
import static com.jojubanking.boot.framework.common.pojo.CommonResult.success;

import com.jojubanking.boot.framework.excel.core.util.ExcelUtils;

import com.jojubanking.boot.framework.operatelog.core.annotations.OperateLog;
import static com.jojubanking.boot.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.jojubanking.boot.module.system.controller.admin.dzquestion.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.dzquestion.DzQuestionDO;
import com.jojubanking.boot.module.system.convert.dzquestion.DzQuestionConvert;
import com.jojubanking.boot.module.system.service.dzquestion.DzQuestionService;

@Api(tags = "管理后台 - 导诊管理")
@RestController
@RequestMapping("/system/dz-question")
@Validated
public class DzQuestionController {

    @Resource
    private DzQuestionService dzQuestionService;

    @PostMapping("/create")
    @ApiOperation("创建导诊管理")
    @PreAuthorize("@ss.hasPermission('system:dz-question:create')")
    public CommonResult<Integer> createDzQuestion(@Valid @RequestBody DzQuestionCreateReqVO createReqVO) {
        return success(dzQuestionService.createDzQuestion(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新导诊管理")
    @PreAuthorize("@ss.hasPermission('system:dz-question:update')")
    public CommonResult<Boolean> updateDzQuestion(@Valid @RequestBody DzQuestionUpdateReqVO updateReqVO) {
        dzQuestionService.updateDzQuestion(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除导诊管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('system:dz-question:delete')")
    public CommonResult<Boolean> deleteDzQuestion(@RequestParam("id") Integer id) {
        dzQuestionService.deleteDzQuestion(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得导诊管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('system:dz-question:query')")
    public CommonResult<DzQuestionRespVO> getDzQuestion(@RequestParam("id") Integer id) {
        DzQuestionDO dzQuestion = dzQuestionService.getDzQuestion(id);
        return success(DzQuestionConvert.INSTANCE.convert(dzQuestion));
    }

    @GetMapping("/list")
    @ApiOperation("获得导诊管理列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:dz-question:query')")
    public CommonResult<List<DzQuestionRespVO>> getDzQuestionList(@RequestParam("ids") Collection<Integer> ids) {
        List<DzQuestionDO> list = dzQuestionService.getDzQuestionList(ids);
        return success(DzQuestionConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得导诊管理分页")
    @PreAuthorize("@ss.hasPermission('system:dz-question:query')")
    public CommonResult<PageResult<DzQuestionRespVO>> getDzQuestionPage(@Valid DzQuestionPageReqVO pageVO) {
        PageResult<DzQuestionDO> pageResult = dzQuestionService.getDzQuestionPage(pageVO);
        return success(DzQuestionConvert.INSTANCE.convertPage(pageResult));
    }


    @GetMapping("/export-excel")
    @ApiOperation("导出导诊管理 Excel")
    @PreAuthorize("@ss.hasPermission('system:dz-question:export')")
    @OperateLog(type = EXPORT)
    public void exportDzQuestionExcel(@Valid DzQuestionExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<DzQuestionDO> list = dzQuestionService.getDzQuestionList(exportReqVO);
        // 导出 Excel
        List<DzQuestionExcelVO> datas = DzQuestionConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "导诊管理.xls", "数据", DzQuestionExcelVO.class, datas);
    }

}
