package com.engine.recruit.entity.recruitflow.po;

import cn.hutool.core.convert.Convert;
import com.engine.recruit.util.RecruitFlowUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;

import java.util.Map;

/**
 * @author:dxfeng
 * @createTime: 2023/10/11
 * @version: 1.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class RecruitTabPo {
    private static final String SEARCH_URL = "/main/cube/search";
    private String key;
    private String title;
    private String url;
    private String viewcondition;

    private String billId;

    public String getUrl() {
        return url;
    }

    public String getTitle() {
        if (StringUtils.isNotBlank(url) && url.contains(SEARCH_URL)) {
            Map<String, String> paramMap = RecruitFlowUtil.parseURL(getUrl());
            String customId = paramMap.get("customid");
            if (StringUtils.isBlank(customId)) {
                return title;
            }
            // 查询对应查询列表的表名
            RecordSet rs = new RecordSet();
            rs.executeQuery("select tablename from workflow_bill where id = (select formid from mode_customsearch where id = ?)", customId);
            if (rs.next()) {
                String tableName = rs.getString("tablename");
                String tabCount = "0";
                if ("uf_jcl_yppc".equalsIgnoreCase(tableName)) {
                    rs.executeQuery("select xm,sjhm from uf_jcl_yppc where id = ?", Convert.toInt(billId, -1));
                    if (rs.next()) {
                        // 只统计展示入库的数据，即formmodeid不为null的数据
                        rs.executeQuery("select count(id) as num from uf_jcl_yppc where formmodeid is not null and xm=? and sjhm =?", rs.getString("xm"), rs.getString("sjhm"));
                    }
                } else {
                    rs.executeQuery("select count(1) as num from " + tableName + "  where ypz = ? ", Convert.toInt(billId, -1));
                }
                if (rs.next()) {
                    tabCount = rs.getString("num");
                }
                title += "(" + tabCount + ")";
            }
        }
        return title;
    }
}
