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

import com.ym.mec.biz.dal.dao.ChargeTypeSubjectMapperDao;
import com.ym.mec.biz.dal.entity.ChargeTypeSubjectMapper;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.page.QueryInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ym.mec.biz.dal.dao.ChargeTypeDao;
import com.ym.mec.biz.dal.entity.ChargeType;
import com.ym.mec.biz.service.ChargeTypeService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class ChargeTypeServiceImpl extends BaseServiceImpl<Integer, ChargeType>  implements ChargeTypeService {
	
	@Autowired
	private ChargeTypeDao chargeTypeDao;
	@Autowired
	private ChargeTypeSubjectMapperDao chargeTypeSubjectMapperDao;

	@Override
	public BaseDAO<Integer, ChargeType> getDAO() {
		return chargeTypeDao;
	}


	@Override
	@Transactional(rollbackFor = Exception.class)
	public void upSet(ChargeType chargeType) {
		if(chargeType.getId() != null){
			//修改
			chargeType.setUpdateTime(new Date());
			chargeTypeDao.update(chargeType);
			//删除关联的声部
			chargeTypeSubjectMapperDao.deleteByChargeTypeId(chargeType.getId());
			//批量插入关联的声部
			chargeTypeSubjectMapperDao.batchAdd(chargeType.getSubjectIds(),chargeType.getId());
		}else {
			//新增
			chargeTypeDao.insert(chargeType);
			//批量插入关联的声部
			chargeTypeSubjectMapperDao.batchAdd(chargeType.getSubjectIds(),chargeType.getId());
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void del(Integer chargeTypeId) {
		//删除关联的声部
		chargeTypeSubjectMapperDao.deleteByChargeTypeId(chargeTypeId);
		chargeTypeDao.delete(chargeTypeId);
	}
}