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

import com.ym.mec.biz.dal.dao.MemberRankOrganizationFeeMapperDao;
import com.ym.mec.biz.dal.entity.MemberFeeSetting;
import com.ym.mec.biz.dal.entity.MemberRankOrganizationFeeMapper;
import com.ym.mec.biz.service.MemberFeeSettingService;
import com.ym.mec.biz.service.MemberRankOrganizationFeeMapperService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
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;

@Service
public class MemberRankOrganizationFeeMapperServiceImpl extends BaseServiceImpl<Integer, MemberRankOrganizationFeeMapper>  implements MemberRankOrganizationFeeMapperService {

	@Autowired
	private MemberRankOrganizationFeeMapperDao memberRankOrganizationFeeMapperDao;
	@Autowired
	private MemberFeeSettingService memberFeeSettingService;

	@Override
	public BaseDAO<Integer, MemberRankOrganizationFeeMapper> getDAO() {
		return memberRankOrganizationFeeMapperDao;
	}

    @Override
	@Transactional(rollbackFor = Exception.class)
    public void add(MemberRankOrganizationFeeMapper memberRankOrganizationFeeMapper) {
		//一个分部只能关联一个会员
		MemberRankOrganizationFeeMapper feeMapper = memberRankOrganizationFeeMapperDao.findByOrganIdAndRankId(memberRankOrganizationFeeMapper.getOrganId(),memberRankOrganizationFeeMapper.getMemberRankSettingId());
		if(feeMapper != null){
			throw new BizException("操作失败: 该分部会员收费标准已存在");
		}
		MemberFeeSetting memberFeeSetting = memberRankOrganizationFeeMapper.getMemberFeeSetting();
		if(memberFeeSetting == null){
			throw new BizException("操作失败: 请设置收费标准");
		}
		memberFeeSettingService.insert(memberFeeSetting);
		memberRankOrganizationFeeMapper.setMemberFeeSettingId(memberFeeSetting.getId());
		memberRankOrganizationFeeMapperDao.insert(memberRankOrganizationFeeMapper);
    }

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateFee(MemberRankOrganizationFeeMapper memberRankOrganizationFeeMapper) {
		MemberRankOrganizationFeeMapper feeMapper = memberRankOrganizationFeeMapperDao.get(memberRankOrganizationFeeMapper.getId());
		if(feeMapper == null){
			throw new BizException("修改失败: 收费标准不存在");
		}
		MemberFeeSetting memberFeeSetting = memberRankOrganizationFeeMapper.getMemberFeeSetting();
		if(memberFeeSetting == null){
			throw new BizException("修改失败: 请设置收费标准");
		}
		if(!feeMapper.getOrganId().equals(memberRankOrganizationFeeMapper.getOrganId())){
			MemberRankOrganizationFeeMapper organIdAndRankId = memberRankOrganizationFeeMapperDao.findByOrganIdAndRankId(memberRankOrganizationFeeMapper.getOrganId(), memberRankOrganizationFeeMapper.getMemberRankSettingId());
			if(organIdAndRankId != null){
				throw new BizException("修改失败: 该分部会员收费标准已存在");
			}
		}
		memberFeeSetting.setId(feeMapper.getMemberFeeSettingId());
		memberFeeSettingService.update(memberFeeSetting);
		memberRankOrganizationFeeMapperDao.update(memberRankOrganizationFeeMapper);
	}
}