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";
|
}
|
|
}
|