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

import com.alibaba.fastjson.JSON;
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.common.util.ContextUtils;
import com.purui.prims.headless.api.pojo.request.BizConditionReq;
import com.purui.prims.headless.api.pojo.request.QueryFilter;
import com.purui.prims.headless.api.pojo.response.BizConditionResp;
import com.purui.prims.headless.server.persistence.dataobject.BizConditionDO;
import com.purui.prims.headless.server.persistence.mapper.BizConditionDOMapper;
import com.purui.prims.headless.server.pojo.MetricFilter;
import com.purui.prims.headless.server.service.BizConditionService;
import com.purui.prims.headless.server.service.MetricService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * 业务限定
 */
@Service
public class BizConditionServiceImpl extends ServiceImpl<BizConditionDOMapper, BizConditionDO> implements BizConditionService {

    @Override
    public boolean saveOrUpdate(BizConditionReq bizConditionReq, User user) {
        BizConditionDO bizConditionDO = new BizConditionDO();
        BeanMapper.mapper(bizConditionReq, bizConditionDO);
        if (!CollectionUtils.isEmpty(bizConditionReq.getFilters())) {
            bizConditionDO.setFilterConfig(JSON.toJSONString(bizConditionReq.getFilters()));
        }
        if (bizConditionDO.getId() == null) {
            return save(bizConditionDO);
        } else {
            return updateById(bizConditionDO);
        }
    }

    @Override
    public void delete(Integer id) {
        removeById(id);
    }

    @Override
    public BizConditionResp getCondition(Integer id) {
        BizConditionDO bizConditionDO = getById(id);
        if (bizConditionDO != null) {
            BizConditionResp bizConditionResp = new BizConditionResp();
            BeanMapper.mapper(bizConditionDO, bizConditionResp);
            return bizConditionResp;
        }
        return null;
    }

    @Override
    public List<BizConditionResp> getConditions(BizConditionReq conditionReq) {
        QueryWrapper<BizConditionDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BizConditionDO::getStatus, conditionReq.getStatus());
        return list(queryWrapper).stream().map(this::convert).collect(Collectors.toList());
    }

    private BizConditionResp convert(BizConditionDO bizConditionDO) {
        MetricService metricService = ContextUtils.getBean(MetricService.class);
        BizConditionResp bizConditionResp = new BizConditionResp();
        BeanMapper.mapper(bizConditionDO, bizConditionResp);
        if (StringUtils.isNotEmpty(bizConditionDO.getFilterConfig())) {
            bizConditionResp.setFilters(JSON.parseArray(bizConditionDO.getFilterConfig(), QueryFilter.class));
        }
        MetricFilter metricFilter = new MetricFilter();
        metricFilter.setConditionId(bizConditionDO.getId());
        bizConditionResp.setMetricsCount(metricService.queryMetric(metricFilter).size());
        return bizConditionResp;
    }
}
