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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.purui.prims.auth.api.authentication.pojo.User;
import com.purui.prims.common.util.BeanMapper;
import com.purui.prims.headless.api.pojo.request.StatisticsPeriodReq;
import com.purui.prims.headless.api.pojo.response.StatisticsPeriodResp;
import com.purui.prims.headless.server.persistence.dataobject.StatisticsPeriodDO;
import com.purui.prims.headless.server.persistence.mapper.StatisticsPeriodDOMapper;
import com.purui.prims.headless.server.service.StatisticsPeriodService;
import org.springframework.stereotype.Service;

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

/**
 * 统计周期服务实现
 *
 * @author charlie
 * @date 2024/6/24
 */
@Service
public class StatisticsPeriodServiceImpl extends ServiceImpl<StatisticsPeriodDOMapper, StatisticsPeriodDO>
    implements StatisticsPeriodService {

    @Override
    public boolean saveOrUpdate(StatisticsPeriodReq periodReq, User user) {
        StatisticsPeriodDO periodDO = new StatisticsPeriodDO();
        BeanMapper.mapper(periodReq, periodDO);
        periodDO.setStartExpr(periodReq.getExpr());
        periodDO.setEndExpr(periodReq.getExpr());
        return super.saveOrUpdate(periodDO);
    }

    @Override
    public List<StatisticsPeriodResp> getStatisticsPeriods() {
        QueryWrapper<StatisticsPeriodDO> queryWrapper = new QueryWrapper<>();
        return list(queryWrapper).stream().map(periodDO -> {
            StatisticsPeriodResp periodResp = new StatisticsPeriodResp();
            BeanMapper.mapper(periodDO, periodResp);
            periodResp.setExpr(periodDO.getStartExpr());
            return periodResp;
        }).collect(Collectors.toList());
    }
}
