package cn.edu.whu.boot.collection.service.impl;

import cn.edu.whu.boot.collection.domain.ExportMediaRecord;
import cn.edu.whu.boot.collection.domain.MultimediaItem;
import cn.edu.whu.boot.collection.mapper.MultimediaMapper;
import cn.edu.whu.boot.collection.service.IMultimediaService;
import cn.edu.whu.boot.common.domain.CommonConstant;
import cn.edu.whu.boot.common.utils.collection.PostgreSqlUtil;
import cn.edu.whu.boot.xml.domain.Task;
import cn.edu.whu.boot.xml.service.ITaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
public class MultimediaServiceImpl implements IMultimediaService {

    @Autowired
    private MultimediaMapper multimediaMapper;

    @Autowired
    private ITaskService taskService;

    @Value("${MediaPath}")
    private String mediaPath;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public String insertMultiMedia(MultimediaItem media)
    {
        int id=multimediaMapper.insertMultiMedia(media);
        if(id == 1)
        {
            return CommonConstant.SUCCEED;
        }
        else
        {
            return CommonConstant.FAILED;
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public String deleteMultiMedia(String mediaTableName , Long polygonId)
    {
        int result = multimediaMapper.deleteMulitMedia(mediaTableName,polygonId);
        if(result != 0)
        {
            return CommonConstant.SUCCEED;
        }
        else
        {
            return CommonConstant.FAILED;
        }

    }

    @Override
    public String deleteMulitMediaGreaterThanVersion(String mediaTableName , Long polygonId , int version)
    {
        int result = multimediaMapper.deleteMulitMediaGreaterThanVersion(mediaTableName, polygonId, version);
        if(result != 0)
        {
            return CommonConstant.SUCCEED;
        }
        else
        {
            return CommonConstant.FAILED;
        }
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public List<MultimediaItem> selectMultiMedia(String mediaTableName, Long polygonId) {
        try{
            List<MultimediaItem> multimediaItemList = multimediaMapper.selectMultiMedia(mediaTableName, polygonId);
            if(multimediaItemList.isEmpty()) {
                return null;
            }
            else {
                return multimediaItemList;
            }
        }catch (Exception e){
            return null;
        }

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public List<MultimediaItem> selectHistoryMultiMedia(String mediaTableName , Long polygonId , int versionCode)
    {
        List<MultimediaItem> multimediaItemList = multimediaMapper.selectHistoryMultiMedia(mediaTableName, polygonId,
                                                                                           versionCode);
        if(multimediaItemList.size() == 0)
        {
            return null;
        }
        else
        {
            return multimediaItemList;
        }
    }

    @Override
    public List<ExportMediaRecord > selectByTaskIdAndTableName(int taskId, String tableName) {
        Task task = taskService.getTaskById(taskId);
        if (task == null) {
            return Collections.emptyList();
        }
        String tableNameInDb = PostgreSqlUtil.buildTableNameString(task.getDatabaseName(), tableName, true, false);
        List<ExportMediaRecord> records = multimediaMapper.selectByTableName(tableNameInDb);
        // 把绝对路径中的根目录路径去掉
        records.forEach(item -> {
            item.setMediaPath(item.getMediaPath().substring(mediaPath.length()));
        });
        return records;
    }
}
