package cn.huge.module.grid.service;
|
|
import cn.huge.base.common.bo.R;
|
import cn.huge.base.common.utils.HttpClientUtils;
|
import cn.huge.base.common.utils.SpringContextUtil;
|
import cn.huge.module.grid.dao.mapper.CitizenEventMapper;
|
import cn.huge.module.grid.domain.po.CitizenEventPo;
|
import cn.huge.module.grid.domain.vo.GenerateQrCodeRequestVo;
|
import cn.huge.module.grid.domain.vo.YaymQrCodeInfoVo;
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.codec.digest.DigestUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Service;
|
|
import java.time.LocalDateTime;
|
import java.time.format.DateTimeFormatter;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* 市民工单服务类
|
*/
|
@Slf4j
|
@Service
|
public class CitizenEventService {
|
|
@Autowired
|
private CitizenEventMapper citizenEventMapper;
|
|
@Value("${grid.url:http://219.137.166.84:8061}")
|
private String gridUrl;
|
|
@Value("${grid.username:mtxt0011}")
|
private String userName;
|
|
@Value("${grid.password}")
|
private String passWord;
|
|
@Value("${grid.secret:BZb2hLCx05}")
|
private String secret;
|
|
@Value("${grid.passid:b08c0ec0-772d-caf8-5c9b-1f7d86295a95}")
|
private String passid;
|
|
/**
|
* 初始化请求头信息
|
*/
|
public HashMap<String,String> initHeadVo() {
|
long timestamp = System.currentTimeMillis()/1000;
|
HashMap<String,String> headVo = new HashMap<>();
|
headVo.put("x-rio-paasid",passid);
|
headVo.put("x-rio-timestamp",timestamp+"");
|
headVo.put("x-rio-nonce","mt");
|
headVo.put("x-rio-signature", DigestUtils.sha256Hex(timestamp+secret+"mt"+timestamp));
|
return headVo;
|
}
|
|
/**
|
* 生成二维码并保存工单信息
|
*/
|
public R<YaymQrCodeInfoVo> generateQrCodeAndSave(GenerateQrCodeRequestVo requestVo) {
|
log.info("生成二维码请求参数:{}", JSON.toJSONString(requestVo));
|
|
// 参数验证
|
if (requestVo == null) {
|
return R.fail("请求参数不能为空");
|
}
|
if (requestVo.getId() == null || requestVo.getId().trim().isEmpty()) {
|
return R.fail("工单id不能为空");
|
}
|
if (requestVo.getBusinessSource() == null || requestVo.getBusinessSource().trim().isEmpty()) {
|
return R.fail("事件业务来源编码不能为空");
|
}
|
if (requestVo.getGenerateUnifiedCode() == null) {
|
return R.fail("是否生成统一工单编码不能为空");
|
}
|
|
// 当generateUnifiedCode为true时,校验必填字段
|
if (Boolean.TRUE.equals(requestVo.getGenerateUnifiedCode())) {
|
if (requestVo.getInfoSource() == null || requestVo.getInfoSource().trim().isEmpty()) {
|
return R.fail("当生成统一工单编码时,事件来源渠道不能为空");
|
}
|
if (requestVo.getGridCode() == null || requestVo.getGridCode().trim().isEmpty()) {
|
return R.fail("当生成统一工单编码时,网格编码不能为空");
|
}
|
}
|
|
// 初始化请求头
|
HashMap<String,String> headVo = initHeadVo();
|
log.info("请求头信息:{}", JSON.toJSONString(headVo));
|
|
String responseStr;
|
try {
|
// 开发环境
|
if (SpringContextUtil.checkDev()) {
|
String testUrl = "http://183.2.142.46:9007/api/thrid/citizen/event/generateQrCode";
|
log.info("开发环境请求URL:{}", testUrl);
|
Map<String, String> headers = new HashMap<>();
|
headers.put("Content-Type", "application/json");
|
responseStr = HttpClientUtils.httpPostRaw(testUrl, JSON.toJSONString(requestVo), headers, "utf-8");
|
} else {
|
// 生产环境 - 使用HTTP Header认证方式
|
String httpPostUrl = gridUrl + "/standard/eventform/generateYaymQrCode";
|
log.info("生产环境请求URL:{}", httpPostUrl);
|
log.info("请求参数:{}", JSON.toJSONString(requestVo));
|
responseStr = HttpClientUtils.httpPostRaw(httpPostUrl, JSON.toJSONString(requestVo), headVo, "utf-8");
|
}
|
} catch (Exception e) {
|
log.error("调用接口异常:", e);
|
throw new RuntimeException("调用接口失败", e);
|
}
|
|
log.info("接口响应:{}", responseStr);
|
|
// 解析响应结果
|
JSONObject response = JSONObject.parseObject(responseStr);
|
Integer code = response.getInteger("code");
|
String message = response.getString("message");
|
if (message == null) {
|
message = response.getString("desc");
|
}
|
|
if (code == null || code != 0) {
|
log.error("接口调用失败, code:{}, message:{}", code, message);
|
return R.fail(message != null ? message : "接口调用失败");
|
}
|
|
// 获取响应数据
|
YaymQrCodeInfoVo yaymQrCodeInfo = response.getObject("data", YaymQrCodeInfoVo.class);
|
|
if (yaymQrCodeInfo != null) {
|
// 保存到数据库
|
saveToDatabase(requestVo.getId(), yaymQrCodeInfo);
|
}
|
|
return R.ok(yaymQrCodeInfo, message);
|
}
|
|
/**
|
* 保存工单信息到数据库
|
*/
|
private void saveToDatabase(String caseId, YaymQrCodeInfoVo yaymQrCodeInfo) {
|
try {
|
CitizenEventPo citizenEvent = new CitizenEventPo();
|
citizenEvent.setCaseId(caseId);
|
citizenEvent.setCitizenEventCode(yaymQrCodeInfo.getCitizenEventCode());
|
citizenEvent.setQrcodeImg(yaymQrCodeInfo.getQrcodeImg());
|
citizenEvent.setTitle(yaymQrCodeInfo.getTitle());
|
|
// 处理时间字符串转换
|
if (yaymQrCodeInfo.getReportTime() != null && !yaymQrCodeInfo.getReportTime().isEmpty()) {
|
try {
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
citizenEvent.setReportTime(LocalDateTime.parse(yaymQrCodeInfo.getReportTime(), formatter));
|
} catch (Exception e) {
|
log.warn("时间格式转换失败:{}", yaymQrCodeInfo.getReportTime(), e);
|
}
|
}
|
|
citizenEvent.setReportUser(yaymQrCodeInfo.getReportUser());
|
citizenEvent.setEntryItemName(yaymQrCodeInfo.getEntryItemName());
|
citizenEvent.setUnifiedCode(yaymQrCodeInfo.getUnifiedCode());
|
citizenEvent.setCreateTime(LocalDateTime.now());
|
citizenEvent.setUpdateTime(LocalDateTime.now());
|
citizenEvent.setDeleteStatus(0);
|
|
// 先尝试更新,如果不存在则插入
|
int updateCount = citizenEventMapper.updateById(citizenEvent);
|
if (updateCount == 0) {
|
citizenEventMapper.insert(citizenEvent);
|
log.info("新增工单信息成功, caseId:{}", caseId);
|
} else {
|
log.info("更新工单信息成功, caseId:{}", caseId);
|
}
|
|
} catch (Exception e) {
|
log.error("保存工单信息到数据库失败, caseId:{}", caseId, e);
|
throw new RuntimeException("保存工单信息失败", e);
|
}
|
}
|
|
/**
|
* 根据工单ID查询工单信息
|
*/
|
public R<CitizenEventPo> getCitizenEventById(String caseId) {
|
try {
|
CitizenEventPo citizenEvent = citizenEventMapper.selectById(caseId);
|
if (citizenEvent == null) {
|
return R.fail("工单信息不存在");
|
}
|
return R.ok(citizenEvent);
|
} catch (Exception e) {
|
log.error("查询工单信息失败, caseId:{}", caseId, e);
|
return R.fail("查询工单信息失败: " + e.getMessage());
|
}
|
}
|
}
|