package com.tzld.piaoquan.recommend.server.web;

import com.google.protobuf.InvalidProtocolBufferException;
import com.tzld.piaoquan.recommend.server.client.ProtobufUtils;
import com.tzld.piaoquan.recommend.server.gen.recommend.RecommendRequest;
import com.tzld.piaoquan.recommend.server.gen.recommend.RecommendResponse;
import com.tzld.piaoquan.recommend.server.service.RecommendService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author dyp
 */
@RestController
@Slf4j
public class RecommendV2Controller {
    @Autowired
    private RecommendService recommendService;

    @RequestMapping("/homepage/recommend/v2")
    public String homepageRecommend(@RequestBody RecommendRequest httpRequest) {
        MDC.put("appType", String.valueOf(httpRequest.getAppType()));
        RecommendResponse response = recommendService.homepageRecommend(httpRequest);
        String result = "";
        try {
            result = ProtobufUtils.toJson(response);
        } catch (InvalidProtocolBufferException e) {
            log.error("homepageRecommend ProtobufUtils.toJson", e);
        }
        return result;
    }

    @RequestMapping("/relevant/recommend/v2")
    public String relevantRecommend(@RequestBody RecommendRequest httpRequest) {
        MDC.put("appType", String.valueOf(httpRequest.getAppType()));

        RecommendResponse response = recommendService.relevantRecommend(httpRequest);
        String result = "";
        try {
            result = ProtobufUtils.toJson(response);
        } catch (InvalidProtocolBufferException e) {
            log.error("relevantRecommend ProtobufUtils.toJson", e);
        }
        return result;
    }

    @RequestMapping("/immerse/recommend/v2")
    public String immerseRecommend(@RequestBody RecommendRequest httpRequest) {
        MDC.put("appType", String.valueOf(httpRequest.getAppType()));
        RecommendResponse response = recommendService.immerseRecommend(httpRequest);
        String result = "";
        try {
            result = ProtobufUtils.toJson(response);
        } catch (InvalidProtocolBufferException e) {
            log.error("immerseRecommend ProtobufUtils.toJson", e);
        }
        return result;
    }
}
