package com.xjrsoft.module.workflow.controller;

import com.github.yulichang.toolkit.MPJWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.organization.entity.User;
import com.xjrsoft.module.system.entity.Stamp;
import com.xjrsoft.module.workflow.dto.ApproveRecordListDto;
import com.xjrsoft.module.workflow.entity.WorkflowApproveRecord;
import com.xjrsoft.module.workflow.service.IWorkflowApproveRecordService;
import com.xjrsoft.module.workflow.vo.WorkflowApproveRecordListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tzx
 * @since 2023-02-10
 */
@RestController
@RequestMapping(GlobalConstant.WORKFLOW_MODULE_PREFIX + "/workflowApproveRecord")
@Api(value = "/workflow"  + "/workflowApproveRecord",tags = "前端控制器")
public class WorkflowApproveRecordController {
    @Autowired
    private IWorkflowApproveRecordService approveRecordService;

    @GetMapping("/approve-record-list")
    @ApiOperation(value = "打印用流转记录")
    public RT<List<WorkflowApproveRecordListVo>> approveRecordList(@Valid ApproveRecordListDto dto) {
        MPJLambdaWrapper<WorkflowApproveRecord> workflowApproveRecordMPJLambdaWrapper = MPJWrappers.<WorkflowApproveRecord>lambdaJoin()
                .disableSubLogicDel()
                .eq(WorkflowApproveRecord::getProcessId, dto.getProcessId())
                .select(WorkflowApproveRecord::getId)
                .select(WorkflowApproveRecord.class, x -> VoToColumnUtil.fieldsToColumns(WorkflowApproveRecordListVo.class).contains(x.getProperty()))
                .selectAs(User::getName, WorkflowApproveRecord::getApproveUserName)
                .leftJoin(User.class, User::getId, WorkflowApproveRecord::getApproveUserId)
                .selectAs(Stamp::getFileUrl, WorkflowApproveRecord::getApproveStampUrl)
                .leftJoin(Stamp.class, Stamp::getId, WorkflowApproveRecord::getApproveStamp);

        List<WorkflowApproveRecordListVo> approveRecords = approveRecordService.selectJoinList(WorkflowApproveRecordListVo.class, workflowApproveRecordMPJLambdaWrapper);
        return RT.ok(approveRecords);
    }
}
