package com.xkrs.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.xkrs.common.encapsulation.PromptMessageEnum; import com.xkrs.common.tool.TokenUtil; import com.xkrs.dao.*; import com.xkrs.model.entity.*; import com.xkrs.model.vo.AppPhotoVo; import com.xkrs.model.vo.AppTaskBodyVo; import com.xkrs.service.FireAndRangerService; import com.xkrs.utils.DateTimeUtil; import com.xkrs.utils.ObjectToBeanUtils; import com.xkrs.utils.RestTemplateUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.cglib.beans.BeanCopier; import org.springframework.context.i18n.LocaleContextHolder; 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.Locale; import java.util.Map; import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject; /** * @author xkrs */ @Service public class FireAndRangerServiceImpl implements FireAndRangerService { public static Logger log = LoggerFactory.getLogger(FireAndRangerServiceImpl.class); @Resource private FireAndRangerDao fireAndRangerDao; @Resource private SysUserDao sysUserDao; @Resource private FirePointDao firePointDao; @Resource private ForestRangerDao forestRangerDao; @Resource private RestTemplateUtil restTemplateUtil; @Resource private FireTaskDao fireTaskDao; @Resource private FireTaskPhotoDao fireTaskPhotoDao; /** * 将火点分配给护林员 * @param rangerName * @param fireCode * @return */ @Transactional(rollbackOn = Exception.class) @Override public String addFireAndRanger(String rangerName, String fireCode, String handler, String verifier, String token) throws UnsupportedEncodingException { // 获取区域信息 Locale locale = LocaleContextHolder.getLocale(); String tokenUserName = TokenUtil.getTokenUserName(token); FireAndRanger byFireCode = fireAndRangerDao.findByFireCode(fireCode); if(byFireCode != null){ return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL,"该火点已经分配,请勿重复操作",locale); } FirePointEntity byFireCode1 = firePointDao.findByFireCode(fireCode); // 给app端推送的消息内容 Map map = new HashMap(3); map.put("latitude",byFireCode1.getLatitude()); map.put("longitude",byFireCode1.getLongitude()); map.put("detailedAddress",byFireCode1.getFirePointAddress()); 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¬ify_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¬ify_id=2&extra.notify_effect=2&extra.intent_uri=fieldverify://xkrs.com&alias="+rangerName+"&extra.notify_foreground=0&extra.channel_id=mipush"; String time = DateTimeUtil.dateTimeToString(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 outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL,"操作失败",locale); } return outputEncapsulationObject(PromptMessageEnum.SUCCESS,"操作成功",locale); } /** * 护林员查看自己的火点任务 * @param token * @return */ @Override public String selectFireInformation(String token) { // 获取区域信息 Locale locale = LocaleContextHolder.getLocale(); String tokenUserName = TokenUtil.getTokenUserName(token); SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName); ForestRanger byRangerPhone = forestRangerDao.findByRangerPhone(sysUserEntity.getUserName()); if(byRangerPhone.getRangerType().equals("1")){ FireAndRanger byRangerName = fireAndRangerDao.findByRangerNameAndProgressType(sysUserEntity.getUserName(),"1"); if(byRangerName == null){ return outputEncapsulationObject(PromptMessageEnum.DATA_NONE,"暂时还没有任何任务",locale); } FirePointEntity byFireCode = firePointDao.findByFireCode(byRangerName.getFireCode()); return outputEncapsulationObject(PromptMessageEnum.SUCCESS,byFireCode,locale); }else { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE,"暂时还没有任何任务",locale); } } /** * 根据火点编码查询核查的信息 * @param fireCode * @return */ @Override public String findFireAndRangerByFireCode(String fireCode, String token) throws Exception { // 获取区域信息 Locale locale = LocaleContextHolder.getLocale(); String tokenUserName = TokenUtil.getTokenUserName(token); FireAndRanger byFireCode = fireAndRangerDao.findByFireCode(fireCode); if(byFireCode == null){ return outputEncapsulationObject(PromptMessageEnum.DATA_NONE,"暂时还没有该火点的核查信息",locale); } Map map = new HashMap(3); map.put("fire",byFireCode); FireTask byTaskFireCode = fireTaskDao.findByTaskFireCode(fireCode); if(byTaskFireCode == null){ map.put("task",null); return outputEncapsulationObject(PromptMessageEnum.SUCCESS,map,locale); }else { AppTaskBodyVo appTaskBodyVo = new AppTaskBodyVo(); // 做映射,返回vo类对象 BeanCopier beanCopier = BeanCopier.create(FireTask.class, AppTaskBodyVo.class, false); beanCopier.copy(byTaskFireCode,appTaskBodyVo,null); List photoPath = fireTaskPhotoDao.findPhotoPath(fireCode); List appPhotoVos = ObjectToBeanUtils.objectToBean(photoPath, AppPhotoVo.class); appTaskBodyVo.setTaskFirePhoto(appPhotoVos); map.put("task",appTaskBodyVo); return outputEncapsulationObject(PromptMessageEnum.SUCCESS,map,locale); } } }