package com.engine.htsc.collaboration.web;

import com.alibaba.fastjson.JSON;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.collaboration.service.TaskService;
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 TaskAction {

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

    @POST
    @Path("/createTask")
    @Produces({MediaType.TEXT_PLAIN})
    public String createTask(@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);
            String sxbillid = Util.null2String(request.getParameter("billid"));
            String rwlx = Util.null2String(request.getParameter("rwlx"));

            TaskService taskService = getService();
            apidatas = taskService.createTask(user,sxbillid,rwlx);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSON.toJSONString(apidatas);
    }

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

            TaskService taskService = getService();
            List<Map<String,Object>> taskList = taskService.getTaskList(sxbillid,rwlx);
            apidatas.put("taskList",taskList);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSON.toJSONString(apidatas);
    }

    @POST
    @Path("/updateTaskState")
    @Produces({MediaType.TEXT_PLAIN})
    public String updateTaskState(@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 rwlx = Util.null2String(request.getParameter("rwlx"));
            String content = Util.null2String(request.getParameter("content"));
            String fj = Util.null2String(request.getParameter("fj"));
            int type = Util.getIntValue(request.getParameter("type"));

            TaskService taskService = getService();
            apidatas = taskService.updateTaskState(user,billid,rwlx,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("/getTaskByRequestid")
    @Produces({MediaType.TEXT_PLAIN})
    public String getTaskByRequestid(@Context HttpServletRequest request, @Context HttpServletResponse response){
        BaseBean bs = new BaseBean();
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            String requestid = request.getParameter("requestid");

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

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

            TaskService taskService = getService();
            List<Map<String,Object>> taskList = taskService.getTaskByRequestid(mainRequestid);
            for(String subrequestid:subList){
                List<Map<String,Object>> taskSubList = taskService.getTaskByRequestid(subrequestid);
                for(Map<String,Object> taskSub:taskSubList){
                    boolean flag = false;
                    for(Map<String,Object> task:taskList){
                        if(task.get("id").equals(taskSub.get("id"))){
                            flag = true;
                            break;
                        }
                    }
                    if(!flag){
                        taskList.add(taskSub);
                    }
                }
            }

            apidatas.put("taskList",taskList);
        }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){
        BaseBean bs = new BaseBean();
        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");

            TaskService taskService = getService();
            apidatas = taskService.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);
    }
}
