package com.engine.htsc.itxqlc.web;

import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.itxqlc.service.ItDemandApplyService;
import com.engine.htsc.itxqlc.service.impl.ItDemandApplyServiceImpl;
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 ItDemandApplyAction {

    private Log log = LogFactory.getLog(this.getClass());
    private ItDemandApplyService getService(User user){
        return ServiceUtil.getService(ItDemandApplyServiceImpl.class,user);
    }

    /**
     * It需求申请分送需求评估人获取所有意见征询人
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/operation")
    @Produces({MediaType.APPLICATION_JSON})
    public Map<String,Object> getOperationRecords(@Context HttpServletRequest request,
                                               @Context HttpServletResponse response,
                                               @QueryParam("nodeid") String nodeid){
        Map<String, Object> apidatas = new HashMap<>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas = this.getService(user).getOperationRecords(nodeid);
            apidatas.put("api_status", 1);
        }catch (Exception e){
            e.printStackTrace();
            apidatas.put("api_status",-1);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return apidatas;
    }

    /**
     * 意见征询回复后汇总明细表4
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/summary")
    @Produces({MediaType.APPLICATION_JSON})
    public Map<String,Object> detailSummary(@Context HttpServletRequest request,
                                                  @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas = this.getService(user).detailSummary(ParamUtil.request2Map(request));
            apidatas.put("api_status", 1);
        }catch (Exception e){
            e.printStackTrace();
            apidatas.put("api_status",-1);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return apidatas;
    }


    /**
     * 获取需求接收人及通知阅读人
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/dealWithPreson")
    @Produces({MediaType.APPLICATION_JSON})
    public Map<String,Object> dealWithPreson(@Context HttpServletRequest request,
                                            @Context HttpServletResponse response,
                                             @QueryParam("deptid") String deptid){
        Map<String, Object> apidatas = new HashMap<>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas = this.getService(user).dealWithPreson(deptid);
            apidatas.put("api_status", 1);
        }catch (Exception e){
            e.printStackTrace();
            apidatas.put("api_status",-1);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return apidatas;
    }


    /**
     * 上级部门赋值（总部二级部门+三级营业部）
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/assignSuperiorDept")
    @Produces({MediaType.APPLICATION_JSON})
    public Map<String,Object> AssignSuperiorDept(@Context HttpServletRequest request,
                                             @Context HttpServletResponse response,
                                             @QueryParam("deptid") String deptid){
        Map<String, Object> apidatas = new HashMap<>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas = this.getService(user).assignSuperiorDept(deptid);
            apidatas.put("api_status", 1);
        }catch (Exception e){
            e.printStackTrace();
            apidatas.put("api_status",-1);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return apidatas;
    }
}

