package com.engine.htsc.riskReport.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.htsc.riskReport.service.RiskReportSendReadService;
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.GET;
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;

/**
 * 公文送阅 传阅Action
 */
public class RiskReportSendReadAction {

    /**
     * 执行前台选择的送阅操作POST
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/doSendRead")
    @Produces(MediaType.TEXT_PLAIN)
    public String sendRead(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            //调用cmd进行业务处理
            RiskReportSendReadService sendReadCmd = new RiskReportSendReadService();
            apidatas = sendReadCmd.saveReaderByCode(ParamUtil.request2Map(request));
        } catch (Exception e) {
            //异常处理
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        //数据转换
        return JSONObject.toJSONString(apidatas);
    }


    /**
     * 获取已经送阅的记录
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getHasSendRead")
    @Produces(MediaType.TEXT_PLAIN)
    public String getHasSendRead(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            //调用cmd进行业务处理
            RiskReportSendReadService sendReadCmd = new RiskReportSendReadService();
            apidatas = sendReadCmd.getHasSendRead(ParamUtil.request2Map(request));
        } catch (Exception e) {
            //异常处理
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        //数据转换
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 获取当前用户可传阅对象及传阅记录
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getHasCirculated")
    @Produces(MediaType.TEXT_PLAIN)
    public String getHasCirculated(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            //调用cmd进行业务处理
            RiskReportSendReadService sendReadCmd = new RiskReportSendReadService();
            apidatas = sendReadCmd.getHasCirculated(ParamUtil.request2Map(request));
        } catch (Exception e) {
            //异常处理
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        //数据转换
        return JSONObject.toJSONString(apidatas);
    }


    /**
     * 公文传阅do方法
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/doCirculate")
    @Produces(MediaType.TEXT_PLAIN)
    public String doCirculate(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            //调用cmd进行业务处理
            RiskReportSendReadService sendReadCmd = new RiskReportSendReadService();
            apidatas = sendReadCmd.doCirculate(ParamUtil.request2Map(request));
        } catch (Exception e) {
            //异常处理
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        //数据转换
        return JSONObject.toJSONString(apidatas);
    }


    /**
     * 判断流程是否已完成送阅POST
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/checkForm")
    @Produces(MediaType.TEXT_PLAIN)
    public String checkForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            //调用cmd进行业务处理
            RiskReportSendReadService sendReadCmd = new RiskReportSendReadService();
            apidatas = sendReadCmd.checkForm(ParamUtil.request2Map(request));
        } catch (Exception e) {
            //异常处理
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        //数据转换
        return JSONObject.toJSONString(apidatas);
    }


    /**
     * 更新看阅记录POST
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/updateKanyueState")
    @Produces(MediaType.TEXT_PLAIN)
    public String updateKanyueState(@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"));
            //调用cmd进行业务处理
            RiskReportSendReadService sendReadCmd = new RiskReportSendReadService();
            apidatas = sendReadCmd.updateKanyueState(user,billid);
        } catch (Exception e) {
            //异常处理
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        //数据转换
        return JSONObject.toJSONString(apidatas);
    }


    /**
     * 看阅列表POST
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/getKanyueList")
    @Produces(MediaType.TEXT_PLAIN)
    public String getKanyueList(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            int num = Util.getIntValue(Util.null2String(request.getParameter("billid")),-1);
            String type = Util.null2String(request.getParameter("type"));

            //调用cmd进行业务处理
            RiskReportSendReadService sendReadCmd = new RiskReportSendReadService();
            apidatas = sendReadCmd.getKanyueList(user,num,type);
        } catch (Exception e) {
            //异常处理
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        //数据转换
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 看阅页面收藏
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/collect")
    @Produces(MediaType.TEXT_PLAIN)
    public String collect(@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"));
            Integer isCancelCollect = Util.getIntValue(request.getParameter("isCancelCollect"));//是否取消收藏，0是1否

            //调用cmd进行业务处理
            RiskReportSendReadService sendReadCmd = new RiskReportSendReadService();
            apidatas = sendReadCmd.collect(user,billid,isCancelCollect);
        } catch (Exception e) {
            //异常处理
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        //数据转换
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 批量取消收藏
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/batchCancelCollect")
    @Produces(MediaType.TEXT_PLAIN)
    public String batchCancelCollect(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            String billids = Util.null2String(request.getParameter("billids"));

            //调用cmd进行业务处理
            RiskReportSendReadService sendReadCmd = new RiskReportSendReadService();
            apidatas = sendReadCmd.batchCancelCollect(user,billids);
        } catch (Exception e) {
            //异常处理
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        //数据转换
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 看阅页面收藏状态
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/getCollectStatus")
    @Produces(MediaType.TEXT_PLAIN)
    public String getCollectStatus(@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"));

            //调用cmd进行业务处理
            RiskReportSendReadService sendReadCmd = new RiskReportSendReadService();
            apidatas = sendReadCmd.getCollectStatus(user,billid);
        } catch (Exception e) {
            //异常处理
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        //数据转换
        return JSONObject.toJSONString(apidatas);
    }

}
