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

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.dztype.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.dztype.DzTypeDO;
import com.jojubanking.boot.module.system.convert.dztype.DzTypeConvert;
import com.jojubanking.boot.module.system.service.dztype.DzTypeService;

@Api(tags = "管理后台 - 导诊类型")
@RestController
@RequestMapping("/system/dz-type")
@Validated
public class DzTypeController {

    @Resource
    private DzTypeService dzTypeService;

    @PostMapping("/create")
    @ApiOperation("创建导诊类型")
    @PreAuthorize("@ss.hasPermission('system:dz-type:create')")
    public CommonResult<Integer> createDzType(@Valid @RequestBody DzTypeCreateReqVO createReqVO) {
        return success(dzTypeService.createDzType(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新导诊类型")
    @PreAuthorize("@ss.hasPermission('system:dz-type:update')")
    public CommonResult<Boolean> updateDzType(@Valid @RequestBody DzTypeUpdateReqVO updateReqVO) {
        dzTypeService.updateDzType(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @ApiOperation("获得导诊类型")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('system:dz-type:query')")
    public CommonResult<DzTypeRespVO> getDzType(@RequestParam("id") Integer id) {
        DzTypeDO dzType = dzTypeService.getDzType(id);
        return success(DzTypeConvert.INSTANCE.convert(dzType));
    }

    @GetMapping("/list")
    @ApiOperation("获得导诊类型列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:dz-type:query')")
    public CommonResult<List<DzTypeRespVO>> getDzTypeList(@RequestParam("ids") Collection<Integer> ids) {
        List<DzTypeDO> list = dzTypeService.getDzTypeList(ids);
        return success(DzTypeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得导诊类型分页")
    @PreAuthorize("@ss.hasPermission('system:dz-type:query')")
    public CommonResult<PageResult<DzTypeRespVO>> getDzTypePage(@Valid DzTypePageReqVO pageVO) {
        PageResult<DzTypeDO> pageResult = dzTypeService.getDzTypePage(pageVO);
        return success(DzTypeConvert.INSTANCE.convertPage(pageResult));
    }
    @GetMapping("/pagegroup")
    @ApiOperation("获得导诊类型分页")
//    @PreAuthorize("@ss.hasPermission('system:dz-type:query')")
    public CommonResult<PageResult<DzTypeRespVO>> getDzTypePageGroup(@Valid DzTypePageReqVO pageVO) {
        PageResult<DzTypeDO> pageResult = dzTypeService.getDzTypePageGroup(pageVO);
        return success(DzTypeConvert.INSTANCE.convertPage(pageResult));
    }


    @GetMapping("/typegroup")
    @ApiOperation("获得导诊类型分页")
//    @PreAuthorize("@ss.hasPermission('system:dz-type:query')")
    public CommonResult<PageResult<DzTypeRespVO>> getDzTypeGroup(@Valid DzTypePageReqVO pageVO) {
        PageResult<DzTypeDO> pageResult = dzTypeService.getDzTypeGroup(pageVO);
        return success(DzTypeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出导诊类型 Excel")
    @PreAuthorize("@ss.hasPermission('system:dz-type:export')")
    @OperateLog(type = EXPORT)
    public void exportDzTypeExcel(@Valid DzTypeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<DzTypeDO> list = dzTypeService.getDzTypeList(exportReqVO);
        // 导出 Excel
        List<DzTypeExcelVO> datas = DzTypeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "导诊类型.xls", "数据", DzTypeExcelVO.class, datas);
    }

}
