package com.engine.htsc.doc.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.doc.cmd.DataSourceCmd;
import com.engine.htsc.doc.cmd.LetterReadCmd;
import com.engine.htsc.doc.service.LetterReadService;
import com.engine.htsc.doc.service.impl.LetterReadServiceImpl;
import com.sun.jersey.multipart.FormDataMultiPart;
import weaver.general.BaseBean;
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 LetterReadAction {

    private LetterReadService getService(User user){
        return ServiceUtil.getService(LetterReadServiceImpl.class,user);
    }


    /**
     * 双系统并行数据传输(DbLink)
     * @return
     */
    @POST
    @Path("/dataSend")
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces(MediaType.TEXT_PLAIN)
    public String dataSendDbLink(String params) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            //调用cmd进行业务处理
            DataSourceCmd dataSourceCmd = new DataSourceCmd();
            apidatas = dataSourceCmd.copyTableDatas(params);
        } catch (Exception e) {
            //异常处理
            e.printStackTrace();
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        //数据转换
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 获取正文文件信息
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/docFileInfo")
    @Produces({MediaType.APPLICATION_JSON})
    public Map<String,Object> getDocFileInfoByDocId(@Context HttpServletRequest request,
                                                  @Context HttpServletResponse response
                                                  ){
        Map<String, Object> apidatas = new HashMap<>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas = this.getService(user).getDocFileInfoByDocId(ParamUtil.request2Map(request));
            apidatas.put("api_status", true);
        }catch (Exception e){
            e.printStackTrace();
            apidatas.put("api_status",false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return apidatas;
    }

    /**
     * 获取附件文件dt4权限信息
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/fileInfo")
    @Produces({MediaType.APPLICATION_JSON})
    public Map<String,Object> getAttachmentFileInfoByDocId(@Context HttpServletRequest request,
                                                    @Context HttpServletResponse response
    ){
        Map<String, Object> apidatas = new HashMap<>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas = this.getService(user).getAttachmentFileInfoByDocId(ParamUtil.request2Map(request));
            apidatas.put("api_status", true);
        }catch (Exception e){
            e.printStackTrace();
            apidatas.put("api_status",false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return apidatas;
    }

}


