package cn.com.countrygarden.bi.main.service.project.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.Put;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.countrygarden.bi.main.dao.project.ProjectJointPlanMapper;
import cn.com.countrygarden.bi.main.dao.project.ProjectMenuMapper;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.project.ProjectJointPlan;
import cn.com.countrygarden.bi.main.model.project.ProjectMenu;
import cn.com.countrygarden.bi.main.service.project.ProjectJointPlanService;
import cn.com.countrygarden.bi.main.service.project.ProjectMenuService;
import cn.com.countrygarden.bi.main.service.project.impl.vo.ProjectMenuVO;
@Service
@Transactional
public class ProjectMenuServiceImpl implements ProjectMenuService{
	private HashMap<String,String> Project = new HashMap<String,String>(){/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

	{
		put("单一项目-对接人身份权利","单一项目-对接人身份权利");
		put("物业费定价测算","物业费定价测算");
		put("客户信息费要求","客户信息费要求");
		put("决策层意见","决策层意见");
		put("招标文件","招标文件");
	}};
	  @Autowired
	  private ProjectMenuMapper mapper;
	@Override
	public List<ProjectMenuVO> search() {
		List<ProjectMenu> list = mapper.selectAll();
		List<ProjectMenuVO> parent = new ArrayList<>();
		for (ProjectMenu projectMenu : list) {
			if(null == projectMenu.getParentId()) {
				ProjectMenuVO pro = new ProjectMenuVO();
				pro.setId(projectMenu.getId());
				pro.setIsOne(projectMenu.getIsOne());
				pro.setName(projectMenu.getName());
				pro.setLevel(1);
				pro.setParentId(projectMenu.getParentId());
				parent.add(pro);
			}
		}
		for (ProjectMenuVO projectMenuVO : parent) {
			List<ProjectMenuVO> plist = new ArrayList<>();
			for (ProjectMenu p : list) {
				if(p.getParentId() == projectMenuVO.getId()) {
					ProjectMenuVO pm = new ProjectMenuVO();
					pm.setId(p.getId());
					pm.setIsOne(p.getIsOne());
					if(Project.containsKey(p.getName())) {
						pm.setIsDoubt(true);
					}
					pm.setName(p.getName());
					pm.setLevel(2);
					pm.setIsOpen(false);
					pm.setParentId(p.getParentId());
					plist.add(pm);
				}
			}
			projectMenuVO.setMenu(plist);
			if(projectMenuVO.getMenu().size() > 0) {
				projectMenuVO.setIsOpen(true);
			}else {
				projectMenuVO.setIsOpen(false);
			}
		}
		return parent;
	}

	@Override
	public Integer save(ProjectMenu p, String userCode) {
		if(null == p.getId()) {
			p.setCreatedDate(new Date());
			p.setCreatedBy(userCode);
			return mapper.insert(p);
		}else {
			p.setUpdatedDate(new Date());
			p.setUpdatedBy(userCode);
			ProjectMenu old = mapper.selectByPrimaryKey(p.getId());
			old.setName(p.getName());
			return mapper.updateByPrimaryKey(old);
		}
		
	}

	@Override
	public Integer delete(Integer id) {
		// TODO Auto-generated method stub
		return mapper.deleteByPrimaryKey(id);
	}

	@Override
	public ProjectMenu findOne(Integer id) {
		
		return mapper.selectByPrimaryKey(id);
	}

}
