广州市综治平台后端
xusd
2025-06-07 36306491396230522fa20585c2621a7fc899849a
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
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());
        }
    }