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.apache.poi.poifs.property.Child;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goafanti.business.bo.JtBusinessCategoryBo;
import com.goafanti.business.bo.JtBusinessCategoryTree;
import com.goafanti.business.service.JtBusinessService;
import com.goafanti.common.dao.JtBusinessCategoryMapper;
import com.goafanti.common.dao.JtBusinessProjectMapper;
import com.goafanti.common.model.JtBusinessCategory;
import com.goafanti.common.model.JtBusinessProject;
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 JtBusinessServiceImpl  extends BaseMybatisDao<JtBusinessProjectMapper> implements JtBusinessService{
	@Autowired
	JtBusinessCategoryMapper jtBusinessCategoryMapper;
	@Autowired
	JtBusinessProjectMapper jtBusinessProjectMapper;
	@Override
	public JtBusinessCategory getBusinessCategoryByLayerAndName(Integer layer, String name) {
		// TODO Auto-generated method stub
		return jtBusinessCategoryMapper.getBusinessCategoryByLayerAndName(layer, name);
	}

	@Override
	public List<JtBusinessCategory> getBusinessCategoryBySuperId(String id,Integer size) {
		// TODO Auto-generated method stub
		return jtBusinessCategoryMapper.getBusinessCategoryBySuperId(id,size);
	}

	@Override
	public List<JtBusinessProject> getBusinessProjectByCategoryId(String id,Integer size) {
		// TODO Auto-generated method stub
		return jtBusinessProjectMapper.getBusinessProjectByCategoryId(id,size);
	}

	@Override
	public JtBusinessProject getBusinessProjectDetail(String id) {
		// TODO Auto-generated method stub
		return jtBusinessProjectMapper.selectByPrimaryKey(id);
	}

	@Override
	public JtBusinessCategoryTree getCategoryTree(String id) {
		// TODO Auto-generated method stub
		return jtBusinessCategoryMapper.getCategoryTree(id);
	}
	
	public List<JtBusinessCategoryBo>getCategoryBoList(){
		return jtBusinessCategoryMapper.getCategoryBoList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<JtBusinessProject> getProjects(String topId, String secondId, String name, Integer pageSize,
			Integer pageNo) {
		// TODO Auto-generated method stub
		if (pageNo == null || pageNo < 0) {
			pageNo = 1;
		}

		if (pageSize == null || pageSize < 0 ) {
			pageSize = 10;
		} 
		Map<String, Object> params=disposeParams(topId, secondId, name);
		params.put("ownerId", TokenManager.getUserId());
		
		return (Pagination<JtBusinessProject>)findPage("findJtBusinessProjectByPage","findJtBusinessCountByPage",params,pageNo,pageSize);
	}
	private Map<String, Object> disposeParams(String topId, String secondId,  String name) {
		Map<String, Object> params = new HashMap<>();

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

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

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

		
		return params;
	}

	@Override
	public int insertCategory(JtBusinessCategory jtBusinessCategory) {
		// TODO Auto-generated method stub
//		jtBusinessCategoryMapper.autoIncreaseSort(jtBusinessCategory.getLayer(),jtBusinessCategory.getSort()); 
		if(jtBusinessCategory.getSuperId()!=null && jtBusinessCategory.getLayer().intValue()==1 && jtBusinessCategory.getSuperId().length()>0)return -2;
		String number="BC";
		if(jtBusinessCategory.getLayer().intValue()==2) {
			//2层，先加第一层
			//找到当前品类父级品类
			JtBusinessCategory sp=jtBusinessCategoryMapper.selectByPrimaryKey(jtBusinessCategory.getSuperId());
			if(sp==null || sp.getLayer()!=1) {
				//找到的父级品类层数不是1 或者未找到
				return -2;
			}
			//加前层
			//父级品类层数为1的情况下 第一层编号为父级sort序号
			int size1=sp.getSort();
			if(size1>99)return -1;
			if(size1<10)number+="0";
			number+=size1;
		}
		
		
		//加层
		int size2;
		//通过父级id获取当前层列表
		List<JtBusinessCategory>businessCategories =jtBusinessCategoryMapper.getBusinessCategoryBySuperId(jtBusinessCategory.getSuperId(), null);
		//列表为0,当前层编号size2设置为1，大于0 设置为当前最大sort+1
		if(businessCategories.size()>0)
		 size2=businessCategories.get(businessCategories.size()-1).getSort()+1;
		else size2=1;
		//每层最多99个
		if(size2>99)return -1;
		//增加0
		if(size2<10)number+="0";
		//完成编号
		number+=size2;
		jtBusinessCategory.setNumber(number);
		//设置sort序号
		jtBusinessCategory.setSort(size2);
		jtBusinessCategory.setCreateTime(new Date());
		jtBusinessCategory.setId(UUID.randomUUID().toString());
		return jtBusinessCategoryMapper.insertSelective(jtBusinessCategory);
	}

	@Override
	public int deleteCategoryById(String id) {
		// TODO Auto-generated method stub
		//查找品类
		JtBusinessCategory sp=jtBusinessCategoryMapper.selectByPrimaryKey(id);
		//查找子品类 存在子品类不允许删除
		List<JtBusinessCategory> chidren=jtBusinessCategoryMapper.getBusinessCategoryBySuperId(sp.getId(), null);
		if(chidren.size()>0) {
			return -1;
		}
		List<JtBusinessProject> childrenProject=jtBusinessProjectMapper.getBusinessProjectByCategoryId(id, 100);
		if(childrenProject!= null && childrenProject.size()>0) {
			return -1;
		}
		return jtBusinessCategoryMapper.deleteByPrimaryKey(id);
	}

	@Override
	public JtBusinessCategory getCategoryById(String id) {
		// TODO Auto-generated method stub
		return jtBusinessCategoryMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateCategory(JtBusinessCategory jtBusinessCategory) {
		// TODO Auto-generated method stub
		return jtBusinessCategoryMapper.updateByPrimaryKeySelective(jtBusinessCategory);
	}

	
	
	private Map<String, Object> disposeCategoryParams(String name,Integer layer ) {
		Map<String, Object> params = new HashMap<>();

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

	

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

		
		return params;
	}

	@Override
	public int insertProject(JtBusinessProject jtBusinessProject) {
		// TODO Auto-generated method stub
		jtBusinessProject.setCreateTime(new Date());
		jtBusinessProject.setId(UUID.randomUUID().toString());
		jtBusinessProject.setOwnerId(TokenManager.getUserId());
		List<JtBusinessProject>projects=jtBusinessProjectMapper.findProjectOrderByNumber();
		int cSize = 1;
		if(projects!=null && projects.size()>0)
		{
			String number=projects.get(projects.size()-1).getNumber();
			if(number!=null && number.length()==6)
			{
				String nbString=number.substring(2);
				try {
				cSize=Integer.parseInt(nbString)+1;}catch (Exception e) {
					// TODO: handle exception
					return -1;
				}
			}
			else cSize=1;
		}
		else cSize=1;
	
		String nbString="BP";
		String sizeString="000"+cSize;
		sizeString=sizeString.substring(sizeString.length()-4);
		nbString+=sizeString;
		jtBusinessProject.setNumber(nbString);
		jtBusinessProjectMapper.insertSelective(jtBusinessProject);
		return 1;
	}

	@Override
	public int deleteProjectById(String id) {
		// TODO Auto-generated method stub
		return jtBusinessProjectMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int updateProject(JtBusinessProject jtBusinessProject) {
		// TODO Auto-generated method stub
		return jtBusinessProjectMapper.updateByPrimaryKeySelective(jtBusinessProject);
	}

	@Override
	public List<JtBusinessProject> getProjectsLimit(Integer size) {
		// TODO Auto-generated method stub
		return jtBusinessProjectMapper.findProjectOrderByNumber();
	}



	
}
