package com.engine.zjrb.pattern.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.zjrb.pattern.service.PatternUnlockingService;
import com.engine.zjrb.pattern.service.impl.PatternUnlockingServiceImpl;
import weaver.general.BaseBean;
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.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;


public class PatternUnlockingAction {

    public PatternUnlockingService getService(){
        return (PatternUnlockingService) ServiceUtil.getService(PatternUnlockingServiceImpl.class);
    }

    @GET
    @Path("/query")
    @Produces({MediaType.TEXT_PLAIN})
    public String query(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String,Object> apidatas = new HashMap<String,Object>();

        BaseBean bb = new BaseBean();
        bb.writeLog("query");
        User user = HrmUserVarify.getUser(request , response) ;

        try{
            Map<String,Object> params = ParamUtil.request2Map(request);
            apidatas.putAll(getService().query(params,user));
            apidatas.put("api_status",true);
        }catch (Exception e){
            apidatas.put("api_status",false);
            apidatas.put("api_errormsg","exception:"+e.getMessage());
        }
        bb.writeLog("apidatas:"+ JSONObject.toJSONString(apidatas));
        return JSONObject.toJSONString(apidatas);
    }


    @GET
    @Path("/add")
    @Produces({MediaType.TEXT_PLAIN})
    public String add(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String,Object> apidatas = new HashMap<String,Object>();

        BaseBean bb = new BaseBean();
        bb.writeLog("query");
        User user = HrmUserVarify.getUser(request , response) ;

        try{
            Map<String,Object> params = ParamUtil.request2Map(request);
            apidatas.putAll(getService().add(params,user));
            apidatas.put("api_status",true);
        }catch (Exception e){
            apidatas.put("api_status",false);
            apidatas.put("api_errormsg","exception:"+e.getMessage());
        }
        bb.writeLog("apidatas:"+ JSONObject.toJSONString(apidatas));
        return JSONObject.toJSONString(apidatas);
    }


    @GET
    @Path("/update")
    @Produces({MediaType.TEXT_PLAIN})
    public String update(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String,Object> apidatas = new HashMap<String,Object>();

        BaseBean bb = new BaseBean();
        bb.writeLog("query");
        User user = HrmUserVarify.getUser(request , response) ;

        try{
            Map<String,Object> params = ParamUtil.request2Map(request);
            apidatas.putAll(getService().update(params,user));
            apidatas.put("api_status",true);
        }catch (Exception e){
            apidatas.put("api_status",false);
            apidatas.put("api_errormsg","exception:"+e.getMessage());
        }
        bb.writeLog("apidatas:"+ JSONObject.toJSONString(apidatas));
        return JSONObject.toJSONString(apidatas);


    }


    @GET
    @Path("/getrestinfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String getrestinfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String,Object> apidatas = new HashMap<String,Object>();

        BaseBean bb = new BaseBean();
        bb.writeLog("query");
        User user = HrmUserVarify.getUser(request , response) ;

        try{
            Map<String,Object> params = ParamUtil.request2Map(request);
            apidatas.putAll(getService().getrestinfo(params,user));
            apidatas.put("api_status",true);
        }catch (Exception e){
            apidatas.put("api_status",false);
            apidatas.put("api_errormsg","exception:"+e.getMessage());
        }
        bb.writeLog("apidatas:"+ JSONObject.toJSONString(apidatas));
        return JSONObject.toJSONString(apidatas);
    }





}
