package com.ym.mec.biz.service.impl;

import com.ym.mec.biz.dal.dao.GoodsCategoryDao;
import com.ym.mec.biz.dal.dao.SubjectGoodsMapperDao;
import com.ym.mec.biz.dal.entity.GoodsCategory;
import com.ym.mec.biz.service.GoodsCategoryService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class GoodsCategoryServiceImpl extends BaseServiceImpl<Integer, GoodsCategory>  implements GoodsCategoryService {
	
	@Autowired
	private GoodsCategoryDao goodsCategoryDao;
	@Autowired
	private SubjectGoodsMapperDao subjectGoodsMapperDao;

	@Override
	public BaseDAO<Integer, GoodsCategory> getDAO() {
		return goodsCategoryDao;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void upsetGoodsCategory(GoodsCategory goodsCategory) {
		goodsCategory.setParentId(0);
		if(goodsCategory.getId() != null){
			goodsCategory.setUpdateTime(new Date());
			goodsCategoryDao.update(goodsCategory);
			//删除关联
			subjectGoodsMapperDao.delByGoodsCategoryId(goodsCategory.getId());
		}else {
			goodsCategoryDao.insert(goodsCategory);
		}
		List<Integer> subjectIds = goodsCategory.getSubjectIds();
		subjectGoodsMapperDao.batchAdd(goodsCategory.getId(),subjectIds);
	}
}