package co.yixiang.yshop.module.member.service.burypoint;

import co.yixiang.yshop.module.member.dal.dataobject.burypoint.TrendChartRespVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import co.yixiang.yshop.module.member.controller.admin.burypoint.vo.*;
import co.yixiang.yshop.module.member.dal.dataobject.burypoint.BuryPointDO;
import co.yixiang.yshop.framework.common.pojo.PageResult;

import co.yixiang.yshop.module.member.convert.burypoint.BuryPointConvert;
import co.yixiang.yshop.module.member.dal.mysql.burypoint.BuryPointMapper;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.member.enums.ErrorCodeConstants.*;

/**
 * 埋点 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class BuryPointServiceImpl implements BuryPointService {

    @Resource
    private BuryPointMapper buryPointMapper;

    @Override
    public Long createBuryPoint(BuryPointCreateReqVO createReqVO) {
        // 插入
        BuryPointDO buryPoint = BuryPointConvert.INSTANCE.convert(createReqVO);
        buryPointMapper.insert(buryPoint);
        // 返回
        return buryPoint.getId();
    }

    @Override
    public void updateBuryPoint(BuryPointUpdateReqVO updateReqVO) {
        // 校验存在
        validateBuryPointExists(updateReqVO.getId());
        // 更新
        BuryPointDO updateObj = BuryPointConvert.INSTANCE.convert(updateReqVO);
        buryPointMapper.updateById(updateObj);
    }

    @Override
    public void deleteBuryPoint(Long id) {
        // 校验存在
        validateBuryPointExists(id);
        // 删除
        buryPointMapper.deleteById(id);
    }

    private void validateBuryPointExists(Long id) {
        if (buryPointMapper.selectById(id) == null) {
            throw exception(BURY_POINT_NOT_EXISTS);
        }
    }

    @Override
    public BuryPointDO getBuryPoint(Long id) {
        return buryPointMapper.selectById(id);
    }

    @Override
    public List<BuryPointDO> getBuryPointList(Collection<Long> ids) {
        return buryPointMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BuryPointDO> getBuryPointPage(BuryPointPageReqVO pageReqVO) {
        return buryPointMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BuryPointDO> getBuryPointList(BuryPointExportReqVO exportReqVO) {
        return buryPointMapper.selectList(exportReqVO);
    }

    @Override
    public List<TrendChartRespVO> getTrendChart() {
        return buryPointMapper.getTrendChart();
    }

}
