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.shiftjobs.BpmOAShiftjobsCreateReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.shiftjobs.BpmOAShiftjobsRespVO;
import cn.iocoder.yudao.module.bpm.convert.oa.BpmOAShiftjobsConvert;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAShiftjobsDO;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOAShiftjobsService;
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.user.AdminUserApi;
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/shiftjobs")
@Validated
public class BpmOAShiftjobsController {

    @Resource
    private BpmOAShiftjobsService shiftjobsService;

    @Resource
    private DeptApi deptApi;

    @Resource
    private AdminUserApi userApi;

    @Resource
    private PostApi postApi;

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

        return success(shiftjobsService.createShiftjobs(getLoginUserId(), createReqVO));
    }

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

        BpmOAShiftjobsDO shiftjobs = shiftjobsService.getShiftjobs(id);

        BpmOAShiftjobsRespVO bpmOAShiftjobsRespVO = BpmOAShiftjobsConvert.INSTANCE.convert(shiftjobs);
        //设置原部门名称
        bpmOAShiftjobsRespVO.setOldDeptName(deptApi.getDept(shiftjobs.getOldDeptId()).getCheckedData().getName());
        //设置原岗位名称
        bpmOAShiftjobsRespVO.setOldPostName(postApi.getPost(shiftjobs.getOldPostId()).getCheckedData().getName());
        //设置原负责人昵称
        bpmOAShiftjobsRespVO.setOldParentName(userApi.getUser(shiftjobs.getOldParentId()).getCheckedData().getNickname());

        //设置新部门名称
        bpmOAShiftjobsRespVO.setNewDeptName(deptApi.getDept(shiftjobs.getNewDeptId()).getCheckedData().getName());
        //设置新岗位名称
        bpmOAShiftjobsRespVO.setNewPostName(postApi.getPost(shiftjobs.getNewPostId()).getCheckedData().getName());
        //设置新负责人昵称
        bpmOAShiftjobsRespVO.setNewParentName(userApi.getUser(shiftjobs.getNewParentId()).getCheckedData().getNickname());


        return success(bpmOAShiftjobsRespVO);
    }
}
