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

import com.ym.mec.biz.dal.dao.MusicGroupPaymentCalenderRepairDao;
import com.ym.mec.biz.dal.dto.MusicGroupPaymentBaseCalender;
import com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender;
import com.ym.mec.biz.dal.entity.MusicGroupPaymentCalenderRepair;
import com.ym.mec.biz.dal.enums.CalenderBaseServiceEnum;
import com.ym.mec.biz.service.MusicGroupPaymentCalenderBaseService;
import com.ym.mec.biz.service.MusicGroupPaymentCalenderRepairService;
import com.ym.mec.biz.service.SysConfigService;
import com.ym.mec.biz.service.SysTenantConfigService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.common.tenant.TenantContextHolder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

import static com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender.PaymentCalenderStatusEnum.AUDITING;
import static com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender.PaymentCalenderStatusEnum.NO;

@Service
public class MusicGroupPaymentCalenderRepairServiceImpl extends BaseServiceImpl<Long, MusicGroupPaymentCalenderRepair>
        implements MusicGroupPaymentCalenderBaseService<MusicGroupPaymentCalenderRepair>, MusicGroupPaymentCalenderRepairService {

    @Autowired
    private MusicGroupPaymentCalenderRepairDao musicGroupPaymentCalenderRepairDao;

    @Override
    public BaseDAO<Long, MusicGroupPaymentCalenderRepair> getDAO() {
        return musicGroupPaymentCalenderRepairDao;
    }

    @Autowired
    private SysTenantConfigService sysTenantConfigService;

    public MusicGroupPaymentCalenderRepairServiceImpl() {
        MusicGroupPaymentCalenderBaseService.calenderBaseServiceMap.put(CalenderBaseServiceEnum.MUSIC_REPAIR,this);
    }

    @Override
    public MusicGroupPaymentCalender.PaymentCalenderStatusEnum checkComponentAmount(MusicGroupPaymentBaseCalender baseCalender) {
        MusicGroupPaymentCalenderRepair musicRepair = baseCalender.getMusicRepair();
        if(musicRepair != null){
            Integer tenantId = TenantContextHolder.getTenantId();
            String configValue = sysTenantConfigService.getTenantConfigValue(SysConfigService.ONE_YEAR_MUSICAL_REPAIR_AMOUNT, tenantId);
            if(StringUtils.isEmpty(configValue)){
                throw new BizException("操作失败：请配置乐保零售价");
            }
            //获取会员总价
            BigDecimal repairPaymentAmount = new BigDecimal(configValue).multiply(musicRepair.getNum()).setScale(0, BigDecimal.ROUND_HALF_UP);
            if(repairPaymentAmount.compareTo(musicRepair.getActualAmount()) != 0){
                return AUDITING;
            }
        }
        return NO;
    }

    @Override
    public BigDecimal getActualAmount(MusicGroupPaymentBaseCalender baseCalender) {
        MusicGroupPaymentCalenderRepair musicRepair = baseCalender.getMusicRepair();
        if(musicRepair != null){
            return musicRepair.getActualAmount();
        }
        return BigDecimal.ZERO;
    }

    @Override
    public BigDecimal getOriginalAmount(MusicGroupPaymentBaseCalender baseCalender) {
        return getAmount(baseCalender,SysConfigService.ONE_YEAR_MUSICAL_REPAIR_ORIGINAL_AMOUNT);
    }

    @Override
    public BigDecimal getCurrentAmount(MusicGroupPaymentBaseCalender baseCalender) {
        return getAmount(baseCalender,SysConfigService.ONE_YEAR_MUSICAL_REPAIR_AMOUNT);
    }

    @Override
    public <E> E initBean(E bean) {
        return bean;
    }

    @Override
    public void deleteByCalenderId(Long calenderId) {
        musicGroupPaymentCalenderRepairDao.deleteByCalenderId(calenderId);
    }

    @Override
    public MusicGroupPaymentCalenderRepair findByCalenderId(Long calenderId) {
        return musicGroupPaymentCalenderRepairDao.findByCalenderId(calenderId);
    }

    private BigDecimal getAmount(MusicGroupPaymentBaseCalender baseCalender,String configName){
        MusicGroupPaymentCalenderRepair musicRepair = baseCalender.getMusicRepair();
        if(musicRepair != null){
            Integer tenantId = TenantContextHolder.getTenantId();
            String configValue = sysTenantConfigService.getTenantConfigValue(configName, tenantId);
            if(StringUtils.isEmpty(configValue)){
                throw new BizException("操作失败：请配置乐保单价");
            }
            //获取会员总价
            return new BigDecimal(configValue).multiply(musicRepair.getNum()).setScale(0, BigDecimal.ROUND_HALF_UP);
        }
        return BigDecimal.ZERO;
    }
}
