package com.goafanti.business.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goafanti.app.bo.ProjectBo;
import com.goafanti.app.bo.ProjectDetailBo;
import com.goafanti.business.bo.BusinessProjectBo;
import com.goafanti.business.bo.ProjectSizeBo;
import com.goafanti.business.service.BusinessProjectService;
import com.goafanti.common.dao.BusinessProjectMapper;
import com.goafanti.common.dao.BusinessVarietiesMapper;
import com.goafanti.common.dao.ProjectInterestMapper;
import com.goafanti.common.dao.ProjectSizeMapper;
import com.goafanti.common.model.BusinessProject;
import com.goafanti.common.model.BusinessVarieties;
import com.goafanti.common.model.ProjectSize;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;

@Service
public class BusinessProjectServiceImpl extends BaseMybatisDao<BusinessProjectMapper> implements BusinessProjectService {
	@Autowired
	BusinessProjectMapper businessProjectMapper;
	@Autowired
	BusinessVarietiesMapper businessVarietiesMapper;
	@Autowired
	ProjectSizeMapper projectSizeMapper;
	@Autowired
	ProjectInterestMapper projectInterestMapper;
	
	@Override
	public int insert(String bname, String cid, String country, String province, String city, String district,String boutique) {
		BusinessProject bp=new BusinessProject();
		bp.setId(UUID.randomUUID().toString());
		bp.setBname(bname);
		bp.setCid(cid);
		bp.setCountry(country);
		if (1!=Integer.valueOf(country)) {
			bp.setProvince(province);
			bp.setCity(city);
			bp.setDistrict(district);
		}
		bp.setDeleteSign(0);
		//bp.setCreateId("f31c0d1a-05bc-4f5c-b4a8-95f983d24131");
		if (StringUtils.isNotBlank(TokenManager.getAdminId())) {
			bp.setCreateId(TokenManager.getAdminId());
		}
		bp.setCreateTime(new Date());
		bp.setStatus("0");
		bp.setBoutique(Integer.valueOf(boutique));
		bp.setFirstPayment(new BigDecimal("0"));
		return businessProjectMapper.insert(bp);
	}
	@SuppressWarnings("unchecked")
	@Override
	public Pagination<BusinessProjectBo> listProject(String bname, String cid, String country,String province,String city,String district, String activityFlag,
			String status, Integer pNo, Integer pSize) {
		if (pNo == null || pNo < 0) {
			pNo = 1;
		}

		if (pSize == null || pSize < 0 ) {
			pSize = 10;
		} 	
		Pagination<BusinessProjectBo> pt=(Pagination<BusinessProjectBo>)findPage("findProjectListByPage", "findProjectListCount",
				disposeParams(bname,  cid,  country, province, city, district,  activityFlag,
						 status),pNo,pSize);
		List<BusinessProjectBo> list=(List<BusinessProjectBo>) pt.getList();
		for (BusinessProjectBo o : list) {
			o.setCname(getAllCname(o.getCid()));
		}
		return pt;
	}
	private Map<String, Object> disposeParams(String bname, String cid, String country,String province,String city,String district, String activityFlag,
			String status) {
		Map<String, Object> params = new HashMap<>();
		if (StringUtils.isNotBlank(bname)) {
			params.put("bname", bname);
		}
		if (StringUtils.isNotBlank(cid)) {
			params.put("cid", cid);
		}
		if (StringUtils.isNotBlank(country)) {
			params.put("country", country);
		}
		if (StringUtils.isNotBlank(province)) {
			params.put("province", province);
		}
		if (StringUtils.isNotBlank(city)) {
			params.put("city", city);
		}
		if (StringUtils.isNotBlank(district)) {
			params.put("district", district);
		}
		if (StringUtils.isNotBlank(activityFlag)) {
			params.put("activityFlag", activityFlag);
		}
		if (StringUtils.isNotBlank(status)) {
			params.put("status", status);
		}
		return params;
	}
	@Override
	public int getBnamecount(String bname) {
		
		return businessProjectMapper.getBnamecount(bname);
	}
	@Override
	public int deleteProject(String id) {
		BusinessProject bp=businessProjectMapper.selectByPrimaryKey(id);
		if (bp.getDeleteSign()==0) {
			bp.setDeleteSign(1);
		}else {
			bp.setDeleteSign(0);
		}
		
		return businessProjectMapper.updateByPrimaryKey(bp);
	}
	@Override
	public int updateStopProject(String id) {
		BusinessProject bp=businessProjectMapper.selectByPrimaryKey(id);
		if (Integer.valueOf(bp.getStatus())==0) {
			bp.setStatus("1");
		}else {
			bp.setStatus("0");
		}
		
		return businessProjectMapper.updateByPrimaryKey(bp);
	}
	@Override
	public BusinessProjectBo orgProjects(String id) {
		BusinessProject bp=businessProjectMapper.selectByPrimaryKey(id);
		BusinessProjectBo bpo=new BusinessProjectBo();
		BeanUtils.copyProperties(bp,bpo);
		String cname=getAllCname(bpo.getCid());
		String createId=bpo.getCreateId();
		bpo.setCreateName(businessProjectMapper.getCreateName(createId));
		bpo.setCname(cname);
		return bpo;
	}
	@Override
	public ProjectDetailBo orgProject(String id) {
		ProjectDetailBo pd=businessProjectMapper.selectAppProjectDetail(id);
		int i=projectInterestMapper.checkUidAndDid(id,TokenManager.getUserId());
		
		if (TokenManager.isLogin()&&i>0) {
			pd.setInterest("1");
		}else {
			pd.setInterest("0");
		}
		pd.setProjectSize(projectSizeMapper.selectPidList(id));
		return pd;
	}
	
	@Override
	public List<Map<String, String>> getAllCnames(Integer flag) {
		List<Map<String, String>> list=new ArrayList<Map<String, String>>();
		List<String> listcid=businessVarietiesMapper.getListId();
		for (String s : listcid) {
			Map<String, String> map=new HashMap<>(); 
			String cname=getAllCname(s);
			map.put("id", s);
			map.put("cname", cname);
			if (flag==1) {
				if (StringUtils.isBlank(cname)) {
					map.put("cname", "平台超级业务品类");
				}
				list.add(map);
			}else {
				if (StringUtils.isNotBlank(cname)) {
					list.add(map);
				}
			}
		}
		return list;
	}
	/**
	 * 递归获取上级来获取全路径
	 * @param s	ID
	 * @return  路径
	 */
	private String getAllCname(String s) {
		String cid=businessVarietiesMapper.getThisCid(s);
		String cname=businessVarietiesMapper.getThisCname(cid);
		BusinessVarieties bv=businessVarietiesMapper.selectByPrimaryKey(s);
		String sid=bv.getSuperId();
		if (cid.length()>4) {
			if(bv.getSuperId().equals(bv.getId())){
				return "";
			}
			String w=getAllCname(sid)+"-"+cname;
			return w;
		}else{
			return cid.length()>3?cname:"";
		}
		
	}
	
	
	
	@Override
	public int updateProject(BusinessProject s) {
		BusinessProject bp=businessProjectMapper.selectByPrimaryKey(s.getId());
		TheGinseng(s, bp);
		return businessProjectMapper.updateByPrimaryKey(bp);
	}
	private void TheGinseng(BusinessProject s, BusinessProject bp) {
		
		if (StringUtils.isNotBlank(s.getBname())) {
			bp.setBname(s.getBname());
		}
		if (StringUtils.isNotBlank(s.getCid())) {
			bp.setCid(s.getCid());
		}
		if (StringUtils.isNotBlank(s.getCountry())) {
			bp.setCountry(s.getCountry());
		}
		if (StringUtils.isNotBlank(s.getProvince())) {
			bp.setProvince(s.getProvince());
			bp.setCity(s.getCity());
			bp.setDistrict(s.getDistrict());
		}
		if (StringUtils.isNotBlank(s.getCity())) {
			bp.setCity(s.getCity());
			bp.setDistrict(s.getDistrict());
		}
		if (StringUtils.isNotBlank(s.getDistrict())) {
			bp.setDistrict(s.getDistrict());
		}

		bp.setPrice(s.getPrice());
		bp.setOffset(s.getOffset());
		bp.setActivityFlag(s.getActivityFlag());
		bp.setMemberPrice(s.getMemberPrice());
		bp.setActivityPrice(s.getActivityPrice());
		bp.setIntroduce(s.getIntroduce());
		bp.setValueEffect(s.getValueEffect());
		bp.setClientSize(s.getClientSize());
		bp.setMinLogo(s.getMinLogo());
		bp.setMaxLogo(s.getMaxLogo());
		bp.setBoutique(s.getBoutique());
		bp.setProjectUrl(s.getProjectUrl());
		bp.setFirstPayment(s.getFirstPayment());
		
		if (null!=(s.getStatus())) {
			bp.setStatus(s.getStatus());
		}
		bp.setUpdateTime(new Date());
	}
	@Override
	public int addProjectSize(ProjectSize ps) {
		ps.setId(UUID.randomUUID().toString());
		if (StringUtils.isNotBlank(TokenManager.getAdminId())) {
		ps.setCreateId(TokenManager.getAdminId());
		}
		//ps.setCreateId("f31c0d1a-05bc-4f5c-b4a8-95f983d24131");
		ps.setCreateTime(new Date());
		ps.setStatus(0);
		return projectSizeMapper.insert(ps);
	}
	@Override
	public int updateOrgProjectSize(ProjectSize ps) {
		ProjectSize psz=projectSizeMapper.selectByPrimaryKey(ps.getId());
		projectSizeConfiguration(ps, psz);
		System.out.println(psz);
		return projectSizeMapper.updateByPrimaryKeySelective(psz);
	}
	private void projectSizeConfiguration(ProjectSize ps, ProjectSize psz) {
		if (StringUtils.isNotBlank(ps.getPid())) {
			psz.setId(ps.getPid());
		}
		if (StringUtils.isNotBlank(ps.getPname())) {
			psz.setPname(ps.getPname());
		}
		if (null!=ps.getPrice()) {
			psz.setPrice(ps.getPrice());
		}
		if (null!=(ps.getOffset())) {
			psz.setOffset(ps.getOffset());
		}
		if (null!=(ps.getActivityFlag())) {
			psz.setActivityFlag(ps.getActivityFlag());
		}
		if (null!=(ps.getMemberPrice())) {
			psz.setMemberPrice(ps.getMemberPrice());
		}
		if (null!=(ps.getActivityPrice())) {
			psz.setActivityPrice(ps.getActivityPrice());
		}
		if (null!=(ps.getStatus())) {
			psz.setStatus(ps.getStatus());
		}
		psz.setUpdateTime(new Date());
	}
	@SuppressWarnings("unchecked")
	@Override
	public Pagination<ProjectSizeBo>  listProjectSize(String pid,Integer pNo,Integer pSize) {
		if (pNo == null || pNo < 0) {
			pNo = 1;
		}

		if (pSize == null || pSize < 0 || pSize > 10) {
			pSize = 10;
		} 
		Map<String, Object>map=new HashMap<>();
		map.put("pid", pid);
			
		
		Pagination<ProjectSizeBo> pt=(Pagination<ProjectSizeBo>)findPage("findProjectSizeListByPage", "findProjectSizeListCount",
				map,pNo,pSize);
		return pt;
	}
	@Override
	public ProjectSizeBo getProjectSize(String id) {
		return projectSizeMapper.selectByBusinessId(id);
	}
	@Override
	public int deleteProjectSize(String id) {
		
		return projectSizeMapper.deleteByPrimaryKey(id);
	}
	@Override
	public int updateSotpProjectSize(String id) {
		ProjectSize ps=projectSizeMapper.selectByPrimaryKey(id);
			int i=ps.getStatus();
			if (i==0) {
				ps.setStatus(1);
			}else {
				ps.setStatus(0);
			}
		return projectSizeMapper.updateByPrimaryKey(ps);
	}
	@Override
	public boolean WhetherRepeat(ProjectSize ps) {
		List<ProjectSizeBo> list=projectSizeMapper.getgetPnamecount(ps.getPname());
		
		for (ProjectSizeBo p : list) {
			if (p.getPid().equals(ps.getPid())) {
				return true;
			}
		}
		return false;
	}
	@Override
	public boolean judgeStatus(ProjectSize ps) {
		 BusinessProject bp=businessProjectMapper.selectByPrimaryKey(ps.getPid());
		if (Integer.valueOf(bp.getStatus())==1) {
			return true;
		}else {
			return false;
		}
		
	}
	@Override
	public boolean judgeBeing(BusinessProject s) {
		BusinessProject bp=businessProjectMapper.selectByPrimaryKey(s.getId());
		BusinessProject bp1=businessProjectMapper.selectByPrimaryBname(s.getBname());
		if (null==bp||null==bp1||bp.getId().equals(bp1.getId())) {
			return false;
		}else {
			return true;
		}
		
	}
	@SuppressWarnings("unchecked")
	@Override
	public Pagination<ProjectBo> getBusinessProject(String id,Integer pNo,Integer pSize) {
		if (pNo == null || pNo <= 0) {
			pNo = 1;
		}

		if (pSize == null || pSize < 0 || pSize > 10) {
			pSize = 10;
		}
		Map<String, Object> params = new HashMap<>();
		if (StringUtils.isNotBlank(id)) {
			params.put("id", id);
		}
		
		Pagination<ProjectBo> data =  (Pagination<ProjectBo>)findPage("getBusinessProject", "getBusinessProjectCount",
				params,pNo,pSize);
		return data;
		
	}
	@SuppressWarnings("unchecked")
	@Override
	public Pagination<ProjectBo> recommendProjectList(Integer pNo, Integer pSize) {
		if (pNo == null || pNo <= 0) {
			pNo = 1;
		}
		if (pSize == null || pSize < 0 || pSize > 10) {
			pSize = 10;
		}
		Map<String, Object> params = new HashMap<>();
		return (Pagination<ProjectBo>)findPage("findRecommendProjectListByPage", "findRecommendProjectListCount",
				params,pNo,pSize);
	}
	@Override
	public boolean checkProject(String id) {
		if (businessProjectMapper.checkProject(id)>0) {
			return true;
		}
		return false;
	}
	@SuppressWarnings("unchecked")
	@Override
	public Pagination<ProjectBo> portalRecommendList(Integer pNo, Integer pSize) {
		Map<String, Object> params = new HashMap<>();
		return (Pagination<ProjectBo>)findPage("findRecommendProjectListByPage", "findRecommendProjectListCount",
				params,pNo,pSize);
	}
	@SuppressWarnings("unchecked")
	@Override
	public Pagination<ProjectBo> portalBusinessProject(String id, Integer pNo, Integer pSize) {
		Map<String, Object> params = new HashMap<>();
		if (StringUtils.isNotBlank(id)) {
			params.put("id", id);
		}
		Pagination<ProjectBo> data =  (Pagination<ProjectBo>)findPage("getBusinessProject", "getBusinessProjectCount",
				params,pNo,pSize);
		return data;
	
	}
	
	@Override
	public ProjectDetailBo ProjectDetail(String id) {
		ProjectDetailBo pd=businessProjectMapper.selectAppProjectDetail(id);
		if (StringUtils.isNotBlank(TokenManager.getUserId())&&projectInterestMapper.checkUidAndDid(id,TokenManager.getUserId())>0) {
			pd.setInterest("1");
		}else {
			pd.setInterest("0");
		}
		pd.setProjectSize(projectSizeMapper.selectPidList(id));
		return pd;
	
	}
	
	@Override
	public List<ProjectBo> portalRecommend() {
		return businessProjectMapper.selectServiceProjectDetail();
	}
	
	@Override
	public List<BusinessProject> selectBusinessProjectByName(String businessName,String cid) {
		return businessProjectMapper.selectBusinessProjectByName(businessName,cid);
	}
	
	@Override
	public List<BusinessVarieties> selectBusinessProjectCatalog() {
		
		return businessProjectMapper.selectBusinessProjectCatalog();
	}
	
}

