yujian
2019-02-26 a4d96fb3100e6aaf65e54d260921ceb1c00e54ef
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
package com.yeshi.fanli.controller.client;
 
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
import com.google.gson.Gson;
import com.yeshi.fanli.entity.AppInfo;
import com.yeshi.fanli.entity.DeviceInfo;
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.customerservice.CustomerServiceCommonQuestion;
import com.yeshi.fanli.entity.customerservice.CustomerServiceHistory;
import com.yeshi.fanli.service.inter.customerservice.CustomerServiceCommonQuestionService;
import com.yeshi.fanli.service.inter.customerservice.CustomerServiceHistoryService;
import com.yeshi.fanli.util.StringUtil;
import org.yeshi.utils.JsonUtil;
 
import net.sf.json.JSONObject;
 
/**
 * 客服api接口
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("api/v1/customerservice")
public class CustomerServiceController {
 
    @Resource
    private CustomerServiceCommonQuestionService customerServiceCommonQuestionService;
 
    @Resource
    private CustomerServiceHistoryService customerServiceHistoryService;
 
    /**
     * 预设标题的关键字列表
     * 
     * @param acceptData
     * @param out
     */
    @RequestMapping(value = "getkeys", method = RequestMethod.POST)
    public void getKeys(AcceptData acceptData, PrintWriter out) {
        List<String> keys = customerServiceCommonQuestionService.listKeysCache();
        JSONObject data = new JSONObject();
        data.put("keys", keys);
        out.print(JsonUtil.loadTrueResult(data));
    }
 
    /**
     * 根据关键字获取答案
     * 
     * @param acceptData
     * @param key
     *            -关键字
     * @param out
     */
    @RequestMapping(value = "getanswer", method = RequestMethod.POST)
    public void getGoodsInfo(AcceptData acceptData, String key, PrintWriter out) {
        if (StringUtil.isNullOrEmpty(key)) {
            out.print(JsonUtil.loadFalseResult(1, "请上传问题"));
            return;
        }
 
        if (key.length() > 512) {
            out.print(JsonUtil.loadFalseResult(2, "问题过长,请精简问题"));
            return;
        }
 
        Gson gson = new Gson();
 
        CustomerServiceHistory history = new CustomerServiceHistory();
        AppInfo appInfo = new AppInfo();
        appInfo.setChannel(acceptData.getChannel());
        appInfo.setPackages(acceptData.getPackages());
        appInfo.setVersion(acceptData.getVersion());
 
        history.setAppInfo(appInfo);
        history.setAppInfoStr(gson.toJson(appInfo));
        history.setContent(key);
        history.setCreateTime(new Date());
        history.setDevice(acceptData.getDevice());
        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.setDevice(acceptData.getDevice());
        deviceInfo.setDeviceType(acceptData.getDeviceType());
        deviceInfo.setNetwork(acceptData.getNetwork());
        deviceInfo.setOsVersion(acceptData.getOsVersion());
        deviceInfo.setPlatform(acceptData.getPlatform());
 
        history.setDeviceInfo(deviceInfo);
        history.setDeviceInfoStr(gson.toJson(deviceInfo));
        history.setPlatform(acceptData.getPlatform());
        history.setUpdateTime(new Date());
 
        // 保存问题历史记录
        customerServiceHistoryService.addHistory(history);
 
        // 搜索问题答案
        CustomerServiceCommonQuestion answer = customerServiceCommonQuestionService.searchByKeyCache(key);
        if (answer == null) {
            answer = new CustomerServiceCommonQuestion();
            answer.setKey("");
            answer.setContentType(CustomerServiceCommonQuestion.TYPE_TEXT);
            answer.setContent("客服机器人不明白您的问题,请输入你问题的准确关键词或转人工客服解决,谢谢!#^.^#");
 
            // out.print(JsonUtil.loadFalseResult(-1, "无答案"));
            // return;
        }
        out.print(JsonUtil.loadTrueResult(JsonUtil.getApiCommonGson().toJson(answer)));
    }
 
}