package com.purui.prims.headless.server.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.purui.prims.common.pojo.PersistenceConf;
import com.purui.prims.common.util.BeanMapper;
import com.purui.prims.common.util.DatePeriodEnum;
import com.purui.prims.headless.api.pojo.request.DatasetPersistenceConfigReq;
import com.purui.prims.headless.api.pojo.response.DatasetPersistenceConfigResp;
import com.purui.prims.headless.server.persistence.dataobject.DatasetPersistenceConfigDO;
import com.purui.prims.headless.server.persistence.mapper.DatasetPersistenceConfigDOMapper;
import com.purui.prims.headless.server.service.DatasetPersistenceConfigService;
import com.purui.prims.headless.server.service.StatisticsPeriodService;
import com.purui.prims.headless.server.utils.CronUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

/**
 * 数据集持久化配置服务
 * @author charlie
 * @date 2024/7/18
 */
@Service
@Slf4j
public class DatasetPersistenceConfigServiceImpl extends ServiceImpl<DatasetPersistenceConfigDOMapper, DatasetPersistenceConfigDO>
    implements
    DatasetPersistenceConfigService {

    StatisticsPeriodService statisticsPeriodService;

    public DatasetPersistenceConfigServiceImpl(StatisticsPeriodService statisticsPeriodService) {
        this.statisticsPeriodService = statisticsPeriodService;
    }

    @Override
    public boolean save(DatasetPersistenceConfigReq configReq) {
        DatasetPersistenceConfigDO datasetPersistenceConfigDO = new DatasetPersistenceConfigDO();
        BeanMapper.mapper(configReq, datasetPersistenceConfigDO);
        QueryWrapper<DatasetPersistenceConfigDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DatasetPersistenceConfigDO::getDatasetId, configReq.getDatasetId());
        DatasetPersistenceConfigDO configDO = this.getOne(queryWrapper);
        if (configDO != null) {
            datasetPersistenceConfigDO.setId(configDO.getId());
        }
        return this.saveOrUpdate(datasetPersistenceConfigDO);
    }

    @Override
    public DatasetPersistenceConfigResp get(Long id) {
        return this.convert(getById(id));
    }

    @Override
    public DatasetPersistenceConfigResp getByDatasetId(Long id) {
        LambdaQueryWrapper<DatasetPersistenceConfigDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DatasetPersistenceConfigDO::getDatasetId, id);
        return convert(getOne(queryWrapper));
    }

    @Override
    public List<DatasetPersistenceConfigResp> getConfigList() {
        return list().stream().map(this::convert).collect(Collectors.toList());
    }

    @Override
    public boolean delete(Long id) {
        return this.removeById(id);
    }

    private DatasetPersistenceConfigResp convert(DatasetPersistenceConfigDO datasetPersistenceConfigDO) {
        DatasetPersistenceConfigResp datasetPersistenceConfigResp = new DatasetPersistenceConfigResp();
        BeanMapper.mapper(datasetPersistenceConfigDO, datasetPersistenceConfigResp);
        if (StringUtils.isNotEmpty(datasetPersistenceConfigDO.getDefaultConfig())) {
            PersistenceConf conf = JSON.parseObject(datasetPersistenceConfigDO.getDefaultConfig(), PersistenceConf.class);
            if ("AUTO".equalsIgnoreCase(conf.getWay())) {
                return null;
            }
            if (StringUtils.isNotEmpty(conf.getPeriod())) {
                DatePeriodEnum period = DatePeriodEnum.valueOf(conf.getPeriod().toUpperCase(Locale.ROOT));
                String[] dateRange = CronUtils.calculateDateRange(period, conf.getUnit());
                conf.setStartDate(dateRange[0]);
                conf.setEndDate(dateRange[1]);
                conf.setCron(CronUtils.buildCron(period, conf.getStartTime()));
            }
            datasetPersistenceConfigResp.setConf(conf);
        }
        return datasetPersistenceConfigResp;
    }
}
