package com.engine.htsc.mhjc.dao;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.interfaces.htsc.util.HT_Util;

import java.util.List;

/**
 * @ClassName HtscPortalUtil
 * @Description: TODO
 * @Author K1810007
 * @Date 2022/9/23
 * @Version V1.0
 **/

@Slf4j
public class HtscPortalDao {

    public String getQueryDataSql(String workcode) {

        HT_Util ht_util = new HT_Util();
        RecordSet rs = new RecordSet();
        StringBuffer sqlBuffer = new StringBuffer();
        if (StringUtils.isNotEmpty(workcode))
        {
            sqlBuffer.append(" and (p.sysyr = 1 ");

            String userids = "";
            rs.executeQuery(" select id,departmentid from hrmresource where workcode=? and status = 1 ",workcode);
            while (rs.next()) {
                String id = Util.null2String(rs.getString("id"));
                String departmentid = Util.null2String(rs.getString("departmentid"));
                userids += StringUtils.isEmpty(userids) ? id : "," + id;

                sqlBuffer.append(" or instr(','||p.syry||',',',"+id+",')>0 ");
                sqlBuffer.append(" or  instr(','||p.sybm||',',',"+departmentid+",')>0 ");

                String szjgid = ht_util.getszjg(id);
                if (StringUtils.isNotEmpty(szjgid)) {
                    sqlBuffer.append(" or instr(','||p.syjg||',',',"+szjgid+",')>0 ");
                }
            }

            if (userids.length() > 0)
            {
                List<String> listvalue = ht_util.getszqz(userids);
                if (listvalue != null && listvalue.size() > 0)
                {
                    for (int i = 0; i < listvalue.size(); i++)
                    {
                        String value = listvalue.get(i);
                        if (StringUtils.isNotEmpty(value))
                        {
                            sqlBuffer.append(" or instr(','||p.syqz||',',',"+value+",')>0 ");
                        }
                    }
                }
            }
            sqlBuffer.append(" ) ");
        }

        return sqlBuffer.toString();
    }

    /***
     *
     * @param workcode
     * @return
     */
    public String getKanyueUserWhere(String workcode) {
        RecordSet rs = new RecordSet();
        StringBuffer sqlBuffer = new StringBuffer();
        String dt5_where = "";
        if (StringUtils.isNotEmpty(workcode))
        {
            rs.executeQuery(" select id,departmentid from hrmresource where workcode=? and status = 1 ",workcode);
            while (rs.next()) {
                String id = Util.null2String(rs.getString("id"));
                dt5_where += StringUtils.isEmpty(dt5_where) ? " l.ry ="+id : " or l.ry ="+id ;
            }
        }

        if(StringUtils.isNotEmpty(dt5_where)){
            sqlBuffer.append(" and ("+dt5_where+") ") ;
        }
        return sqlBuffer.toString();
    }


    /***
     *
     * @param userid
     * @return
     */
    public String getUserCodeByUserid(int userid) {
        RecordSet rs = new RecordSet();

        String workcode = "" ;
        String sql = " select workcode from hrmresource where id = ? and status = 1" ;
        rs.executeQuery( sql,userid);
        if (rs.next()) {
            workcode = Util.null2String(rs.getString("workcode"));
        }
        return workcode;
    }


    public static void main(String[] args) {
        GCONST.setRootPath("WEB-INF/");
        GCONST.setServerName("ecology");

        HtscPortalDao htscMhjcCmd=new HtscPortalDao();
        htscMhjcCmd.getQueryDataSql("012901");

    }


}
