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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.entry.BpmOAEntryCreateReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.entry.BpmOAEntryRespVO;
import cn.iocoder.yudao.module.bpm.convert.oa.BpmOAEntryConvert;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAEntryDO;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOAEntryService;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.PostApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.api.dept.dto.PostRespVO;
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.annotation.security.PermitAll;
import javax.validation.Valid;

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

/**
 * OA 入职申请 Controller
 *
 * @author 符溶馨
 */
@Tag(name = "管理后台 - OA 入职申请")
@RestController
@RequestMapping("/bpm/oa/entry")
@Validated
public class BpmOAEntryController {

    @Resource
    private BpmOAEntryService entryService;

    @Resource
    private DeptApi deptApi;

    @Resource
    private PostApi postApi;

    @Resource
    private PositionApi positionApi;

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

        return success(entryService.createEntry(createReqVO));
    }

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

        BpmOAEntryDO entry = entryService.getEntry(id);

        BpmOAEntryRespVO respVO = BpmOAEntryConvert.INSTANCE.convert(entry);
        // 设备部门名称
        respVO.setEntryDeptName(getDept(entry.getEntryDeptId()).getName());
        // 设置职称
        respVO.setEntryPostName(getPost(entry.getEntryPostId()).getName());
        // 设置岗位名称
        respVO.setEntryPositionName(getPosition(entry.getEntryPositionId()).getName());
        // 设置公司名称
        respVO.setContractCompanyName(getDept(entry.getContractCompany()).getName());

        return success(respVO);
    }

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

        BpmOAEntryDO entry = entryService.getEntryByProcessInstanceId(processInstanceId);

        BpmOAEntryRespVO respVO = BpmOAEntryConvert.INSTANCE.convert(entry);
        // 设备部门名称
        respVO.setEntryDeptName(getDept(entry.getEntryDeptId()).getName());
        // 设备岗位名称
        respVO.setEntryPostName(getPost(entry.getEntryPostId()).getName());
        // 设置岗位名称
        respVO.setEntryPositionName(getPosition(entry.getEntryPositionId()).getName());
        // 设置公司名称
        respVO.setContractCompanyName(getDept(entry.getContractCompany()).getName());

        return success(respVO);
    }

    /**
     * 获得部门信息
     * @param deptId 部门id
     * @return 部门信息
     */
    public DeptRespDTO getDept(Long deptId) {

        if (deptId == null) {
            return new DeptRespDTO();
        }
        return deptApi.getDept(deptId).getCheckedData();
    }

    /**
     * 获得岗位信息
     * @param postId 岗位ID
     * @return 岗位信息
     */
    public PostRespVO getPost(Long postId) {

        if (postId == null) {
            return new PostRespVO();
        }
        return postApi.getPost(postId).getCheckedData();
    }

    public PositionDTO getPosition(Long positionId) {
        if (positionId == null) {
            return new PositionDTO();
        }
        return positionApi.getPosition(positionId).getCheckedData();
    }
}
