liuxiaolong
2019-05-09 0d1d88cdb668e75ea8609417ac18ae19947e9525
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package com.basic.x01.location;
 
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.json.JsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
import com.basic.x01.base.BaseController;
import com.basic.x01.location.mapper.LocationMapper;
import com.basic.x01.location.model.TbLocDevice;
import com.basic.x01.location.model.TbLocPosition;
import com.basic.x01.location.model.TbLocTransfer;
import com.basic.x01.system.model.TbSchoolClass;
import com.basic.x01.system.model.TbSchoolStudent;
 
/**
 * 定位
 * 
 * @company 北京贝思科技术有限公司
 * @author liuyajun, 8384503@qq.com
 * @date 2016年2月15日
 * @time 上午10:57:56
 */
 
@Controller
@Transactional(rollbackFor=Throwable.class)
public class LocationApi extends BaseController {
    public final static String ACTION_ID = "locationApi";
    
    Logger log = Logger.getLogger(this.getClass());
    
    @Resource
    LocationMapper locMapper;
    
    @RequestMapping(value=ACTION_ID, method=RequestMethod.POST)
    public String api(@Param("requestId") String requestId,
            @Param("schoolId") String schoolId,
            @Param("transferId") String transferId,
            @Param("accessKey") String accessKey,
            HttpServletRequest req,
            HttpServletResponse resp) {
 
    /*    if(this.isEmpty(requestId) 
                || this.isEmpty(schoolId) 
                || this.isEmpty(transferId)
                || this.isEmpty(accessKey)){
            return this.ajax("-1,请求参数不正确");
        }
        
        if(locMapper.checkSchoolAccessKey(schoolId, accessKey)<=0){
            resp.setStatus(500);
            return this.ajax("-20,请求参数的学校不存在");
        }
        
        TbLocTransfer transfer = locMapper.getSchoolTransferDevice(
                schoolId, Integer.valueOf(transferId));
        
        if(transfer ==null){
            resp.setStatus(500);
            return this.ajax("-21,请求参数的转发设备不存在");
        }
        */
        String ret = null;
        try{
            if(requestId.equals("getLocateConfig")){
                //this.getLocateConfig(this.locMapper, transfer);
                Date now = new Date(); 
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
 
 
                String timenow = dateFormat.format( now ); 
                ret=timenow;
            }
            else if(requestId.endsWith("uploadLocation"))
            {
                String locations = req.getParameter("locations");
                if(this.isEmpty(locations)){
                    resp.setStatus(500);
                    return this.ajax("-30,上报数据为空");
                }
                ret = this.uploadLocationTest(locations);
            }
        }catch(Throwable t){
            log.error(t);
        }
        
        if(ret ==null){
            resp.setStatus(500);
            return this.ajax("-90,系统处理错误");
        }
        
        return this.ajax(ret);
    }
    
    private String getLocateConfig(LocationMapper locMapper, 
            TbLocTransfer transfer) {
 
        List<TbLocDevice> locator = locMapper.getLocDeviceListBySchoolIdTransferId(
                transfer.getSchoolId(), transfer.getTransferId());
        
        StringBuffer s = new StringBuffer("{\"locators\":\"");
        
        StringBuffer device = new StringBuffer();
        for(TbLocDevice d : locator){
            device.append("|").append(d.getDeviceRn()).append("|")
            .append(d.getDeviceIp());
        }
        if(device.toString().length()>0){
            device.deleteCharAt(0);
        }
        s.append(device.toString()).append("\",\"positions\":\"");
        
        List<TbLocPosition> posiList = locMapper.getLocPositionListBySchoolId(
                    transfer.getSchoolId());
        
        StringBuffer position = new StringBuffer();
        for(TbLocPosition p : posiList){
            position.append("|").append(p.getPositionId())
            .append("|").append(p.getOutConfig()==null?"":p.getOutConfig())
            .append("|").append(p.getInConfig()==null?"":p.getInConfig())
            ;
        }
        if(position.toString().length()>0){
            position.deleteCharAt(0);
        }
        
        s.append(position.toString()).append("\",");
        s.append("\"validCardList\":\"");
        
        //现在有效的卡号
        List<String> validCardList = locMapper.getValidCardNoList(transfer.getSchoolId());
        StringBuffer card = new StringBuffer();
        for(int i=0,size=validCardList==null?0:validCardList.size();
                i<size; i++){
            card.append("|").append(validCardList.get(i));
        }
        if(card.toString().length()>0){
            card.deleteCharAt(0);
        }
        s.append(card.toString());
        
        s.append("\",");
        s.append("\"relay\":\"").append(JsonUtil.regString(transfer.getRelayConfig()));
        s.append("\",\"serverTime\":\"")
        .append(fmt.format(new Date())).append("\"}");
        
        return s.toString();
    }
 
    private SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    
    private String uploadLocation(LocationMapper locMapper, String schoolId, String locations) {
        String[] location = locations.split("[|]");
        if(location.length %4 !=0){
            return null;
        }
        
        for(int i=0; i<location.length; i+=4){
            String cardNo = location[i].trim();
            String positionId = location[i+1].trim();
            String locType = location[i+2].trim();
            String locTime = location[i+3].trim();
            
            TbSchoolStudent stu = locMapper.getStudentBySchoolIdLocCardNo(schoolId, cardNo);
            if(stu ==null){
                continue;
            }
            
            TbLocPosition posi = locMapper.getLocPositionBySchoolIdPositionId(
                    schoolId, Integer.valueOf(positionId));
            if(posi ==null){
                continue;
            }
            
            TbSchoolClass cls = locMapper.getSchoolClassBySchoolIdClassId(
                    schoolId, stu.getClassId());
            if(cls ==null){
                continue;
            }
            
            Date time = new Date(Long.valueOf(locTime));
            locTime = fmt.format(time);
            
            locMapper.insertTbLocLog(stu, posi, cls, locTime, locType);
        }
        
        //TODO: 如果配置发生变化,则需返回 ok:config,提示下位机重新获取配置
        
        return "ok";
    }
    
    
    
    //4 19fuzhen 增加测试
    private String uploadLocationTest(String locations) {
        String[] location = locations.split("[|]");
        if(location.length %4 !=0){
            return null;
        }
        
        for(int i=0; i<location.length; i+=4){
            String cardNo = location[i].trim();
            String positionId = location[i+1].trim();
            String locType = location[i+2].trim();
            String locTime = location[i+3].trim();
            
            
            
            Date time = new Date(Long.valueOf(locTime));
            locTime = fmt.format(time);
            
            locMapper.insertForm_sign( java.util.UUID.randomUUID().toString(),"1",locTime,"1",locTime,"1","0",locTime, cardNo,Integer.parseInt(locType) , Integer.parseInt(positionId));
            }
        
        //TODO: 如果配置发生变化,则需返回 ok:config,提示下位机重新获取配置
        
        return "ok";
    }
 
}