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

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.doctor.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.doctor.DoctorDO;
import com.jojubanking.boot.module.system.convert.doctor.DoctorConvert;
import com.jojubanking.boot.module.system.service.doctor.DoctorService;

@Api(tags = "管理后台 - 医生信息")
@RestController
@RequestMapping("/system/doctor")
@Validated
public class DoctorController {

    @Resource
    private DoctorService doctorService;

    @PostMapping("/create")
    @ApiOperation("创建医生信息")
    @PreAuthorize("@ss.hasPermission('system:doctor:create')")
    public CommonResult<Integer> createDoctor(@Valid @RequestBody DoctorCreateReqVO createReqVO) {
        return success(doctorService.createDoctor(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新医生信息")
    @PreAuthorize("@ss.hasPermission('system:doctor:update')")
    public CommonResult<Boolean> updateDoctor(@Valid @RequestBody DoctorUpdateReqVO updateReqVO) {
        doctorService.updateDoctor(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @ApiOperation("获得医生信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('system:doctor:query')")
    public CommonResult<DoctorRespVO> getDoctor(@RequestParam("id") Integer id) {
        DoctorDO doctor = doctorService.getDoctor(id);
        return success(DoctorConvert.INSTANCE.convert(doctor));
    }

    @GetMapping("/list")
    @ApiOperation("获得医生信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:doctor:query')")
    public CommonResult<List<DoctorRespVO>> getDoctorList(@RequestParam("ids") Collection<Integer> ids) {
        List<DoctorDO> list = doctorService.getDoctorList(ids);
        return success(DoctorConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得医生信息分页")
    @PreAuthorize("@ss.hasPermission('system:doctor:query')")
    public CommonResult<PageResult<DoctorRespVO>> getDoctorPage(@Valid DoctorPageReqVO pageVO) {
        PageResult<DoctorDO> pageResult = doctorService.getDoctorPage(pageVO);
        return success(DoctorConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出医生信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:doctor:export')")
    @OperateLog(type = EXPORT)
    public void exportDoctorExcel(@Valid DoctorExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<DoctorDO> list = doctorService.getDoctorList(exportReqVO);
        // 导出 Excel
        List<DoctorExcelVO> datas = DoctorConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "医生信息.xls", "数据", DoctorExcelVO.class, datas);
    }

}
