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

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

import java.util.Map;

@Slf4j
public class DeleteBatchScheduleCmd extends AbstractCommonCommand<Map<String,Object>> {
    public DeleteBatchScheduleCmd(Map<String,Object> params){
        this.params=params;
    }

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

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String,Object> resultMap = Maps.newHashMap();
        log.info("param : [{}]",params);
        //开始日期
        String startDate = Util.null2String(params.get("startDate"));
        //结束日期
        String endDate = Util.null2String(params.get("endDate"));
        //排班途径
        String pbtj = Util.null2String(params.get("pbtj"));
        //人员排班对象
        String pbdxry = Util.null2String(params.get("pbdxry"));
        //人员分组排班对象
        String pbdxryfz = Util.null2String(params.get("pbdxryfz"));
        //部门排班对象
        String pbdxbm = Util.null2String(params.get("pbdxbm"));
        //分部排班对象
        String pbdxfb = Util.null2String(params.get("pbdxfb"));
        //主键id
        String ids = Util.null2String(params.get("ids"));
        //类型0:单个，1：批量
        String type = Util.null2String(params.get("type"));

        String sql = "delete from uf_pbjg ";
        if (type.equals("0")){

        }else if (type.equals("1")){
            sql = sql + " where bcrq >= ? and bcrq <= ?";
        }
        if (!pbtj.equals("")){
            sql = sql + " and pbtj="+pbtj;
        }
        String condition = "";
        if (!"".equals(ids)){
            if ("".equals(condition)){
                condition = condition + " id in ("+ids+")";
            }else {
                condition = condition + " or id in ("+ids+")";
            }
        }
        if (!"".equals(pbdxry)){
            if ("".equals(condition)){
                condition = condition + " pbdxry in ("+pbdxry+")";
            }else {
                condition = condition + " or pbdxry in ("+pbdxry+")";
            }
        }
        if (!"".equals(pbdxryfz)){
            if ("".equals(condition)){
                condition = condition + " pbdxryfz in ("+pbdxryfz+")";
            }else {
                condition = condition + " or pbdxryfz in ("+pbdxryfz+")";
            }
        }
        if (!"".equals(pbdxbm)){
            if ("".equals(condition)){
                condition = condition + " pbdxbm in ("+pbdxbm+")";
            }else {
                condition = condition + " or pbdxbm in ("+pbdxbm+")";
            }
        }
        if (!"".equals(pbdxfb)){
            if ("".equals(condition)){
                condition = condition + " pbdxfb in ("+pbdxfb+")";
            }else {
                condition = condition + " or pbdxfb in ("+pbdxfb+")";
            }
        }
        if (!"".equals(condition)){
            log.info("DeleteBatchScheduleCmd sql :[{}]",sql);
            if (type.equals("0")){
                sql = sql +" where "+condition;
                resultMap.put("result",DbTools.update(sql));
            }else if (type.equals("1")){
                sql = sql +" and ("+condition+")";
                resultMap.put("result",DbTools.update(sql,startDate,endDate));
            }
        }

        return resultMap;
    }
}
