package cn.iocoder.yudao.module.employee.controller.admin.employeeinfo;


import java.util.*;
import java.io.IOException;


import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.employee.controller.admin.employeeinfo.vo.*;
import cn.iocoder.yudao.module.employee.dal.dataobject.employeeinfo.EmployeeInfoDO;
import cn.iocoder.yudao.module.employee.service.employeeinfo.EmployeeInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 员工信息")
@RestController
@RequestMapping("/presonnel/employee")
@Validated
public class EmployeeInfoController {

    @Resource
    private EmployeeInfoService infoService;

    @PostMapping("/create")
    @Operation(summary = "创建员工信息")
//    @PreAuthorize("@ss.hasPermission('employee:info:create')")
    public CommonResult<Long> createInfo(@Valid @RequestBody EmployeeInfoSaveReqVO createReqVO) {
        return success(infoService.createInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新员工信息")
    @PreAuthorize("@ss.hasPermission('employee:info:update')")
    public CommonResult<Boolean> updateInfo(@Valid @RequestBody EmployeeInfoSaveReqVO updateReqVO) {
        infoService.updateInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除员工信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('employee:info:delete')")
    public CommonResult<Boolean> deleteInfo(@RequestParam("id") Long id) {
        infoService.deleteInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得员工信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('employee:info:query')")
    public CommonResult<EmployeeInfoRespVO> getInfo(@RequestParam("id") Long id) {
        EmployeeInfoDO info = infoService.getInfo(id);
        return success(BeanUtils.toBean(info, EmployeeInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得员工信息分页")
    @PreAuthorize("@ss.hasPermission('employee:info:query')")
    public CommonResult<PageResult<EmployeeInfoRespVO>> getInfoPage(@Valid EmployeeInfoPageReqVO pageReqVO) {
        PageResult<EmployeeInfoDO> pageResult = infoService.getInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EmployeeInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出员工信息 Excel")
    @PreAuthorize("@ss.hasPermission('employee:info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInfoExcel(@Valid EmployeeInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EmployeeInfoDO> list = infoService.getInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "员工信息.xls", "数据", EmployeeInfoRespVO.class,
                        BeanUtils.toBean(list, EmployeeInfoRespVO.class));
    }

}