package com.engine.htsc.contacts.web;


import com.alibaba.fastjson.JSON;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.contacts.service.ContactsService;
import weaver.general.BaseBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

public class ContactsAction {

    ContactsService getService(){
        return ServiceUtil.getService(ContactsService.class);
    }

    @POST
    @Path("/getTopContacts")
    @Produces({MediaType.TEXT_PLAIN})
    public String getTopContacts(@Context HttpServletRequest request, @Context HttpServletResponse response){
        BaseBean bs = new BaseBean();
        Map<String, Object> apidatas = new HashMap<String, Object>();
        bs.writeLog("----ContactsAction-----");
        try{
            String userid = request.getParameter("userid");

            ContactsService contactsService = getService();
            apidatas = contactsService.getTopContacts(userid);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }

        return JSON.toJSONString(apidatas);
    }

    @POST
    @Path("/checkoutFreeNode")
    @Produces({MediaType.TEXT_PLAIN})
    public String checkoutFreeNode(@Context HttpServletRequest request, @Context HttpServletResponse response){
        BaseBean bs = new BaseBean();
        Map<String, Object> apidatas = new HashMap<String, Object>();
        bs.writeLog("----ContactsAction-----");
        try{
            String nodeid = request.getParameter("nodeid");

            ContactsService contactsService = getService();
            apidatas = contactsService.checkoutFreeNode(nodeid);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }

        return JSON.toJSONString(apidatas);
    }
}
