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

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.ksinfo.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.ksinfo.KsInfoDO;
import com.jojubanking.boot.module.system.convert.ksinfo.KsInfoConvert;
import com.jojubanking.boot.module.system.service.ksinfo.KsInfoService;

@Api(tags = "管理后台 - 科室信息")
@RestController
@RequestMapping("/system/ks-info")
@Validated
public class KsInfoController {

    @Resource
    private KsInfoService ksInfoService;

    @PostMapping("/create")
    @ApiOperation("创建科室信息")
    @PreAuthorize("@ss.hasPermission('system:ks-info:create')")
    public CommonResult<Integer> createKsInfo(@Valid @RequestBody KsInfoCreateReqVO createReqVO) {
        return success(ksInfoService.createKsInfo(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新科室信息")
    @PreAuthorize("@ss.hasPermission('system:ks-info:update')")
    public CommonResult<Boolean> updateKsInfo(@Valid @RequestBody KsInfoUpdateReqVO updateReqVO) {
        ksInfoService.updateKsInfo(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @ApiOperation("获得科室信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('system:ks-info:query')")
    public CommonResult<KsInfoRespVO> getKsInfo(@RequestParam("id") Integer id) {
        KsInfoDO ksInfo = ksInfoService.getKsInfo(id);
        return success(KsInfoConvert.INSTANCE.convert(ksInfo));
    }

    @GetMapping("/list")
    @ApiOperation("获得科室信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:ks-info:query')")
    public CommonResult<List<KsInfoRespVO>> getKsInfoList(@RequestParam("ids") Collection<Integer> ids) {
        List<KsInfoDO> list = ksInfoService.getKsInfoList(ids);
        return success(KsInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得科室信息分页")
    @PreAuthorize("@ss.hasPermission('system:ks-info:query')")
    public CommonResult<PageResult<KsInfoRespVO>> getKsInfoPage(@Valid KsInfoPageReqVO pageVO) {
        PageResult<KsInfoDO> pageResult = ksInfoService.getKsInfoPage(pageVO);
        return success(KsInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/dzinfo")
    @ApiOperation("获得科室信息分页")
    @PreAuthorize("@ss.hasPermission('system:ks-info:query')")
    public CommonResult<PageResult<KsInfoRespVO>> getDzInfoPage(@Valid KsInfoPageReqVO pageVO) {
        PageResult<KsInfoDO> pageResult = ksInfoService.getDzInfoPage(pageVO);
        return success(KsInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出科室信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:ks-info:export')")
    @OperateLog(type = EXPORT)
    public void exportKsInfoExcel(@Valid KsInfoExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<KsInfoDO> list = ksInfoService.getKsInfoList(exportReqVO);
        // 导出 Excel
        List<KsInfoExcelVO> datas = KsInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "科室信息.xls", "数据", KsInfoExcelVO.class, datas);
    }

}
