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.SalaryTypeItemCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.hr.vo.type.SalaryTypeItemRespVO;
import cn.iocoder.yudao.module.system.service.hr.type.SalaryTypeItemService;
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-item")
public class SalaryTypeItemController {

    @Resource
    private SalaryTypeItemService salaryTypeItemService;

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

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

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

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

        return success(BeanUtils.toBean(salaryTypeItemService.getSalaryTypeItem(id), SalaryTypeItemRespVO.class));
    }

    @GetMapping("/listByTypeId")
    @Operation(summary = "获得工资类型配置列表")
    @Parameter(name = "typeId", description = "类型编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('system:hr:salary-type-item:query')")
    public CommonResult<List<SalaryTypeItemRespVO>> getSalaryTypeList(@RequestParam("typeId") Long typeId) {

        return success(BeanUtils.toBean(salaryTypeItemService.getListByTypeId(typeId), SalaryTypeItemRespVO.class));
    }
}
