import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.yuxue.constant.Constant;
import com.yuxue.entity.PlateFileEntity;
import com.yuxue.entity.TempPlateFileEntity;
import com.yuxue.enumtype.PlateColor;
import com.yuxue.mapper.PlateFileMapper;
import com.yuxue.mapper.TempPlateFileMapper;
import com.yuxue.service.PlateService;
import com.yuxue.util.FileUtil;
import com.yuxue.util.GenerateIdUtil;
import com.yuxue.util.PlateUtil;
@Service
public class PlateServiceImpl implements PlateService {
@Autowired
private PlateFileMapper plateFileMapper;
@Autowired
private TempPlateFileMapper tempPlateFileMapper;
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
@Override
@Transactional(propagation = Propagation.REQUIRED)
public Object refreshFileInfo() {
File baseDir = new File(Constant.DEFAULT_DIR);
if(!baseDir.exists() || !baseDir.isDirectory()) {
return null;
}
List resultList = Lists.newArrayList();
// 获取baseDir下第一层级的目录, 仅获取文件夹,不递归子目录,遍历
List folderList = FileUtil.listFile(baseDir, “;”, false);
folderList.parallelStream().forEach(folder -> {
if(!folder.getName().equals(“temp”)) {
// 遍历每一个文件夹, 递归获取文件夹下的图片
List imgList = FileUtil.listFile(folder, Constant.DEFAULT_TYPE, true);
if(null != imgList && imgList.size() > 0) {
imgList.parallelStream().forEach(n->{
TempPlateFileEntity entity = new TempPlateFileEntity();
entity.setFilePath(n.getAbsolutePath().replaceAll(“\\”, “/”));
entity.setFileName(n.getName());
entity.setFileType(n.getName().substring(n.getName().lastIndexOf(“.”) + 1));
resultList.add(entity);
});
}
}
});
tempPlateFileMapper.turncateTable();
tempPlateFileMapper.batchInsert(resultList);
tempPlateFileMapper.updateFileInfo();
return 1;
}
@Override
public Object recognise(String filePath, boolean reRecognise) {
filePath = filePath.replaceAll(“\\”, “/”);
File f = new File(filePath);
PlateFileEntity entity = null;
Map<String, Object> paramMap = Maps.newHashMap();
paramMap.put(“filePath”, filePath);
List list= plateFileMapper.selectByCondition(paramMap);
if(null == list || list.size() <= 0) {
if(FileUtil.checkFile(f)) {
entity = new PlateFileEntity();
entity.setFileName(f.getName());
entity.setFilePath(f.getAbsolutePath().replaceAll(“\\”, “/”));
entity.setFileType(f.getName().substring(f.getName().lastIndexOf(“.”) + 1));
plateFileMapper.insertSelective(entity);
}
reRecognise = true;
} else {
entity = list.get(0);
}
if(reRecognise || StringUtils.isEmpty(entity.getTempPath())) {
doRecogn