signDataPushAction.java 16.9 KB
package com.cntytz;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.file.ImageFileManager;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 *
 * @author XuBin
 * @date 2020/4/15
 */
public class signDataPushAction extends BaseBean implements Action {

    Log logger = LogFactory.getLog(signDataPushAction.class);

    /**
     * 执行器
     *
     * @param requestInfo
     * @return
     */
    @Override
    public String execute(RequestInfo requestInfo) {

        JSONObject json = new JSONObject();
        Boolean isNewWorkFolw=true;
        writeLog("===================================start  1 2 3 =================================================");
        RequestManager manager = requestInfo.getRequestManager();
        String requestid = requestInfo.getRequestid();
        writeLog("get the ======  requrstId=", requestInfo + "====requestid" + requestid);
        String workflowid = requestInfo.getWorkflowid();
        String tableName = getTableName(workflowid);
        writeLog("get the ======   workflowid=", workflowid + "get the tableName===" + tableName);
        RecordSet rsInfo = new RecordSet();
        RecordSet rsInfo1 = new RecordSet();
        RecordSet rsInfo2 = new RecordSet();
        RecordSet rsInfo3 = new RecordSet();
        RecordSet rsInfo4 = new RecordSet();
        RecordSet rsInfo7 = new RecordSet();
        RecordSet rsInfo8 = new RecordSet();
        String mainID = "";

        String sql;
        //获取申请信息
        sql = "select * from " + tableName + " where requestid = " + requestid + "";
        writeLog("get the ======   sql=", sql);
        rsInfo.execute(sql);
        rsInfo.next();

        mainID= rsInfo.getString("id");


        //判断申请是  重新申请还是新流程发起
        sql = "select * from workflow_requestlog where REQUESTID = "+requestid+"";
        rsInfo8.execute(sql);
        rsInfo8.next();
        List<List<Object>> requestLogList= rsInfo8.getArray();
        writeLog("the requestlog list is list={}",JSONObject.toJSONString(requestLogList));
        if(requestLogList.size()>1){
            //重新发起流程
            isNewWorkFolw = false;
        }

        //获取申请人信息
        sql = "select h.LOGINID as loginName,c.SUBCOMPANYNAME as companyName,h.mobile as phone from hrmresource as h left join  hrmsubcompany as c on h.subcompanyid1 = c.id  where h.id =" + rsInfo.getString("sqr") + "";
        writeLog("get the ======   sql=", sql);
        rsInfo1.execute(sql);
        rsInfo1.next();
        //申请人账户
        String loginName = rsInfo1.getString("loginName");
        //申请人公司
        String companyName = rsInfo1.getString("companyName");
        //申请人手机号
        String mobile = rsInfo1.getString("mobile");

        //获取项目经理
        sql = "select h.LOGINID as loginName , h.LASTNAME as name from  hrmresource as h where id =" + rsInfo.getString("xmjl1") + "";
        writeLog("get the ======   sql=", sql);
        rsInfo2.execute(sql);
        rsInfo2.next();
        //项目经理名称
        String pmName = rsInfo2.getString("name");
        //项目经理登录账户
        String pmLoginName = rsInfo2.getString("loginName");

        //获取材料科审核人
        sql = "select h.LOGINID as loginName , h.LASTNAME as name from  hrmresource as h where id =" + rsInfo.getString("clkshr1") + "";
        writeLog("get the ======   sql=", sql);
        rsInfo3.execute(sql);
        rsInfo3.next();
        //材料科审核人名称
        String clName = rsInfo3.getString("name");
        //材料科审核人登录账户
        String clLoginName = rsInfo3.getString("loginName");

        //获取所选项目部名称
        sql = "select d.DEPARTMENTNAME as departName from hrmdepartment as d where id = " + rsInfo.getString("xmmc") + "";
        writeLog("get the ======   sql=", sql);
        rsInfo4.execute(sql);
        rsInfo4.next();
        //项目部名称
        String departName = rsInfo4.getString("departName");
        writeLog("get the departName======   departName=", departName);

        //获取用印盖章文件 list

        List<Object> fileList = new ArrayList();
        String yywj = rsInfo.getString("yywj");
        writeLog("111111111111111111111111111111111111111111111111111111111111111111111111111111");
        String[] yywjList = yywj.split(",");
        writeLog("yywjListyywjListyywjListyywjList", yywjList);

        for (int i = 0; i < yywjList.length; i++) {
            Map<String, Object> fileMap = new HashMap<>(16);
            RecordSet rsInfo5 = new RecordSet();
            sql = "select imagefile.IMAGEFILENAME as  fileName,imagefile.FILEREALPATH as filepath,imagefile.IMAGEFILEID as imageFileId from docimagefile  left join   imagefile on docimagefile.IMAGEFILEID = imagefile.IMAGEFILEID where  docimagefile.DOCID =" + yywjList[i] + "";
            writeLog("=========fileListsqlsqlqslqsqlsql========", sql);
            rsInfo5.execute(sql);
            rsInfo5.next();
            String fileName = rsInfo5.getString("fileName");
            String initialFileId = rsInfo5.getString("filepath");
            String imageFileId = rsInfo5.getString("imageFileId");
            //换成主建id

            fileMap.put("initialFileId", imageFileId);
            String newFilePath = getNewFilePath(imageFileId);
            //原有的docid
            fileMap.put("initialDocId", yywjList[i]);
            fileMap.put("fileId", imageFileId+fileName.substring(fileName.lastIndexOf(".")));
            fileMap.put("fileName", fileName);
            fileMap.put("initialImageFileId", mainID);
            fileList.add(fileMap);
        }
        writeLog("=========fileListfileListfileListfileList========", JSONObject.toJSONString(fileList));

        //其他附件
        List<Object> attachmentList = new ArrayList();
        String qtfj = rsInfo.getString("qfj");
        writeLog("2222222222222222222222222222222222222222222222222222222222222222");
        String[] qtfjList = qtfj.split(",");
        for (int i = 0; i < qtfjList.length; i++) {
            if (qtfjList[i] ==null || qtfjList[i].length() ==0 ){
            }else{
                Map<String, Object> fileMap = new HashMap<>(16);
                RecordSet rsInfo6 = new RecordSet();
                sql = "select imagefile.IMAGEFILENAME as  fileName,imagefile.FILEREALPATH as filepath ,imagefile.IMAGEFILEID as imageFileId,imagefile.IMAGEFILEID as imageFileId from docimagefile  left join   imagefile on docimagefile.IMAGEFILEID = imagefile.IMAGEFILEID where  docimagefile.DOCID =" + qtfjList[i] + "";
                writeLog("=========qtfjListqtfjListqtfjListqtfjList========", sql);
                rsInfo6.execute(sql);
                rsInfo6.next();
                String fileName = rsInfo6.getString("fileName");
                String initialFileId = rsInfo6.getString("filepath");
                String imageFileId = rsInfo6.getString("imageFileId");
                String newFilePath = getNewFilePath(imageFileId);
                //原有的docid
                fileMap.put("initialDocId", qtfjList[i]);
                fileMap.put("fileId", imageFileId+fileName.substring(fileName.lastIndexOf(".")));
                fileMap.put("fileName", fileName);
                attachmentList.add(fileMap);
            }
        }
        writeLog("=========qtfjListqtfjListqtfjList========", JSONObject.toJSONString(attachmentList));


        //获取材料明细数据
        //formtable_main_38  formtable_main_38_dt1
        String tableDt1 = tableName+"_dt1";
        sql = "select dt1.* from "+tableDt1+" as  dt1 left join "+tableName+" as fm on dt1.mainid = fm.id  where fm.id = " + rsInfo.getString("id") + "";
//        sql = "select dt1.* from formtable_main_38_dt1 as  dt1 left join formtable_main_38 as fm on dt1.mainid = fm.id  where fm.id = " + rsInfo.getString("id") + "";
        writeLog("get the  userFileSql======   sql=", sql);
        rsInfo7.execute(sql);
        rsInfo7.next();
        List<List<Object>> materialInfoList = rsInfo7.getArray();
        String[] columnName = rsInfo7.getColumnName();
        writeLog("=========materialInfoListmaterialInfoList========", JSONObject.toJSONString(materialInfoList));
        writeLog("=========columnNamecolumnName========", JSONObject.toJSONString(columnName));
        UseSealApplySubmitRequest useSealApplySubmitRequest = new UseSealApplySubmitRequest();
        //发票类型
        useSealApplySubmitRequest.setInvoiceType(rsInfo.getString("fplx"));

        writeLog("get the departName======   fplx=", departName);
        //税率
        useSealApplySubmitRequest.setTaxRate(rsInfo.getString("sl"));
        //任务ID
        useSealApplySubmitRequest.setRequestId(requestid);

        //用印申请
        useSealApplySubmitRequest.setContractType(1);
        //ecology用印流程的workFlowId
        useSealApplySubmitRequest.setWorkFlowId(workflowid);
        //泛微中对应的流程业务表
        useSealApplySubmitRequest.setEcologyTable(tableName);

        //项目经理
        useSealApplySubmitRequest.setPmName(pmName);

        writeLog("get the departName======   pmName1=", departName);
        //项目经理登录账户
        useSealApplySubmitRequest.setPmName(pmLoginName);
        writeLog("get the departName======   pmName2");
        //供应商登录账户
        useSealApplySubmitRequest.setSupplierUserLoginName(rsInfo.getString("gysdlzh"));
        writeLog("get the departName======   pmName3");
        //申请时间
        useSealApplySubmitRequest.setApplyTime(new Date());
        writeLog("get the departName======   pmName4");
        //申请人名称
        useSealApplySubmitRequest.setApplyUserName(loginName);
        writeLog("get the departName======   pmName5");
        //申请人所在公司
        useSealApplySubmitRequest.setApplyUserCompanyName(companyName);
        writeLog("get the departName======   pmName6");
        //申请人所在部门
        useSealApplySubmitRequest.setApplyUserDeptName(rsInfo.getString("szbm"));
        //联系电话
        useSealApplySubmitRequest.setApplyUserTel(rsInfo.getString(mobile));
        //项目名称
        useSealApplySubmitRequest.setProjectName(departName);
        //工程名称
        useSealApplySubmitRequest.setProgramName(rsInfo.getString("gczwmc"));
        //申请主题
        useSealApplySubmitRequest.setApplySubject(rsInfo.getString("sqzt"));
        //用印类型
        useSealApplySubmitRequest.setUseSealType(rsInfo.getString("yylx"));
        //备注
        useSealApplySubmitRequest.setRemarks(rsInfo.getString("yywz").replace(" ",""));
        //供应商id
        useSealApplySubmitRequest.setSupplierId(rsInfo.getString("gyssj"));
        //结算方式
        useSealApplySubmitRequest.setPaymentMethod(rsInfo.getString("jsfs")==""?"无":rsInfo.getString("jsfs").replace(" ",""));
        //原始未盖章的文件id
        useSealApplySubmitRequest.setInitialFileId(rsInfo.getString("yywj"));
        //计征方式
        int jzfs = Integer.valueOf(rsInfo.getString("jzfs"))+1;
        useSealApplySubmitRequest.setLevyType(String.valueOf(jzfs));
        //材料大类
        useSealApplySubmitRequest.setMaterialType(rsInfo.getString("cldl"));
        //项目涉及的财务组织
        useSealApplySubmitRequest.setProjectFinancialOrg(rsInfo.getString("sjcwzz"));
        //材料审核人名称
        useSealApplySubmitRequest.setMaterialUserName(clName);
        //是否新流程
        useSealApplySubmitRequest.setNewWorkFolw(isNewWorkFolw);
        //是否未立项 1是 2不是
        useSealApplySubmitRequest.setProjectApproval(rsInfo.getString("wlxgc"));
        //是否加盖法人代表章 1是 2不是
        useSealApplySubmitRequest.setLegalSeal(rsInfo.getString("jgfrdbz"));

        writeLog("get the departName======   clName=", departName);
        //材料审核人登录名称
        useSealApplySubmitRequest.setMaterialLoginName(clLoginName);

        this.writeLog("get the departName======   clLoginName=", clLoginName);
        json.put("useSealApplySubmitRequest", JSONObject.toJSONString(useSealApplySubmitRequest));
        json.put("attachmentList", attachmentList);
        json.put("fileList", fileList);
        json.put("materialInfoList", materialInfoList);
        writeLog("json =============json " + JSONObject.toJSONString(json));
        String url = "https://ty.cntytz.com/uasSign/ecology/startWorkFlow";
        writeLog("json =============url " + url);
        JSONObject jsonObject = HttpUtils.doPost(url, json);
        return SUCCESS;
    }

    /**
     * 得到表名
     * @param wfid
     * @return
     */
    public static String getTableName (String wfid){
        String sql = "select a.tablename from workflow_bill a,workflow_base b where a.id=b.formid and b.id=" + wfid;
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        rs.next();
        return rs.getString(1);
    }


    /**
     * 创建新文件路径 根据文件的imageFileid
     * @param imageFileId
     * @return
     */
    public String getNewFilePath(String imageFileId){
        //获取文件
        try {
            ImageFileManager imageFileManager = new ImageFileManager();
            imageFileManager.getImageFileInfoById(Util.getIntValue(imageFileId));
            InputStream inputStream = imageFileManager.getInputStream();
            String filename = imageFileManager.getImageFileName();
            //编码后文件名称
            filename = new String(filename.getBytes(StandardCharsets.UTF_8));
            writeLog("save file =========",filename);
            String targetFilePath = saveAsFile(filename, inputStream,imageFileId);
            writeLog(" ========= ========= ========= ========= ========= ========= =========",filename);
            return targetFilePath;
        } catch (Exception e) {
            writeLog("get the new filePath fail error={}",e.getMessage());
            return null;
        }
    }

    /**
     * 保存文件
     * @param filename
     * @param in
     * @return
     */
    private String saveAsFile(String filename, InputStream in,String imageFileId) throws UnknownHostException {
        String targetFilePath ="/share/ecologyfile/newfile/filesystem/sign/" + imageFileId+filename.substring(filename.lastIndexOf("."));
        writeLog("-=====targetFilePath====",targetFilePath);
//        String targetFilePath = GCONST.getRootPath() + "weaverjn" + File.separator + "tempfile" + File.separator + filename;
        writeLog("the linux file path is targetFilePath={}",targetFilePath);
        File file1 = new File(targetFilePath);
        File parent = file1.getParentFile();
        if (!parent.exists()) {
            boolean bool = parent.mkdirs();
            if (!bool) {
                System.err.println("创建目标文件夹 tempfile 失败");
                return targetFilePath;
//                return false;
            }
        }
        if (file1.exists()) {
            return targetFilePath;
//            return true;
        }
        int index;
        byte[] bytes = new byte[1024];
        try {
            FileOutputStream downloadFile = new FileOutputStream(targetFilePath);
            while ((index = in.read(bytes)) != -1) {
                downloadFile.write(bytes, 0, index);
                downloadFile.flush();
            }
            downloadFile.close();
            in.close();
            return targetFilePath;
//            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return targetFilePath;
//        return false;
    }

    /**
     * 根据网络接口获取IP地址
     * @param ethNum 网络接口名,Linux下是eth0
     * @return
     */
    private String getIpByEthNum(String ethNum) {
        try {
            Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            InetAddress ip;
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                if (ethNum.equals(netInterface.getName())) {
                    Enumeration addresses = netInterface.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        ip = (InetAddress) addresses.nextElement();
                        if (ip != null && ip instanceof Inet4Address) {
                            return ip.getHostAddress();
                        }
                    }
                }
            }
        } catch (SocketException e) {
            logger.error(e.getMessage(), e);
        }
        return "获取服务器IP错误";
    }
}