package com.unis.crk.basic.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.crk.basic.entity.Attaches;
import com.unis.crk.basic.mapper.AttachesMapper;
import com.unis.crk.basic.service.IAttachesService;
import com.unis.crk.common.service.IDeleteDataRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 * 附件列表 服务实现类
 * </p>
 *
 * @author ghh
 * @since 2021-10-14
 */
@Service
public class AttachesServiceImpl extends ServiceImpl<AttachesMapper, Attaches> implements IAttachesService {

    @Resource
    private AttachesMapper attachesMapper;
    @Autowired
    private IDeleteDataRecordService deleteDataRecordService;

    @Override
    public List<Attaches> getAttachesByReferencesID(String referencesID) {
        QueryWrapper<Attaches> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ReferencesID",referencesID);
        queryWrapper.orderByDesc("UPLOAD_TIME");
        List<Attaches> attachesList = attachesMapper.selectList(queryWrapper);
        return attachesList;
    }

    @Override
    public Attaches getOneAttachesByReferencesID(String referencesID) {
        QueryWrapper<Attaches> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ReferencesID",referencesID);
        queryWrapper.orderByDesc("UPLOAD_TIME");
        queryWrapper.last("limit 1");
        Attaches attaches = attachesMapper.selectOne(queryWrapper);
        return attaches;
    }

    @Override
    public int deletAttachesByReferencesID(String referencesID) {
        Attaches oneAttachesByReferencesID = this.getOneAttachesByReferencesID(referencesID);
        QueryWrapper<Attaches> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ReferencesID",referencesID);
        List<Attaches> attachesList = attachesMapper.selectList(queryWrapper);
        Iterator<Attaches> iterator = attachesList.iterator();
        if(iterator.hasNext()){
            Attaches attaches = iterator.next();
            if (attaches !=null){
                File delFile = new File(attaches.getUrl());
                if (delFile.exists()){
                    delFile.delete();
                }
                deleteDataRecordService.save(attaches.getSelfID(),"attaches",attaches.getUnitID(),new Date());
            }
        }
        int delete = attachesMapper.delete(queryWrapper);

        return delete;
    }

    @Override
    public boolean delFile(Attaches param) {
        File delFile = new File(param.getUrl());
        if(StrUtil.isNotBlank(param.getSelfID())){
            this.removeById(param.getSelfID());
            deleteDataRecordService.save(param.getSelfID(),"attaches",param.getUnitID(),new Date());
        }

        if(delFile.isFile() && delFile.exists()) {
            delFile.delete();
            return true;
        }else {
            return false;
        }
    }
}
