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 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))); } }