package com.goafanti.portal.controller;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.goafanti.achievement.service.AchievementService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.BaseController;
import com.goafanti.common.enums.AchievementMaturity;
import com.goafanti.common.enums.DemandPortalSearchSignType;
import com.goafanti.common.enums.UserLevel;
import com.goafanti.common.enums.UserType;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.demand.service.DemandService;
import com.goafanti.portal.bo.AchievementSearchListBo;
import com.goafanti.portal.bo.BoutiqueListBo;
import com.goafanti.user.service.OrganizationIdentityService;
import com.goafanti.user.service.UserIdentityService;

@RestController
@RequestMapping(value = "/portal/search")
public class PortalSearchController extends BaseController {
	@Resource
	private AchievementService			achievementService;
	@Resource
	private DemandService				demandService;
	@Resource
	private UserIdentityService			userIdentityService;
	@Resource
	private OrganizationIdentityService	organizationIdentityService;

	/**
	 * 项目搜索
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/achievementList", method = RequestMethod.GET)
	public Result achievementSearchList(HttpServletRequest req,  String keyword,Integer dataCategory,Integer category,
			Integer fieldA, String pageNo, String pageSize,String transferMode, Integer dateSort,String upperPrice,String lowerPrice,
			Integer internationalFlag) {
		Result res = new Result();
		Integer pNo = 1;
		Integer pSize = 20;
		String url=req.getServerName();
		url=url.substring(url.indexOf(".")+1, url.length());
		if(null != dateSort && dateSort != 0 && dateSort != 1){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "时间排序"));
		}
		if (null==dateSort) {
			dateSort=0;
		}
		if (StringUtils.isNumeric(pageSize)) {
			pSize = Integer.parseInt(pageSize);
		}
		if (StringUtils.isNumeric(pageNo)) {
			pNo = Integer.parseInt(pageNo);
		}
		if (null!=upperPrice&&null!=lowerPrice) {
			try{
				new BigDecimal(upperPrice);
				new BigDecimal(lowerPrice);
			}catch (Exception e) {
				res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "金额"));
				return res;
			}
		}
		
		
		Pagination<AchievementSearchListBo> achievementSearchList = achievementService.listAchievementSearchList( url,
				  keyword, dataCategory,category,fieldA, transferMode, pNo, pSize,dateSort,upperPrice,lowerPrice,internationalFlag);
		
		
		List<AchievementSearchListBo> list = (List<AchievementSearchListBo>) achievementSearchList.getList();
		for (int i = 0; i < list.size(); i++) {
			Integer maturity = list.get(i).getMaturity();
			if (null != maturity) {
				if (AchievementMaturity.RESEARCH.getCode().equals(maturity)) {
					list.get(i).setMaturityS(AchievementMaturity.RESEARCH.getDesc());
				} else if (AchievementMaturity.SAMPLE.getCode().equals(maturity)) {
					list.get(i).setMaturityS(AchievementMaturity.SAMPLE.getDesc());
				} else if (AchievementMaturity.PRIMARYTEST.getCode().equals(maturity)) {
					list.get(i).setMaturityS(AchievementMaturity.PRIMARYTEST.getDesc());
				} else if (AchievementMaturity.INTERMEDIATETEST.getCode().equals(maturity)) {
					list.get(i).setMaturityS(AchievementMaturity.INTERMEDIATETEST.getDesc());
				} else if (AchievementMaturity.MASSPRODUCTION.getCode().equals(maturity)) {
					list.get(i).setMaturityS(AchievementMaturity.MASSPRODUCTION.getDesc());
				}
			}
			
			String pic=list.get(i).getTechnicalPictureUrl();//在存在多张图片的情况下,暂时先取第一张
			if(null!= pic) {
				boolean hascomma = pic.contains(",");
				if(hascomma) {
					String[] picurl = pic.split(",");
					list.get(i).setTechnicalPictureUrl(picurl[0]);
				}
			}
		}
		achievementSearchList.setList(list);
		res.setData(achievementSearchList);
		return res;
	}

	/**
	 * 科技需求搜索
	 */
	@RequestMapping(value = "/demandList", method = RequestMethod.GET)
	public Result demandSearchList(HttpServletRequest req,Integer sign, String keyword, Integer industryCategoryA, Integer industryCategoryB,
			Integer demandType, BigDecimal budgetCostLower, BigDecimal budgetCostUpper, String pageNo,
			String pageSize,Integer dateSort) {
		Result res = new Result();
		String url=req.getServerName();
		url=url.substring(url.indexOf(".")+1, url.length());
		Integer pNo = 1;
		Integer pSize = 10;
		if (null == sign) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "查询标记"));
			return res;
		}

		if (!DemandPortalSearchSignType.ALL.getCode().equals(sign)
				&& !DemandPortalSearchSignType.NEGOATION.getCode().equals(sign)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "查询标记"));
			return res;
		}
		if (null==dateSort) {
			dateSort=0;
		}
		if (StringUtils.isNumeric(pageSize)) {
			pSize = Integer.parseInt(pageSize);
		}
		if (StringUtils.isNumeric(pageNo)) {
			pNo = Integer.parseInt(pageNo);
		}
		res.setData(demandService.listDemandSearchList(url,sign, keyword, industryCategoryA, industryCategoryB, demandType,
				budgetCostLower, budgetCostUpper, pNo, pSize,dateSort));
		return res;
	}

	/**
	 * 用户搜索
	 */
	@RequestMapping(value = "/subscriberList", method = RequestMethod.GET)
	public Result subscriberSearchList(HttpServletRequest req,String name, Integer level, Integer type, String field, Integer province,
			Integer city, Integer area,Integer international, String pageNo, String pageSize) {
		Result res = new Result();
		String url=req.getServerName();
		url=url.substring(url.indexOf(".")+1, url.length());
		if (null == type) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "用户类型"));
			return res;
		}

		if (!UserType.PERSONAL.getCode().equals(type) && !UserType.ORGANIZATION.getCode().equals(type)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "用户类型"));
			return res;
		}

		if (null != level && !UserLevel.GENERAL.getCode().equals(level)
				&& !UserLevel.CERTIFIED.getCode().equals(level)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "用户认证标记"));
			return res;
		}
		Integer pNo = 1;
		Integer pSize = 12;
		if (StringUtils.isNumeric(pageSize)) {
			pSize = Integer.parseInt(pageSize);
		}
		if (StringUtils.isNumeric(pageNo)) {
			pNo = Integer.parseInt(pageNo);
		}
		if (UserType.PERSONAL.getCode().equals(type)) {
			res.setData(userIdentityService.listSubscriber(url,name, level, field, province, city, area, international, pNo, pSize));
		} else {
			res.setData(organizationIdentityService.listSubscriber(url,name, level, field, province, city, area, pNo, pSize));
		}
		return res;
	}
	
	/**
	 * 
	 * @param boutiqueType 0 - 专利 , 1 - 技术, 2 - 成果
	 * @param industryCatalog 行业类别
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/boutiqueSearchList",method = RequestMethod.POST)
	public Result boutiqueSearchList(Integer boutiqueType, Integer industryCatalog,String pageNo, String pageSize){
		Result res = new Result();
		if(null == boutiqueType){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "精品类型"));
		}
		if(null == industryCatalog){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","行业类别"));
		}
		Integer pNo = 1;
		Integer pSize = 9;
		if (StringUtils.isNumeric(pageSize)) {
			pSize = Integer.parseInt(pageSize);
		}
		if (StringUtils.isNumeric(pageNo)) {
			pNo = Integer.parseInt(pageNo);
		}
		Pagination<BoutiqueListBo> boutiqueSearchList = null ;
		if(boutiqueType == 0){
			boutiqueSearchList=achievementService.boutiqueSearchList(null, 0, industryCatalog, pNo, pSize);
		}else if(boutiqueType == 1){
			boutiqueSearchList=achievementService.boutiqueSearchList(1, null, industryCatalog, pNo, pSize);
		}else if(boutiqueType == 2){
			boutiqueSearchList=demandService.boutiqueSearchList(industryCatalog, pNo, pSize);
		}
		
		@SuppressWarnings("unchecked")
		List<BoutiqueListBo> BoutiqueListBo = (List<BoutiqueListBo>) boutiqueSearchList.getList();
		
		for (int i = 0; i < BoutiqueListBo.size(); i++) {
			String pic=BoutiqueListBo.get(i).getPictureUrl();//在存在多张图片的情况下,暂时先取第一张
			if(null!= pic) {
				boolean hascomma = pic.contains(",");
				if(hascomma) {
					String[] picurl = pic.split(",");
					BoutiqueListBo.get(i).setPictureUrl(picurl[0]);
				}
			}
		};
		boutiqueSearchList.setList(BoutiqueListBo);
		res.setData(boutiqueSearchList);
		return res;
	}
	
}
