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

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.hospital.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.hospital.HospitalDO;
import com.jojubanking.boot.module.system.convert.hospital.HospitalConvert;
import com.jojubanking.boot.module.system.service.hospital.HospitalService;

@Api(tags = "管理后台 - 医院信息")
@RestController
@RequestMapping("/system/hospital")
@Validated
public class HospitalController {

    @Resource
    private HospitalService hospitalService;

    @PostMapping("/create")
    @ApiOperation("创建医院信息")
    @PreAuthorize("@ss.hasPermission('system:hospital:create')")
    public CommonResult<Integer> createHospital(@Valid @RequestBody HospitalCreateReqVO createReqVO) {
        return success(hospitalService.createHospital(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新医院信息")
    @PreAuthorize("@ss.hasPermission('system:hospital:update')")
    public CommonResult<Boolean> updateHospital(@Valid @RequestBody HospitalUpdateReqVO updateReqVO) {
        hospitalService.updateHospital(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除医院信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('system:hospital:delete')")
    public CommonResult<Boolean> deleteHospital(@RequestParam("id") Integer id) {
        hospitalService.deleteHospital(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得医院信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('system:hospital:query')")
    public CommonResult<HospitalRespVO> getHospital(@RequestParam("id") Integer id) {
        HospitalDO hospital = hospitalService.getHospital(id);
        return success(HospitalConvert.INSTANCE.convert(hospital));
    }

    @GetMapping("/list")
    @ApiOperation("获得医院信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:hospital:query')")
    public CommonResult<List<HospitalRespVO>> getHospitalList(@RequestParam("ids") Collection<Integer> ids) {
        List<HospitalDO> list = hospitalService.getHospitalList(ids);
        return success(HospitalConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得医院信息分页")
    @PreAuthorize("@ss.hasPermission('system:hospital:query')")
    public CommonResult<PageResult<HospitalRespVO>> getHospitalPage(@Valid HospitalPageReqVO pageVO) {
        PageResult<HospitalDO> pageResult = hospitalService.getHospitalPage(pageVO);
        return success(HospitalConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出医院信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:hospital:export')")
    @OperateLog(type = EXPORT)
    public void exportHospitalExcel(@Valid HospitalExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<HospitalDO> list = hospitalService.getHospitalList(exportReqVO);
        // 导出 Excel
        List<HospitalExcelVO> datas = HospitalConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "医院信息.xls", "数据", HospitalExcelVO.class, datas);
    }

}
