package weaver.interfaces.htsc.gwky;

import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractCustomSqlConditionJavaCode;
import weaver.general.BaseBean;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.util.HT_Util;

import java.util.List;
import java.util.Map;


/**
 *  公告查询接口
 * 说明
 * 修改时
 * 类名要与文件名保持一致
 * class文件存放位置与路径保持一致。
 * 请把编译后的class文件，放在对应的目录中才能生效
 * 注意 同一路径下java名不能相同。
 * @author Administrator
 *
 */
public class GonggaoDataCustomSql3 extends AbstractCustomSqlConditionJavaCode {

    /**
     * 生成SQL查询限制条件
     * @param param
     *  param包含(但不限于)以下数据
     *  user 当前用户
     *
     * @return
     *  返回的查询限制条件的格式举例为: t1.a = '1' and t1.b = '3' and t1.c like '%22%'
     *  其中t1为表单主表表名的别名
     */
    public String generateSqlCondition(Map<String, Object> param) throws Exception {
        try {
            RecordSet rs = new RecordSet();

            User user = (User)param.get("user");
            String userid = user.getUID()+"";

            String sqlCondition = doGetSqlCondition(userid);
            return sqlCondition;

        }catch (Exception exo){
            return "t1.id in (-1)";
        }
    }


    /***
     *
     * @param userid
     * @return
     */
    public String doGetSqlCondition(String userid){

        BaseBean bb = new BaseBean();
        HT_Util ht_util = new HT_Util();
        try {
            RecordSet rs = new RecordSet();
            rs.executeQuery(" select id,departmentid from hrmresource where workcode in (select workcode from hrmresource where id=? ) and status = 1 ", userid);
            String userids = "";
            String hrmwhere = "";
            String deptwhere = "";
            String jgwhere = "";
            String qzwhere = "";
            while (rs.next()) {
                String id = Util.null2String(rs.getString("id"));
                String departmentid = Util.null2String(rs.getString("departmentid"));
                userids += userids == "" ? id : "," + id;
                hrmwhere += hrmwhere == "" ? " instr(','||t1.syry||',','," + id + ",')>0" : " or instr(','||t1.syry||',','," + id + ",')>0";
                deptwhere += deptwhere == "" ? " instr(','||t1.sybm||',','," + departmentid + ",')>0 " : " or instr(','||t1.sybm||',','," + departmentid + ",')>0 ";
                String szjgid = ht_util.getszjg(id);
                if (szjgid != "") {
                    jgwhere += jgwhere == "" ? " instr(','||t1.syjg||',','," + szjgid + ",')>0 " : " or instr(','||t1.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 (value != "")
                        {
                            qzwhere += qzwhere == "" ? " instr(','||t1.syqz||',','," + value + ",')>0 " : " or instr(','||t1.syqz||',','," + value + ",')>0 ";
                        }
                    }
                }
            }

            String userSqls = " and ( " ;
            userSqls += " (nvl(t1.sysyr,0) = 1) " ;
            if(hrmwhere!=""){
                userSqls+=	"  or ((" + hrmwhere +") and nvl(t1.sysyr,0) !=1 ) ";
            }
            if(deptwhere!="")
            {
                userSqls+=	" or ((" + deptwhere +") and nvl(t1.sysyr,0) !=1 ) ";
            }
            if (jgwhere != "") {
                userSqls += "  or ((" + jgwhere +") and nvl(t1.sysyr,0) !=1 ) ";
            }
            if (qzwhere != "") {
                userSqls += " or ((" + qzwhere +") and nvl(t1.sysyr,0) !=1 )";
            }
            userSqls += " ) " ;

            String sql_px =  " t1.gwgg = '1'  and ((to_char(sysdate,'yyyy-mm-dd') <=  t1.yxqx and  t1.yxqx is not null) or t1.yxqx is null) " + userSqls  ;
            System.out.println("sql_px:"+sql_px);
            return sql_px;
        }catch (Exception exo){
            return "t1.id in (-1)";
        }
    }


    public static void main(String[] args) {
        GCONST.setRootPath("WEB-INF/");
        GCONST.setServerName("ecology");
        String userid = "11260" ;
        GonggaoDataCustomSql3 gonggaoDataCustomSql3 = new GonggaoDataCustomSql3();
        gonggaoDataCustomSql3.doGetSqlCondition(userid);
    }

}