package com.engine.htsc.collaboration.web;

import com.alibaba.fastjson.JSON;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.collaboration.service.DubanCoWorkService;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

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;

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

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

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

            DubanCoWorkService service = getService();
            apidatas = service.createDubanCoWork(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("/updateDubanCoWorkState")
    @Produces({MediaType.TEXT_PLAIN})
    public String updateDubanCoWorkState(@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"));

            DubanCoWorkService service = getService();
            apidatas = service.updateDubanCoWorkState(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("/getDubanCoWorkStatusByTaskid")
    @Produces({MediaType.TEXT_PLAIN})
    public String getDubanCoWorkStatusByTaskid(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            String billid = Util.null2String(request.getParameter("billid"));

            DubanCoWorkService service = getService();
            apidatas = service.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("/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");

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