package com.engine.htsc.doc.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.htsc.doc.cmd.LetterReadCmd;
import com.engine.htsc.doc.cmd.LetterThread;
import com.engine.htsc.doc.cmd.SendReadCmd;
import com.engine.htsc.doc.util.DocUtil;
import com.engine.htsc.doc.util.WorkFlowUtil;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;

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

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

    /**
     * 执行前台选择的送阅操作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 {
            Integer ofd_docid = null;
            //(关闭信创数据传输)
            //String enable = pb.getPropName("enable");
            String contextPath = request.getContextPath();
                //转ofd
            //    RecordSet rs = new RecordSet();
            //    String requestId = (String) ParamUtil.request2Map(request).get("requestId");
            //    rs.executeQuery("select zwofd from uf_gwky where requestid = ?", requestId);
            //    if (rs.next()) { //ofd只转一次
            //       ofd_docid = Util.getIntValue(rs.getString("zwofd"));
            //    }
            //    if (ofd_docid == null || ofd_docid < 0) {
            //        if ("true".equals(enable) && "/htoa".equals(contextPath)) {
            //            Integer zwpdfDocid = WorkFlowUtil.getZwpdfDocid(requestId);
            //           ofd_docid = DocUtil.generateFile(zwpdfDocid);
            //       }
            //   }

            //调用cmd进行业务处理
            SendReadCmd sendReadCmd = new SendReadCmd();
            apidatas = sendReadCmd.saveReaderByCode(ParamUtil.request2Map(request),ofd_docid,contextPath);


        } 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进行业务处理
            SendReadCmd sendReadCmd = new SendReadCmd();
            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进行业务处理
            SendReadCmd sendReadCmd = new SendReadCmd();
            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进行业务处理
            SendReadCmd sendReadCmd = new SendReadCmd();
            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);
    }

}
