package com.mortals.xhx.module.error.dao.ibatis;


import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.stereotype.Repository;
import com.mortals.xhx.module.error.dao.ErrorLogDao;
import com.mortals.xhx.module.error.model.ErrorLogEntity;
import java.util.Date;
import com.mortals.framework.model.TableParam;
import com.mortals.framework.model.TableStrategy;
import com.mortals.framework.util.DateUtils;
import com.mortals.framework.util.StringUtils;
import org.springframework.util.ObjectUtils;
import com.mortals.framework.dao.ibatis.SubmeterDaoImpl;
import com.mortals.xhx.module.error.model.ErrorLogQuery;
import java.util.List;
/**
* 异常日志DaoImpl DAO接口
*
* @author zxfei
* @date 2022-08-24
*/
@Repository("errorLogDao")
public class ErrorLogDaoImpl extends SubmeterDaoImpl<ErrorLogEntity,Long> implements ErrorLogDao {

    @Override
    protected TableStrategy getTableStrategy() {
        return new TableStrategy("mortals_xhx_error_log_${unionTime}", TableStrategy.STRATEGY_BY_MONTH, 3);
    }

    @Override
    public TableParam getTableParam(ErrorLogEntity makeTableNameOfentity) {
        TableParam tableParam = new TableParam();
        if (!ObjectUtils.isEmpty(makeTableNameOfentity.getCreateTime())) {
            tableParam.unionTime = makeTableNameOfentity.getCreateTime();
        }else  if (StringUtils.isNotEmpty(makeTableNameOfentity.getCreateTimeStart())) {
            tableParam.unionTime = DateUtils.StrToDate(makeTableNameOfentity.getCreateTimeStart());
        } else if (StringUtils.isNotEmpty(makeTableNameOfentity.getCreateTimeEnd())) {
            tableParam.unionTime = DateUtils.StrToDate(makeTableNameOfentity.getCreateTimeEnd());
        }

        if (makeTableNameOfentity instanceof ErrorLogQuery) {
            ErrorLogQuery ErrorLogQuery = (ErrorLogQuery) makeTableNameOfentity;
            if (StringUtils.isNotEmpty(ErrorLogQuery.getCreateTimeStart())) {
                tableParam.unionTime = DateUtils.StrToDate(ErrorLogQuery.getCreateTimeStart());
            } else if (StringUtils.isNotEmpty(ErrorLogQuery.getCreateTimeEnd())) {
                tableParam.unionTime = DateUtils.StrToDate(ErrorLogQuery.getCreateTimeEnd());
            }
        }
        if (ObjectUtils.isEmpty(tableParam.unionTime)) {
            tableParam.unionTime = new Date();
        }
        return tableParam;
    }



    @Override
    public ErrorLogEntity get(Long id) {
        ErrorLogEntity errorLogEntity = new ErrorLogEntity();
        Snowflake snowflake = IdUtil.getSnowflake(0, 1);
        long generateDateTime = snowflake.getGenerateDateTime(id);
        errorLogEntity.setCreateTime(new Date(generateDateTime));
        return this.get(id,errorLogEntity);
    }



}
