package cn.axzo.workflow.server.controller.web.es;

import cn.axzo.workflow.client.feign.es.EsProcessInstanceApi;
import cn.axzo.workflow.common.model.request.es.InstanceSearchReqDTO;
import cn.axzo.workflow.common.model.request.es.TaskSearchReqDTO;
import cn.axzo.workflow.common.model.response.BpmPageResult;
import cn.axzo.workflow.common.model.response.es.ProcessInstanceDocumentVO;
import cn.axzo.workflow.common.model.response.es.ProcessTaskDocumentVO;
import cn.axzo.workflow.es.service.aggregation.AggregateProcessInstanceService;
import cn.axzo.workflow.es.service.aggregation.AggregateProcessTaskService;
import cn.azxo.framework.common.model.CommonResponse;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * ES 搜索相关操作
 *
 * @author wangli
 * @since 2024-09-26 14:47
 */
@Slf4j
@RequestMapping({"/web/v1/api/es", "/api/es"})
@RestController
@Validated
public class ElasticSearchController implements EsProcessInstanceApi {
    @Resource
    private AggregateProcessInstanceService aggregateProcessInstanceService;
    @Autowired
    private AggregateProcessTaskService aggregateProcessTaskService;

    /**
     * 审批数据搜索
     *
     * @param dto
     * @return
     */
    @PostMapping("/instance/search")
    public CommonResponse<BpmPageResult<ProcessInstanceDocumentVO>> searchInstanceInEs(@Validated @RequestBody InstanceSearchReqDTO dto) {
        log.info("审批数据搜索 searchInstanceInEs===>>>参数：{}", JSONUtil.toJsonStr(dto));
        return CommonResponse.success(aggregateProcessInstanceService.search(dto));
    }

    /**
     * 审批任务数据搜索
     *
     * @param dto
     * @return
     */
    @PostMapping("/task/search")
    public CommonResponse<List<ProcessTaskDocumentVO>> searchTaskInEs(@Validated @RequestBody TaskSearchReqDTO dto) {
        log.info("审批任务数据搜索 searchTaskInEs===>>>参数：{}", JSONUtil.toJsonStr(dto));
        return CommonResponse.success(aggregateProcessTaskService.search(dto));
    }

}
