fire_point/src/main/java/com/xkrs/service/impl/FireAndRangerServiceImpl.java

172 lines
6.8 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.xkrs.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xkrs.dao.*;
import com.xkrs.entity.*;
import com.xkrs.service.FireAndRangerService;
import com.xkrs.utils.*;
import com.xkrs.vo.AppPhotoVo;
import com.xkrs.vo.AppTaskBodyVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author xkrs
*/
@Service
public class FireAndRangerServiceImpl implements FireAndRangerService {
public static Logger log = LoggerFactory.getLogger(FireAndRangerServiceImpl.class);
@Resource
private FireAndRangerDao fireAndRangerDao;
@Resource
private ForestUserDao forestUserDao;
@Resource
private FirePointDao firePointDao;
@Resource
private XkRsForestRangerDao forestRangerDao;
@Resource
private RestTemplateUtil restTemplateUtil;
@Resource
private FireTaskDao fireTaskDao;
@Resource
private FireTaskPhotoDao fireTaskPhotoDao;
/**
* 将火点分配给护林员
* @param rangerName
* @param fireCode
* @return
*/
@Transactional(rollbackOn = Exception.class)
@Override
public Result addFireAndRanger(String rangerName, String fireCode, String handler, String verifier, String token) throws UnsupportedEncodingException {
String tokenUserName = TokenUtil.getTokenUserName(token);
if(tokenUserName == null || "".equals(tokenUserName)){
return Result.error("您还未注册和登录,请先注册和登录!");
}
FireAndRanger byFireCode = fireAndRangerDao.findByFireCode(fireCode);
if(byFireCode != null){
return Result.error("该火点已经分配,请勿重复操作");
}
XkRsFirePoint byFireCode1 = firePointDao.findByFireCode(fireCode);
// 给app端推送的消息内容
Map<String,Object> map = new HashMap(3);
map.put("latitude",byFireCode1.getLatitude());
map.put("longitude",byFireCode1.getLongitude());
map.put("detailedAddress",byFireCode1.getDetailedAddress());
String content = JSON.toJSONString(map);
String ss = URLEncoder.encode(content,"utf-8");
//String url = "https://api.xmpush.xiaomi.com/v3/message/alias?title=收到消息&description=请"+rangerName+ "去核查该火点信息&payload="+content+"&restricted_package_name=com.xkrs.fieldverification&notify_id=1&extra.notify_effect=2&extra.intent_uri=fieldverify://xkrs.com&alias="+rangerName;
String url = "https://api.xmpush.xiaomi.com/v3/message/alias?title=收到消息&description=请"+verifier+"去核查该火点信息&payload="+ss+"&restricted_package_name=com.xkrs.fieldverification&notify_id=2&extra.notify_effect=2&extra.intent_uri=fieldverify://xkrs.com&alias="+rangerName+"&extra.notify_foreground=0&extra.channel_id=mipush";
String time = LocalDateTimeUtil.localDateTime2String(LocalDateTime.now());
FireAndRanger fireAndRanger = new FireAndRanger();
fireAndRanger.setRangerName(rangerName);
fireAndRanger.setFireCode(fireCode);
fireAndRanger.setHandlerTime(time);
fireAndRanger.setHandler(handler);
fireAndRanger.setVerifier(verifier);
fireAndRanger.setProgressType("1");
// 将火点分配给该护林员后将护林员状态修改为1 表示正在执行火点任务
forestRangerDao.updateRangerTypeByPhone(rangerName,"1");
JSONObject jsonObject = new JSONObject();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Authorization","key=QW+6vVbWptJHscqD7zuM5w==");
log.info("-----------已向app发送消息");
restTemplateUtil.doPostMessageToApp(url, httpHeaders, jsonObject);
FireAndRanger save = fireAndRangerDao.save(fireAndRanger);
if(save == null){
return Result.error("操作失败");
}
return Result.ok("操作成功");
}
/**
* 护林员查看自己的火点任务
* @param token
* @return
*/
@Override
public Result selectFireInformation(String token) {
String tokenUserName = TokenUtil.getTokenUserName(token);
if(tokenUserName == null || "".equals(tokenUserName)){
return Result.error("您还未注册和登录,请先注册和登录!");
}
XkRsUser xkRsUser = forestUserDao.selectUserName(tokenUserName);
XkRsForestRanger byRangerPhone = forestRangerDao.findByRangerPhone(xkRsUser.getNickName());
if(byRangerPhone.getRangerType().equals("1")){
FireAndRanger byRangerName = fireAndRangerDao.findByRangerNameAndProgressType(xkRsUser.getNickName(),"1");
if(byRangerName == null){
return Result.error("暂时还没有任何任务");
}
XkRsFirePoint byFireCode = firePointDao.findByFireCode(byRangerName.getFireCode());
return Result.ok(byFireCode);
}else {
return Result.error("暂时还没有任务");
}
}
/**
* 根据火点编码查询核查的信息
* @param fireCode
* @return
*/
@Override
public Result findFireAndRangerByFireCode(String fireCode, String token) throws Exception {
String tokenUserName = TokenUtil.getTokenUserName(token);
if(tokenUserName == null || "".equals(tokenUserName)){
return Result.error("您还未注册和登录,请先注册和登录!");
}
FireAndRanger byFireCode = fireAndRangerDao.findByFireCode(fireCode);
if(byFireCode == null){
return Result.error("暂时还没有该火点核查的信息");
}
Map map = new HashMap(3);
map.put("fire",byFireCode);
FireTask byTaskFireCode = fireTaskDao.findByTaskFireCode(fireCode);
if(byTaskFireCode == null){
map.put("task",null);
return Result.ok().data(map);
}else {
AppTaskBodyVo appTaskBodyVo = new AppTaskBodyVo();
// 做映射返回vo类对象
BeanCopier beanCopier = BeanCopier.create(FireTask.class, AppTaskBodyVo.class, false);
beanCopier.copy(byTaskFireCode,appTaskBodyVo,null);
List<Object[]> photoPath = fireTaskPhotoDao.findPhotoPath(fireCode);
List<AppPhotoVo> appPhotoVos = ObjectToBeanUtils.objectToBean(photoPath, AppPhotoVo.class);
appTaskBodyVo.setTaskFirePhoto(appPhotoVos);
map.put("task",appTaskBodyVo);
return Result.ok().data(map);
}
}
}