package com.engine.kq.cmd.balanceofleaverp;

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.Util;
import weaver.hrm.User;

import java.util.HashMap;
import java.util.Map;

/**
 * 假期余额报表--假期使用记录--分页组件
 */
public class GetDetailPageInfoCmd extends AbstractCommonCommand<Map<String, Object>> {

    public GetDetailPageInfoCmd(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> resultMap = new HashMap<String, Object>();

        int resourceId = Util.getIntValue((String) params.get("resourceId"), -1);//人员ID
        int ruleId = Util.getIntValue((String) params.get("ruleId"), -1);//假期规则ID
        String changeType = Util.null2String(params.get("changeType"));//使用记录还是新增记录

        int total = 0;
        int pageSize = 10;
        String sql = "select count(*) total from kq_UsageHistory where leaveRulesId=" + ruleId + " and relatedId=" + resourceId;
        if(changeType.equals("Reduce")){
            sql += " and ((newBaseAmount+newExtraAmount-newUsedAmount)-(oldBaseAmount+oldExtraAmount-oldUsedAmount)+(newBaseAmount2+newExtraAmount2-newUsedAmount2)-(oldBaseAmount2+oldExtraAmount2-oldUsedAmount2))<0 ";
        }else if(changeType.equals("Add")){
            sql += " and ((newBaseAmount+newExtraAmount-newUsedAmount)-(oldBaseAmount+oldExtraAmount-oldUsedAmount)+(newBaseAmount2+newExtraAmount2-newUsedAmount2)-(oldBaseAmount2+oldExtraAmount2-oldUsedAmount2))>0 ";
        }
        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sql);
        if (recordSet.next()) {
            total = recordSet.getInt("total");
        }
        resultMap.put("total", total);
        resultMap.put("pageSize", pageSize);
        return resultMap;
    }
}
