package com.skr.mdm.api.system.service.impl;

import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.skr.mdm.api.system.service.SystemCleanService;
import com.skr.mdm.bean.qo.system.SystemCleanQO;
import com.skr.mdm.bean.vo.system.SysCleanVO;
import com.skr.mdm.dao.SysCleanDao;
import com.skr.mdm.entity.SysCleanDO;
import com.skr.mdm.publish.MdmEventPublisher;
import com.skr.mdm.result.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author nfq
 * @date 2023-03-13 15:28
 */
@Service
@Slf4j
public class SystemCleanServiceImpl implements SystemCleanService {

    @Autowired
    private SysCleanDao sysCleanDao;
    @Autowired
    private MdmEventPublisher mdmEventPublisher;

    @Override
    public ResponseResult getSystemCleanParam() {
        SysCleanDO sysCleanDO = sysCleanDao.selectById(1);
        SysCleanVO sysCleanVO = new SysCleanVO();
        BeanUtils.copyProperties(sysCleanDO, sysCleanVO);
        return ResponseResult.success(sysCleanVO);
    }

    @Override
    public ResponseResult systemCleanConfig(SystemCleanQO systemCleanQO) {
        new LambdaUpdateChainWrapper<>(sysCleanDao)
                .set(systemCleanQO.getIsEnabled() != null, SysCleanDO::getIsEnabled, systemCleanQO.getIsEnabled())
                .set(systemCleanQO.getMaxLimit() != null, SysCleanDO::getMaxLimit, systemCleanQO.getMaxLimit())
                .set(systemCleanQO.getFileRetentionDays() != null, SysCleanDO::getFileRetentionDays, systemCleanQO.getFileRetentionDays())
                .eq(SysCleanDO::getId, 1)
                .update();
        if (systemCleanQO.getIsEnabled().equals(1)) {
            log.info("系统数据定时清理已启用 开始清理");
            mdmEventPublisher.publishDataBaseCleanEvent(systemCleanQO.getMaxLimit());
            mdmEventPublisher.publishMinIoFileCleanEvent(systemCleanQO.getFileRetentionDays());
        }
        return ResponseResult.success();
    }
}
