package com.mortals.xhx.module.biz.service.impl;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.mortals.framework.model.PageInfo;
import com.mortals.xhx.module.access.model.AccessLogEntity;
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.biz.dao.BizLogDao;
import com.mortals.xhx.module.biz.model.BizLogEntity;
import com.mortals.xhx.module.biz.service.BizLogService;
import org.springframework.util.ObjectUtils;

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

/**
* BizLogService
* 业务日志 service实现
*
* @author zxfei
* @date 2022-08-17
*/
@Service("bizLogService")
public class BizLogServiceImpl extends AbstractCRUDServiceImpl<BizLogDao, BizLogEntity, Long> implements BizLogService {


    @Override
    public BizLogEntity save(BizLogEntity 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
    protected void findAfter(BizLogEntity params, PageInfo pageInfo, Context context, List<BizLogEntity> list) throws AppException {
        super.findAfter(params, pageInfo, context, list);
        list.forEach(entity -> {
            entity.setMsg(Base64.decodeStr(entity.getMsg()));
        });
    }


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

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