package com.engine.organization.util.field;

import com.engine.hrm.biz.HrmClassifiedProtectionBiz;
import com.engine.hrm.biz.HrmCountryCityBiz;
import com.engine.organization.entity.SelectOptionParam;
import com.engine.organization.entity.extend.po.ExtendInfoPO;
import weaver.conn.RecordSet;
import weaver.cpt.capital.CapitalComInfo;
import weaver.crm.Maint.CustomerInfoComInfo;
import weaver.docs.docs.DocComInfo;
import weaver.docs.senddoc.DocReceiveUnitComInfo;
import weaver.file.ImageFileManager;
import weaver.formmode.browser.FormModeBrowserUtil;
import weaver.formmode.tree.CustomTreeUtil;
import weaver.general.BaseBean;
import weaver.general.StaticObj;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.city.CityComInfo;
import weaver.hrm.city.CitytwoComInfo;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.company.SubCompanyComInfo;
import weaver.hrm.job.JobTitlesComInfo;
import weaver.hrm.province.ProvinceComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.hrm.tools.TrainTypeComInfo;
import weaver.hrm.train.TrainLayoutComInfo;
import weaver.hrm.train.TrainResourceComInfo;
import weaver.interfaces.workflow.browser.Browser;
import weaver.interfaces.workflow.browser.BrowserBean;
import weaver.proj.Maint.ProjectInfoComInfo;
import weaver.workflow.field.BrowserComInfo;
import weaver.workflow.request.ResourceConditionManager;
import weaver.workflow.workflow.WorkflowRequestComInfo;

import java.util.List;

/**
 * @author:dxfeng
 * @createTime: 2022/10/08
 * @version: 1.0
 */
public class FieldDefinedValueUtil {

    /**
     * 根据值获取展示信息
     * @param user
     * @param infoPO
     * @param fieldValue
     * @return
     */
    public static String getFieldValue(User user, ExtendInfoPO infoPO, String fieldValue) {
        int fieldHtmlType = infoPO.getControlType();
        int fieldType = Integer.parseInt(infoPO.getBrowserType());
        String requestId = SelectOptionParam.getCustomBrowserId(infoPO.getCustomValue());
        RecordSet rs = new RecordSet();
        StringBuilder showName = new StringBuilder();
        if (fieldHtmlType == 3) {
            if (fieldType == 402 || fieldType == 403) {
                showName = new StringBuilder(fieldValue);
            }
            List<String> tempshowidlist = Util.splitString2List(fieldValue, ",");
            if (fieldType == 1 || fieldType == 17) { // 人员，多人员
                for (String o : tempshowidlist) {
                    try {
                        showName.append(new ResourceComInfo()
                                .getResourcename(o)).append(",");
                    } catch (Exception e) {
                        new BaseBean().writeLog(e);
                        throw new RuntimeException(e);
                    }
                }
            } else if (fieldType == 2 || fieldType == 19) { // 日期,时间
                showName.append(fieldValue);
            } else if (fieldType == 141) { // 人力资源条件
                ResourceConditionManager rcm = new ResourceConditionManager();
                showName.append(rcm.getFormShowName(fieldValue, user.getLanguage()));
            } else if (fieldType == 4 || fieldType == 57) { // 部门，多部门
                for (String s : tempshowidlist) {
                    showName.append(new DepartmentComInfo()
                            .getDepartmentname((String) s)).append(",");
                }
            } else if (fieldType == 8 || fieldType == 135) { // 项目，多项目
                for (String s : tempshowidlist) {
                    showName.append(new ProjectInfoComInfo()
                            .getProjectInfoname((String) s)).append(",");
                }
            } else if (fieldType == 7 || fieldType == 18) { // 客户，多客户
                for (String s : tempshowidlist) {
                    showName.append(new CustomerInfoComInfo()
                            .getCustomerInfoname((String) s)).append(",");
                }
            } else if (fieldType == 164) { // 分部
                for (String s : tempshowidlist) {
                    showName.append(new SubCompanyComInfo()
                            .getSubCompanyname((String) s)).append(",");
                }
            } else if (fieldType == 170) { // 分权多分部
                for (String s : tempshowidlist) {
                    showName.append(new SubCompanyComInfo().getSubCompanyname((String) s)).append(",");
                }
            } else if (fieldType == 9) { // 单文档
                for (String s : tempshowidlist) {
                    showName.append(new DocComInfo()
                            .getDocname((String) s));
                }
            } else if (fieldType == 37) { // 多文档
                for (String s : tempshowidlist) {
                    showName.append(new DocComInfo()
                            .getDocname((String) s)).append(",");
                }
            } else if (fieldType == 23) { // 资产
                for (String s : tempshowidlist) {
                    showName.append(new CapitalComInfo()
                            .getCapitalname((String) s)).append(",");
                }
            } else if (fieldType == 16 || fieldType == 152) { // 相关请求
                for (String s : tempshowidlist) {
                    showName.append(new WorkflowRequestComInfo()
                            .getRequestName((String) s)).append(",");
                }
            } else if (fieldType == 142) {// 收发文单位
                DocReceiveUnitComInfo docReceiveUnitComInfo = new DocReceiveUnitComInfo();
                for (String s : tempshowidlist) {
                    showName.append(docReceiveUnitComInfo
                            .getReceiveUnitName((String) s)).append(",");
                }
            } else if (fieldType == 226 || fieldType == 227) {// -zzl系统集成浏览按钮
                showName.append(fieldValue);
            } else if (fieldType == 301) {//培训种类
                showName.append(new TrainTypeComInfo().getTrainTypename(fieldValue));
            } else if (fieldType == 302) {//培训规划
                showName.append(new TrainLayoutComInfo().getLayoutname(fieldValue));
            } else if (fieldType == 303) {//培训资源
                showName.append(new TrainResourceComInfo().getResourcename(fieldValue));
            } else if (fieldType == 304) {//人事--合同模板
                String sql = "select templetname from HrmContractTemplet where id=" + fieldValue;
                RecordSet recordSet = new RecordSet();
                recordSet.executeQuery(sql);
                if (recordSet.next()) {
                    showName.append(recordSet.getString("templetname"));
                }
            } else if (fieldType == 305) {//人事--考核项目
                String sql = "select checkitemname from HrmCheckItem where id=" + fieldValue;
                RecordSet recordSet = new RecordSet();
                recordSet.executeQuery(sql);
                if (recordSet.next()) {
                    showName.append(recordSet.getString("checkitemname"));
                }
            } else if (fieldType == 307) {//人事--招聘信息
                String sql = "select b.jobtitlename from HrmCareerInvite a left join HrmJobTitles b on a.careername = b.id where a.id=" + fieldValue;
                RecordSet recordSet = new RecordSet();
                recordSet.executeQuery(sql);
                if (recordSet.next()) {
                    showName.append(recordSet.getString("jobtitlename"));
                }
            } else if (fieldType == 308) {//人事--招聘计划
                String sql = "select topic from HrmCareerPlan where id=" + fieldValue;
                RecordSet recordSet = new RecordSet();
                recordSet.executeQuery(sql);
                if (recordSet.next()) {
                    showName.append(recordSet.getString("topic"));
                }
            } else if (fieldType == 309) {//人事--用工需求
                String sql = "select j.jobtitlename from HrmUseDemand a left join HrmJobTitles j on a.demandjobtitle = j.id where a.id=" + fieldValue;
                RecordSet recordSet = new RecordSet();
                recordSet.executeQuery(sql);
                if (recordSet.next()) {
                    showName.append(recordSet.getString("jobtitlename"));
                }
            } else if (fieldType == 311) {
                HrmClassifiedProtectionBiz hrmClassifiedProtectionBiz = new HrmClassifiedProtectionBiz();
                showName = new StringBuilder(hrmClassifiedProtectionBiz.getUserSecLevelShowName(fieldValue, "" + user.getLanguage()));
            } else if (fieldType == 312) {
                HrmClassifiedProtectionBiz hrmClassifiedProtectionBiz = new HrmClassifiedProtectionBiz();
                showName = new StringBuilder(hrmClassifiedProtectionBiz.getResourceSecLevelShowName(fieldValue, "" + user.getLanguage()));
            } else if (fieldType == 313) {//人事--网段策略
                String sql = "select * from HrmNetworkSegStr where id in (" + fieldValue + ") ";
                RecordSet recordSet = new RecordSet();
                recordSet.executeQuery(sql);
                while (recordSet.next()) {
                    String inceptipaddress = Util.null2String(recordSet.getString("inceptipaddress"));//起始IP地址
                    String endipaddress = Util.null2String(recordSet.getString("endipaddress"));//截止IP地址

                    showName.append(",").append(inceptipaddress).append("~").append(endipaddress);
                }
                showName = new StringBuilder(showName.length() > 0 ? showName.substring(1) : "");
            } else if (fieldType == 24) {//岗位
                JobTitlesComInfo jobTitlesComInfo = new JobTitlesComInfo();
                String[] fieldValueArr = fieldValue.split(",");
                for (String s : fieldValueArr) {
                    showName.append(",").append(jobTitlesComInfo.getJobTitlesmark(s));
                }
                if (showName.length() > 0) {
                    showName = new StringBuilder(showName.substring(1));
                }
            } else if (fieldType == 2222) { // 省份
                ProvinceComInfo provinceComInfo = new ProvinceComInfo();
                HrmCountryCityBiz hrmCountryCityBiz = new HrmCountryCityBiz(fieldType + "");
                String name = hrmCountryCityBiz.getPath(fieldValue, fieldType + "");
                showName.append(name).append(provinceComInfo.getProvincename(fieldValue));
            } else if (fieldType == 58) { //城市
                CityComInfo cityComInfo = new CityComInfo();
                HrmCountryCityBiz hrmCountryCityBiz = new HrmCountryCityBiz(fieldType + "");
                String name = hrmCountryCityBiz.getPath(fieldValue, fieldType + "");
                showName.append(name).append(cityComInfo.getCityname(fieldValue));
            } else if (fieldType == 263) { //区县
                CitytwoComInfo citytwoComInfo = new CitytwoComInfo();
                HrmCountryCityBiz hrmCountryCityBiz = new HrmCountryCityBiz(fieldType + "");
                String name = hrmCountryCityBiz.getPath(fieldValue, fieldType + "");
                showName.append(name).append(citytwoComInfo.getCityname(fieldValue));
            } else if (fieldType == 278) {
                JobTitlesComInfo jobTitlesComInfo = new JobTitlesComInfo();
                String[] fieldValueArr = fieldValue.split(",");
                for (String s : fieldValueArr) {
                    showName.append(",").append(jobTitlesComInfo.getJobTitlesmark(s));
                }
                if (showName.length() > 0) {
                    showName = new StringBuilder(showName.substring(1));
                }
            } else if (fieldType == 161 || fieldType == 162) { //自定义浏览按钮
                try {
                    String fieldDbType = requestId;//用requestid传递数据 dmlurl
                    if (Util.null2String(fieldDbType).length() == 0 || "emptyVal".equals(fieldDbType)) {
                        return "";
                    }
                    String sql = "select count(1) from mode_browser where showname = '" + fieldDbType + "'";
                    rs.execute(sql);
                    if (rs.next()) {//建模浏览框
                        if (!fieldDbType.startsWith("browser.")) {
                            fieldDbType = "browser." + fieldDbType;
                        }
                        FormModeBrowserUtil FormModeBrowserUtil = new FormModeBrowserUtil();
                        showName = new StringBuilder(Util.null2String(FormModeBrowserUtil.getBrowserNameById(fieldDbType, fieldValue, user).get("browsername")));
                    } else {//集成浏览框
                        if (!fieldDbType.startsWith("browser.")) {
                            fieldDbType = "browser." + fieldDbType;
                        }
                        Browser browser = (Browser) StaticObj.getServiceByFullname(fieldDbType, Browser.class);
                        for (String s : tempshowidlist) {
                            try {
                                BrowserBean bb = browser.searchById(s);
                                String name = Util.null2String(bb.getName());
                                if ("".equals(showName.toString())) {
                                    showName.append(name);
                                } else {
                                    showName.append(",").append(name);
                                }
                            } catch (Exception e) {
                                new BaseBean().writeLog(e);
                            }
                        }
                    }
                } catch (Exception e) {
                    new BaseBean().writeLog(e);
                }

            } else if (fieldType == 256 || fieldType == 257) {
                if (!"null".equals(fieldValue)) {
                    CustomTreeUtil customTreeUtil = new CustomTreeUtil();
                    for (String s : tempshowidlist) {
                        String show_val = Util.null2String(s);
                        String name = customTreeUtil.getTreeFieldShowName(show_val, requestId);
                        if ("".equals(showName.toString())) {
                            showName.append(name);
                        } else {
                            showName.append(",").append(name);
                        }
                    }
                }
            } else {
                String sql = "";
                String tableName = new BrowserComInfo().getBrowsertablename(""
                        + fieldType);
                String columName = new BrowserComInfo().getBrowsercolumname(""
                        + fieldType);
                String keyColumName = new BrowserComInfo()
                        .getBrowserkeycolumname("" + fieldType);
                if ("".equals(columName) || "".equals(tableName)
                        || "".equals(keyColumName) || "".equals(fieldValue)) {
                    new BaseBean().writeLog("GET FIELD ERR: fieldType=" + fieldType);
                } else {
                    sql = "select " + columName + " from " + tableName
                            + " where " + keyColumName + " in(" + fieldValue
                            + ")";
                    rs.execute(sql);
                    while (rs.next()) {
                        showName.append(rs.getString(1)).append(",");
                    }
                }
            }
            if (showName.toString().endsWith(",")) {
                showName = new StringBuilder(showName.substring(0, showName.length() - 1));
            }
        } else if (fieldHtmlType == 4) { // check框
            if ("1".equals(fieldValue)) {
                showName.append("√");
            }
        } else if (fieldHtmlType == 5) {
            showName = new StringBuilder(SelectOptionParam.getSelectOption(infoPO.getCustomValue(), fieldValue));
        } else if (fieldHtmlType == 6) { // 附件
            if (Util.null2String(fieldValue).length() > 0) {
                String[] fileids = Util.splitString(fieldValue, ",");
                for (String fileid : fileids) {
                    ImageFileManager manager = new ImageFileManager();
                    manager.getImageFileInfoById(Util.getIntValue(fileid));
                    //showName.append("<div id='file").append(fileid).append("'>").append(manager.getImageFileName()).append("&nbsp;").append("<A href=\"/weaver/weaver.file.FileDownload?fileid=").append(fileid).append("&download=1\" target=\"_blank\">").append(SystemEnv.getHtmlLabelName(258, user.getLanguage())).append("</A>&nbsp;</div>");
                    showName.append("/weaver/weaver.file.FileDownload?fileid=").append(fileid);
                }
            }
        } else {
            showName = new StringBuilder(fieldValue);
        }
        return showName.toString();
    }
}
