SuggestController.java 6.28 KB
package com.yoho.search.consumer.restapi;

import com.yoho.search.base.utils.CharUtils;
import com.yoho.search.base.constants.ISearchConstants;
import com.yoho.search.base.utils.MD5Util;
import com.yoho.search.consumer.common.IYohoIndexService;
import com.yoho.search.consumer.index.rebuild.RebuildFlagService;
import com.yoho.search.consumer.job.SuggestionJob;
import com.yoho.search.consumer.service.daoService.SuggestWordDefService;
import com.yoho.search.consumer.suggests.common.KeywordType;
import com.yoho.search.consumer.suggests.counter.KeywordCounterService;
import com.yoho.search.dal.model.SuggestWordDef;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wangnan
 * @version 2019/6/4
 */
@Controller
public class SuggestController {

    private static final Logger logger = LoggerFactory.getLogger(SuggestController.class);

    @Autowired
    private IYohoIndexService yohoIndexService;
    @Autowired
    private RebuildFlagService rebuildFlagService;
    @Autowired
    private SuggestionJob suggestionJob;
    @Autowired
    private SuggestWordDefService suggestWordDefService;
    @Autowired
    private KeywordCounterService keywordCounterService;


    @RequestMapping(value = "/index/suggestion/flow")
    public Map<String, Object> runSuggestionFlow() {
        try {
            if (rebuildFlagService.isRebuilding()) {
                return getResultMap(400, "current has index rebuilding, please wait......");
            }
            suggestionJob.executeAll();
            return getResultMap(200, "success");
        } catch (Exception e) {
            logger.error("[func=runSuggestionFlow][step=execption][e={}]", e.getMessage());
            Map<String, Object> rtnMap = new HashMap<String, Object>();
            rtnMap.put("code", 400);
            rtnMap.put("msg", e.getMessage());
            return rtnMap;
        }
    }

    @RequestMapping(value = "/index/suggestionCounter")
    public Map<String, Object> suggestionCounter(@RequestParam String flowName) {
        try {
            if (rebuildFlagService.isRebuilding()) {
                return getResultMap(400, "current has index rebuilding, please wait......");
            }
            if ("all".equalsIgnoreCase(flowName)) {
                suggestionJob.executeCounter();
                return getResultMap(200, "success");
            }
            suggestionJob.executeCounterForSingleFlow(flowName);
            return getResultMap(200, "success");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return this.getErrorResult(e);
        }
    }

    @RequestMapping(value = "/index/suggestionDiscovery")
    public Map<String, Object> suggestionDiscovery(@RequestParam String flowName) {
        try {
            if (rebuildFlagService.isRebuilding()) {
                return getResultMap(400, "current has index rebuilding, please wait......");
            }
            if ("all".equalsIgnoreCase(flowName)) {
                suggestionJob.executeDiscovery();
            } else {
                suggestionJob.executeDiscoveryForSingleFlow(flowName);
            }
            return getResultMap(200, "success");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return this.getErrorResult(e);
        }
    }

    @RequestMapping(value = "/index/suggestWordMgr")
    public Map<String, Object> suggestWordMgr(@RequestParam String keyword, @RequestParam(defaultValue = "false") boolean isDelete) {
        try {
            SuggestWordDef suggestWordDef = suggestWordDefService.selectByKeyword(keyword);
            if (isDelete) {
                if (suggestWordDef == null) {
                    throw new RuntimeException("The keyword is not found!");
                }
                if (!Integer.valueOf(1).equals(suggestWordDef.getStatus())) {
                    throw new RuntimeException("The keyword has been deleted already!");
                }
                suggestWordDefService.updateStatusByPrimaryKey(suggestWordDef.getId(), 0);
                yohoIndexService.deleteIndexData(ISearchConstants.INDEX_NAME_SUGGEST, MD5Util.string2MD5(keyword.trim().toLowerCase()));
            } else {
                if (suggestWordDef != null) {
                    throw new RuntimeException("The keyword has been added!");
                }
                suggestWordDef = keywordCounterService.countKeyword(keyword);
                suggestWordDef.setStatus(1);
                suggestWordDef.setType(KeywordType.Customized.getType());
                suggestWordDef.setWeight(KeywordType.Customized.getWeightValue());
                suggestWordDefService.insertBatch(Arrays.asList(suggestWordDef));

                Map<String, Object> dataMap = new HashMap<String, Object>();
                dataMap.put("keyword", keyword);
                dataMap.put("standardKeyword", CharUtils.standardized(keyword));
                dataMap.put("type", suggestWordDef.getType());
                dataMap.put("weight", suggestWordDef.getWeight());
                dataMap.put("count", suggestWordDef.getCount());
                dataMap.put("countForApp", suggestWordDef.getCountForApp());
                dataMap.put("countForBlk", suggestWordDef.getCountForBlk());
                yohoIndexService.addIndexData(ISearchConstants.INDEX_NAME_SUGGEST, MD5Util.string2MD5(keyword.trim().toLowerCase()), dataMap);
            }
            return getResultMap(200, "success");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return this.getErrorResult(e);
        }
    }

    private Map<String, Object> getResultMap(final int code, final String message) {
        Map<String, Object> rtnMap = new HashMap<String, Object>();
        rtnMap.put("code", code);
        rtnMap.put("msg", message);
        return rtnMap;
    }

    private Map<String, Object> getErrorResult(Exception e) {
        Map<String, Object> rtnMap = new HashMap<String, Object>();
        rtnMap.put("code", 400);
        rtnMap.put("msg", e.getMessage());
        return rtnMap;
    }
}