package org.cs.mgr.product.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.cs.core.service.impl.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.cs.util.Pager;
import org.cs.util.StringUtil;
import org.cs.mgr.product.service.IMgmProductCategoryService;
import org.cs.mgr.product.dao.IMgmProductCategoryDao;
import org.cs.mgr.product.model.MgmProductCategory;

/**
 * 
 * @ClassName: IMgmProductCategoryService
 * @Description:  
 * @author: sunny_shi
 * @date: 2018-06-09 13:29:09
 */
@Service("mgmProductCategoryService")
public class MgmProductCategoryService extends BaseService<MgmProductCategory> implements IMgmProductCategoryService{
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private IMgmProductCategoryDao mgmProductCategoryDao;
	
	@Override
	@Transactional(readOnly = true)
	public Pager getMgmProductCategorys(Pager page){
		return this.mgmProductCategoryDao.queryMgmProductCategorys(page);
	}
	
	/**
	 * 自动生成
	 * @Title: delMgmProductCategory 
	 * @Description: TODO
	 * @param id						主键id
	 * @return
	 * @return: boolean
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean delMgmProductCategory(Integer id){
	
		MgmProductCategory mgmProductCategory = this.mgmProductCategoryDao.findById(id);
		/*List<MgmProductCategory> mlist = this.mgmProductCategoryDao.nextCategory(mgmProductCategory.getId());
		for(MgmProductCategory mpc: mlist){
			if(mpc != null){
				mpc.setIsDelete(true);				
				this.mgmProductCategoryDao.update(mpc);
			}
		}
		if(mgmProductCategory != null){
			mgmProductCategory.setIsDelete(true);				
			this.mgmProductCategoryDao.update(mgmProductCategory);
		}*/
		delCategory(mgmProductCategory.getId());
		return true;
	}
	
	public void delCategory(Integer id){
	
		MgmProductCategory mgmProductCategory = this.mgmProductCategoryDao.findById(id);
		if(mgmProductCategory != null){
			mgmProductCategory.setIsDelete(true);				
			this.mgmProductCategoryDao.update(mgmProductCategory);
		}
		List<MgmProductCategory> mlist = this.mgmProductCategoryDao.nextCategory(mgmProductCategory.getId());
		for(MgmProductCategory mpc: mlist){
			if(mpc != null){
				delCategory(mpc.getId());
			}
		}
	}
	
	/**
	 * 自动生成
	 * @Title: addMgmProductCategory 
	 * @Description: TODO
	 * @param mgmProductCategory						对象
	 * @return
	 * @return: boolean
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean addMgmProductCategory(MgmProductCategory mgmProductCategory){
	
		if(mgmProductCategory != null){
			this.mgmProductCategoryDao.insert(mgmProductCategory);
		}
		return true;
	}
	
	
	/**
	 *  自动生成
	 * @Title: updateMgmProductCategory 
	 * @Description: TODO
	 * @param mgmProductCategory						对象
	 * @return
	 * @return: boolean
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean updateMgmProductCategory(MgmProductCategory mgmProductCategory){
	
		MgmProductCategory mgmProductCategory2 = this.mgmProductCategoryDao.findById(mgmProductCategory.getId());
		if(mgmProductCategory2 != null){
			mgmProductCategory.mergeTo(mgmProductCategory2);
			this.mgmProductCategoryDao.update(mgmProductCategory2);
		}
		
		return true;
	}
	
	@Override
	public List<MgmProductCategory> nextCategory(Integer id){
		return this.mgmProductCategoryDao.nextCategory(id);
	}
	
	@Override
	public List<MgmProductCategory> firstCategory(){
		return this.mgmProductCategoryDao.firstCategory();
	}
	@Override
	public List<MgmProductCategory> queryAll(){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("isdelete", false);
		return (List<MgmProductCategory>) this.mgmProductCategoryDao.findByHql("from MgmProductCategory where isDelete=:isdelete order by gcSort asc", params);
	}
	
	@Override
	public List<MgmProductCategory> queryShowCategory(Map<String, String> map){
		return this.mgmProductCategoryDao.queryShowCategory(map);
	}
	
}
