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

import com.ym.mec.biz.dal.dao.MusicGroupOrganizationCourseSettingsDao;
import com.ym.mec.biz.dal.dao.MusicGroupOrganizationCourseSettingsDetailDao;
import com.ym.mec.biz.dal.entity.MusicGroupOrganizationCourseSettings;
import com.ym.mec.biz.dal.entity.MusicGroupOrganizationCourseSettingsDetail;
import com.ym.mec.biz.service.MusicGroupOrganizationCourseSettingsService;
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;

import java.math.BigDecimal;

@Service
public class MusicGroupOrganizationCourseSettingsServiceImpl extends BaseServiceImpl<Integer, MusicGroupOrganizationCourseSettings> implements MusicGroupOrganizationCourseSettingsService {
    @Autowired
    private MusicGroupOrganizationCourseSettingsDao musicGroupOrganizationCourseSettingsDao;
    @Autowired
    private MusicGroupOrganizationCourseSettingsDetailDao musicGroupOrganizationCourseSettingsDetailDao;

    @Override
    public BaseDAO<Integer, MusicGroupOrganizationCourseSettings> getDAO() {
        return musicGroupOrganizationCourseSettingsDao;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MusicGroupOrganizationCourseSettings addSetting(MusicGroupOrganizationCourseSettings setting) {
        if (setting.getDetails() == null || setting.getDetails().size() <= 0) {
            throw new BizException("请设置对应课程的收费");
        }

		for (MusicGroupOrganizationCourseSettingsDetail detail : setting.getDetails()) {
			if (detail.getCourseTotalMinuties() <= 0) {
				throw new BizException(detail.getCourseType().getMsg() + "课程总时长必须大于0");
			}
			setting.setTotalCurrentPrice(setting.getTotalCurrentPrice().add(detail.getCourseCurrentPrice()));
			setting.setTotalOriginalPrice(setting.getTotalOriginalPrice().add(detail.getCourseOriginalPrice()));
		}
        musicGroupOrganizationCourseSettingsDao.insert(setting);
        for (MusicGroupOrganizationCourseSettingsDetail detail : setting.getDetails()) {
            detail.setMusicGroupOrganizationCourseSettingsId(setting.getId());
            detail.setUnitPrice(detail.getCourseCurrentPrice().divide(new BigDecimal(detail.getCourseTotalMinuties()), 8, BigDecimal.ROUND_HALF_UP));
        }
        musicGroupOrganizationCourseSettingsDetailDao.batchInsert(setting.getDetails());
        return setting;
    }

    @Override
    public MusicGroupOrganizationCourseSettings delSetting(Integer id) {
        MusicGroupOrganizationCourseSettings setting = musicGroupOrganizationCourseSettingsDao.get(id);
        if (setting == null) {
            throw new BizException("收费方式记录不存在，请核查");
        }
        musicGroupOrganizationCourseSettingsDetailDao.deleteBySettingId(setting.getId());
        musicGroupOrganizationCourseSettingsDao.delete(setting.getId());
        return setting;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MusicGroupOrganizationCourseSettings updateSetting(MusicGroupOrganizationCourseSettings setting) {
        if (setting.getDetails() == null || setting.getDetails().size() <= 0) {
            throw new BizException("请设置对应课程的收费");
        }
        musicGroupOrganizationCourseSettingsDetailDao.deleteBySettingId(setting.getId());

		for (MusicGroupOrganizationCourseSettingsDetail detail : setting.getDetails()) {
			if (detail.getCourseTotalMinuties() <= 0) {
				throw new BizException(detail.getCourseType().getMsg() + "课程总时长必须大于0");
			}
			setting.setTotalCurrentPrice(setting.getTotalCurrentPrice().add(detail.getCourseCurrentPrice()));
			setting.setTotalOriginalPrice(setting.getTotalOriginalPrice().add(detail.getCourseOriginalPrice()));
		}
        musicGroupOrganizationCourseSettingsDao.update(setting);
        for (MusicGroupOrganizationCourseSettingsDetail detail : setting.getDetails()) {
            detail.setId(null);
            detail.setMusicGroupOrganizationCourseSettingsId(setting.getId());
            detail.setUnitPrice(detail.getCourseCurrentPrice().divide(new BigDecimal(detail.getCourseTotalMinuties()), 8, BigDecimal.ROUND_HALF_UP));
        }
        musicGroupOrganizationCourseSettingsDetailDao.batchInsert(setting.getDetails());
        return setting;
    }
}
