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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.hr.vo.type.SalaryTypeCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.hr.vo.type.SalaryTypeRespVO;
import cn.iocoder.yudao.module.system.controller.admin.hr.vo.type.SalaryTypeUpdateSortVO;
import cn.iocoder.yudao.module.system.service.hr.type.SalaryTypeService;
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.web.bind.annotation.*;

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

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

@Tag(name = "管理后台 - 工资类型")
@RestController
@RequestMapping("/system/hr/salary-type")
public class SalaryTypeController {

    @Resource
    private SalaryTypeService salaryTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建工资类型")
    @PreAuthorize("@ss.hasPermission('system:hr:salary-type:create')")
    public CommonResult<Long> createSalaryType(@Valid @RequestBody SalaryTypeCreateReqVO createReqVO) {
        return success(salaryTypeService.createSalaryType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工资类型")
    @PreAuthorize("@ss.hasPermission('system:hr:salary-type:update')")
    public CommonResult<Boolean> updateSalaryType(@Valid @RequestBody SalaryTypeCreateReqVO updateReqVO) {
        salaryTypeService.updateSalaryType(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "更新工资类型状态")
    @PreAuthorize("@ss.hasPermission('system:hr:salary-type:update-status')")
    public CommonResult<Boolean> updateSalaryTypeStatus(@RequestParam("id") Long id,
                                                   @RequestParam("status") Integer status) {
        salaryTypeService.updateSalaryTypeStatus(id, status);
        return success(true);
    }

    @PutMapping("/update-sort")
    @Operation(summary = "更新工资类型排序")
    @PreAuthorize("@ss.hasPermission('system:hr:salary-type:update-status')")
    public CommonResult<Boolean> updateSort(@RequestBody List<SalaryTypeUpdateSortVO> updateSortVO) {
        salaryTypeService.updateSort(updateSortVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工资类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('system:hr:salary-type:query')")
    public CommonResult<SalaryTypeRespVO> getSalaryType(@RequestParam("id") Long id) {

        return success(BeanUtils.toBean(salaryTypeService.getSalaryType(id), SalaryTypeRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得工资类型列表")
    @PreAuthorize("@ss.hasPermission('system:hr:salary-type:query')")
    public CommonResult<List<SalaryTypeRespVO>> getSalaryTypeList() {

        return success(BeanUtils.toBean(salaryTypeService.getSalaryTypeList(), SalaryTypeRespVO.class));
    }
}
