package com.goafanti.common.controller;

import com.goafanti.achievement.bo.AchievementListBo;
import com.goafanti.achievement.bo.AchievementPartnerListBo;
import com.goafanti.achievement.service.AchievementService;
import com.goafanti.app.bo.ProjectBo;
import com.goafanti.app.bo.ProjectDetailBo;
import com.goafanti.app.bo.ServiceImages;
import com.goafanti.business.service.BusinessProjectService;
import com.goafanti.common.enums.*;
import com.goafanti.common.service.DistrictGlossoryService;
import com.goafanti.common.service.FieldGlossoryService;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.demand.bo.DemandListBo;
import com.goafanti.demand.service.DemandInterestService;
import com.goafanti.demand.service.DemandService;
import com.goafanti.order.service.OrderService;
import com.goafanti.portal.bo.AchievementPortalDetailBo;
import com.goafanti.portal.bo.AchievementPortalSimilarListBo;
import com.goafanti.portal.bo.DemandPortalDetailBo;
import com.goafanti.portal.bo.DemandPortalSimilarListBo;
import com.goafanti.user.bo.UserPartnerDetailBo;
import com.goafanti.user.service.UserIdentityService;
import com.goafanti.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

@Controller
public class PortalController extends BaseController {
	@Resource
	private FieldGlossoryService glossoryService;
	@Resource
	private AchievementService achievementService;
	@Resource
	private DemandService demandService;
	@Resource
	private UserIdentityService	userIdentityService;
	@Resource
	private DemandInterestService  demandInterestService;
	@Resource
	private UserService userService;
	@Resource
	private DistrictGlossoryService districtglossoryservice;
	@Resource
	private BusinessProjectService	businessProjectService;
	@Resource
	private OrderService	orderService;


	private static final String UNIT = "万元";

	private static final int MULTIPLE = 20;

	private static final String DELIMITER = "/";

	private static final String PRICE_NEGOTIABLE = "价格面议";

	private static final String UNCERTIFY = "未认证";

	@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() + "/admin/login");
		rv.setExposeModelAttributes(false);
		modelview.setView(rv);
		return modelview;
	}

	/**
	 * 科技需求详情
	 */
	@RequestMapping(value = "/portal/technologyTrading/demandDetail", method = RequestMethod.GET)
	public ModelAndView portalDemandDetail(String id, Integer type) {
		ModelAndView mv = new ModelAndView();
		DemandPortalDetailBo demand = null;
		int countDemandInterest = demandInterestService.countDemandInterest(id);
		if(demandInterestService.selectDemandInterestByUidAndDemandId(TokenManager.getUserId(),id) != null) {
			mv.addObject("isInterester", 0);
		}else{
			mv.addObject("isInterester", 1);
		};
		if (UserType.PERSONAL.getCode().equals(type)) {
			demand = demandService.findUserPortalDemandDetail(id);
		} else if (UserType.ORGANIZATION.getCode().equals(type)) {
			demand = demandService.findOrgPortalDemandDetail(id);
		}
		if (null != demand) {
			if (null != demand.getBudgetCost()) {
				demand.setBudgetCostS(demand.getBudgetCost() + UNIT);
			} else {
				demand.setBudgetCostS(PRICE_NEGOTIABLE);
			}
			if (StringUtils.isBlank(demand.getEmployerName())) {
				if (UserLevel.GENERAL.getCode().equals(demand.getLevel())) {
					demand.setEmployerName(UNCERTIFY);
				} else {
					demand.setEmployerName(" ");
				}
			}
			demand.setCountDemandInterest(countDemandInterest);
			Integer industryCategoryA = demand.getIndustryCategoryA();
			boolean hasCategoryA = false;
			boolean hasCategoryB = false;
			if (null != industryCategoryA) {
				List<DemandPortalSimilarListBo> similarList = demandService.findByIndustryCategoryA(industryCategoryA,
						demand.getId());
				if (!similarList.isEmpty()) {
					for (DemandPortalSimilarListBo s : similarList) {
						hasCategoryA = StringUtils.isNotBlank(s.getIndustryCategoryAS());
						hasCategoryB = StringUtils.isNotBlank(s.getIndustryCategoryBS());
						s.setIndustryCategory((hasCategoryA ? s.getIndustryCategoryAS() : "")
								+ (hasCategoryA && hasCategoryB ? DELIMITER : "")
								+ (hasCategoryB ? s.getIndustryCategoryBS() : ""));
						if (null != s.getBudgetCost() && s.getBudgetCost().compareTo(BigDecimal.ZERO) > 0) {
							s.setBudgetCostS(s.getBudgetCost() + UNIT);
						} else {
							s.setBudgetCostS(PRICE_NEGOTIABLE);
						}
					}
				}
				mv.addObject("similarList", similarList);
				hasCategoryA = StringUtils.isNotBlank(demand.getIndustryCategoryAS());
			}
			hasCategoryB = StringUtils.isNotBlank(demand.getIndustryCategoryBS());

			demand.setIndustryCategory((hasCategoryA ? demand.getIndustryCategoryAS() : "")
					+ (hasCategoryA && hasCategoryB ? DELIMITER : "")
					+ (hasCategoryB ? demand.getIndustryCategoryBS() : ""));

			demand.setLocation((StringUtils.isBlank(demand.getProvince()) ? "" : demand.getProvince())
					+ (StringUtils.isBlank(demand.getCity()) ? "" : demand.getCity()));
		}

		String demandpic = demand.getPictureUrl();// 在存在多张图片的情况下,暂时先取第一张
		if (null != demandpic) {
			boolean hascomma = demandpic.contains(",");
			if (hascomma) {
				String[] picurl = demandpic.split(",");
				demand.setPictureUrl(picurl[0]);
			}
		}

		/*--------------查询精品需求---------------*/
		List<DemandListBo> boutiquedemand = demandService.selectDemandList(1);
		List<DemandListBo> reboutiquedemand = new ArrayList<DemandListBo>();
		if (boutiquedemand.size() >= 4) {
			int index = new Random().nextInt(boutiquedemand.size() - 3);
			reboutiquedemand.add(boutiquedemand.get(index));
			reboutiquedemand.add(boutiquedemand.get(index + 1));
			reboutiquedemand.add(boutiquedemand.get(index + 2));
		} else {
			reboutiquedemand.addAll(boutiquedemand);
		}
		;

		for (int i = 0; i < reboutiquedemand.size(); i++) {
			String pic = reboutiquedemand.get(i).getPictureUrl();// 在存在多张图片的情况下,暂时先取第一张
			if (null != pic) {
				boolean hascomma = pic.contains(",");
				if (hascomma) {
					String[] picurl = pic.split(",");
					reboutiquedemand.get(i).setPictureUrl(picurl[0]);
				}
			}
		}
		;

		// 判断登录没登录. 登录了.查找用户喜欢的. 没登录就随机查四个.
		boolean isLogin = TokenManager.isLogin();
		List<DemandListBo> demandlist = demandService.selectDemandList(0);// 查询所有专利
		List<DemandListBo> redemandlist = new ArrayList<DemandListBo>();

		if (demandlist.size() >= 5) {
			int idx = new Random().nextInt(demandlist.size() - 4);
			redemandlist.add(demandlist.get(idx));
			redemandlist.add(demandlist.get(idx + 1));
			redemandlist.add(demandlist.get(idx + 2));
			redemandlist.add(demandlist.get(idx + 3));
		} else {
			redemandlist.addAll(demandlist);
		}

		if (isLogin) {
			List<DemandListBo> customerlikelist = demandService.selectCsutomerLike();// 猜你喜欢专利
			int size;
			if (customerlikelist.size() < redemandlist.size()) {
				size = customerlikelist.size();
			} else {
				size = redemandlist.size();
			}

			for (int i = 0; i < size; i++) {
				redemandlist.set(i, customerlikelist.get(i));
			}
		}

		for (int i = 0; i < redemandlist.size(); i++) {
			Integer demandType = redemandlist.get(i).getDemandType();
			if (null != redemandlist) {
				if (demandType == 1) {
					redemandlist.get(i).setDemandTypeS("技术购买需求");
				} else if (demandType == 2) {
					redemandlist.get(i).setDemandTypeS("技术设备类需求");
				} else if (demandType == 3) {
					redemandlist.get(i).setDemandTypeS("技术方案型需求");
				} else if (demandType == 4) {
					redemandlist.get(i).setDemandTypeS("技术攻关型需求");
				} else if (demandType == 5) {
					redemandlist.get(i).setDemandTypeS("技术人才型需求");
				}
			}
		}
		;

		for (int i = 0; i < redemandlist.size(); i++) {
			String pic = redemandlist.get(i).getPictureUrl();// 在存在多张图片的情况下,暂时先取第一张
			if (null != pic) {
				boolean hascomma = pic.contains(",");
				if (hascomma) {
					String[] picurl = pic.split(",");
					redemandlist.get(i).setPictureUrl(picurl[0]);
				}
			}
		};
		mv.setViewName("/portal/technologyTrading/demandDetail");
		mv.addObject("demand", demand);
		mv.addObject("reboutiquedemand", reboutiquedemand);
		mv.addObject("redemandlist", redemandlist);

		return mv;
	}

	/**
	 * 科技成果详情
	 */
	@SuppressWarnings("unlikely-arg-type")
	@RequestMapping(value = "/portal/technologyTrading/achievementDetail", method = RequestMethod.GET)
	public ModelAndView portalAchievementDetail(String id, Integer type) {
		ModelAndView mv = new ModelAndView();
		AchievementPortalDetailBo achievementdetail = null;
		if (UserType.PERSONAL.getCode().equals(type)) {
			achievementdetail = achievementService.findUserPortalAchievementDetail(id);
		} else if (UserType.ORGANIZATION.getCode().equals(type)) {
			achievementdetail = achievementService.findOrgPortalAchievementDetail(id);
		}
		if (null != achievementdetail) {
			achievementdetail.setId(id);
			achievementdetail.setOwnerType(String.valueOf(type));
			Integer category = achievementdetail.getCategory();
			if (null != category) {
				if (category == 0) {
					achievementdetail.setCategoryS("专利");
				} else if (category == 1) {
					achievementdetail.setCategoryS("软著");
				} else if (category == 2) {
					achievementdetail.setCategoryS("项目");
				} else if (category == 3) {
					achievementdetail.setCategoryS("版权");
				} else if (category == 4) {
					achievementdetail.setCategoryS("工业设计");
				} else if (category == 5) {
					achievementdetail.setCategoryS("配方");
				} else if (category == 6) {
					achievementdetail.setCategoryS("非标");
				}

			}
			;

			Integer fieldA = achievementdetail.getFieldA();
			String field = "";
			boolean a = Boolean.FALSE;
			boolean b = Boolean.FALSE;
			if (null != fieldA) {
				List<AchievementPortalSimilarListBo> similarList = achievementService.findByFieldA(fieldA,
						achievementdetail.getId(), type);
				if (!similarList.isEmpty()) {
					for (AchievementPortalSimilarListBo bo : similarList) {
						if (null != bo.getMaturity()) {
							bo.setMaturityS(bo.getMaturity() * MULTIPLE + "%");
						}

						if (null == bo.getBargainingMode()
								|| AchievementBargainingMode.NEGOTIABLE.getCode().equals(bo.getBargainingMode())) {
							bo.setTransferPriceS(PRICE_NEGOTIABLE);
						} else {
							if (null != bo.getTransferPrice() && bo.getTransferPrice().compareTo(BigDecimal.ZERO) > 0) {
								bo.setTransferPriceS(bo.getTransferPrice() + UNIT);
							} else {
								bo.setTransferPriceS(PRICE_NEGOTIABLE);
							}
						}
						String ulocation = "";
						String uprovince = bo.getUprovince();
						String ucity = bo.getUcity();
						ulocation += (StringUtils.isBlank(uprovince) ? "" : uprovince)
								+ (StringUtils.isBlank(ucity) ? "" : ucity);
						String olocation = "";
						String oprovince = bo.getOprovince();
						String ocity = bo.getOcity();
						olocation += (StringUtils.isBlank(oprovince) ? "" : oprovince)
								+ (StringUtils.isBlank(ocity) ? "" : ocity);
						bo.setLocation(StringUtils.isNotBlank(ulocation) ? ulocation
								: (StringUtils.isNotBlank(olocation) ? olocation : ""));
					}
				}
				mv.addObject("similarList", similarList);
				if (StringUtils.isNotBlank(achievementdetail.getFieldAS())) {
					a = Boolean.TRUE;
				}
			}
			if (StringUtils.isNotBlank(achievementdetail.getFieldBS())) {
				b = Boolean.TRUE;
			}

			field += (a ? achievementdetail.getFieldAS() : "") + (a && b ? DELIMITER : "")
					+ (b ? achievementdetail.getFieldBS() : "");
			achievementdetail.setField(field);
			String technicalPictureUrl = achievementdetail.getTechnicalPictureUrl();
			if (StringUtils.isNotBlank(technicalPictureUrl)) {
				achievementdetail.setPictureList(Arrays.asList(technicalPictureUrl.trim().split(",|，")));
			}
			Integer transfermode = achievementdetail.getTransferMode();
			if (null != transfermode) {
				if (AchievementTransferMode.FULLTRANSFER.getCode().equals(transfermode)) {
					achievementdetail.setTransferModeS(AchievementTransferMode.FULLTRANSFER.getDesc());
				} else if (AchievementTransferMode.PERMISSIONTRANSFER.getCode().equals(transfermode)) {
					achievementdetail.setTransferModeS(AchievementTransferMode.PERMISSIONTRANSFER.getDesc());
				} else if (AchievementTransferMode.TECHNOLOGYSHARES.getCode().equals(transfermode)) {
					achievementdetail.setTransferModeS(AchievementTransferMode.TECHNOLOGYSHARES.getDesc());
				} else if (AchievementTransferMode.EXCLUSIVETECHNOLOGYSHARES.getCode().equals(transfermode)) {
					achievementdetail.setTransferModeS(AchievementTransferMode.EXCLUSIVETECHNOLOGYSHARES.getDesc());
				}
			}

			Integer maturity = achievementdetail.getMaturity();
			if (null != maturity) {
				if (AchievementMaturity.RESEARCH.getCode().equals(maturity)) {
					achievementdetail.setMaturityS(AchievementMaturity.RESEARCH.getDesc());
				} else if (AchievementMaturity.SAMPLE.getCode().equals(maturity)) {
					achievementdetail.setMaturityS(AchievementMaturity.SAMPLE.getDesc());
				} else if (AchievementMaturity.PRIMARYTEST.getCode().equals(maturity)) {
					achievementdetail.setMaturityS(AchievementMaturity.PRIMARYTEST.getDesc());
				} else if (AchievementMaturity.INTERMEDIATETEST.getCode().equals(maturity)) {
					achievementdetail.setMaturityS(AchievementMaturity.INTERMEDIATETEST.getDesc());
				} else if (AchievementMaturity.MASSPRODUCTION.getCode().equals(maturity)) {
					achievementdetail.setMaturityS(AchievementMaturity.MASSPRODUCTION.getDesc());
				}
			}

			String pic = achievementdetail.getTechnicalPictureUrl();// 在存在多张图片的情况下,暂时先取第一张
			if (null != pic) {
				boolean hascomma = pic.contains(",");
				if (hascomma) {
					String[] picurl = pic.split(",");
					achievementdetail.setTechnicalPictureUrl(picurl[0]);
				}
			}
			String location = "";
			String province = achievementdetail.getProvince();
			String city = achievementdetail.getCity();
			location += (StringUtils.isBlank(province) ? "" : province) + (StringUtils.isBlank(city) ? "" : city);

			if (null == achievementdetail.getBargainingMode()
					|| AchievementBargainingMode.NEGOTIABLE.equals(achievementdetail.getBargainingMode())) {
				achievementdetail.setTransferPriceS(PRICE_NEGOTIABLE);
			} else {
				if (null != achievementdetail.getTransferPrice()) {
					achievementdetail.setTransferPriceS(achievementdetail.getTransferPrice() + UNIT);
				} else {
					achievementdetail.setTransferPriceS(PRICE_NEGOTIABLE);
				}
			}

			achievementdetail.setLocation(location);

		}
		// 查询精品成果
		List<AchievementListBo> boutiqueachievement = achievementService.getAchievement(1);
		List<AchievementListBo> reboutiqueachievement = new ArrayList<AchievementListBo>();
		if (boutiqueachievement.size() >= 4) {
			int index = new Random().nextInt(boutiqueachievement.size() - 3);
			reboutiqueachievement.add(boutiqueachievement.get(index));
			reboutiqueachievement.add(boutiqueachievement.get(index + 1));
			reboutiqueachievement.add(boutiqueachievement.get(index + 2));
		} else {
			reboutiqueachievement.addAll(boutiqueachievement);
		}
		;

		for (int i = 0; i < boutiqueachievement.size(); i++) {
			String pic = boutiqueachievement.get(i).getTechnicalpictureurl();// 在存在多张图片的情况下,暂时先取第一张
			if (null != pic) {
				boolean hascomma = pic.contains(",");
				if (hascomma) {
					String[] picurl = pic.split(",");
					boutiqueachievement.get(i).setTechnicalpictureurl(picurl[0]);
				}
			}
		}
		;
		// 判断登录没登录. 登录了.查找用户喜欢的. 没登录就随机查四个.
		boolean isLogin = TokenManager.isLogin();
		List<AchievementListBo> achievementlist = achievementService.getAchievement(0);// 查询所有专利
		List<AchievementListBo> reachievementlist = new ArrayList<AchievementListBo>();

		if (achievementlist.size() >= 5) {
			int idx = new Random().nextInt(achievementlist.size() - 4);
			reachievementlist.add(achievementlist.get(idx));
			reachievementlist.add(achievementlist.get(idx + 1));
			reachievementlist.add(achievementlist.get(idx + 2));
			reachievementlist.add(achievementlist.get(idx + 3));
		} else {
			reachievementlist.addAll(achievementlist);
		}

		if (isLogin) {
			List<AchievementListBo> customerlikelist = achievementService.getPerhapLike();// 猜你喜欢专利
			int size;
			if (customerlikelist.size() < reachievementlist.size()) {
				size = customerlikelist.size();
			} else {
				size = reachievementlist.size();
			}

			for (int i = 0; i < size; i++) {
				reachievementlist.set(i, customerlikelist.get(i));
			}
		}
		/*----------------------------------------------------------*/

		for (int i = 0; i < reachievementlist.size(); i++) {
			Integer fieldA = reachievementlist.get(i).getFieldA();
			String field = "";
			boolean a = Boolean.FALSE;
			if (null != fieldA && null != achievementdetail) {
				List<AchievementPortalSimilarListBo> similarList = achievementService.findByFieldA(fieldA,
						achievementdetail.getId(), type);
				if (!similarList.isEmpty()) {
					for (AchievementPortalSimilarListBo bo : similarList) {
						if (null != bo.getMaturity()) {
							bo.setMaturityS(bo.getMaturity() * MULTIPLE + "%");
						}

						if (null == bo.getBargainingMode()
								|| AchievementBargainingMode.NEGOTIABLE.getCode().equals(bo.getBargainingMode())) {
							bo.setTransferPriceS(PRICE_NEGOTIABLE);
						} else {
							if (null != bo.getTransferPrice() && bo.getTransferPrice().compareTo(BigDecimal.ZERO) > 0) {
								bo.setTransferPriceS(bo.getTransferPrice() + UNIT);
							} else {
								bo.setTransferPriceS(PRICE_NEGOTIABLE);
							}
						}
					}
				}
				mv.addObject("similarList", similarList);
				if (StringUtils.isNotBlank(reachievementlist.get(i).getFieldAS())) {
					a = Boolean.TRUE;
				}

				String fieldAs = glossoryService.getName(fieldA);
				reachievementlist.get(i).setFieldAS(fieldAs);
			}

			field += (a ? reachievementlist.get(i).getFieldAS() : "");
			reachievementlist.get(i).setField(field);

			Integer ownertyp = reachievementlist.get(i).getOwnerType();
			if (null != ownertyp) {
				if (ownertyp == 0) {
					reachievementlist.get(i).setOwnerTypeS("个人所有");
				} else if (ownertyp == 1) {
					reachievementlist.get(i).setOwnerTypeS("组织所有");
				}
			}
			Integer transfermode = reachievementlist.get(i).getTransferMode();
			if (null != transfermode) {
				if (AchievementTransferMode.FULLTRANSFER.getCode().equals(transfermode)) {
					reachievementlist.get(i).setTransferModeS(AchievementTransferMode.FULLTRANSFER.getDesc());
				} else if (AchievementTransferMode.PERMISSIONTRANSFER.getCode().equals(transfermode)) {
					reachievementlist.get(i).setTransferModeS(AchievementTransferMode.PERMISSIONTRANSFER.getDesc());
				} else if (AchievementTransferMode.TECHNOLOGYSHARES.getCode().equals(transfermode)) {
					reachievementlist.get(i).setTransferModeS(AchievementTransferMode.TECHNOLOGYSHARES.getDesc());
				} else if (AchievementTransferMode.EXCLUSIVETECHNOLOGYSHARES.getCode().equals(transfermode)) {
					reachievementlist.get(i)
							.setTransferModeS(AchievementTransferMode.EXCLUSIVETECHNOLOGYSHARES.getDesc());
				}
			}
			Integer maturity = reachievementlist.get(i).getMaturity();
			if (null != maturity) {
				if (AchievementMaturity.RESEARCH.getCode().equals(maturity)) {
					reachievementlist.get(i).setMaturityS(AchievementMaturity.RESEARCH.getDesc());
				} else if (AchievementMaturity.SAMPLE.getCode().equals(maturity)) {
					reachievementlist.get(i).setMaturityS(AchievementMaturity.SAMPLE.getDesc());
				} else if (AchievementMaturity.PRIMARYTEST.getCode().equals(maturity)) {
					reachievementlist.get(i).setMaturityS(AchievementMaturity.PRIMARYTEST.getDesc());
				} else if (AchievementMaturity.INTERMEDIATETEST.getCode().equals(maturity)) {
					reachievementlist.get(i).setMaturityS(AchievementMaturity.INTERMEDIATETEST.getDesc());
				} else if (AchievementMaturity.MASSPRODUCTION.getCode().equals(maturity)) {
					reachievementlist.get(i).setMaturityS(AchievementMaturity.MASSPRODUCTION.getDesc());
				}
			}
			;
			String pic = reachievementlist.get(i).getTechnicalpictureurl();// 在存在多张图片的情况下,暂时先取第一张
			if (null != pic) {
				boolean hascomma = pic.contains(",");
				if (hascomma) {
					String[] picurl = pic.split(",");
					reachievementlist.get(i).setTechnicalpictureurl(picurl[0]);
				}
			}

		}

		mv.setViewName("/portal/technologyTrading/achievementDetail");
		mv.addObject("achievementdetail", achievementdetail);
		mv.addObject("reboutiqueachievement", reboutiqueachievement);
		mv.addObject("reachievementlist", reachievementlist);
		return mv;
	}

	/**
	 * 订单确认页(成果专利)
	 */
	@RequestMapping(value = "/portal/technologyTrading/achievementOrder", method = RequestMethod.GET)
	public ModelAndView portalAchievementOrder(String id, Integer type) {
		ModelAndView mv = new ModelAndView();
		AchievementPortalDetailBo achievementdetail = null;
		achievementdetail = achievementService.findUserPortalAchievementDetail(id);
		if (null != achievementdetail) {
			achievementdetail.setId(id);
			achievementdetail.setOwnerType(String.valueOf(type));
			Integer category = achievementdetail.getCategory();
			if (null != category) {
				if (category == 0) {
					achievementdetail.setCategoryS("专利");
				} else if (category == 1) {
					achievementdetail.setCategoryS("软著");
				} else if (category == 2) {
					achievementdetail.setCategoryS("项目");
				} else if (category == 3) {
					achievementdetail.setCategoryS("版权");
				} else if (category == 4) {
					achievementdetail.setCategoryS("工业设计");
				} else if (category == 5) {
					achievementdetail.setCategoryS("配方");
				} else if (category == 6) {
					achievementdetail.setCategoryS("非标");
				}

			}
			;

			Integer transfermode = achievementdetail.getTransferMode();
			if (null != transfermode) {
				if (AchievementTransferMode.FULLTRANSFER.getCode().equals(transfermode)) {
					achievementdetail.setTransferModeS(AchievementTransferMode.FULLTRANSFER.getDesc());
				} else if (AchievementTransferMode.PERMISSIONTRANSFER.getCode().equals(transfermode)) {
					achievementdetail.setTransferModeS(AchievementTransferMode.PERMISSIONTRANSFER.getDesc());
				} else if (AchievementTransferMode.TECHNOLOGYSHARES.getCode().equals(transfermode)) {
					achievementdetail.setTransferModeS(AchievementTransferMode.TECHNOLOGYSHARES.getDesc());
				} else if (AchievementTransferMode.EXCLUSIVETECHNOLOGYSHARES.getCode().equals(transfermode)) {
					achievementdetail.setTransferModeS(AchievementTransferMode.EXCLUSIVETECHNOLOGYSHARES.getDesc());
				}
			}

		}

		// 查询精品成果
		List<AchievementListBo> boutiqueachievement = achievementService.getAchievement(1);
		List<AchievementListBo> reboutiqueachievement = new ArrayList<AchievementListBo>();
		if (boutiqueachievement.size() >= 4) {
			int index = new Random().nextInt(boutiqueachievement.size() - 3);
			reboutiqueachievement.add(boutiqueachievement.get(index));
			reboutiqueachievement.add(boutiqueachievement.get(index + 1));
			reboutiqueachievement.add(boutiqueachievement.get(index + 2));
		} else {
			reboutiqueachievement.addAll(boutiqueachievement);
		}
		;

		for (int i = 0; i < boutiqueachievement.size(); i++) {
			String pic = boutiqueachievement.get(i).getTechnicalpictureurl();// 在存在多张图片的情况下,暂时先取第一张
			if (null != pic) {
				boolean hascomma = pic.contains(",");
				if (hascomma) {
					String[] picurl = pic.split(",");
					boutiqueachievement.get(i).setTechnicalpictureurl(picurl[0]);
				}
			}
		}
		;

		// 判断登录没登录. 登录了.查找用户喜欢的. 没登录就随机查四个.
		boolean isLogin = TokenManager.isLogin();
		List<AchievementListBo> achievementlist = achievementService.getAchievement(0);// 查询所有专利
		List<AchievementListBo> reachievementlist = new ArrayList<AchievementListBo>();

		if (achievementlist.size() >= 5) {
			int idx = new Random().nextInt(achievementlist.size() - 4);
			reachievementlist.add(achievementlist.get(idx));
			reachievementlist.add(achievementlist.get(idx + 1));
			reachievementlist.add(achievementlist.get(idx + 2));
			reachievementlist.add(achievementlist.get(idx + 3));
		} else {
			reachievementlist.addAll(achievementlist);
		}

		if (isLogin) {
			List<AchievementListBo> customerlikelist = achievementService.getPerhapLike();// 猜你喜欢专利
			int size;
			if (customerlikelist.size() < reachievementlist.size()) {
				size = customerlikelist.size();
			} else {
				size = reachievementlist.size();
			}

			for (int i = 0; i < size; i++) {
				reachievementlist.set(i, customerlikelist.get(i));
			}
		}

		/*----------------------------------------------------------*/

		for (int i = 0; i < reachievementlist.size(); i++) {
			Integer fieldA = reachievementlist.get(i).getFieldA();
			String field = "";
			boolean a = Boolean.FALSE;
			if (null != fieldA && null != achievementdetail) {
				List<AchievementPortalSimilarListBo> similarList = achievementService.findByFieldA(fieldA,
						achievementdetail.getId(), type);
				if (!similarList.isEmpty()) {
					for (AchievementPortalSimilarListBo bo : similarList) {
						if (null != bo.getMaturity()) {
							bo.setMaturityS(bo.getMaturity() * MULTIPLE + "%");
						}

						if (null == bo.getBargainingMode()
								|| AchievementBargainingMode.NEGOTIABLE.getCode().equals(bo.getBargainingMode())) {
							bo.setTransferPriceS(PRICE_NEGOTIABLE);
						} else {
							if (null != bo.getTransferPrice() && bo.getTransferPrice().compareTo(BigDecimal.ZERO) > 0) {
								bo.setTransferPriceS(bo.getTransferPrice() + UNIT);
							} else {
								bo.setTransferPriceS(PRICE_NEGOTIABLE);
							}
						}
					}
				}
				mv.addObject("similarList", similarList);
				if (StringUtils.isNotBlank(reachievementlist.get(i).getFieldAS())) {
					a = Boolean.TRUE;
				}

				String fieldAs = glossoryService.getName(fieldA);
				reachievementlist.get(i).setFieldAS(fieldAs);
			}

			field += (a ? reachievementlist.get(i).getFieldAS() : "");
			reachievementlist.get(i).setField(field);

			Integer ownertyp = reachievementlist.get(i).getOwnerType();
			if (null != ownertyp) {
				if (ownertyp == 0) {
					reachievementlist.get(i).setOwnerTypeS("个人所有");
				} else if (ownertyp == 1) {
					reachievementlist.get(i).setOwnerTypeS("组织所有");
				}
			}

			Integer transfermode = reachievementlist.get(i).getTransferMode();
			if (null != transfermode) {
				if (AchievementTransferMode.FULLTRANSFER.getCode().equals(transfermode)) {
					reachievementlist.get(i).setTransferModeS(AchievementTransferMode.FULLTRANSFER.getDesc());
				} else if (AchievementTransferMode.PERMISSIONTRANSFER.getCode().equals(transfermode)) {
					reachievementlist.get(i).setTransferModeS(AchievementTransferMode.PERMISSIONTRANSFER.getDesc());
				} else if (AchievementTransferMode.TECHNOLOGYSHARES.getCode().equals(transfermode)) {
					reachievementlist.get(i).setTransferModeS(AchievementTransferMode.TECHNOLOGYSHARES.getDesc());
				} else if (AchievementTransferMode.EXCLUSIVETECHNOLOGYSHARES.getCode().equals(transfermode)) {
					reachievementlist.get(i)
							.setTransferModeS(AchievementTransferMode.EXCLUSIVETECHNOLOGYSHARES.getDesc());
				}
			}

			Integer maturity = reachievementlist.get(i).getMaturity();
			if (null != maturity) {
				if (AchievementMaturity.RESEARCH.getCode().equals(maturity)) {
					reachievementlist.get(i).setMaturityS(AchievementMaturity.RESEARCH.getDesc());
				} else if (AchievementMaturity.SAMPLE.getCode().equals(maturity)) {
					reachievementlist.get(i).setMaturityS(AchievementMaturity.SAMPLE.getDesc());
				} else if (AchievementMaturity.PRIMARYTEST.getCode().equals(maturity)) {
					reachievementlist.get(i).setMaturityS(AchievementMaturity.PRIMARYTEST.getDesc());
				} else if (AchievementMaturity.INTERMEDIATETEST.getCode().equals(maturity)) {
					reachievementlist.get(i).setMaturityS(AchievementMaturity.INTERMEDIATETEST.getDesc());
				} else if (AchievementMaturity.MASSPRODUCTION.getCode().equals(maturity)) {
					reachievementlist.get(i).setMaturityS(AchievementMaturity.MASSPRODUCTION.getDesc());
				}
			};
			String pic = reachievementlist.get(i).getTechnicalpictureurl();// 在存在多张图片的情况下,暂时先取第一张
			if (null != pic) {
				boolean hascomma = pic.contains(",");
				if (hascomma) {
					String[] picurl = pic.split(",");
					reachievementlist.get(i).setTechnicalpictureurl(picurl[0]);
				}
			}
		}


		mv.setViewName("/portal/technologyTrading/achievementOrder");
		mv.addObject("achievementdetail", achievementdetail);
		mv.addObject("reachievementlist", reachievementlist);
		return mv;
	}


	/**
	 * 下单页面(需求)
	 */
	@RequestMapping(value = "/portal/technologyTrading/demandOrder", method = RequestMethod.GET)
	public ModelAndView portalDemandOrder(String id, Integer type) {
		ModelAndView mv = new ModelAndView();
		DemandPortalDetailBo demand = null;
		demand = demandService.findUserPortalDemandDetail(id);
		if (null != demand) {
			if (null != demand.getBudgetCost()) {
				demand.setBudgetCostS(demand.getBudgetCost() + UNIT);
			} else {
				demand.setBudgetCostS(PRICE_NEGOTIABLE);
			}
			if (StringUtils.isBlank(demand.getEmployerName())) {
				if (UserLevel.GENERAL.getCode().equals(demand.getLevel())) {
					demand.setEmployerName(UNCERTIFY);
				} else {
					demand.setEmployerName(" ");
				}
			}
			Integer industryCategoryA = demand.getIndustryCategoryA();
			boolean hasCategoryA = false;
			boolean hasCategoryB = false;
			if (null != industryCategoryA) {
				List<DemandPortalSimilarListBo> similarList = demandService.findByIndustryCategoryA(industryCategoryA,
						demand.getId());
				if (!similarList.isEmpty()) {
					for (DemandPortalSimilarListBo s : similarList) {
						hasCategoryA = StringUtils.isNotBlank(s.getIndustryCategoryAS());
						hasCategoryB = StringUtils.isNotBlank(s.getIndustryCategoryBS());
						s.setIndustryCategory((hasCategoryA ? s.getIndustryCategoryAS() : "")
								+ (hasCategoryA && hasCategoryB ? DELIMITER : "")
								+ (hasCategoryB ? s.getIndustryCategoryBS() : ""));
						if (null != s.getBudgetCost() && s.getBudgetCost().compareTo(BigDecimal.ZERO) > 0) {
							s.setBudgetCostS(s.getBudgetCost() + UNIT);
						} else {
							s.setBudgetCostS(PRICE_NEGOTIABLE);
						}
					}
				}
				mv.addObject("similarList", similarList);
				hasCategoryA = StringUtils.isNotBlank(demand.getIndustryCategoryAS());
			}
			hasCategoryB = StringUtils.isNotBlank(demand.getIndustryCategoryBS());

			demand.setIndustryCategory((hasCategoryA ? demand.getIndustryCategoryAS() : "")
					+ (hasCategoryA && hasCategoryB ? DELIMITER : "")
					+ (hasCategoryB ? demand.getIndustryCategoryBS() : ""));

			demand.setLocation((StringUtils.isBlank(demand.getProvince()) ? "" : demand.getProvince())
					+ (StringUtils.isBlank(demand.getCity()) ? "" : demand.getCity()));
		}

		String demandpic = demand.getPictureUrl();// 在存在多张图片的情况下,暂时先取第一张
		if (null != demandpic) {
			boolean hascomma = demandpic.contains(",");
			if (hascomma) {
				String[] picurl = demandpic.split(",");
				demand.setPictureUrl(picurl[0]);
			}
		}

		/*--------------查询精品需求---------------*/
		List<DemandListBo> boutiquedemand = demandService.selectDemandList(1);
		List<DemandListBo> reboutiquedemand = new ArrayList<DemandListBo>();
		if (boutiquedemand.size() >= 4) {
			int index = new Random().nextInt(boutiquedemand.size() - 3);
			reboutiquedemand.add(boutiquedemand.get(index));
			reboutiquedemand.add(boutiquedemand.get(index + 1));
			reboutiquedemand.add(boutiquedemand.get(index + 2));
		} else {
			reboutiquedemand.addAll(boutiquedemand);
		}
		;

		for (int i = 0; i < reboutiquedemand.size(); i++) {
			String pic = reboutiquedemand.get(i).getPictureUrl();// 在存在多张图片的情况下,暂时先取第一张
			if (null != pic) {
				boolean hascomma = pic.contains(",");
				if (hascomma) {
					String[] picurl = pic.split(",");
					reboutiquedemand.get(i).setPictureUrl(picurl[0]);
				}
			}
		}
		;

		// 判断登录没登录. 登录了.查找用户喜欢的. 没登录就随机查四个.
				boolean isLogin = TokenManager.isLogin();
				List<DemandListBo> demandlist = demandService.selectDemandList(0);// 查询所有专利
				List<DemandListBo> redemandlist = new ArrayList<DemandListBo>();

				if (demandlist.size() >= 5) {
					int idx = new Random().nextInt(demandlist.size() - 4);
					redemandlist.add(demandlist.get(idx));
					redemandlist.add(demandlist.get(idx + 1));
					redemandlist.add(demandlist.get(idx + 2));
					redemandlist.add(demandlist.get(idx + 3));
				} else {
					redemandlist.addAll(demandlist);
				}

				if (isLogin) {
					List<DemandListBo> customerlikelist = demandService.selectCsutomerLike();// 猜你喜欢专利
					int size;
					if (customerlikelist.size() < redemandlist.size()) {
						size = customerlikelist.size();
					} else {
						size = redemandlist.size();
					}

					for (int i = 0; i < size; i++) {
						redemandlist.set(i, customerlikelist.get(i));
					}
				}

		for (int i = 0; i < redemandlist.size(); i++) {
			Integer demandType = redemandlist.get(i).getDemandType();
			if (null != redemandlist) {
				if (demandType == 1) {
					redemandlist.get(i).setDemandTypeS("技术购买需求");
				} else if (demandType == 2) {
					redemandlist.get(i).setDemandTypeS("技术设备类需求");
				} else if (demandType == 3) {
					redemandlist.get(i).setDemandTypeS("技术方案型需求");
				} else if (demandType == 4) {
					redemandlist.get(i).setDemandTypeS("技术攻关型需求");
				} else if (demandType == 5) {
					redemandlist.get(i).setDemandTypeS("技术人才型需求");
				}
			}
		}
		;

		for (int i = 0; i < redemandlist.size(); i++) {
			String pic = redemandlist.get(i).getPictureUrl();// 在存在多张图片的情况下,暂时先取第一张
			if (null != pic) {
				boolean hascomma = pic.contains(",");
				if (hascomma) {
					String[] picurl = pic.split(",");
					redemandlist.get(i).setPictureUrl(picurl[0]);
				}
			}
		};
		mv.setViewName("/portal/technologyTrading/demandOrder");
		mv.addObject("demand", demand);
		mv.addObject("reboutiquedemand", reboutiquedemand);
		mv.addObject("redemandlist", redemandlist);
		return mv;
	}

	@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);
		return mv;

	}

	/**
	 * 服务
	 */
	@RequestMapping(value = "/portal/services", method = RequestMethod.GET)
	public ModelAndView services(HttpServletRequest request, ModelAndView modelview) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("");
		//获取首页轮播图
		mv.addObject("serviceImages",new ServiceImages().getImagers());
		return mv;
	}
	/**
	 * 服务 权威咨询师
	 */
	@RequestMapping(value = "/portal/services/consultant", method = RequestMethod.GET)
	public ModelAndView consultant(HttpServletRequest request, ModelAndView modelview) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("");
		return mv;
	}

	/**
	 * 项目详情
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "portal/service/projectDetails", method = RequestMethod.GET)
	public ModelAndView projectDetail(HttpServletRequest request, ModelAndView modelview,String id) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("portal/service/projectDetails");
		//获取项目详情
		ProjectDetailBo p=businessProjectService.ProjectDetail(id);
		if (StringUtils.isNotBlank(p.getIntroduce()))
			p.setIntroduce(p.getIntroduce().replace("\n","<br>"));
		if (StringUtils.isNotBlank(p.getValueEffect()))
		p.setValueEffect(p.getValueEffect().replace("\n", "<br>"));
		if (StringUtils.isNotBlank(p.getClientSize()))
		p.setClientSize(p.getClientSize().replace("\n", "<br>"));

		mv.addObject("projectDetail",p);
		//获取3条推荐
		Pagination<ProjectBo> ps=businessProjectService.recommendProjectList(null,3);
		List<ProjectBo> list=(List<ProjectBo>) ps.getList();
		mv.addObject("recommend", list);
		return mv;
	}

	/**
	 * 咨询师详情
	 */
	@RequestMapping(value = "/portal/services/consultantDetail", method = RequestMethod.GET)
	public ModelAndView consultantDetail(HttpServletRequest request, ModelAndView modelview,String id,Integer pSize ) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("");
		//获取咨询师详情
		mv.addObject("consultant", userIdentityService.consultantDetail( id));
		//获取咨询师列表（传入显示数）
		mv.addObject("recommend",userIdentityService.consultantList(null,pSize));
		return mv;
	}



}
