package weaver.interfaces.htsc.payment;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;


public class DocumentsNumberAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = Util.null2String(requestInfo.getRequestid());//流程请求ID
//        String requestid = "5793835";
        String mainTable = JAVATools.getMaintable(requestid);
        Integer pjsl = getWfAnnexCount(requestid);//获取附件数量
        Integer djzs = getDjsz(requestid);//获取票据数量
        pjsl = pjsl+djzs;
        RecordSet rs = new RecordSet();
        String sql = "update "+mainTable+" set djsl = '"+pjsl+"' where requestid = '"+requestid+"'";
        JAVATools.writelog("更新主表的djsl单据数量sql:"+sql);
        rs.executeUpdate(sql);
        return Action.SUCCESS;
    }

    public Integer getDjsz(String requestid) {
        RecordSet rs = new RecordSet();
        Integer annexCountNum = 0;
        String sql = "select djzs from "+JAVATools.getMaintable(requestid)+" where requestid = '"+requestid+"'";
        rs.execute(sql);
        if (rs.next()){
            annexCountNum = Integer.parseInt(Util.null2String(rs.getString("djzs")));
        }
        return annexCountNum;
    }

    /**
     * 根据request获取流程所有附件类型的数量
     *
     * @param requestid
     * @return
     */
    public static Integer getWfAnnexCount(String requestid) {
        Integer annexCountNum = 0;
        RecordSet rs = new RecordSet();
        String sql;
        String mainTableName = "";
        // 1、获取主表名
        sql = "select t2.tablename from workflow_requestbase t0,workflow_base t1,workflow_bill t2 " +
                "where t0.workflowid = t1.id and t1.formid = t2.id and  t0.requestid = ?";
        rs.executeQuery(sql, requestid);
        if (rs.next()) {
            mainTableName = Util.null2String(rs.getString("tablename"));
        }
        // 2、获取该流程的所有附件属性字段
        String annexFiledName = "";
        if ("".equals(mainTableName)) {
            return 0;
        } else {
            sql = "select wm_concat_old(FIELDNAME) as Annexs from workflow_billfield where viewtype =0 and id in (" +
                    "       select FIELDID from WORKFLOW_FILEUPLOAD where WORKFLOWID = (" +
                    "              select t1.id from workflow_requestbase t0,workflow_base t1,workflow_bill t2 " +
                    "              where t0.workflowid = t1.id and t1.formid = t2.id and  t0.requestid = ?" +
                    "       ) and DOCCATEGORY is not NULL" +
                    ")";
            rs.executeQuery(sql, requestid);
            if (rs.next()) {
                annexFiledName = Util.null2String(rs.getString("Annexs"));
            }
            if ("".equals(annexFiledName)) {
                return 0;
            } else {
                sql = "select " + annexFiledName + " from " + mainTableName + " where requestid = '" + requestid + "'";
                rs.executeQuery(sql);
                if (rs.next()){
                    for (String column : rs.getColumnName()) {
                        String annextDetail = Util.null2String(rs.getString(column));
                        if (!"".equals(annextDetail)){
                            annexCountNum += annextDetail.split(",").length;
                        }
                    }
                    return annexCountNum;
                }else {
                    return 0;
                }
            }
        }
    }
}
