package com.pqs9000.serviceimpl.responsibility;

import cn.hutool.core.io.FileUtil;
import com.pqs9000.mapper.responsibility.UserExcelDataMapper;
import com.pqs9000.mapper.responsibility.UserExcelDataNoIntegrityMapper;
import com.pqs9000.pojo.responsibility.UserExcelData;
import com.pqs9000.pojo.responsibility.UserExcelDataNoIntegrity;
import com.pqs9000.service.responsibility.IUserExcelDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * @author hongawen
 * @Date: 2019/4/22 10:04
 */
@Service
public class UserExcelDataServiceImpl implements IUserExcelDataService {

    @Autowired
    private UserExcelDataMapper userExcelDataMapper;

    @Autowired
    private UserExcelDataNoIntegrityMapper userExcelDataNoIntegrityMapper;

    /**
     * 获取用采数据列表
     */
    @Override
    public List<UserExcelData> getUserExcelDataList() {
        return userExcelDataMapper.selectAll();
    }

    /**
     * 根据用采数据记录删除数据
     * @param userDataIndex
     */
    @Override
    public void deleteExcelDataByIndex(String userDataIndex) {
        UserExcelData userExcelData=new UserExcelData();
        userExcelData.setExcelIndex(userDataIndex);
        userExcelData = userExcelDataMapper.selectOne(userExcelData);
        if(null!=userExcelData){
            userExcelDataMapper.deleteByIndex(userDataIndex);
            //同时删除完整性不足的关联数据
            userExcelDataNoIntegrityMapper.deleteByExcelIndex(userDataIndex);
            //删除相关文件
            String filePath=userExcelData.getExcelPath();
            FileUtil.del(new File(filePath));
        }
    }
}
