package com.engine.htsc.announcement.web;

import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.announcement.service.HeadquartersAnncService;
import com.engine.htsc.announcement.service.impl.HeadquartersAnncServiceImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

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.HashMap;
import java.util.Map;

public class HeadquartersAnncAction {
    private Log log = LogFactory.getLog(this.getClass());
    private HeadquartersAnncService getService(User user){
        return ServiceUtil.getService(HeadquartersAnncServiceImpl.class,user);
    }

    /**
     * 总部公告判断是否是营业部
     * @param request
     * @param response
     * @param userid
     * @return
     */
    @GET
    @Path("/ispostion")
    @Produces({MediaType.APPLICATION_JSON})
    public Map<String,Object> isPostion(@Context HttpServletRequest request,
                                              @Context HttpServletResponse response,
                                              @QueryParam("userid") String userid) {
        Map<String, Object> apidatas = new HashMap<>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas = this.getService(user).isPostion(userid);
            apidatas.put("api_status", 1);
        }catch (Exception e){
            e.printStackTrace();
            apidatas.put("api_status",-1);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
            log.error("isPostion catch exception");
        }
        return apidatas;
    }

    /**
     * 总部公告流程是否已经过所有领导会签
     * @param request
     * @param response
     * @param jobs
     * @return
     */
    @GET
    @Path("/afterLeaderSign")
    @Produces({MediaType.APPLICATION_JSON})
    public Map<String,Object> afterLeaderSign(@Context HttpServletRequest request,
                                        @Context HttpServletResponse response,
                                        @QueryParam("jobs") String jobs,
                                        @QueryParam("requestid") Integer requestid) {
        Map<String, Object> apidatas = new HashMap<>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas = this.getService(user).afterLeadSign(jobs,requestid);
            apidatas.put("api_status", 1);
        }catch (Exception e){
            e.printStackTrace();
            apidatas.put("api_status",-1);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
            log.error("afterLeaderSign catch exception");
        }
        return apidatas;
    }

    /**
     * 总部公告会签部门（命名格式 部门名称+部门职责）
     * @param request
     * @param response
     * @param formid
     * @param requestid
     * @param workflowid
     * @return
     */
    @GET
    @Path("/signDepartment")
    @Produces({MediaType.APPLICATION_JSON})
    public Map<String,Object> signDepartment(@Context HttpServletRequest request,
                                              @Context HttpServletResponse response,
                                              @QueryParam("formid") String formid,
                                              @QueryParam("requestid") Integer requestid,
                                             @QueryParam("workflowid") String workflowid) {
        Map<String, Object> apidatas = new HashMap<>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas = this.getService(user).signDepartment(formid,requestid,workflowid);
            apidatas.put("api_status", 1);
        }catch (Exception e){
            e.printStackTrace();
            apidatas.put("api_status",-1);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
            log.error("afterLeaderSign catch exception");
        }
        return apidatas;
    }

    /**
     * 总部公告自动构建OA流程
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/creatWorkflow")
    @Produces({MediaType.APPLICATION_JSON})
    public Map<String,Object> createHeadAnnouncementWorkflow(@Context HttpServletRequest request,
                                                           @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas = this.getService(user).createHeadAnnouncementWorkflow(ParamUtil.request2Map(request));
            apidatas.put("code", 200);
            apidatas.put("message","接口调用成功");
        }catch (Exception e){
            e.printStackTrace();
            apidatas.put("code",-1);
            apidatas.put("message","接口调用失败");
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
            log.error("isPostion catch exception");
        }
        return apidatas;
    }




}
