package com.mortals.xhx.module.operate.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.operate.dao.OperateLogDao;
import com.mortals.xhx.module.operate.model.OperateLogEntity;

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.operate.model.OperateLogQuery;

import java.util.List;

/**
 * 平台用户操作日志业务DaoImpl DAO接口
 *
 * @author zxfei
 * @date 2022-08-17
 */
@Repository("operateLogDao")
public class OperateLogDaoImpl extends SubmeterDaoImpl<OperateLogEntity, Long> implements OperateLogDao {

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

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

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


}
