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.ObjectDigital;
import com.persagy.dmp.rwd.version.dao.ObjectDigitalHistoryMapper;
import com.persagy.dmp.rwd.version.entity.ObjectDigitalHistory;
import com.persagy.dmp.rwd.version.service.IObjectDigitalHistoryService;
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 IObjectDigitalHistoryServiceImpl extends ServiceImpl<ObjectDigitalHistoryMapper,ObjectDigitalHistory> implements IObjectDigitalHistoryService {

    @Autowired
    private ObjectDigitalHistoryMapper objectDigitalHistoryMapper;

    /**
     * 数量统计对象历史数量
     */
    @Override
    public Integer queryCount(Wrapper<ObjectDigitalHistory> queryWrapper) {
        return objectDigitalHistoryMapper.selectCount(queryWrapper);
    }

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

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

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

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

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


}
