package com.mortals.xhx.module.operate.service.impl;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.mortals.xhx.module.error.model.ErrorLogEntity;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.operate.dao.OperateLogDao;
import com.mortals.xhx.module.operate.model.OperateLogEntity;
import com.mortals.xhx.module.operate.service.OperateLogService;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
* OperateLogService
* 平台用户操作日志业务 service实现
*
* @author zxfei
* @date 2022-08-17
*/
@Service("operateLogService")
public class OperateLogServiceImpl extends AbstractCRUDServiceImpl<OperateLogDao, OperateLogEntity, Long> implements OperateLogService {


    @Override
    public OperateLogEntity save(OperateLogEntity entity, Context context) throws AppException {
        entity.setId(IdUtil.getSnowflake(0, 1).nextId());
        if(ObjectUtils.isEmpty(entity.getCreateUserId())){
            entity.setCreateUserId(1L);
            entity.setCreateUserName("system");
            entity.setCreateTime(new Date());
        }
        return super.save(entity, context);
    }

    @Override
    public int save(List<OperateLogEntity> list, Context context) throws AppException {
        list.forEach(item -> item.setId(IdUtil.getSnowflake(0, 1).nextId()));
        return super.save(list, context);
    }
}