package com.engine.htsc.collaboration.web;

import com.alibaba.fastjson.JSON;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.collaboration.service.CoWorkService;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
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.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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * create by PTY
 */
public class CoWorkAction {

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

    @POST
    @Path("/createCoWork")
    @Produces({MediaType.TEXT_PLAIN})
    public String createCoWork(@Context HttpServletRequest request, @Context HttpServletResponse response){
        PropBean pb = new PropBean();
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            User user = HrmUserVarify.getUser(request, response);

            CoWorkService coWorkService = getService();
            apidatas = coWorkService.createCoWork(user);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSON.toJSONString(apidatas);
    }

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

            CoWorkService coWorkService = getService();
            List<Map<String,Object>> coWorkList = coWorkService.getCoWorkByRequestid(requestid);
            apidatas.put("coWorkList",coWorkList);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSON.toJSONString(apidatas);
    }

    @POST
    @Path("/getCoWorkBySubRequestid")
    @Produces({MediaType.TEXT_PLAIN})
    public String getCoWorkBySubRequestid(@Context HttpServletRequest request, @Context HttpServletResponse response){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            String requestid = request.getParameter("requestid");

            String mainRequestid = "";
            String sql = "select mainrequestid from WORKFLOW_SUBWFREQUEST where subrequestid = '"+requestid+"'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                mainRequestid = Util.null2String(rs.getString("mainrequestid"));
            }else{
                mainRequestid = requestid;
            }

            List<String> subList = new ArrayList<>();
            sql = "select subrequestid from WORKFLOW_SUBWFREQUEST where mainrequestid = '"+mainRequestid+"'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            while(rs.next()){
                subList.add(Util.null2String(rs.getString("subrequestid")));
            }

            CoWorkService coWorkService = getService();
            List<Map<String,Object>> coWorkList = coWorkService.getCoWorkByRequestid(mainRequestid);
            for(String subrequestid:subList){
                List<Map<String,Object>> coWorkSubList = coWorkService.getCoWorkByRequestid(subrequestid);
                for(Map<String,Object> coWorkSub:coWorkSubList){
                    boolean flag = false;
                    for(Map<String,Object> coWork:coWorkList){
                        if(coWork.get("id").equals(coWorkSub.get("id"))){
                            flag = true;
                            break;
                        }
                    }
                    if(!flag){
                        coWorkList.add(coWorkSub);
                    }
                }
            }

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

    @POST
    @Path("/updateCoWorkState")
    @Produces({MediaType.TEXT_PLAIN})
    public String updateCoWorkState(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            User user = HrmUserVarify.getUser(request, response);
            String billid = Util.null2String(request.getParameter("billid"));
            String content = Util.null2String(request.getParameter("content"));
            String fj = Util.null2String(request.getParameter("fj"));
            int type = Util.getIntValue(request.getParameter("type"));

            CoWorkService coWorkService = getService();
            apidatas = coWorkService.updateCoWorkState(user,billid,type,content,fj);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSON.toJSONString(apidatas);
    }

    @POST
    @Path("/closeCoWorkState")
    @Produces({MediaType.TEXT_PLAIN})
    public String closeCoWorkState(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            User user = HrmUserVarify.getUser(request, response);
            String billid = Util.null2String(request.getParameter("billid"));
            String content = Util.null2String(request.getParameter("content"));
            String fj = Util.null2String(request.getParameter("fj"));
            int type = Util.getIntValue(request.getParameter("type"));

            CoWorkService coWorkService = getService();
            apidatas = coWorkService.closeCoWorkState(user,billid,type,content,fj);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSON.toJSONString(apidatas);
    }

    @POST
    @Path("/getCoWorkStatusByTaskid")
    @Produces({MediaType.TEXT_PLAIN})
    public String getCoWorkStatusByTaskid(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            String billid = Util.null2String(request.getParameter("billid"));

            CoWorkService coWorkService = getService();
            apidatas = coWorkService.getCoWorkStatusByTaskid(billid);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSON.toJSONString(apidatas);
    }

    @POST
    @Path("/sendApproval")
    @Produces({MediaType.TEXT_PLAIN})
    public String sendApproval(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            User user = HrmUserVarify.getUser(request, response);
            String billid = request.getParameter("billid");
            String approver = request.getParameter("approver");

            CoWorkService coWorkService = getService();
            apidatas = coWorkService.sendApproval(user,billid,approver);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSON.toJSONString(apidatas);
    }

    @POST
    @Path("/approve")
    @Produces({MediaType.TEXT_PLAIN})
    public String approve(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            User user = HrmUserVarify.getUser(request, response);
            String billid = request.getParameter("billid");
            String content = request.getParameter("content");
            String fj = request.getParameter("fj");

            CoWorkService coWorkService = getService();
            apidatas = coWorkService.approve(user,billid,content,fj);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSON.toJSONString(apidatas);
    }

    @POST
    @Path("/getApproveInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String getApproveInfo(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            User user = HrmUserVarify.getUser(request, response);
            String billid = request.getParameter("billid");

            CoWorkService coWorkService = getService();
            apidatas = coWorkService.getApproveInfo(user,billid);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSON.toJSONString(apidatas);
    }

    @POST
    @Path("/forwardTask")
    @Produces({MediaType.TEXT_PLAIN})
    public String forwardTask(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            User user = HrmUserVarify.getUser(request, response);
            String billid = request.getParameter("billid");
            String forwardid = request.getParameter("forwardid");
            String content = request.getParameter("content");
            String fj = request.getParameter("fj");

            CoWorkService coWorkService = getService();
            apidatas = coWorkService.forwardTask(user,billid,forwardid,content,fj);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSON.toJSONString(apidatas);
    }

    @POST
    @Path("/forwardTaskApproval")
    @Produces({MediaType.TEXT_PLAIN})
    public String forwardTaskApproval(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            User user = HrmUserVarify.getUser(request, response);
            String billid = request.getParameter("billid");
            String forwardid = request.getParameter("forwardid");
            String content = request.getParameter("content");
            String fj = request.getParameter("fj");

            CoWorkService coWorkService = getService();
            apidatas = coWorkService.forwardTaskApproval(user,billid,forwardid,content,fj);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSON.toJSONString(apidatas);
    }
}
