package com.xjrsoft.module.workflow.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.yulichang.toolkit.MPJWrappers;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.model.result.R;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.organization.entity.User;
import com.xjrsoft.module.workflow.dto.HistoryChangeDto;
import com.xjrsoft.module.workflow.entity.WorkflowSchemaHistory;
import com.xjrsoft.module.workflow.service.IWorkflowSchemaHistoryService;
import com.xjrsoft.module.workflow.vo.SchemaHistoryListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * <p>
 * 流程模板历史记录表 前端控制器
 * </p>
 *
 * @author tzx
 * @since 2022-11-16
 */
@RestController
@RequestMapping(GlobalConstant.WORKFLOW_MODULE_PREFIX + "/schema-history")
@Api(value = GlobalConstant.WORKFLOW_MODULE_PREFIX + "/schema-history", tags = "流程模板历史记录接口")
@AllArgsConstructor
public class WorkflowSchemaHistoryController {

    private final IWorkflowSchemaHistoryService workflowSchemaHistoryService;


    @GetMapping("/list")
    @ApiOperation(value = "流程列表（分页）")
    public R list(@RequestParam Long schemaId) {
        List<SchemaHistoryListVo> schemaHistoryListVos = workflowSchemaHistoryService.selectJoinList(SchemaHistoryListVo.class,
                MPJWrappers.<WorkflowSchemaHistory>lambdaJoin()
                        .eq(WorkflowSchemaHistory::getSchemaId, schemaId)
                        .select(WorkflowSchemaHistory::getId)
                        .select(WorkflowSchemaHistory.class, x -> VoToColumnUtil.fieldsToColumns(SchemaHistoryListVo.class).contains(x.getProperty()))
                        .selectAs(User::getName, SchemaHistoryListVo::getCreateUserName)
                        .leftJoin(User.class, User::getId, WorkflowSchemaHistory::getCreateUserId)
                        .orderByAsc(WorkflowSchemaHistory::getVersion)
        );

        List<SchemaHistoryListVo> listVos = BeanUtil.copyToList(schemaHistoryListVos, SchemaHistoryListVo.class);
        return R.ok(listVos);
    }

    @PutMapping("/set-current")
    @ApiOperation(value = "更新到此版本")
    public R change(@Valid @RequestBody HistoryChangeDto dto) {

        return R.ok(workflowSchemaHistoryService.change(dto));
    }

    @PutMapping("/processChanges")
    @ApiOperation(value = "新流程变更")
    public R processChanges(@Valid @RequestBody HistoryChangeDto dto) {
        return R.ok(workflowSchemaHistoryService.change(dto));
    }

    @GetMapping("/preview")
    @ApiOperation(value = "历史记录的xml")
    public R preview(@RequestParam String historyId){
        WorkflowSchemaHistory history = workflowSchemaHistoryService.getById(historyId);
        return R.ok(history.getXmlContent());
    }
}
