package com.engine.htsc.ityssq.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.ityssq.service.ItyssqService;
import com.engine.htsc.ityssq.service.impl.ItyssqServiceImpl;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.interfaces.htsc.comInfo.PropBean;

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

public class ItxqsqAction {

    public ItyssqService getService(HttpServletRequest request, HttpServletResponse response){
        User user = HrmUserVarify.getUser( request, response );
        return  (ItyssqService) ServiceUtil.getService(ItyssqServiceImpl.class,user);
    }

    @POST
    @Path("/createFreeNode")
    @Produces({MediaType.TEXT_PLAIN})
    public String createFreeNode(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String,Object> apidatas = new HashMap<String,Object>();
        try {
            User user = HrmUserVarify.getUser( request, response );
            Map<String,Object> param = ParamUtil.request2Map(request);
            apidatas = getService(request,response).createFreeNode(user,param);
            apidatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }


    @GET
    @Path("/getNsBmsdInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String getNsBmsdInfo(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String,Object> apidatas = new HashMap<String,Object>();
        try {
            User user = HrmUserVarify.getUser( request, response );
            Map<String,Object> param = ParamUtil.request2Map(request);
            apidatas = getService(request,response).getNsryInfo(param);
            apidatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }

    @GET
    @Path("/getszbm")
    @Produces({MediaType.TEXT_PLAIN})
    public String getszbm(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String,Object> apidatas = new HashMap<String,Object>();
        try {
            RecordSet rs = new RecordSet();
            Map<String,Object> param = ParamUtil.request2Map(request);
            String szbm = Util.null2String(param.get("szbm"));
            PropBean pb = new PropBean();
            String xxjsb = pb.getPropName("ityssq_xxjsb");
            String isxxjsb = "0";
            String departmentcode = "" ;
            if(!"".equals(szbm) && !"".equals(xxjsb)){
                String sql = " select departmentcode from hrmdepartment where id="+xxjsb ;
                rs.execute(sql);
                if(rs.next()){
                    departmentcode = Util.null2String(rs.getString("departmentcode"));
                }
            }
            if(!"".equals(departmentcode) && departmentcode.equals(xxjsb)){
                isxxjsb = "1";
            }
            apidatas.put("isxxjsb", isxxjsb);
            apidatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }
}
