package cn.iocoder.yudao.module.bpm.controller.admin.oa;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.hire.BpmOAHireCreateReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.hire.BpmOAHireRespVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAHireDO;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOAHireService;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.api.position.PositionApi;
import cn.iocoder.yudao.module.system.api.position.dto.PositionDTO;
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 static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * OA 招聘申请 Controller
 *
 * @author 符溶馨
 */
@Tag(name = "管理后台 - OA 招聘申请")
@RestController
@RequestMapping("/bpm/oa/hire")
@Validated
public class BpmOAHireController {

    @Resource
    private BpmOAHireService bpmOAHireService;

    @Resource
    private DeptApi deptApi;

    @Resource
    private PositionApi positionApi;

    @PostMapping("/create")
    @Operation(summary = "创建请求申请")
    public CommonResult<Long> createHire(@Valid @RequestBody BpmOAHireCreateReqVO createReqVO) {

        return success(bpmOAHireService.createHire(getLoginUserId(), createReqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "获得招聘申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BpmOAHireRespVO> getHire(@RequestParam("id") Long id) {

        BpmOAHireDO bpmOAHireDO = bpmOAHireService.getHire(id);

        return success(setBpmOAHireRespVOFun(bpmOAHireDO));
    }

    @GetMapping("/getByProcessInstanceId")
    @Operation(summary = "获得招聘申请")
    @Parameter(name = "processInstanceId", description = "流程实例编号", required = true, example = "1024")
    public CommonResult<BpmOAHireRespVO> getCashByProcessInstanceId(@RequestParam("processInstanceId") String processInstanceId) {

        BpmOAHireDO bpmOAHireDO = bpmOAHireService.getByProcessInstanceId(processInstanceId);

        return success(setBpmOAHireRespVOFun(bpmOAHireDO));
    }

    private BpmOAHireRespVO setBpmOAHireRespVOFun(BpmOAHireDO bpmOAHireDO) {

        BpmOAHireRespVO respVO = BeanUtils.toBean(bpmOAHireDO, BpmOAHireRespVO.class);
        if (respVO != null) {
            // 获取部门信息
            DeptRespDTO deptRespDTO = deptApi.getDept(bpmOAHireDO.getDemandDeptId()).getCheckedData();
            // 获取岗位信息
            PositionDTO positionDTO = positionApi.getPosition(bpmOAHireDO.getDemandPositionId()).getCheckedData();

            // 设置需求部门名称
            respVO.setDemandDeptName(deptRespDTO.getName());
            // 设置岗位名称
            respVO.setDemandPositionName(positionDTO.getName());
        }

        return respVO;
    }
}
