package com.engine.htsc.seal.web;

import com.alibaba.fastjson.JSON;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.seal.service.SealApplicationService;
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 SealApplicationAction {

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

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

            SealApplicationService sealApplicationService = getService();
            apidatas = sealApplicationService.getLeader(yz,szbm);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }

        return JSON.toJSONString(apidatas);
    }

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

            SealApplicationService sealApplicationService = getService();
            apidatas = sealApplicationService.getDeptBySealAndFileType(yz,wjlb,wjlx,szbm);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }

        return JSON.toJSONString(apidatas);
    }

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

            SealApplicationService sealApplicationService = getService();
            apidatas = sealApplicationService.getCommonSeal(szbm);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }

        return JSON.toJSONString(apidatas);
    }

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

            SealApplicationService sealApplicationService = getService();
            String result = sealApplicationService.getNextValue(requestid,yz,wjlb,wjlx);
            apidatas.put("result",result);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }

        return JSON.toJSONString(apidatas);
    }


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

            SealApplicationService sealApplicationService = getService();
            apidatas = sealApplicationService.getLicenseList(zzlx);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }

        return JSON.toJSONString(apidatas);
    }

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

            SealApplicationService sealApplicationService = getService();
            apidatas = sealApplicationService.checkElectronicSealLabel(requestid,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("/getUserByDeptids")
    @Produces({MediaType.TEXT_PLAIN})
    public String getUserByDeptids(@Context HttpServletRequest request, @Context HttpServletResponse response){
        BaseBean bs = new BaseBean();
        Map<String, Object> apidatas = new HashMap<String, Object>();
        bs.writeLog("----sealApplication-----");
        try{
            String deptids = request.getParameter("deptids");

            SealApplicationService sealApplicationService = getService();
            apidatas.put("result",sealApplicationService.getUserByDeptids(deptids));
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }

        return JSON.toJSONString(apidatas);
    }

}
