package cn.iocoder.yudao.module.smartfactory.controller.admin.staff;

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.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.smartfactory.controller.admin.staff.vo.StaffPageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.staff.vo.StaffRespVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.staff.vo.StaffSaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.staff.StaffDO;
import cn.iocoder.yudao.module.smartfactory.service.staff.StaffService;
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 java.io.IOException;
import java.util.List;

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

@Tag(name = "管理后台 - 员工")
@RestController
@RequestMapping("/factory/staff")
@Validated
public class StaffController {

    @Resource
    private StaffService staffService;

    @PostMapping("/create")
    @Operation(summary = "创建员工")
    @PreAuthorize("@ss.hasPermission('factory:staff:create')")
    public CommonResult<Long> createStaff(@Valid @RequestBody StaffSaveReqVO createReqVO) {
        return success(staffService.createStaff(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新员工")
    @PreAuthorize("@ss.hasPermission('factory:staff:update')")
    public CommonResult<Boolean> updateStaff(@Valid @RequestBody StaffSaveReqVO updateReqVO) {
        staffService.updateStaff(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得员工")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('factory:staff:query')")
    public CommonResult<StaffRespVO> getStaff(@RequestParam("id") Long id) {
        StaffDO staff = staffService.getStaff(id);
        return success(BeanUtils.toBean(staff, StaffRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得员工分页")
    @PreAuthorize("@ss.hasPermission('factory:staff:query')")
    public CommonResult<PageResult<StaffRespVO>> getStaffPage(@Valid StaffPageReqVO pageReqVO) {
        PageResult<StaffDO> pageResult = staffService.getStaffPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StaffRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出员工 Excel")
    @PreAuthorize("@ss.hasPermission('factory:staff:export')")
    @OperateLog(type = EXPORT)
    public void exportStaffExcel(@Valid StaffPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StaffDO> list = staffService.getStaffPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "员工.xls", "数据", StaffRespVO.class,
                        BeanUtils.toBean(list, StaffRespVO.class));
    }

}