package com.engine.htsc.portal.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.htsc.itcglc.cmd.ItcglcLdspInfoCmd;
import com.engine.htsc.portal.util.QueryTools;
import com.engine.htsc.response.enums.ResultEnum;
import com.engine.htsc.response.exception.ResultException;
import com.weaver.general.Util;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.GCONST;
import weaver.hrm.User;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ParticipateListCmd extends AbstractCommonCommand<Map<String, Object>> {

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

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String,Object> datas = new HashMap<>();
        RecordSet rs = new RecordSet();
        PropBean pb = new PropBean();

        String usercode = Util.null2String((String) params.get("userid"));
        int page = Util.getIntValue(Util.null2String((String) params.get("page")),1);
        int size = Util.getIntValue(Util.null2String((String) params.get("size")),20);

        List<Map<String,String>> list = new ArrayList<Map<String,String>>();
        Map<String,String> map = null;
        ResourceComInfo resourceComInfo = null;
        DepartmentComInfo departmentComInfo = null;
        try
        {
            String xzUrl = pb.getPropName("xz_portal_url") ;
            String dbUrl = pb.getPropName("db_portal_url") ;
            String rwUrl = pb.getPropName("rw_portal_url") ;

            Map<String,String> xzMap = new HashMap<String,String>();
            String xz_zt_fieldid = "" ;
            String v_sql =  " select k.id from workflow_billfield k\n" +
                            " inner join workflow_bill l on l.id = k.billid\n" +
                            " where l.tablename = 'uf_xzsy' and fieldname = 'xzzt' " ;
            rs.executeQuery(v_sql);
            if(rs.next()){
                xz_zt_fieldid = Util.null2String(rs.getString("id"));
            }
            if(!"".equals(xz_zt_fieldid)){

                v_sql = " select selectvalue,selectname from workflow_selectitem where fieldid ="+xz_zt_fieldid;
                rs.execute(v_sql);
                while (rs.next())
                {
                    String selectvalue = weaver.general.Util.null2String(rs.getString("selectvalue"));
                    String selectname = weaver.general.Util.null2String(rs.getString("selectname"));
                    xzMap.put(selectvalue,selectname);
                }

            }

            Map<String,String> rwMap = new HashMap<String,String>();
            String rw_zt_fieldid = "" ;
            v_sql =  " select k.id from workflow_billfield k\n" +
                    " inner join workflow_bill l on l.id = k.billid\n" +
                    " where l.tablename = 'uf_rw' and fieldname = 'rwzt' " ;
            rs.executeQuery(v_sql);
            if(rs.next()){
                rw_zt_fieldid = Util.null2String(rs.getString("id"));
            }
            if(!"".equals(rw_zt_fieldid)){
                v_sql = " select selectvalue,selectname from workflow_selectitem where fieldid ="+rw_zt_fieldid;
                rs.execute(v_sql);
                while (rs.next())
                {
                    String selectvalue = weaver.general.Util.null2String(rs.getString("selectvalue"));
                    String selectname = weaver.general.Util.null2String(rs.getString("selectname"));
                    rwMap.put(selectvalue,selectname);
                }
            }

            Map<String,String> dbMap = new HashMap<String,String>();
            String db_zt_fieldid = "" ;
            v_sql =  " select k.id from workflow_billfield k\n" +
                    " inner join workflow_bill l on l.id = k.billid\n" +
                    " where l.tablename = 'uf_xzsx' and fieldname = 'xzzt' " ;
            rs.executeQuery(v_sql);
            if(rs.next()){
                db_zt_fieldid = Util.null2String(rs.getString("id"));
            }
            if(!"".equals(xz_zt_fieldid)){

                v_sql = " select selectvalue,selectname from workflow_selectitem where fieldid ="+db_zt_fieldid;
                rs.execute(v_sql);
                while (rs.next())
                {
                    String selectvalue = weaver.general.Util.null2String(rs.getString("selectvalue"));
                    String selectname = weaver.general.Util.null2String(rs.getString("selectname"));
                    dbMap.put(selectvalue,selectname);
                }
            }




            int count= 0;
            resourceComInfo = new ResourceComInfo();
            departmentComInfo = new DepartmentComInfo();
            String where = "" ;
            if(!"".equals(usercode))
            {
                String sql = " select id from hrmresource where workcode='"+usercode+"'and status=1 ";
                rs.executeQuery(sql);
                while(rs.next()){
                    String id = Util.null2String(rs.getString("id"));
                    where +=  where=="" ? "  instr(',' || tzydzzhyc || ',', ',"+id+",') > 0 "  : " or instr(',' ||tzydzzhyc || ',', ',"+id+",') > 0\n" ;
                }
            }

            if(!"".equals(where)){
                where = " and ( "+where+" ) " ;
            }

            String sql1 =" select id, bt,cjr,to_char(jbr) as jbr,substr(cjrq, 0, 16) cjrq,xzzt as zt,'1' as lx " +
                        "        from uf_xzsy\n" +
                        " where cjzt = 1 " + where +
                        " union all "+
                        " select id,rwbt as bt,cjr,to_char(jbr) as jbr,substr(cjrq, 0, 16) cjrq,rwzt as zt,'2' as lx\n" +
                        "        from uf_rw \n" +
                        " where cjzt = 1  " + where +
                        " union all "+
                        " select id,bt,cjr,to_char(jbr) as jbr,substr(cjrq, 0, 16) cjrq,xzzt,'3' as lx\n" +
                        "        from uf_xzsx \n" +
                        " where cjzt = 1 " + where ;

            String countsql = " select count(1) as cou from (" +
                                sql1+
                                " ) p1 ";

            rs.executeQuery(countsql);
            if (rs.next()){
                count = Util.getIntValue(Util.null2String(rs.getString("cou")));
            }
            System.out.println("count:"+count);
            String sql ="  select *\n" +
                        "  from (select p.*, rownum as xh\n" +
                        "       from (   \n" +
                        "           select l.* from ( " + sql1 + " ) l order by substr(l.cjrq, 0, 16) desc\n" +
                        "        ) p\n" +
                        "   ) w\n" +
                        "   where w.xh > " + ((page-1) *size) +
                        "   and w.xh <=  " + (page*size) ;
            rs.executeQuery(sql);
            while (rs.next()){
                String id = Util.null2String(rs.getString("id"));
                String bt = Util.null2String(rs.getString("bt"));
                String cjr = Util.null2String(rs.getString("cjr"));
                String jbr = Util.null2String(rs.getString("jbr"));
                String cjrq = Util.null2String(rs.getString("cjrq"));
                String zt = Util.null2String(rs.getString("zt"));
                String ztmc = "" ;

                String lx = Util.null2String(rs.getString("lx"));
                String url = "" ;
                if("1".equals(lx)){
                    url = xzUrl +"&billid="+id;

                    if(!"".equals(zt)){
                        ztmc = xzMap.get(zt);
                    }

                }else if("2".equals(lx)){
                    url = rwUrl +"&billid="+id;

                    if(!"".equals(zt)){
                        ztmc = rwMap.get(zt);
                    }

                }else if("3".equals(lx)){
                    url = dbUrl +"&billid="+id;
                    if(!"".equals(zt)){
                        ztmc = dbMap.get(zt);
                    }
                }

                String jbrname = "" ;
                String cjrname = "" ;
                String cjrbm = "" ;
                String cjrbmname = "" ;
                if(!"".equals(jbr)){
                    jbrname = resourceComInfo.getLastnames(jbr);
                    cjrbm = resourceComInfo.getDepartmentID(jbr);

                }

                if(!"".equals(cjrbm)){
                    cjrbmname = departmentComInfo.getDepartmentName(cjrbm);
                }
                if(!"".equals(cjr)){
                    cjrname = resourceComInfo.getLastname(cjr);
                }
                map = new HashMap<String,String>();
                map.put("id",id);
                map.put("bt",bt);
                map.put("cjr",cjr);
                map.put("zxr",jbr);
                map.put("cjrq",cjrq);
                map.put("zt",zt);
                map.put("cjrname",cjrname);
                map.put("cjrbmname",cjrbmname);
                map.put("zxrname",jbrname);
                map.put("ztmc",ztmc);
                map.put("url",url);
                list.add(map);
            }

            datas.put("data",list);
            datas.put("count",count);
        }catch (Exception e){
            e.printStackTrace();
        }
        return datas;
    }


    public static void main(String[] args) {
        GCONST.setRootPath("WEB-INF/");
        GCONST.setServerName("ecology");
        Map<String, Object> params=new HashMap<>();
        params.put("userid","012901");
        params.put("page","1");
        params.put("size","10");
        User user = new User();
        user.setUid(11260);
        ParticipateListCmd ParticipateListCmd = new ParticipateListCmd(params,user);
        CommandContext ceshi= new CommandContext(null,null);
        ParticipateListCmd.execute(ceshi);
    }






}
