package com.gic.haoban.manage.service.service.impl;

import java.util.Date;
import java.util.List;

import com.gic.haoban.manage.api.dto.qdto.MaterialCategorySortQDTO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gic.haoban.common.utils.EntityUtil;
import com.gic.haoban.common.utils.StringUtil;
import com.gic.haoban.manage.api.dto.MaterialCategoryDTO;
import com.gic.haoban.manage.service.dao.mapper.TabHaobanMaterialCategoryMapper;
import com.gic.haoban.manage.service.entity.TabHaobanMaterialCategory;
import com.gic.haoban.manage.service.service.MaterialCategoryService;

@Service
public class MaterialCategoryServiceImpl implements MaterialCategoryService{
	
	@Autowired
	private TabHaobanMaterialCategoryMapper mapper;

	@Override
	public MaterialCategoryDTO hasCategoryNameExsit(String categoryName, String categoryParentId,String wxEnterpriseId) {
		return 	EntityUtil.changeEntityByJSON(MaterialCategoryDTO.class, mapper.selectByCategoryNameAndParentId(categoryName,categoryParentId,wxEnterpriseId));
	}

	@Override
	public void insert(MaterialCategoryDTO materialCategoryDTO) {
		materialCategoryDTO.setCategoryId(StringUtil.randomUUID());
		materialCategoryDTO.setCreateTime(new Date());
		materialCategoryDTO.setUpdateTime(materialCategoryDTO.getCreateTime());
		materialCategoryDTO.setStatusFlag(1);
		materialCategoryDTO.setSortNum(-1);
		mapper.insert(EntityUtil.changeEntityByJSON(TabHaobanMaterialCategory.class, materialCategoryDTO));
		// 修改排序
		List<MaterialCategoryDTO> list = this.listByParentCategory(materialCategoryDTO.getCategoryParentId()) ;
		for(int i=0;i<list.size();i++) {
			MaterialCategoryDTO item = list.get(i) ;
			item.setSortNum(i+1);
		}
		this.mapper.updateSortNum(list);
	}

	@Override
	public List<MaterialCategoryDTO> listCategory(String wxEnterpriseId) {
		return EntityUtil.changeEntityListByJSON(MaterialCategoryDTO.class, mapper.listCategory(wxEnterpriseId));
	}

	@Override
	public MaterialCategoryDTO selectMaterialCategoryById(String categoryId) {
		return EntityUtil.changeEntityByJSON(MaterialCategoryDTO.class, mapper.selectByPrimaryKey(categoryId));
	}

	@Override
	public void editCategory(MaterialCategoryDTO materialCategoryDTO) {
		materialCategoryDTO.setUpdateTime(new Date());
		mapper.updateByPrimaryKeySelective(EntityUtil.changeEntityByJSON(TabHaobanMaterialCategory.class, materialCategoryDTO));
	}

	@Override
	public List<MaterialCategoryDTO> listByParentCategory(String categoryId) {
		return EntityUtil.changeEntityListByJSON(MaterialCategoryDTO.class, mapper.listByParentCategory(categoryId));
	}

	@Override
	public void materialSortCategory(MaterialCategorySortQDTO qdto) {
		String categoryId = qdto.getCategoryId();
		Integer sortNum = qdto.getSortNum();
		MaterialCategoryDTO dto = this.selectMaterialCategoryById(categoryId) ;
		String parentId = dto.getCategoryParentId();
		List<MaterialCategoryDTO> list = this.listByParentCategory(parentId) ;
		int oldSortNum = 0 ;
		for(int i=0;i<list.size();i++) {
			MaterialCategoryDTO item = list.get(i) ;
			if(item.getCategoryId().equals(categoryId)) {
				oldSortNum = i+1 ;
			}
		}
		int index = 1 ;
		int maxIndex = sortNum + 1 ;
		if(CollectionUtils.isNotEmpty(list)) {
			for (int i=0;i<list.size();i++) {
				int x = i+1 ;
				MaterialCategoryDTO item = list.get(i) ;
				if(item.getCategoryId().equals(categoryId)) {
					item.setSortNum(sortNum);
					continue;
				}
				// 往后排
				if(sortNum > oldSortNum) {
					if(x > sortNum) {
						item.setSortNum(maxIndex);
						maxIndex++ ;
					}else {
						item.setSortNum(index);
						index++ ;
					}
					continue;
				}
				// 往前排
				if(x >= sortNum) {
					item.setSortNum(maxIndex);
					maxIndex++ ;
				}else {
					item.setSortNum(index);
					index++ ;
				}
			}
		}
		this.mapper.updateSortNum(list) ;
	}
}
