package com.mortals.xhx.modules.call.dao.ibatis;

import com.mortals.framework.util.DateUtils;
import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.modules.call.dao.CallRecordDao;
import com.mortals.xhx.modules.call.model.CallRecordEntity;
import com.mortals.xhx.modules.call.model.CallRecordQuery;
import org.springframework.stereotype.Repository;
import com.mortals.framework.dao.ibatis.SubmeterDaoImpl;
import com.mortals.framework.model.TableParam;
import com.mortals.framework.model.TableStrategy;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
* <p>Title: 接口调用记录</p>
* <p>Description: CallRecordDaoImpl DAO接口 </p>
* @author
* @version 1.0.0
*/
@Repository("callRecordDao")
public class CallRecordDaoImpl extends SubmeterDaoImpl<CallRecordEntity,Long> implements CallRecordDao {
    @Override
    protected TableStrategy getTableStrategy()
    {
        return new TableStrategy("mortals_iot_zd_callrecord_${unionTime}", TableStrategy.STRATEGY_BY_MONTH,3);
    }

    @Override
    public TableParam getTableParam(CallRecordEntity entity)
    {
        TableParam param = new TableParam();
        if (!ObjectUtils.isEmpty(entity.getCreateTime())){
            param.unionTime = entity.getCreateTime();
        }else if (entity instanceof CallRecordQuery) {
            CallRecordQuery accountDetailQuery = (CallRecordQuery) entity;
            if (StringUtils.isNotEmpty(accountDetailQuery.getCreateDateStart())) {
                param.unionTime = DateUtils.StrToDate(accountDetailQuery.getCreateDateStart());
            } else if (StringUtils.isNotEmpty(accountDetailQuery.getCreateDateEnd())) {
                param.unionTime = DateUtils.StrToDate(accountDetailQuery.getCreateDateEnd());
            }
        }
        if (ObjectUtils.isEmpty(param.unionTime)) {
            param.unionTime = new Date();
        }
        return param;
    }
}
