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 lombok.extern.slf4j.Slf4j;
import weaver.general.Util;
import java.util.List;
import java.util.Map;

@Slf4j
public class GetDatatableCountCmd extends AbstractCommonCommand<Map<String,Object>> {
    @Override
    public BizLogContext getLogContext() {
        return null;
    }
    public GetDatatableCountCmd(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"));


        String sql = "select count(0) total from "+tableName +" where 1=1";
        List<Object> params = Lists.newArrayList();

        String conditions = "";
        if (!"".equals(startDate) && !"".equals(endDate)){
            conditions += " and bcrq >= ? and bcrq<= ?";
            params.add(startDate);
            params.add(endDate);
        }
        if (!"".equals(pblx)){
            conditions += " and dxlx = ?";
            params.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 = ?";
            }
            params.add(pbdx);
        }

        sql = sql + conditions;
        log.info("GetDatatableCountCmd sql :{}",sql);
        log.info("params : [{}]",params);
        Map<String,Object> dataMap = DbTools.getSqlToMap(sql,params.toArray());

        return dataMap;
    }
}
