package com.engine.htsc.convertor;

import com.engine.htsc.doc.model.UserInfoModel;
import weaver.conn.RecordSet;
import weaver.general.Util;

/**
 * 泛微流程表单字段转换服务
 * selectvalue <------> selectname
 */
public class EcologyValueConvertor {

    /**
     * 根据requestId获取主表表名
     * @param requestId
     * @return
     */
    public static String getMainTableNameByRequestId(String requestId) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("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 = ?",new Object[]{requestId});
        String tablename = "";
        if(rs.next()){
            tablename = rs.getString("tablename");
        }
        return tablename;
    }

    /**
     * 获取展示的字段名
     * @param requestId 流程实例号
     * @param filedName 表单字段
     * @param selectValue 数据库存的字段值
     * @return 展示的字段名
     */
    public static String getSelectNameByRequestIdAndFieldValue(String requestId, String filedName, String selectValue) {
        RecordSet rs = new RecordSet();
        String reuslt = "";
        String sql = " select t4.selectname from workflow_requestbase t0,workflow_base t1,workflow_bill t2,workflow_billfield t3,workflow_selectitem t4 where t0.workflowid = t1.id "+
                " and t1.formid = t2.id and t3.billid = t2.id  and t4.fieldid = t3.id and t0.requestid =  '"+requestId+"' "+
                " and t3.fieldname = '"+filedName+"' "+
                " and t4.selectvalue = '"+selectValue+"' ";
        rs.execute(sql);
        if(rs.next()) {
            reuslt = Util.null2String(rs.getString("selectname"));
        }
        return reuslt;
    }

    /**
     * 根据泛微人员Id获取人员DTO
     * @param id
     * @return
     */
    public static UserInfoModel getUserInfoModelById(String id) {
        RecordSet rs = new RecordSet();
        UserInfoModel user = new UserInfoModel();
        String sql = " select t0.id,t0.loginid,t0.workcode,t0.lastname,t3.jobtitlecode,t3.jobtitlename,t5.deptid,t4.departmentcode,t4.departmentname,t5.depttypecode,t5.depttypename, "+
            " t5.deptscalelevelcode,t5.deptscalelevelname,t5.deptcodepath,t5.deptnamepath,t0.seclevel,t0.email,t0.telephone,t0.mobile "+
            " from hrmresource t0 ,hrmjobtitles t3, HRMDEPARTMENT t4, hrmdepartmentdefined t5 "+
            " where t0.jobtitle = T3.id and T3.jobdepartmentid = T4.id and t4.id = t5.deptid and t0.id = '"+id+"' ";
        rs.executeQuery(sql);
        if(rs.next()) {
            user.setUserId(Util.null2String(rs.getString("id")));
            user.setLoginId(Util.null2String(rs.getString("loginid")));
            user.setWorkCode(Util.null2String(rs.getString("workcode")));
            user.setName(Util.null2String(rs.getString("lastname")));
            user.setJobCode(Util.null2String(rs.getString("jobtitlecode")));
            user.setJobName(Util.null2String(rs.getString("jobtitlename")));
            user.setDepartmentId(Util.null2String(rs.getString("deptid")));
            user.setDepartmentCode(Util.null2String(rs.getString("departmentcode")));
            user.setDepartmentName(Util.null2String(rs.getString("departmentname")));
            user.setDeptTypeCode(Util.null2String(rs.getString("depttypecode")));
            user.setDeptTypeName(Util.null2String(rs.getString("depttypename")));
            user.setDeptClassCode(Util.null2String(rs.getString("deptscalelevelcode")));
            user.setDeptClassName(Util.null2String(rs.getString("deptscalelevelname")));
            user.setDeptCodePath(Util.null2String(rs.getString("deptcodepath")));
            user.setDeptNamePath(Util.null2String(rs.getString("deptnamepath")));
            user.setSeclLevel(Integer.valueOf(rs.getString("seclevel")));
            user.setEmail(Util.null2String(rs.getString("email")));
            user.setTelephone(Util.null2String(rs.getString("telephone")));
            user.setMobile(Util.null2String(rs.getString("mobile")));
        }
        return user;
    }

}
