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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.purui.prims.auth.api.authentication.pojo.User;
import com.purui.prims.common.pojo.enums.TypeEnums;
import com.purui.prims.common.util.BeanMapper;
import com.purui.prims.headless.api.pojo.Collect;
import com.purui.prims.headless.server.persistence.dataobject.CollectDO;
import com.purui.prims.headless.server.persistence.mapper.CollectMapper;
import com.purui.prims.headless.server.service.CollectService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class CollectServiceImpl implements CollectService {

    public static final String type = "metric";
    @Resource
    private CollectMapper collectMapper;

    @Override
    public Boolean createCollectionIndicators(User user, Collect collectReq) {
        CollectDO collect = new CollectDO();
        collect.setType(StringUtils.isEmpty(collectReq.getType()) ? type : collectReq.getType());
        collect.setUsername(user.getUserName());
        collect.setCollectId(collectReq.getCollectId());
        collectMapper.insert(collect);
        return true;
    }

    @Override
    public Boolean deleteCollectionIndicators(User user, Long id) {
        QueryWrapper<CollectDO> collectDOQueryWrapper = new QueryWrapper<>();
        collectDOQueryWrapper.lambda().eq(CollectDO::getUsername, user.getDisplayName());
        collectDOQueryWrapper.lambda().eq(CollectDO::getId, id);
        collectDOQueryWrapper.lambda().eq(CollectDO::getType, type);
        collectMapper.delete(collectDOQueryWrapper);
        return true;
    }

    @Override
    public Boolean deleteCollectionIndicators(User user, Collect collectReq) {
        QueryWrapper<CollectDO> collectDOQueryWrapper = new QueryWrapper<>();
        collectDOQueryWrapper.lambda().eq(CollectDO::getUsername, user.getUserName());
        collectDOQueryWrapper.lambda().eq(CollectDO::getCollectId, collectReq.getCollectId());
        collectDOQueryWrapper.lambda().eq(CollectDO::getType, collectReq.getType());
        collectMapper.delete(collectDOQueryWrapper);
        return true;
    }

    @Override
    public List<Collect> getCollectList(String username) {
        QueryWrapper<CollectDO> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(username)) {
            queryWrapper.lambda().eq(CollectDO::getUsername, username);
        }
        return collectMapper.selectList(queryWrapper).stream().map(this::convert).collect(Collectors.toList());
    }

    @Override
    public List<Collect> getCollectList(String username, TypeEnums typeEnums) {
        QueryWrapper<CollectDO> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(username)) {
            queryWrapper.lambda().eq(CollectDO::getUsername, username);
        }
        queryWrapper.lambda().eq(CollectDO::getType, typeEnums.name().toLowerCase());
        return collectMapper.selectList(queryWrapper).stream().map(this::convert).collect(Collectors.toList());
    }

    private Collect convert(CollectDO collectDO) {
        Collect collect = new Collect();
        BeanMapper.mapper(collectDO, collect);
        return collect;
    }
}
