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

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.kstype.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.kstype.KsTypeDO;
import com.jojubanking.boot.module.system.convert.kstype.KsTypeConvert;
import com.jojubanking.boot.module.system.service.kstype.KsTypeService;

@Api(tags = "管理后台 - 科室类别")
@RestController
@RequestMapping("/system/ks-type")
@Validated
public class KsTypeController {

    @Resource
    private KsTypeService ksTypeService;

    @PostMapping("/create")
    @ApiOperation("创建科室类别")
    @PreAuthorize("@ss.hasPermission('system:ks-type:create')")
    public CommonResult<Integer> createKsType(@Valid @RequestBody KsTypeCreateReqVO createReqVO) {
        return success(ksTypeService.createKsType(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新科室类别")
    @PreAuthorize("@ss.hasPermission('system:ks-type:update')")
    public CommonResult<Boolean> updateKsType(@Valid @RequestBody KsTypeUpdateReqVO updateReqVO) {
        ksTypeService.updateKsType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除科室类别")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('system:ks-type:delete')")
    public CommonResult<Boolean> deleteKsType(@RequestParam("id") Integer id) {
        ksTypeService.deleteKsType(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得科室类别")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('system:ks-type:query')")
    public CommonResult<KsTypeRespVO> getKsType(@RequestParam("id") Integer id) {
        KsTypeDO ksType = ksTypeService.getKsType(id);
        return success(KsTypeConvert.INSTANCE.convert(ksType));
    }

    @GetMapping("/list")
    @ApiOperation("获得科室类别列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:ks-type:query')")
    public CommonResult<List<KsTypeRespVO>> getKsTypeList(@RequestParam("ids") Collection<Integer> ids) {
        List<KsTypeDO> list = ksTypeService.getKsTypeList(ids);
        return success(KsTypeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得科室类别分页")
    @PreAuthorize("@ss.hasPermission('system:ks-type:query')")
    public CommonResult<PageResult<KsTypeRespVO>> getKsTypePage(@Valid KsTypePageReqVO pageVO) {
        PageResult<KsTypeDO> pageResult = ksTypeService.getKsTypePage(pageVO);
        return success(KsTypeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出科室类别 Excel")
    @PreAuthorize("@ss.hasPermission('system:ks-type:export')")
    @OperateLog(type = EXPORT)
    public void exportKsTypeExcel(@Valid KsTypeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<KsTypeDO> list = ksTypeService.getKsTypeList(exportReqVO);
        // 导出 Excel
        List<KsTypeExcelVO> datas = KsTypeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "科室类别.xls", "数据", KsTypeExcelVO.class, datas);
    }

}
