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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.purui.prims.auth.api.authentication.pojo.User;
import com.purui.prims.common.pojo.JoinCondition;
import com.purui.prims.common.pojo.ModelRela;
import com.purui.prims.common.util.BeanMapper;
import com.purui.prims.headless.server.persistence.dataobject.ModelRelaDO;
import com.purui.prims.headless.server.persistence.mapper.ModelRelaDOMapper;
import com.purui.prims.headless.server.service.ModelRelaService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

@Service
public class ModelRelaServiceImpl
    extends ServiceImpl<ModelRelaDOMapper, ModelRelaDO> implements ModelRelaService {

    @Override
    public boolean save(ModelRela modelRela, User user) {
        modelRela.createdBy(user.getUserName());
        if (isExists(modelRela)) {
            return true;
        }
        ModelRelaDO modelRelaDO = convert(modelRela);
        return save(modelRelaDO);
    }

    // 判断是否已经存在
    private boolean isExists(ModelRela modelRela) {
        QueryWrapper<ModelRelaDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ModelRelaDO::getDomainId, modelRela.getDomainId())
               .eq(ModelRelaDO::getFromModelId, modelRela.getFromModelId())
               .eq(ModelRelaDO::getToModelId, modelRela.getToModelId());
        return count(wrapper) > 0;
    }

    @Override
    public void update(ModelRela modelRela, User user) {
        modelRela.updatedBy(user.getUserName());
        ModelRelaDO modelRelaDO = convert(modelRela);
        updateById(modelRelaDO);
    }

    @Override
    public List<ModelRela> getModelRelaList(Long domainId) {
        QueryWrapper<ModelRelaDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ModelRelaDO::getDomainId, domainId);
        return list(wrapper).stream().map(this::convert).collect(Collectors.toList());
    }

    @Override
    public List<ModelRela> getModelRela(List<Long> modelIds) {
        QueryWrapper<ModelRelaDO> wrapper = new QueryWrapper<>();
        if (CollectionUtils.isEmpty(modelIds)) {
            return Lists.newArrayList();
        }
        wrapper.lambda().in(ModelRelaDO::getFromModelId, modelIds).or()
               .in(ModelRelaDO::getToModelId, modelIds);
        return list(wrapper).stream().map(this::convert).collect(Collectors.toList());
    }

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

    private ModelRela convert(ModelRelaDO modelRelaDO) {
        ModelRela modelRela = new ModelRela();
        BeanMapper.mapper(modelRelaDO, modelRela);
        modelRela.setJoinConditions(JSONObject.parseArray(modelRelaDO.getJoinCondition(), JoinCondition.class));
        return modelRela;
    }

    private ModelRelaDO convert(ModelRela modelRelaReq) {
        ModelRelaDO modelRelaDO = new ModelRelaDO();
        BeanMapper.mapper(modelRelaReq, modelRelaDO);
        modelRelaDO.setJoinCondition(JSONObject.toJSONString(modelRelaReq.getJoinConditions()));
        return modelRelaDO;
    }

}
