package com.wx.application.adapter.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wx.application.adapter.dto.qo.GorseQ;
import com.wx.application.base.BaseController;
import com.wx.application.base.ResponseData;
import com.wx.application.core.service.EntrysService;
import com.wx.application.core.service.RiskUserService;
import com.wx.application.gorse4j.GorseService;
import com.wx.application.gorse4j.Item;
import com.wx.application.nebula.graph.query.NebulaModel;
import com.wx.application.nebula.graph.query.NebulaNode;
import com.wx.application.nebula.graph.service.ImportGraphService;
import com.wx.application.nebula.graph.service.NebulaOperateService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequestMapping("/gorse")
@RestController("gorseController")
public class GorseController extends BaseController {
	
	@Autowired
	GorseService gorseService;
	
	@Autowired
	RiskUserService riskUserService;
	
	@Autowired
	EntrysService entrysService;
	
	@Autowired
	NebulaOperateService nebulaOperateService;
	
	@Autowired
	ImportGraphService importGraphService;
	
	/**
	 * 推荐根据userId
	 * @param gQo
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/recommend_by_userid")
    public ResponseData recommendByUserId(@RequestBody GorseQ gQo) throws Exception {
		List<String> res = gorseService.getRecommend(gQo.getUserId());
		if(res == null || res.size() == 0) {
			return success();
		}
		Map mQ = new HashMap<>();
		mQ.put("INS_fid", StringUtils.join(res, ","));
		return success(entrysService.queryList(mQ));
	}
	
	/**
	 * 根据列表推荐
	 * @param gQo
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/popular_by_category")
    public ResponseData popularByCategory(@RequestBody GorseQ gQo) throws Exception {
		List<Item> res = gorseService.getPopular(gQo.getCategory());
		return success(res);
	}
	
	@PostMapping(value = "/ipt")
    public ResponseData ipt() throws Exception {
		importGraphService.iptUser();
		importGraphService.iptentrys();
		return success();
	}
	
	/**
	 * 推荐根据知识图谱
	 * @param gQo
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/recommend_by_userid_with_graph")
    public ResponseData recommendByUserIdWithGraph(@RequestBody GorseQ gQo) throws Exception {
		
		NebulaModel nebulaModel = nebulaOperateService.findOnePathById("", gQo.getUserId());
		List<NebulaNode> nodes = nebulaModel.getNodes();
		
		if(nodes != null && nodes.size() > 0) {
			nodes.get(0).getVid();
		} 
		
		List<String> res = gorseService.getRecommend(gQo.getUserId());
		if(res == null || res.size() == 0) {
			return success();
		}
		Map mQ = new HashMap<>();
		mQ.put("INS_fid", StringUtils.join(res, ","));
		return success(entrysService.queryList(mQ));
	}
	
	
}
