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.salaryadjustment.BpmOASalaryAdjustmentCreateReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.salaryadjustment.BpmOASalaryAdjustmentRespVO;
import cn.iocoder.yudao.module.bpm.convert.oa.BpmOASalaryAdjustmentConvert;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOASalaryAdjustmentDO;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOASalaryAdjustmentService;
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.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
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/salary-adjustment")
@Validated
public class BpmOASalaryAdjustmentController {
    
    @Resource
    private BpmOASalaryAdjustmentService salaryService;

    @Resource
    private DeptApi deptApi;

    @Resource
    private AdminUserApi userApi;

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

        return success(salaryService.createSalaryAdjustment(getLoginUserId(), createReqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "获得薪资付款申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BpmOASalaryAdjustmentRespVO> getSalaryAdjustment(@RequestParam("id") Long id) {

        BpmOASalaryAdjustmentDO salary = salaryService.getSalaryAdjustment(id);
        if (salary == null) {
            return success(new BpmOASalaryAdjustmentRespVO());
        }

        // 获得用户详情
        AdminUserRespDTO userRespDTO = userApi.getUser(salary.getAdjustmentUserId()).getCheckedData();

        // 获取部门详情
        DeptRespDTO dto = getDept(salary.getAdjustmentDeptId());

        BpmOASalaryAdjustmentRespVO respVO = BpmOASalaryAdjustmentConvert.INSTANCE.convert(salary)
                .setAdjustmentDeptName(dto.getName())
                .setAdjustmentUserName(userRespDTO.getNickname());

        return success(respVO);
    }

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

        BpmOASalaryAdjustmentDO salary = salaryService.getByProcessInstanceId(processInstanceId);
        if (salary == null) {
            return success(new BpmOASalaryAdjustmentRespVO());
        }

        // 获得用户详情
        AdminUserRespDTO userRespDTO = userApi.getUser(salary.getAdjustmentUserId()).getCheckedData();

        // 获取部门详情
        DeptRespDTO dto = getDept(salary.getAdjustmentDeptId());

        BpmOASalaryAdjustmentRespVO respVO = BpmOASalaryAdjustmentConvert.INSTANCE.convert(salary)
                .setAdjustmentDeptName(dto.getName())
                .setAdjustmentUserName(userRespDTO.getNickname());

        return success(respVO);
    }

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

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