package com.engine.attendance.component.persongroup.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.common.util.DbTools;
import com.engine.core.interceptor.CommandContext;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import weaver.conn.RecordSet;
import weaver.general.Util;

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

@Slf4j
public class GetDatatableCmd extends AbstractCommonCommand<Map<String,Object>> {
    @Override
    public BizLogContext getLogContext() {
        return null;
    }
    public GetDatatableCmd(Map<String,Object> params){
        this.params=params;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        String tableName = Util.null2String(params.get("tableName"));
        String startDate = Util.null2String(params.get("startDate"));
        String endDate = Util.null2String(params.get("endDate"));
        String pblx = Util.null2String(params.get("pblx"));
        String pbdx = Util.null2String(params.get("pbdx"));
        int total = Integer.valueOf(Util.null2String(params.get("total")));
        int current = Integer.valueOf(Util.null2String(params.get("current")));
        int pageSize = Integer.valueOf(Util.null2String(params.get("pageSize")));
        RecordSet rs = new RecordSet();
        Map<String, Object> resultMap = Maps.newHashMap();
        String sql = "select id from "+tableName +" where 1=1 ";
        List<Object> param = Lists.newArrayList();

        String conditions = "";
        if (!"".equals(startDate) && !"".equals(endDate)){
            conditions += " and bcrq >= ? and bcrq<= ?";
            param.add(startDate);
            param.add(endDate);
        }
//        if (!"".equals(pblx)){
//            conditions += " and dxlx = ?";
//            param.add(pblx);
//        }
        if (!"".equals(pblx)){
            conditions += " and dxlx = ?";
            param.add(pblx);
        }
        if (!"".equals(pbdx)){
            if ("0".equals(pblx)){
                //人员
                conditions += " and pbdxry = ?";

            }else if ("1".equals(pblx)){
                //人员分组
                conditions += " and pbdxryfz = ?";

            }else if ("2".equals(pblx)){
                //部门
                conditions += " and pbdxbm = ?";

            }else if ("3".equals(pblx)){
                //分部
                conditions += " and pbdxfb = ?";
            }
            param.add(pbdx);
        }
        int startindex = (current-1)*pageSize;
        int endindex = current*pageSize;
        String dbType = rs.getDBType();

        if ("oracle".equals(dbType) || "dm".equals(dbType) || "jc".equals(dbType) || "st".equals(dbType)){
            sql = "select a.id as keyid,b.lastname,a.*,ROWNUM rn from "+tableName +" a left join hrmresource b on a.pbdxry=b.id  where ROWNUM<="+endindex;
            sql = sql+conditions;

            String queryDatatablesql = "select * from ("+sql+") where rn >"+startindex;

            log.info("queryDatatablesql : {}",queryDatatablesql);
            log.info("param : {}",param);
            List<Map<String,Object>> dataTable = DbTools.getSqlToList(queryDatatablesql,param.toArray());
            resultMap.put("data",dataTable);

        }else if ("sqlserver".equals(dbType)){
            sql = "select row_number() over(order by id asc) rownum ,id from "+tableName +" where 1=1 ";
            sql = sql+conditions;
            sql = "select top 1 a.id from ( "+sql+" ) a where a.rownum>"+startindex;

            String queryDatatablesql = "select top "+pageSize+" a.id as keyid,b.lastname,a.* from "+tableName +" a left join hrmresource b on a.pbdxry=b.id where a.id>=("+sql+") "+conditions;

            log.info("queryDatatablesql : {}",queryDatatablesql);
            param.addAll(param);
            log.info("param : {}",param);
            List<Map<String,Object>> dataTable = DbTools.getSqlToList(queryDatatablesql,param.toArray());
            resultMap.put("data",dataTable);
        }else {
            sql = sql + conditions +" limit "+startindex+",1";
            String queryDatatablesql = "select a.id as keyid,b.lastname,a.* from "+tableName +" a left join hrmresource b on a.pbdxry=b.id where a.id>=("+sql+") "+conditions+" limit "+pageSize;
            log.info("queryDatatablesql : {}",queryDatatablesql);
            param.addAll(param);
            log.info("param : {}",param);
            List<Map<String,Object>> dataTable = DbTools.getSqlToList(queryDatatablesql,param.toArray());
            resultMap.put("data",dataTable);
        }

        return resultMap;
    }
}
