package com.goafanti.common.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.goafanti.achievement.bo.AchievementPartnerListBo;
import com.goafanti.achievement.service.AchievementService;
import com.goafanti.business.service.JtBusinessService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.enums.AchievementMaturity;
import com.goafanti.common.model.UserInterest;
import com.goafanti.common.service.DistrictGlossoryService;
import com.goafanti.common.service.FieldGlossoryService;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.demand.service.DemandInterestService;
import com.goafanti.demand.service.DemandService;
import com.goafanti.order.service.OrderService;
import com.goafanti.user.bo.UserPartnerDetailBo;
import com.goafanti.user.service.UserIdentityService;
import com.goafanti.user.service.UserInterestService;
import com.goafanti.user.service.UserService;

@Controller
public class PortalController extends BaseController {
	@Resource
	private FieldGlossoryService glossoryService;
	@Resource
	private AchievementService achievementService;
	@Resource
	private DemandService demandService;
	@Resource
	UserIdentityService	userIdentityService;
	@Resource
	private FieldGlossoryService fieldGlossoryService;
	@Resource
	private DemandInterestService  demandInterestService;
	@Resource
	private UserService userService;
	@Resource
	private DistrictGlossoryService districtglossoryservice;
	@Resource
	OrderService	orderService;
	@Resource
	UserInterestService userInterestService;
	@Resource
	JtBusinessService	jtBusinessService;

	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public ModelAndView index(HttpServletRequest request, ModelAndView modelview) {
		return home(request, modelview);
	}

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView home(HttpServletRequest request, ModelAndView modelview) {
		RedirectView rv = new RedirectView();
		rv.setUrl(request.getContextPath() + "/portal/index");
		rv.setExposeModelAttributes(false);
		modelview.setView(rv);
		return modelview;
	}
	
	@RequestMapping(value = "/portal/subscriberDetail", method = RequestMethod.GET)
	public ModelAndView subscriberDetail(HttpServletRequest request, ModelAndView modelview, String uid, Integer type) {
		ModelAndView mv = new ModelAndView();
		UserPartnerDetailBo rePartnerDetail = new UserPartnerDetailBo();
		/* 查询智者详细信息 */

//		if (UserType.PERSONAL.getCode().equals(type)) {
			rePartnerDetail = userService.findUserPartnerDetail(uid);
			if (null != rePartnerDetail) {
				Integer province = rePartnerDetail.getProvince();
				if (null != province) {
					rePartnerDetail.setProvinceS(districtglossoryservice.selectNameById(province));
				}
			}
//		} else {
//			rePartnerDetail = (UserPartnerDetailBo) userService.findOrgPartnerDetail(uid);
//			if (null != rePartnerDetail) {
//				Integer province = rePartnerDetail.getProvince();
//				if (null != province) {
//					rePartnerDetail.setProvinceS(districtglossoryservice.selectNameById(province));
//				}
//			}
//		}

		/* 查询该智者的相关成果信息 */
		List<AchievementPartnerListBo> partnerAchievementList = achievementService.findPartnerAchievementList(uid);
		for (int i = 0; i < partnerAchievementList.size(); i++) {
			Integer maturity = partnerAchievementList.get(i).getMaturity();
			if (null != maturity) {
				if (AchievementMaturity.RESEARCH.getCode().equals(maturity)) {
					partnerAchievementList.get(i).setMaturityS(AchievementMaturity.RESEARCH.getDesc());
				} else if (AchievementMaturity.SAMPLE.getCode().equals(maturity)) {
					partnerAchievementList.get(i).setMaturityS(AchievementMaturity.SAMPLE.getDesc());
				} else if (AchievementMaturity.PRIMARYTEST.getCode().equals(maturity)) {
					partnerAchievementList.get(i).setMaturityS(AchievementMaturity.PRIMARYTEST.getDesc());
				} else if (AchievementMaturity.INTERMEDIATETEST.getCode().equals(maturity)) {
					partnerAchievementList.get(i).setMaturityS(AchievementMaturity.INTERMEDIATETEST.getDesc());
				} else if (AchievementMaturity.MASSPRODUCTION.getCode().equals(maturity)) {
					partnerAchievementList.get(i).setMaturityS(AchievementMaturity.MASSPRODUCTION.getDesc());
				}
			}
		}

		/* 查询相关智者信息 */
		List<UserPartnerDetailBo> userPartner = userService.findUserPartner();
		List<UserPartnerDetailBo> reuserPartner = new ArrayList<UserPartnerDetailBo>();
		if (userPartner != null && userPartner.size() >= 6) {
			int index = new Random().nextInt(userPartner.size() - 5);
			reuserPartner.add(userPartner.get(index));
			reuserPartner.add(userPartner.get(index + 1));
			reuserPartner.add(userPartner.get(index + 2));
			reuserPartner.add(userPartner.get(index + 3));
			reuserPartner.add(userPartner.get(index + 4));
		} else {
			reuserPartner.addAll(userPartner);
		}
		for (int i = 0; i < reuserPartner.size(); i++) {
			String personalProfile = reuserPartner.get(i).getPersonalProfile();
			if (null != personalProfile && "" != personalProfile && personalProfile.length() > 40) {
				personalProfile = personalProfile.substring(0, 40) + "... ...";
				reuserPartner.get(i).setPersonalProfile(personalProfile);
			}
		}
		rePartnerDetail.setUid(uid);
		mv.setViewName("/user/subscriberDetail");
		mv.addObject("rePartnerDetail", rePartnerDetail);
		mv.addObject("reuserPartner", reuserPartner);
		partnerAchievementList.size();
		if (partnerAchievementList.size()<=0) {
			mv.addObject("showflg", 0);
		}else{
			mv.addObject("showflg", 1);
		};
		mv.addObject("uid", uid);
		if(StringUtils.isNotBlank(TokenManager.getUserId())) {
		UserInterest userInterest=userInterestService.findByFromUidAndToUid(TokenManager.getUserId(), uid);
		if(userInterest!=null )mv.addObject("interested",true);
		}
		return mv;

	}
	
	
	}
