package com.engine.organization.web;

import com.engine.common.util.ServiceUtil;
import com.engine.organization.entity.po.Demo;
import com.engine.organization.service.TestService;
import com.engine.organization.util.response.ReturnResult;
import com.engine.organization.wrapper.DemoWrapper;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

/**
 * @Author weaver_cl
 * @description:
 * @Date 2022/4/26
 * @Version V1.0
 **/
public class DemoController {

    public DemoWrapper getDemoWrapper(User user){
        return ServiceUtil.getService(DemoWrapper.class,user);
    }

    @GET
    @Path("/test")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult testDemo(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                 @QueryParam("name") String name) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return getDemoWrapper(user).testDemo(name);
        }catch (Exception e) {
            return ReturnResult.exceptionHandle(e.getMessage());
        }
    }


    /**
     * 方案2 基于weaverboot 实现后端架构（推荐使用）
     */
    //@WeaAutowired
    private TestService testService;

    @GET
    @Path("/get")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult testGet(@Context HttpServletRequest request, @Context HttpServletResponse response,
                          @QueryParam("name") String name) {
        try {
            Demo list = testService.list(name);
            return ReturnResult.successed(list);
        }catch (Exception e) {
            return ReturnResult.exceptionHandle(e.getMessage());
        }
    }


}
