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

import cn.hutool.core.collection.CollUtil;
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.common.constant.ValidEnum;
import com.persagy.dmp.digital.entity.ObjectRelation;
import com.persagy.dmp.rwd.version.dao.ObjectRelationHistoryMapper;
import com.persagy.dmp.rwd.version.entity.ObjectRelationHistory;
import com.persagy.dmp.rwd.version.service.IObjectRelationHistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 对象关系历史记录
 * @author:linhuili
 * @date:2021/11/15
 */
@Slf4j
@Service
public class IObjectRelationHistoryServiceImpl extends ServiceImpl<ObjectRelationHistoryMapper, ObjectRelationHistory>  implements IObjectRelationHistoryService {

    @Autowired
    private ObjectRelationHistoryMapper ObjectRelationHistoryMapper;

    /**
     * 数量统计对象关系历史数据
     */
    @Override
    public Integer queryCount(Wrapper<ObjectRelationHistory> queryWrapper) {
        return ObjectRelationHistoryMapper.selectCount(queryWrapper);
    }

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

    /**
     * 分页查询对象关系历史数据
     * @param page 分页条件
     * @param queryWrapper 查询条件
     * @return
     */
    @Override
    public Page queryByCondition(Page page, Wrapper<ObjectRelationHistory> queryWrapper) {
        return ObjectRelationHistoryMapper.selectPage(page, queryWrapper);
    }

    /**
     * 保存对象关系历史记录
     * @param vo 待新增数据
     * @return
     * @throws Exception
     */
    @Override
    public ObjectRelationHistory insert(ObjectRelation vo) throws Exception {
        //设置对象历史信息
        ObjectRelationHistory ObjectRelationHistory = new ObjectRelationHistory();
        BeanUtils.copyProperties(vo,ObjectRelationHistory);
        ObjectRelationHistory.setRelId(vo.getId());
        ObjectRelationHistory.setId(null);
        ObjectRelationHistory.setLastFlag(true);
        ObjectRelationHistory.setStartTime(vo.getCreationTime());
        //保存对象历史记录
        save(ObjectRelationHistory);
        return ObjectRelationHistory;
    }

    /**
     * 修改对象关系历史记录
     * @param vo 待修改数据
     * @return
     * @throws Exception
     */
    @Override
    public ObjectRelationHistory update(ObjectRelation vo) throws Exception {
        //查询最新历史记录
        QueryWrapper<ObjectRelationHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("last_flag",true);
        wrapper.eq("obj_id",vo.getId());
        wrapper.eq("valid", ValidEnum.TRUE.getType());
        List<ObjectRelationHistory> objHistoryList = queryByCondition(wrapper);
        if(CollUtil.isNotEmpty(objHistoryList)){
            for (ObjectRelationHistory objHistory : objHistoryList) {
                objHistory.setLastFlag(false);
                objHistory.setEndTime(vo.getModifiedTime());
            }
            //修改最新历史记录标识
            saveOrUpdateBatch(objHistoryList);
        }
        //新增历史记录
        return insert(vo);
    }

    /**
     * 删除对象关系，历史记录
     * @param vo
     */
    @Override
    public ObjectRelationHistory delete(ObjectRelation vo) throws Exception  {
        return update(vo);
    }
}
