wang-hao-jie
2021-12-27 76e9e8e05cbe7dafeba51a235386f20ebf986e25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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.service.IAlarmService;
import cn.exrick.xboot.your.service.ICarService;
import cn.exrick.xboot.your.util.FaceSystem;
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.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;
 
 
    @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);
 
            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();
                                int result1 = FaceSystem.getResult(avatar, avatar2);
                                if(result1<70){
                                    flag++;
                                }else{
                                    deleteFile(f.getId());
                                }
                            }
 
                            if(user1!=null){
                                String avatar = user1.getAvatar();
                                int result1 = FaceSystem.getResult(avatar, avatar2);
                                if(result1<70){
                                    flag++;
                                }else{
                                    deleteFile(f.getId());
                                }
                            }
 
                            if(flag>0){
                                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);
                            }
 
                        } 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());
 
}