package com.persagy.dmp.rwd.digital.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.persagy.dmp.basic.constant.DigitalMessageConstant;
import com.persagy.dmp.common.constant.ValidEnum;
import com.persagy.dmp.common.context.AppContext;
import com.persagy.dmp.digital.entity.ObjectDigital;
import com.persagy.dmp.digital.entity.ObjectRelation;
import com.persagy.dmp.rwd.basic.utils.DigitalCommonUtils;
import com.persagy.dmp.rwd.basic.utils.DigitalMessageHelper;
import com.persagy.dmp.rwd.digital.dao.ObjectRelationMapper;
import com.persagy.dmp.rwd.digital.service.IObjectRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 关系数据 实现类
 * @author Charlie Yu
 * @date 2021-07-08
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ObjectRelationServiceImpl extends ServiceImpl<ObjectRelationMapper, ObjectRelation> implements IObjectRelationService  {

    @Resource
    private ObjectRelationMapper dao;

    @Autowired
    private DigitalMessageHelper messageSender;

    @Override
    public List<ObjectRelation> save(List<ObjectRelation> voList) {
        if(CollUtil.isEmpty(voList)) {
            return null;
        }
        ensureDefaultValue(voList);
        // 新增关系数据
        dao.save(voList);
        // 发送新增消息 -- 不重新查询了，主键不保真。应基于objFrom、objTo等信息判定唯一
        voList.forEach(vo->{
            messageSender.sendMessage(DigitalMessageConstant.OPERATE_AFTER_INSERT, null, vo, false);
        });
        return voList;
    }

    /**
     * 补充默认值
     * @param voList
     */
    private void ensureDefaultValue(List<ObjectRelation> voList) {
        String groupCode = AppContext.getContext().getGroupCode();
        String projectId = AppContext.getContext().getProjectId();
        String appId = AppContext.getContext().getAppId();
        for (ObjectRelation vo : voList) {
            // 图实例id
            vo.setGraphId(DigitalCommonUtils.getDefaultGraphId(vo.getGraphCode()));
            vo.setGroupCode(groupCode);
            vo.setProjectId(projectId);
            vo.setCreateApp(appId);
            vo.setValid(ValidEnum.TRUE.getType());
            if(StrUtil.isBlank(vo.getId())) {
                vo.setId(IdUtil.simpleUUID());
            }
        }
    }

    @Override
    public List<ObjectRelation> delete(Wrapper<ObjectRelation> wrapper) {
        List<ObjectRelation> list = dao.selectList(wrapper);
        if(CollUtil.isEmpty(list)){
            return new ArrayList<>();
        }
        // 20211201 不使用逻辑删除了，改为真删除
        dao.delete(wrapper);
        //发送删除消息
        list.forEach(vo->{
            messageSender.sendMessage(DigitalMessageConstant.OPERATE_AFTER_DELETE, null, vo, false);
        });
        return list;
    }

    @Override
    public List<ObjectDigital> queryRelatedObjects(Iterable<String> objIds, Iterable<String> relCodes) {
        if(CollUtil.isEmpty(objIds) || CollUtil.isEmpty(relCodes)) {
            return null;
        }
        return dao.queryRelatedObjects(objIds, relCodes);
    }
}
