package com.engine.htsc.collaboration.web;

import com.alibaba.fastjson.JSON;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.collaboration.service.TaskLogService;
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.List;
import java.util.Map;

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

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

    @POST
    @Path("/createTaskLog")
    @Produces({MediaType.TEXT_PLAIN})
    public String createTaskLog(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            User user = HrmUserVarify.getUser(request, response);
            String taskid = request.getParameter("taskid");
            String content = request.getParameter("content");
            String fj = request.getParameter("fj");
            String jllx = request.getParameter("jllx");
            String sjlx = request.getParameter("sjlx");
            String title = "发布了动态";

            TaskLogService taskLogService = getService();
            boolean result = taskLogService.createTaskLog(user,taskid,fj,jllx,sjlx,content,title);
            if(result){
                taskLogService.sendTaskLogMessage(user,taskid,jllx,content);
            }
            apidatas.put("isSuccess",result);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }

        return JSON.toJSONString(apidatas);
    }

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

            TaskLogService taskLogService = getService();
            List<Map<String,Object>> taskLogList = taskLogService.getTaskLog(billid,type);
            apidatas.put("taskLogList",taskLogList);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }

        return JSON.toJSONString(apidatas);
    }

}
