package com.engine.htsc.historyfile.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.util.HT_WorkflowInfo;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HtscHistoryFileListCmd extends AbstractCommonCommand<Map<String, Object>> {

    public HtscHistoryFileListCmd(Map<String, Object> params) {
        this.params = params;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean() ;
        Map<String, Object> apimap = new HashMap<String, Object>();
        PropBean pb = new PropBean();

        String requestid = Util.null2String(params.get("requestid"));
        String docid = Util.null2String(params.get("docid"));
        bb.writeLog("HtscHistoryFileListCmd 入参 requestid:"+requestid+" docid:"+docid);
        try{

            int count  = 0;

            if(StringUtils.isNotEmpty(requestid)){
                count = deletefilebyrequestid(requestid);
            }else if(StringUtils.isNotEmpty(docid)){
                count = deletefilebydocid(docid);
            }
            apimap.put("count",count);
        }catch (Exception e){
            e.printStackTrace();
            bb.warningLog("HtscHistoryFileListCmd 删除nas盘文件异常",e);
        }
        return apimap;
    }


    /***
     *
     * @param requestid
     * @return
     */
    public int deletefilebyrequestid(String requestid){
        PropBean pb = new PropBean();
        String lsqy_zbzysx_fzjgsx = pb.getPropName("lsqy_zbzysx_fzjgsx");
        String lsqy_zbyy_fzjgyy = pb.getPropName("lsqy_zbyy_fzjgyy");
        String lsqy_zbtyfk = pb.getPropName("lsqy_zbtyfk");

        int count = 0;
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean() ;
        HT_WorkflowInfo HT_WorkflowInfo = new HT_WorkflowInfo();
        try{
            bb.writeLog("HtscHistoryFileListCmd deletefilebyrequestid requestid:"+requestid);

            String formtable = HT_WorkflowInfo.getFormTableByRequestid(requestid);
            String workflowid = HT_WorkflowInfo.getWorkflowidByRequestId(requestid);

            String isvalid = "" ;
            String sql = " select isvalid from workflow_base where id = "+workflowid;
            bb.writeLog("HtscHistoryFileListCmd deletefilebyrequestid 查询流程是否合法sql:"+sql);
            rs.execute(sql);
            if(rs.next()){
                isvalid = Util.null2String(rs.getString("isvalid"));
            }

            bb.writeLog("HtscHistoryFileListCmd deletefilebyrequestid 流程合法结果isvalid:"+isvalid);
            List<String> list = new ArrayList<String>();
            if(!"".equals(isvalid) && !"1".equals(isvalid)){
                String unnumber = HT_WorkflowInfo.getWorkflowUnNumberByWorkflowid(workflowid);
                bb.writeLog("HtscHistoryFileListCmd deletefilebyrequestid 流程唯一性标识unnumber:"+unnumber);
                if(!"".equals(unnumber)){
                    if((","+lsqy_zbzysx_fzjgsx+",").indexOf(","+unnumber+",")>-1){
                        sql = " select sbwj,fj from "+formtable+" where lslclj is not null and requestid="+requestid;
                        bb.writeLog("HtscHistoryFileListCmd deletefilebyrequestid--lsqy_zbzysx_fzjgsx 查询历史重要事项的附件字段sql:"+sql);
                        System.out.println("HtscHistoryFileListCmd deletefilebyrequestid--lsqy_zbzysx_fzjgsx 查询历史重要事项的附件字段sql:"+sql);
                        rs.execute(sql);
                        while (rs.next()){
                            String fj = Util.null2String(rs.getString("fj"));
                            String sbwj = Util.null2String(rs.getString("sbwj"));
                            if(!"".equals(fj)){
                                list.add(fj);
                            }
                            if(!"".equals(sbwj)){
                                list.add(sbwj);
                            }
                        }
                    }else if((","+lsqy_zbyy_fzjgyy+",").indexOf(","+unnumber+",")>-1){
                        sql = " select yywj,fj from "+formtable+" where lslclj is not null and requestid="+requestid ;
                        bb.writeLog("HtscHistoryFileListCmd deletefilebyrequestid 查询历史用印的附件字段sql:"+sql);
                        System.out.println("HtscHistoryFileListCmd deletefilebyrequestid--lsqy_zbzysx_fzjgsx 查询历史用印的附件字段sql:"+sql);
                        rs.execute(sql);
                        while (rs.next()){
                            String yywj = Util.null2String(rs.getString("yywj"));
                            String fj = Util.null2String(rs.getString("fj"));

                            System.out.println("fj:"+fj);
                            System.out.println("yywj:"+yywj);
                            if(!"".equals(fj)){
                                list.add(fj);
                            }
                            if(!"".equals(yywj)){
                                list.add(yywj);
                            }
                        }
                    }else if((","+lsqy_zbtyfk+",").indexOf(","+unnumber+",")>-1){
                        sql = " select fj from "+formtable+" where lslclj is not null and requestid="+requestid ;
                        bb.writeLog("HtscHistoryFileListCmd deletefilebyrequestid 查询历史用印的附件字段sql:"+sql);
                        System.out.println("HtscHistoryFileListCmd deletefilebyrequestid--lsqy_zbtyfk 查询历史用印的附件字段sql:"+sql);

                        rs.execute(sql);
                        while (rs.next()){
                            String fj = Util.null2String(rs.getString("fj"));
                            if(!"".equals(fj)){
                                list.add(fj);
                            }
                        }
                    }else{
                        sql = " select fj from "+formtable+" where lslclj is not null and requestid="+requestid ;
                        bb.writeLog("HtscHistoryFileListCmd deletefilebyrequestid 查询历史用印的附件字段sql:"+sql);
                       System.out.println("HtscHistoryFileListCmd deletefilebyrequestid222 查询历史用印的附件字段sql:"+sql);
                        rs.execute(sql);
                        while (rs.next()){
                            String fj = Util.null2String(rs.getString("fj"));
                            System.out.println("fj:"+fj);
                            if(!"".equals(fj)){
                                list.add(fj);
                            }
                        }
                    }
                }

                System.out.println("list:"+list.size());

                List<String> fjlist = new ArrayList<String>();
                for(String fj : list){
                    sql = " select t2.filerealpath from imagefile t2 where t2.imagefileid in ( " +
                          " select  t3.imagefileid  from docimagefile t3 where t3.docid in ("+fj+")) " +
                          " and t2.fileid is not null" ;
                    bb.writeLog("查询最终附件信息sql:"+sql);
                    System.out.println("查询最终附件信息sql:"+sql);
                    rs.execute(sql);
                    while (rs.next()){
                        String filerealpath = Util.null2String(rs.getString("filerealpath"));
                        fjlist.add(filerealpath);
                    }
                }

                for(String filepath : fjlist)
                {
                    bb.writeLog("filepath:"+filepath);
                    System.out.println("filepath:"+filepath);
                    if(!"".equals(filepath)) {
                        File file = new File(filepath);
                        if (file.exists())
                        {
                            bb.writeLog("文件在nas盘存在"+file.getPath());
                            if (file.isFile()) {  //如果此file对象是文件的话，直接删除
                                bb.writeLog("执行nas盘删除操作"+file.getPath());
                                file.delete();
                                count++;
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            bb.warningLog("删除泛微nas盘文件出错",e);
        }
        return count;
    }

    /***
     *
     */
    public int deletefilebydocid(String docid){
        int count = 0;
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean() ;
        try{
            List<String> fjlist = new ArrayList<String>();
            String sql =" select t2.filerealpath from imagefile t2 where t2.imagefileid in ( " +
                        " select  t3.imagefileid  from docimagefile t3 where t3.docid in ( "+docid+" )) " +
                        " and t2.fileid is not null" ;
            bb.writeLog("根据docid 精确删除 nas盘文件 sql:"+sql);
            rs.execute(sql);
            while (rs.next()){
                String filerealpath = Util.null2String(rs.getString("filerealpath"));
                fjlist.add(filerealpath);
            }

            for(String filepath : fjlist)
            {
                bb.writeLog("filepath:"+filepath);
                if(!"".equals(filepath)) {
                    File file = new File(filepath);
                    if (file.exists())
                    {
                        bb.writeLog("文件在nas盘存在"+file.getPath());
                        if (file.isFile()) {  //如果此file对象是文件的话，直接删除
                            bb.writeLog("开始执行nas盘删除操作"+file.getPath());
                            file.delete();
                            count++;
                        }
                    }
                }
            }
        }catch (Exception e){
            System.out.println(e);
            bb.writeLog("e:"+e);
        }
        return count;
    }


    public static void main(String[] args) {
        GCONST.setRootPath("WEB-INF/");
        GCONST.setServerName("ecology");
        Map<String, Object> params=new HashMap<>();
        params.put("requestid","5628624");
        //params.put("deptcode","629");
        HtscHistoryFileListCmd HtscHistoryFileListCmd = new HtscHistoryFileListCmd(params);
        CommandContext ceshi = new CommandContext(null,null);
        HtscHistoryFileListCmd.execute(ceshi);
    }


}
