package com.engine.htsc.mhjc.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.util.HT_Util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HtscKanyueTotalCmd extends AbstractCommonCommand<Map<String, Object>> {
    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    public HtscKanyueTotalCmd(Map<String, Object> params) {
        this.params = params;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        RecordSet rs = new RecordSet();
        Map<String, Object> appdate = new HashMap<String, Object>();
        ResourceComInfo resComInfo;
        DepartmentComInfo deptComInfo;
        BaseBean bb = new BaseBean();
        try{
            String workcode = Util.null2String(params.get("workcode"));
            String type = Util.null2String(params.get("type"));

//            bb.writeLog("workcode:"+workcode);
//            bb.writeLog("type:"+type);
            int total = 0;
            if(!"".equals(workcode))
            {
                resComInfo = new ResourceComInfo();
                deptComInfo = new DepartmentComInfo();

                String sql =" with a as(\n" +
                            " select t1.bt,t1.syqz,t1.syjg,t1.wh,t1.szbm,t1.ngr,t1.modedatacreatedate as fbrq,t1.id,t1.formmodeid,m.formid,\n" +
                            "        t1.sysyr,t1.syry,t1.sybm\n" +
                            " from uf_gwky t1\n" +
                            " left join modeinfo m on m.id = t1.formmodeid\n" +
                            " ) " ;

                //bb.writeLog("sql:"+sql);

                int rycount = 0;

                String userids = "" ;
                String hrmwhere = "";
                String deptwhere  = "";
                String jgwhere="";
                String qzwhere="";
                String qzsplitvalue="";
                HT_Util ht_util=new HT_Util();
                rs.executeQuery(" select id from hrmresource where workcode=? and status = 1 ",workcode);
                while (rs.next())
                {
                    String id = Util.null2String(rs.getString("id"));
                    userids += userids=="" ? id : ","+id;
                    String departmentid = resComInfo.getDepartmentID(id);
                    hrmwhere += hrmwhere =="" ? " instr(','||a1.syry||',',',"+id+",')>0" : " or instr(','||a1.syry||',',',"+id+",')>0";
                    deptwhere += deptwhere == "" ? " instr(','||a1.sybm||',',',"+departmentid+",')>0 " : " or instr(','||a1.sybm||',',',"+departmentid+",')>0 ";

                    String szjgid= ht_util.getszjg(id);
                    if(szjgid!=""){
                        jgwhere += jgwhere == "" ? " instr(','||a1.syjg||',',',"+szjgid+",')>0 " : " or instr(','||a1.syjg||',',',"+szjgid+",')>0 ";
                    }
                    qzsplitvalue +=  id+",";
                    rycount++;
                }

                if(rycount > 0)
                {
                    if(qzsplitvalue.length()>0)
                    {
                        qzsplitvalue=qzsplitvalue.substring(0,qzsplitvalue.length()-1);
                        List<String> listvalue=ht_util.getszqz(qzsplitvalue);
                        if(listvalue!=null&&listvalue.size()>0){
                            for (int i=0;i<listvalue.size();i++){
                                String value= listvalue.get(0).toString();
                                if(value!=""){
                                    qzwhere += qzwhere == "" ? " instr(','||a1.syqz||',',',"+value+",')>0 " : " or instr(','||a1.syqz||',',',"+value+",')>0 ";
                                }
                            }
                        }
                    }

                    String userSqls =   " select a1.bt,a1.wh,a1.szbm,a1.ngr,a1.fbrq,a1.id,a1.formmodeid,a1.formid" +
                            " from a a1 " +
                            " where a1.sysyr = 1 " +
                            " union all " +
                            " select a1.bt,a1.wh,a1.szbm,a1.ngr,a1.fbrq,a1.id,a1.formmodeid,a1.formid" +
                            " from a a1 " +
                            " where " +hrmwhere+
                            " union all " +
                            " select a1.bt,a1.wh,a1.szbm,a1.ngr,a1.fbrq,a1.id,a1.formmodeid,a1.formid" +
                            " from a a1 " +
                            " where " +deptwhere;
                    if(jgwhere!="")
                    {
                        userSqls+=" union all " +
                                " select a1.bt,a1.wh,a1.szbm,a1.ngr,a1.fbrq,a1.id,a1.formmodeid,a1.formid" +
                                " from a a1 " +
                                " where " +jgwhere;
                    }

                    if(qzwhere!=""){
                        userSqls+=" union all " +
                                " select a1.bt,a1.wh,a1.szbm,a1.ngr,a1.fbrq,a1.id,a1.formmodeid,a1.formid" +
                                " from a a1 " +
                                " where " +qzwhere;
                    }

                    //bb.writeLog("userSqls:"+userSqls);
                    String sql_px = "" ;
                    if("0".equals(type))
                    {
                        sql_px =  " select p.* from("+userSqls  +") p where exists(select 1 from uf_gwky_dt5 l where instr(',"+userids+",',','||l.ry||',')>0 and l.mainid = p.id) " ;
                        //bb.writeLog("sql_px:"+sql_px);
                    }else{
                        sql_px = " select p.* from("+userSqls  +") p where not exists(select 1 from uf_gwky_dt5 l where instr(',"+userids+",',','||l.ry||',')>0 and l.mainid = p.id) " ;
                        //bb.writeLog("sql_px:"+sql_px);
                    }

                    String countsql = sql + " select count(1) as total from ("+sql_px+") w ";
                    //bb.writeLog("countsql:"+countsql);
                    rs.execute(countsql);
                    if(rs.next()){
                        total = Util.getIntValue(rs.getString("total"));
                    }
                }
            }
            appdate.put("total",total);
        }catch (Exception e){

        }
        return appdate;
    }
}
