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


import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.mortals.xhx.module.error.model.ErrorLogEntity;
import org.springframework.stereotype.Repository;
import com.mortals.xhx.module.biz.dao.BizLogDao;
import com.mortals.xhx.module.biz.model.BizLogEntity;
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.biz.model.BizLogQuery;
import java.util.List;
/**
* 业务日志DaoImpl DAO接口
*
* @author zxfei
* @date 2022-08-17
*/
@Repository("bizLogDao")
public class BizLogDaoImpl extends SubmeterDaoImpl<BizLogEntity,Long> implements BizLogDao {

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

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


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


}
