package com.ssj.sys.controller;

import com.ssj.bean.weixin.problem.domain.Problem;
import com.ssj.framework.core.security.manager.TokenManager;
import com.ssj.framework.core.util.PropertiesUtil;
import com.ssj.service.conch.conch.service.ConchDerverService;
import com.ssj.service.weixin.problem.service.ProblemService;
import com.ssj.weixin.utils.Location;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 类描述：首页页面控制<br>
 * @author DELL
 *
 */
@Controller
public class WebController {
	@Autowired
	private TokenManager tokenManager;
	@Autowired
	private ConchDerverService conchDerverService;
	@Autowired
	private ProblemService  problemService;
	 /**
     * 首页
     * @throws Exception
     **/
    @RequestMapping(value = { "/"})
    public String home(ModelMap model,HttpServletRequest request) throws Exception {
    	 return "redirect:/login.html"; 
    }
    
    @RequestMapping(value = {"/binding" })
    public String binding(ModelMap model,HttpServletRequest request,
    		@RequestParam(required = true) String  accessToken) throws Exception {
    	Location  location=tokenManager.get(accessToken, Location.class);
    	Map<String,String> map = new HashMap<String, String>();
    	model.addAttribute("imgUrl",PropertiesUtil.getValue("STATIC_FILE_URL"));
    	if(location!=null){
    		map.putAll(conchDerverService.bindingVip(location.getVipId(), location.getDerver(), location.getUserId(),location.getOpenId()));
    	}else{
    		map.put("stat", "-1");
    		map.put("mgs", "绑定信息不存在或已失效,<br/>请重新扫描绑定");
    	}
    	model.addAttribute("map", map);
    	 return "sys/login/binding";
    }
    @RequestMapping(value = {"/article/{id}" })
    public String article(ModelMap model,HttpServletRequest request,@PathVariable String id) throws Exception {
    	Problem problem=problemService.getById(id);
    	model.addAttribute("problem", problem!=null?problem:new Problem() );
    	return "sys/login/article";
    }
    
}
