package com.docus.server.collect.basic.http.test;

import com.docus.core.util.json.JSON;
import com.docus.infrastructure.web.api.CommonResult;
import com.docus.infrastructure.web.response.PageResult;
import com.docus.log.executor.TrackRetrySpringExecutor;
import com.docus.log.handler.IJobHandler;
import com.docus.log.handler.impl.MethodJobHandler;
import com.docus.server.entity.TaskMessage;
import com.docus.server.archivefile.service.ITaskMessageService;
import com.docus.server.collect.IConverter;
import com.docus.server.collect.IHttpResult;
import com.docus.server.collect.web.process.VisitorProcessor;
import com.docus.server.collect.web.service.CollectService;
import com.docus.server.enums.CollectTypeEnum;
import com.docus.server.enums.StateEnum;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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


/**
 * @author wen yongbin
 * @date 2023年2月25日21:56:33
 * @see CollectTypeEnum 枚举
 * @see IConverter 通用转化器
 * @see IHttpResult 通用返回结果
 * @see VisitorProcessor 通用处理器
 */

@Api(value = "TEST-用户管理接口", tags = "TEST-用户管理接口")
@Slf4j
@RestController("a")
@RequestMapping("/api/rest/user")
public class TestRestController {
    @Resource
    private CollectService collectService;
    @Resource
    private ITaskMessageService messageService;

    @ApiOperation("TEST-2")
    @GetMapping("/get2")
    public void get2(String msg) throws Exception {
        try {
            TaskMessage message = messageService.findByState(StateEnum.FAIL).get(0);
            IJobHandler jobHandler = TrackRetrySpringExecutor.loadJobHandler(message.getRetryKey());
            String name = ((MethodJobHandler) jobHandler).getMethod().getParameterTypes()[0].getName();
            Object o = JSON.fromJSON(message.getJsonStr(), Class.forName(name));
            jobHandler.execute(o);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    @ApiOperation("TEST-3")
    @GetMapping("/get3")
    public CommonResult<PageResult<TaskMessage>> get3() throws Exception {
        PageHelper.startPage(1, 10);
        List<TaskMessage> list = messageService.findByState(StateEnum.OK);
        PageResult<TaskMessage> pageResult = new PageResult<>(list);

        return CommonResult.success(pageResult);
    }

    @ApiOperation("TEST-4")
    @GetMapping("/get4")
    public PageResult<TaskMessage> get4() throws Exception {
        PageHelper.startPage(1, 10);
        List<TaskMessage> list = messageService.findByState(StateEnum.OK);
        return new PageResult<>(list);
    }
}
