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

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

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) {
		return 	EntityUtil.changeEntityByJSON(MaterialCategoryDTO.class, mapper.selectByCategoryNameAndParentId(categoryName,categoryParentId));
	}

	@Override
	public void insert(MaterialCategoryDTO materialCategoryDTO) {
		materialCategoryDTO.setCategoryId(StringUtil.randomUUID());
		materialCategoryDTO.setCreateTime(new Date());
		materialCategoryDTO.setUpdateTime(materialCategoryDTO.getCreateTime());
		materialCategoryDTO.setStatusFlag(1);
		mapper.insert(EntityUtil.changeEntityByJSON(TabHaobanMaterialCategory.class, materialCategoryDTO));
	}

	@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));
	}

}
