package com.goafanti.business.service.impl;

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.business.bo.BusinessVarietiesBo;
import com.goafanti.business.bo.VarietiesBo;
import com.goafanti.business.service.BusinessVarietiesService;
import com.goafanti.common.dao.BusinessCategoryMapper;
import com.goafanti.common.dao.ProjectInterestMapper;
import com.goafanti.common.model.BusinessCategory;
import com.goafanti.common.model.ProjectInterest;
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 BusinessVarietiesServiceImpl extends BaseMybatisDao<BusinessCategoryMapper> implements BusinessVarietiesService {
	
	@Autowired
	private BusinessCategoryMapper businessVarietiesMapper;
	@Autowired
	private ProjectInterestMapper	projectInterestMapper;
	
	public int insert(String cname, String superId) {
		BusinessVarietiesBo buv=new BusinessVarietiesBo();
		buv.setId(UUID.randomUUID().toString());
		buv.setCreateTime(new Date());
		buv.setCname(cname);
		buv.setSuperId(superId);
		buv.setDeleteSign(0);
		if (null!=businessVarietiesMapper.getSortMax()) {
			Integer x=Integer.valueOf(businessVarietiesMapper.getSortMax());
			x+=1;
			buv.setSort(x);
		}else {
			buv.setSort(1);
		}
		String str=businessVarietiesMapper.getCidMax(superId);
		String scid=businessVarietiesMapper.getCid(superId);
		if (null==str) {
			buv.setCid(scid+"01");	
		}else {
			int i=Integer.valueOf(str.substring(str.length()-2, str.length()));
			 i=(i%99)+1;
				String s;
				if (i<10) {
					s=str.substring(0,str.length()-2)+"0"+i;
				}else {
					s=str.substring(0,str.length()-2)+i;
				}
				buv.setCid(s);
		}
		
		//判断是否数据库这个ID已经存在
		String n=businessVarietiesMapper.ThanCid(buv.getCid());
		while(StringUtils.isNotBlank(n)) {
			String s1=buv.getCid().substring(0, buv.getCid().length()-2);
			String s2=buv.getCid().substring(buv.getCid().length()-2, buv.getCid().length());
			int i1=Integer.valueOf(s2)+1;
			if(i1<10){
				buv.setCid(s1+"0"+i1);
			}else {
				buv.setCid(s1+i1);
			}
			n=businessVarietiesMapper.ThanCid(buv.getCid());
		}
		
		buv.setLayer(buv.getCid().length()/2-1);
		
		//buv.setCreateId("f31c0d1a-05bc-4f5c-b4a8-95f983d24131");
		if (StringUtils.isNotBlank(TokenManager.getAdminId())) {
			buv.setCreateId(TokenManager.getAdminId());
		}
		buv.setStatus("0");
		BusinessCategory bv=new BusinessCategory();
		BeanUtils.copyProperties(buv,bv);
		bv.getSort();
		return businessVarietiesMapper.insert(bv);
		
	}
	@SuppressWarnings("unchecked")
	@Override
	public Pagination<BusinessCategory> listVarieties(String cname, String superId, Integer layer, String status,
			Integer pNo, Integer pSize) {
		if (pNo == null || pNo < 0) {
			pNo = 1;
		}

		if (pSize == null || pSize < 0 || pSize > 10) {
			pSize = 10;
		} 
		
		
		return (Pagination<BusinessCategory>)findPage("findVarietiesListByPage", "findVarietiesListCount",
				disposeParams(cname,superId,layer,status),pNo,pSize);
	}

	private Map<String, Object> disposeParams(String cname, String superId, Integer layer, String status) {
		Map<String, Object> params = new HashMap<>();

		if (StringUtils.isNotBlank(cname)) {
			params.put("cname", cname);
		}

		if (StringUtils.isNotBlank(superId)) {
			params.put("superId", superId);
		}

		if (null != layer) {
			params.put("layer", layer);
		}
		if (StringUtils.isNotBlank(status)) {
			params.put("status", status);
		}

		
		return params;
	}
	public List<BusinessVarietiesBo> getSuperList() {
		
		return businessVarietiesMapper.getSuperList();
	}
	@Override
	public BusinessVarietiesBo editVarieties(String id) {
		
		return businessVarietiesMapper.editVarieties(id);
	}
	@Override
	public int updateVarietoes(String id, String cname, String superId, String status,String varietiesLogo,Integer sort) {
		BusinessCategory bs=new BusinessCategory();
		bs=businessVarietiesMapper.selectByPrimaryKey(id);
		bs.setCname(cname);
		String str=businessVarietiesMapper.getCidMax(superId);
		String scid=businessVarietiesMapper.getCid(superId);
		if (null==str) {
			bs.setCid(scid+"01");	
		}else {
			int i=Integer.valueOf(str.substring(str.length()-2, str.length()));
			 i=(i%99)+1;
				String s;
				if (i<10) {
					s=str.substring(0,str.length()-2)+"0"+i;
				}else {
					s=str.substring(0,str.length()-2)+i;
				}
				bs.setCid(s);
		}
			//判断是否数据库这个ID已经存在
			checkCid(bs);
		//将子品类名称编号做修改
		String c1=bs.getCid();
		List<BusinessVarietiesBo> list=businessVarietiesMapper.selectSuperid(id);
		for (BusinessVarietiesBo bv : list) {
			String n=bv.getId();
			String w=bv.getCid();
			BusinessCategory bvs=businessVarietiesMapper.selectByPrimaryKey(n);
			String s2=bvs.getCid().substring(w.length()-2, w.length());
			bvs.setCid(c1+s2);
			bvs.setLayer(bvs.getCid().length()/2-1);
			checkCid(bvs);
			businessVarietiesMapper.updateByPrimaryKey(bvs);
		}
		bs.setSuperId(superId);
		bs.setLayer(bs.getCid().length()/2-1);
		bs.setUpdateTime(new Date());
		bs.setStatus(status);
		bs.setSort(sort);
		bs.setVarietiesLogo(varietiesLogo);
		return businessVarietiesMapper.updateByPrimaryKeySelective(bs);
	}
	
	/**
	 * 检查用户是否存在
	 * @param bs
	 */
	private void checkCid(BusinessCategory bs) {
		String n=businessVarietiesMapper.ThanCid(bs.getCid());
		
		while(null!=n) {
			String s1=bs.getCid().substring(0, bs.getCid().length()-2);
			String s2=bs.getCid().substring(bs.getCid().length()-2, bs.getCid().length());
			int i1=Integer.valueOf(s2)+1;
			if(i1<10){
				bs.setCid(s1+"0"+i1);
			}else {
				bs.setCid(s1+i1);
			}
			n=businessVarietiesMapper.ThanCid(bs.getCid());
		}
	}
	@Override
	public int deleteState(String id) {
		BusinessCategory bs=new BusinessCategory();
		bs=businessVarietiesMapper.selectByPrimaryKey(id);
		if (0 ==bs.getDeleteSign()) {
			bs.setDeleteSign(1);
		}else {
			bs.setDeleteSign(0);
		}
		if (null!=bs.getSort()) {
			businessVarietiesMapper.updateFollowSort(bs.getSort());
		}
		return businessVarietiesMapper.updateByPrimaryKey(bs);
	}
	@Override
	public int updateStatus(String id) {
		BusinessCategory bs=new BusinessCategory();
		bs=businessVarietiesMapper.selectByPrimaryKey(id);
		
		if (0==Integer.valueOf(bs.getStatus())) {
			bs.setStatus("1");
		}else {
			bs.setStatus("0");
		}
		
		return businessVarietiesMapper.updateByPrimaryKey(bs);
	}
	@Override
	public int isBeing(String cname) {
		
		return businessVarietiesMapper.getcname(cname);
	}
	@Override
	public int getCountSon(String id) {
		
		return Integer.valueOf(businessVarietiesMapper.getCount(id));
	}
	@Override
	public int getLayer(String superId) {
		return businessVarietiesMapper.getLayer(superId);
	}
	@Override
	public boolean ifLayer(String superId,String id) {
		if(businessVarietiesMapper.getLayer(superId)>2){
			return true;
		}
		if (businessVarietiesMapper.getLayer(superId)>1&&Integer.valueOf(businessVarietiesMapper.getCount(id))>0) {
			return true;
		}
		return false;
	}
	@Override
	public List<VarietiesBo> getVarieties() {
		return businessVarietiesMapper.getVarieties();
	}
	@Override
	public int updateLocation(String id, Integer sort, Integer flag) {
		if (flag==0) {
			Integer x=sort+1;
			BusinessCategory bv=businessVarietiesMapper.selectByPrimaryKey(id);
			BusinessCategory bv2=businessVarietiesMapper.selectBySort(x);
			bv.setSort(bv.getSort()+1);
			businessVarietiesMapper.updateByPrimaryKey(bv);
			bv2.setSort(sort);
			return businessVarietiesMapper.updateByPrimaryKey(bv2);
		}else {
			Integer x=sort-1;
			BusinessCategory bv=businessVarietiesMapper.selectByPrimaryKey(id);
			BusinessCategory bv2=businessVarietiesMapper.selectBySort(x);
			bv.setSort(bv.getSort()-1);
			businessVarietiesMapper.updateByPrimaryKey(bv);
			bv2.setSort(sort);
			return businessVarietiesMapper.updateByPrimaryKey(bv2);
		}

	}
	@Override
	public List<VarietiesBo> portalVarieties() {
		return businessVarietiesMapper.getVarieties();
	}
	@Override
	public int saveInterest(String uid, String objectId, String interest) {
		if (Integer.valueOf(interest)==0) {
			ProjectInterest p=new ProjectInterest();
			p.setId(UUID.randomUUID().toString());
			p.setUid(uid);
			p.setProjectId(objectId);
			p.setCreateTime(new Date());
		return projectInterestMapper.insert(p);
		}else {
		return projectInterestMapper.deleteInterest(uid,objectId);	
		}
	}
	
}


