package cn.exrick.xboot.your.controller.face;
|
|
import cn.exrick.xboot.core.common.constant.CommonConstant;
|
import cn.exrick.xboot.core.common.constant.SettingConstant;
|
import cn.exrick.xboot.core.common.redis.RedisTemplateHelper;
|
import cn.exrick.xboot.core.common.utils.CommonUtil;
|
import cn.exrick.xboot.core.entity.User;
|
import cn.exrick.xboot.core.service.SettingService;
|
import cn.exrick.xboot.core.service.UserService;
|
import cn.exrick.xboot.core.vo.OssSetting;
|
import cn.exrick.xboot.file.entity.File;
|
import cn.exrick.xboot.file.manage.FileManageFactory;
|
import cn.exrick.xboot.file.service.FileService;
|
import cn.exrick.xboot.your.entity.Alarm;
|
import cn.exrick.xboot.your.entity.Car;
|
import cn.exrick.xboot.your.entity.EventLog;
|
import cn.exrick.xboot.your.service.IAlarmService;
|
import cn.exrick.xboot.your.service.ICarService;
|
import cn.exrick.xboot.your.service.IEventLogService;
|
import cn.exrick.xboot.your.util.FaceSystem;
|
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.json.JSONArray;
|
import cn.hutool.json.JSONObject;
|
import cn.hutool.json.JSONUtil;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.google.gson.Gson;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
import javax.servlet.ServletException;
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.Part;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.util.Collection;
|
import java.util.Date;
|
import java.util.Iterator;
|
import java.util.List;
|
//人脸比对检测车厢异常开启
|
@RestController
|
@RequestMapping("/hk")
|
public class FaceImgController {
|
|
@Autowired
|
private FileManageFactory fileManageFactory;
|
@Autowired
|
private FileService fileService;
|
@Autowired
|
private ICarService iCarService;
|
@Autowired
|
private UserService userService;
|
@Autowired
|
private SettingService settingService;
|
@Autowired
|
private IAlarmService iAlarmService;
|
@Autowired
|
private RedisTemplateHelper redisTemplate;
|
@Autowired
|
private IEventLogService iEventLogService;
|
|
|
@RequestMapping(value = "/faceImg", method = RequestMethod.POST)
|
public void saveOrUpdate(HttpServletRequest request) throws IOException, ServletException {
|
OssSetting os = new Gson().fromJson(settingService.get(SettingConstant.LOCAL_OSS).getValue(), OssSetting.class);
|
|
String faceCapture = request.getParameter("faceCapture");
|
if(StrUtil.isNotEmpty(faceCapture)){
|
JSONObject jsonObject = JSONUtil.parseObj(faceCapture);
|
String faceCaptures = jsonObject.get("faceCapture").toString();
|
JSONArray objects = JSONUtil.parseArray(faceCaptures);
|
JSONObject jsonObject1 = objects.getJSONObject(0);
|
String targetAttrs = jsonObject1.get("targetAttrs").toString();
|
JSONObject jsonObject2 = JSONUtil.parseObj(targetAttrs);
|
String deviceName = jsonObject2.get("deviceName").toString();
|
//System.out.println(deviceName);
|
|
QueryWrapper<Car> wrapper = new QueryWrapper<Car>();
|
wrapper.eq("car_no",deviceName.substring(0,deviceName.length()-1));
|
Car one = iCarService.getOne(wrapper);
|
if(one==null){
|
return;
|
}
|
|
Date date1 = new Date();
|
long time = date1.getTime()-2*60*1000;
|
Date date = new Date(time);
|
String format1 = DateUtil.format(date,"yyyy-MM-dd HH:mm:ss");
|
String format2 = DateUtil.format(date1,"yyyy-MM-dd HH:mm:ss");
|
QueryWrapper<Alarm> awrapper = new QueryWrapper<>();
|
awrapper.between("create_time",format1,format2);
|
awrapper.eq("car_no",one.getCarNo());
|
awrapper.eq("type",5);
|
List<Alarm> list = iAlarmService.list(awrapper);
|
if(list.size()>0){
|
return;
|
}
|
|
User user = userService.get(one.getUserId());
|
User user1 = userService.get(one.getFollowUserId());
|
|
Collection parts = request.getParts();
|
for (Iterator iterator = parts.iterator(); iterator.hasNext();) {
|
Part part = (Part) iterator.next();
|
if(part.getContentType().equals("image/jpeg")){
|
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
List<MultipartFile> imgs = multipartRequest.getFiles(part.getName());
|
for(MultipartFile multipartFile:imgs){
|
String result = "";
|
String fKey = CommonUtil.renamePic(part.getName())+".jpeg";
|
File f = new File();
|
try {
|
InputStream inputStream = part.getInputStream();
|
result = fileManageFactory.getFileManage(null).inputStreamUpload(inputStream, fKey, multipartFile);
|
f.setLocation(0).setName(multipartFile.getOriginalFilename()).setSize(multipartFile.getSize()).setType(multipartFile.getContentType()).setFKey(fKey).setUrl(result);
|
fileService.save(f);
|
|
int flag = 0;
|
String avatar2 = os.getHttp() + os.getEndpoint() + "/" + f.getId();
|
if(user!=null){
|
String avatar = user.getAvatar();
|
if(StrUtil.isNotEmpty(avatar)){
|
int result1 = FaceSystem.getResult(avatar, avatar2);
|
if(result1<50){
|
flag++;
|
}else{
|
deleteFile(f.getId());//是一个人删除图片 flag=0
|
//如果抓拍图片跟司机照片对比是一个人,则下面的都不执行了。
|
}
|
}
|
}
|
|
//如果抓拍图片跟司机照片对比不是一个人,则再对比下配送员。
|
if(user1!=null&&flag>0){
|
String avatar = user1.getAvatar();
|
if(StrUtil.isNotEmpty(avatar)){
|
int result1 = FaceSystem.getResult(avatar, avatar2);
|
if(result1<50){
|
flag++;
|
}else{
|
deleteFile(f.getId());
|
}
|
}
|
}
|
|
if(flag>1){
|
Alarm alarm = new Alarm();
|
alarm.setCarNo(one.getCarNo());
|
alarm.setCarId(one.getId());
|
alarm.setCarUserId(one.getUserId());
|
alarm.setFollowUserId(one.getFollowUserId());
|
alarm.setType(5);
|
alarm.setValue("车厢异常开启");
|
alarm.setAlarmImg(avatar2);
|
iAlarmService.saveOrUpdate(alarm);
|
|
EventLog eventLog = new EventLog();
|
eventLog.setCarNo(one.getCarNo());
|
eventLog.setType(5);
|
eventLog.setRefId(avatar2);
|
iEventLogService.saveOrUpdate(eventLog);
|
}
|
|
} catch (Exception e) {
|
//System.out.println(e.toString());
|
}
|
}
|
}
|
}
|
|
}
|
|
}
|
|
|
public void deleteFile(String id){
|
File file = fileService.get(id);
|
// 特殊处理本地服务器
|
String key = file.getFKey();
|
if (CommonConstant.OSS_LOCAL.equals(file.getLocation())) {
|
key = file.getUrl();
|
}
|
fileManageFactory.getFileManage(file.getLocation()).deleteFile(key);
|
fileService.delete(id);
|
redisTemplate.delete("file::" + id);
|
}
|
|
// System.out.println("-----类型名称------->"+part.getName());
|
// System.out.println("-----提交的类型名称------->"+part.getSubmittedFileName());
|
// System.out.println("----流-------->"+part.getInputStream());
|
|
}
|