package com.engine.htxc.itcglc.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.htxc.itcglc.service.ItcglcService;
import com.engine.htxc.itcglc.service.impl.ItcglcServiceImpl;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.hrm.resource.ResourceComInfo;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ItcglcAction {

    public ItcglcService getService(HttpServletRequest request, HttpServletResponse response){
        return  (ItcglcService) ServiceUtil.getService(ItcglcServiceImpl.class);
    }

    @POST
    @Path("/getItcgProject")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String getItcgProject(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String,Object> apidatas = new HashMap<String,Object>();
        ResourceComInfo ResourceComInfo = null;
        try {
            Map<String,Object> param = ParamUtil.request2Map(request);
            String userid = Util.null2String(param.get("usercode"));
            System.out.println("userid:"+userid);
            apidatas = getService(request,response).getItcgProject(param);

            apidatas.put("api_status", true);
            apidatas.put("api_errormsg", "");
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }


    @GET
    @Path("/getItcgProjectInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String getItlxProjectInfo(@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).getItcgProjectInfo(user,param);
            apidatas.put("api_status", true);
            apidatas.put("api_errormsg", "");
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }


    @GET
    @Path("/getBshqbm")
    @Produces({MediaType.TEXT_PLAIN})
    public String getBshqbm(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String,Object> apidatas = new HashMap<String,Object>();
        try {
            RecordSet rs = new RecordSet();
            PropBean pb = new PropBean();
            String itlxlx_bshqbm = pb.getPropName("itcglx_bshqbm");

            if(!"".equals(itlxlx_bshqbm))
            {
                itlxlx_bshqbm = "'"+itlxlx_bshqbm.replaceAll(",","','")+"'";

                String deptids = "";
                List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
                Map<String,Object> deptamap = null;
                String sql = " select id,departmentname from hrmdepartment where departmentcode in ("+itlxlx_bshqbm+") ";
                rs.execute(sql);
                while (rs.next()){
                    String deptid = Util.null2String(rs.getString("id")) ;
                    String departmentname = Util.null2String(rs.getString("departmentname")) ;

                    deptids += deptids=="" ? deptid : ","+deptid ;
                    deptamap = new HashMap<String,Object>();
                    deptamap.put("id",deptid);
                    deptamap.put("name",departmentname);
                    list.add(deptamap);
                }

                apidatas.put("value",deptids);
                apidatas.put("specialobj",list);

                apidatas.put("api_status", true);

            }else{
                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("/getLsdpInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String getLsdpInfo(@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).getLsdpInfo(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);
    }


    @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("/getldsplist")
    @Produces({MediaType.TEXT_PLAIN})
    public String getldsplist(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String,Object> apidatas = new HashMap<String,Object>();
        try {
            int requestid = Util.getIntValue(Util.null2String(request.getParameter("requestid")));    //
            int workflowid = Util.getIntValue(Util.null2String(request.getParameter("workflowid")));  //

            PropBean pb = new PropBean();
            String nodeid = pb.getNodeIdsByUnNumber("itcgsq_gsldsp");

            List<Map<String,String>> dataList = new ArrayList<Map<String,String>>();
            String fieldvalue = "";
            RecordSet rs = new RecordSet();

            String sql =" select t.userid,h.lastname,k.departmentname \n" +
                    " from workflow_currentoperator t\n" +
                    " inner join hrmresource h on h.id = t.userid\n" +
                    " inner join hrmdepartment k on k.id = h.departmentid\n" +
                    " where t.requestid=" +requestid +
                    " and t.workflowid=" + workflowid+
                    " and t.nodeid in("+nodeid+") " +
                    " order by receivedate,receivetime";
            rs.execute(sql);
            while (rs.next()){
                String userid = Util.null2String(rs.getString("userid"));
                String lastname = Util.null2String(rs.getString("lastname"));
                String departmentname = Util.null2String(rs.getString("departmentname"));
                fieldvalue += fieldvalue == ""? userid : ","+userid ;
                Map<String,String> itemMap = new HashMap<String,String>();
                itemMap.put("id",userid);
                itemMap.put("name",lastname+"("+departmentname+")");
                dataList.add(itemMap);
            }
            apidatas.put("value",fieldvalue);
            apidatas.put("specialobj",dataList);
            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("/getcgplanlist")
    @Produces({MediaType.TEXT_PLAIN})
    public String getcgplanlist(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String,Object> apidatas = new HashMap<String,Object>();
        try {
            Map<String,Object> param = ParamUtil.request2Map(request);
            apidatas = getService(request,response).getcgplanlist(param);
            apidatas.put("api_status", true);
            apidatas.put("api_errormsg", "");
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }


    @GET
    @Path("/createCgrwdRequest")
    @Produces({MediaType.TEXT_PLAIN})
    public String createCgrwdRequest(@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).createCgrwdRequest(param,user);
            apidatas.put("api_status", true);
            apidatas.put("api_errormsg", "");
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }

}
