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.admin.bo.AdminVideoBo;
import com.goafanti.admin.bo.KnowledgeBaseBo;
import com.goafanti.admin.service.AdminVideoService;
import com.goafanti.admin.service.CommunityService;
import com.goafanti.business.bo.JtBusinessProjectResult;
import com.goafanti.business.service.JtBusinessService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.enums.AchievementMaturity;
import com.goafanti.common.model.JtBusinessProject;
import com.goafanti.common.model.JtKnowledgeBase;
import com.goafanti.common.model.JtVideo;
import com.goafanti.common.model.News;
import com.goafanti.common.model.Policy;
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.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.demand.service.DemandInterestService;
import com.goafanti.demand.service.DemandService;
import com.goafanti.news.service.NewsService;
import com.goafanti.news.service.PolicyService;
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;
import com.goafanti.voucher.bo.JtVoucherListBo;
import com.goafanti.voucher.service.VoucherService;

@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
	private OrderService	orderService;
	@Resource
	private UserInterestService userInterestService;
	@Resource
	private JtBusinessService	jtBusinessService;
	@Resource
	private VoucherService	voucherService;
	
	@Resource
	private AdminVideoService adminVideoService;
	@Resource
	private CommunityService communityService ;
	@Resource
	private PolicyService policyService;
	@Resource
	private NewsService newsService;
	
	@RequestMapping(value = "/skjt", method = RequestMethod.GET)
	public ModelAndView jkjt(HttpServletRequest request, ModelAndView modelview) {
		modelview.setViewName("/skjt/s_index");
		return modelview;
	}
	
	
	@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;

	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/portal/invRegister",method = RequestMethod.GET)
	public ModelAndView portalActivityDetails(ModelAndView modelAndView){
		modelAndView.setViewName("/portal/invRegister");
		//获得项目抵用券
		List<JtVoucherListBo> vlist=voucherService.selectActivityVoucher();
		for (JtVoucherListBo j : vlist) {
			if (j!=null&&j.getName().length()>12) {
				j.setName(j.getName().substring(0, 11)+"…");
			}
		}
		modelAndView.addObject("vouchers",vlist);
		//高企服务
		List<JtBusinessProject> glist=jtBusinessService.getKJListByIds(2, 0, 2);
		for (JtBusinessProject j : glist) {
			if (j.getName().length()>5) {
				j.setName(j.getName().substring(0, 4)+"…");
			}
		}
		modelAndView.addObject( "gqfw",glist);
		//随机热门项目
		Pagination<JtBusinessProjectResult> p=jtBusinessService.getProjects(null,null,null,null, 3,1,null,2,null,1,null,null,null);
		List<JtBusinessProjectResult> plist=(List<JtBusinessProjectResult>) p.getList();
		for (JtBusinessProjectResult j : plist) {
			if (j.getName().length()>8) {
				j.setName(j.getName().substring(0, 7)+"…");
			}
		}
		modelAndView.addObject("hotProject",plist);
		//军民融合
		List<JtBusinessProject> jlist=jtBusinessService.getKJListByIds(3, 0, 3);
		for (JtBusinessProject j : jlist) {
			if (j.getName().length()>12) {
				j.setName(j.getName().substring(0, 11)+"…");
			}
		}
		modelAndView.addObject( "jmrh",jlist);
		//知识产权
		List<JtBusinessProject> zlist=jtBusinessService.getKJListByIds(1, 0, 3);
		for (JtBusinessProject j : zlist) {
			if (j.getName().length()>8) {
				j.setName(j.getName().substring(0, 7)+"…");
			}
		}
		modelAndView.addObject( "zscq",zlist);
		//政府扶持
		List<JtBusinessProject> zfList=jtBusinessService.getProjectByTag("政府扶持", 4);
		for (JtBusinessProject j : zlist) {
			if (j.getName().length()>11) {
				j.setName(j.getName().substring(0, 10)+"…");
			}
		}
		modelAndView.addObject( "zffc",zfList);
		
		
		return modelAndView;
	}
	
	@RequestMapping(value="/portal/video",method = RequestMethod.GET)
	public ModelAndView video(ModelAndView modelAndView){
		modelAndView.setViewName("/portal/video/video");
		return modelAndView;
	}
	
	//获得所有视频数据
	@RequestMapping(value="/portal/getVideoList", method = RequestMethod.GET)
	@ResponseBody
	public Result getVideoList(JtVideo video, Integer pageNo, Integer pageSize){
		Result result = new Result();
		result.setData(adminVideoService.getVideoList(video, pageNo, pageSize,2));
		return result;
	}
	
	//跳转视频详情
	@RequestMapping(value="/open/protal/videoDetail", method = RequestMethod.GET)
	public ModelAndView videoDetail(ModelAndView modelAndView,String id){
		//获得视频信息
		JtVideo v = new JtVideo();
		v.setId(id);
		v.setStatus(1);
		JtVideo video = adminVideoService.getVideoById(v);
		if(null != video){
			modelAndView.addObject("videoPlay",video);
			//获得所有视频列表
			modelAndView.addObject("videoList",adminVideoService.getAllVideoExcludeId(id));
			modelAndView.setViewName("/portal/video/videoDetail");
		}else{
			modelAndView.setViewName("/portal/video/video");
		}
		return modelAndView;
	}
	
	//跳转知识社区
	@SuppressWarnings("unchecked")
	@RequestMapping("/portal/special")
	public ModelAndView special(ModelAndView modelAndView){
		modelAndView.setViewName("/portal/special/special");
		//视频
		AdminVideoBo video =  adminVideoService.getIndexVideo(2);//知识社区首页
		if(null != video ){
			modelAndView.addObject("video", video);
		}
		//政策
		List<Policy> policies = (List<Policy>) policyService.searchPolicy(null, 2, null, null, "web_community", 1, 1, null)
				.getList();
		if(policies.size()>0){
			Policy p = policies.get(0);
			if(StringUtils.isNotBlank(p.getTitleImg()) && p.getTitleImg().indexOf(",") > 0){
				p.setTitleImg(p.getTitleImg().substring(0,p.getTitleImg().indexOf(",")));
			}
			modelAndView.addObject("policy", p);
		}
		//资讯
		List<News> newsList = (List<News>) newsService
				.listNews(99, null, null, null, null, null, null, 1, 4, "2", null, null,null).getList();
		modelAndView.addObject("news", newsList);//资讯
		return modelAndView;
	}
	
	//跳转知识社区详情
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/portal/special/specialDetail", method = RequestMethod.GET)
	public ModelAndView specialDetail(ModelAndView modelAndView,String id){
		//新增一个阅读量
		communityService.addRead(id);
		//视频
		AdminVideoBo video =  adminVideoService.getIndexVideo(2);//知识社区首页
		if(null != video ){
			modelAndView.addObject("video", video);
		}
		//政策
		List<Policy> policies = (List<Policy>) policyService.searchPolicy(null, 2, null, null, "web_community", 1, 1, null)
				.getList();
		if(policies.size()>0){
			Policy p = policies.get(0);
			if(StringUtils.isNotBlank(p.getTitleImg()) && p.getTitleImg().indexOf(",") > 0){
				p.setTitleImg(p.getTitleImg().substring(0,p.getTitleImg().indexOf(",")));
			}
			modelAndView.addObject("policy", p);
		}
		//资讯
		List<News> newsList = (List<News>) newsService
				.listNews(99, null, null, null, null, null, null, 1, 4, "2", null, null,null).getList();
		modelAndView.addObject("news", newsList);//资讯
		//单个社区知识查看
		JtKnowledgeBase o = new JtKnowledgeBase();
		o.setId(id);
		JtKnowledgeBase kb = communityService.getCommunintyById(o);
		if(null != kb){
			modelAndView.setViewName("/portal/special/specialDetail");
			modelAndView.addObject("kb", kb);
		}else{
			modelAndView.setViewName("/portal/special/special");
		}
		return modelAndView;
	}
	
	//新增阅读
	@RequestMapping(value="/portal/special/addRead",method = RequestMethod.GET)
	@ResponseBody
	public Result addRead(String id){
		Result result = new Result();
		result.setData(communityService.addRead(id));
		return result;
	}
	//新增评论
	@RequestMapping(value="/portal/special/addComment",method = RequestMethod.GET)
	@ResponseBody
	public Result addComment(String id){
		Result result = new Result();
		result.setData(communityService.addComment(id));
		return result;
	}
	
	//新增点赞
	@RequestMapping(value="/portal/special/addStar",method = RequestMethod.GET)
	@ResponseBody
	public Result addStar(String id){
		Result result = new Result();
		result.setData(communityService.addStar(id));
		return result;
	}
	
	//获得知识社区的列表数据
	@RequestMapping("/portal/special/specialList")
	@ResponseBody
	public Result specialList(KnowledgeBaseBo knowledgeBase , Integer pageSize, Integer pageNo){
		Result res = new Result();
		//查看更多
		res.setData(communityService.getJtKnowledgeBaseList(knowledgeBase, pageNo, pageSize, 2));
		return res;
	}
}
