package cn.iocoder.yudao.module.system.controller.admin.biz;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.StaffingPlanRecordPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.StaffingPlanRecordRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.StaffingPlanRecordSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.StaffingPlanRecordDO;
import cn.iocoder.yudao.module.system.service.biz.StaffingPlanRecordService;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 编制计划记录
 */
@Tag(name = "管理后台 - 编制计划记录")
@RestController
@RequestMapping("/staffing-plan-record")
@Validated
public class StaffingPlanRecordController {

    @Resource
    private StaffingPlanRecordService staffingPlanRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建编制计划记录")
    public CommonResult<Long> create(@Valid @RequestBody StaffingPlanRecordSaveReqVO createReqVO) {
        createReqVO.setTenantId(Objects.isNull(createReqVO.getTenantId())
                ? TenantContextHolder.getTenantId() : createReqVO.getTenantId());
        return success(staffingPlanRecordService.createStaffingPlanRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新编制计划记录")
    public CommonResult<Boolean> update(@Valid @RequestBody StaffingPlanRecordSaveReqVO updateReqVO) {
        updateReqVO.setTenantId(Objects.isNull(updateReqVO.getTenantId())
                ? TenantContextHolder.getTenantId() : updateReqVO.getTenantId());
        staffingPlanRecordService.updateStaffingPlanRecord(updateReqVO);
        return success(Boolean.TRUE);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除编制计划记录")
    @Parameter(name = "id", description = "记录编号", required = true)
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        staffingPlanRecordService.deleteStaffingPlanRecord(id);
        return success(Boolean.TRUE);
    }

    @GetMapping("/get")
    @Operation(summary = "获得编制计划记录")
    @Parameter(name = "id", description = "记录编号", required = true, example = "1024")
    public CommonResult<StaffingPlanRecordRespVO> get(@RequestParam("id") Long id) {
        StaffingPlanRecordDO record = staffingPlanRecordService.getStaffingPlanRecord(id);
        return success(BeanUtils.toBean(record, StaffingPlanRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得编制计划记录分页")
    public CommonResult<PageResult<StaffingPlanRecordRespVO>> getPage(@Valid StaffingPlanRecordPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds())
                ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        PageResult<StaffingPlanRecordDO> pageResult = staffingPlanRecordService.getStaffingPlanRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StaffingPlanRecordRespVO.class));
    }
}

