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

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.persagy.dmp.basic.dto.RelationCalDTO;
import com.persagy.dmp.common.constant.ValidEnum;
import com.persagy.dmp.digital.entity.ObjectRelation;
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;

    @Autowired
    private IObjectRelationService iService;

    /**
     * 查询对象关系数据
     * @param queryWrapper 查询条件
     * @return
     */
    @Override
    public List<ObjectRelation> queryByCondition(Wrapper<ObjectRelation> queryWrapper) {
        return dao.selectList(queryWrapper);
    }

    @Override
    public Integer queryCount(Wrapper<ObjectRelation> queryWrapper) {
        return dao.selectCount(queryWrapper);
    }

    @Override
    public Page queryByCondition(Page page, Wrapper<ObjectRelation> queryWrapper) {
        return dao.selectPage(page, queryWrapper);
    }

    /**
     * 新增关系数据
     * @param voList 待新增数据
     * @return
     */
    @Override
    public List<ObjectRelation> insert(List<ObjectRelation> voList) {
        iService.saveOrUpdateBatch(voList);
        return voList;
    }


    /**
     * 修改关系数据
     * @param voList 修改条件
     * @return
     */
    @Override
    public List<ObjectRelation> update(List<ObjectRelation> voList) {
        iService.saveOrUpdateBatch(voList);
        return voList;
    }

    /**
     * 删除关系数据
     * @param wrapper 删除条件
     * @return
     */
    @Override
    public List<ObjectRelation> delete(QueryWrapper wrapper) {
        //根据条件查询有效关系数据
        wrapper.eq("valid",ValidEnum.TRUE.getType());
        List<ObjectRelation> list = dao.selectList(wrapper);
        if(list == null || list.size()<=0){
            return new ArrayList<>();
        }
        //批量删除关系数据
        for (ObjectRelation relation : list) {
            relation.setValid(ValidEnum.FALSE.getType());
        }
        iService.saveOrUpdateBatch(list);
        return list;
    }

}
