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

import com.ym.mec.biz.dal.dao.MusicGroupPaymentCalenderActivityDao;
import com.ym.mec.biz.dal.dao.VipGroupActivityDao;
import com.ym.mec.biz.dal.dao.VipGroupCategoryDao;
import com.ym.mec.biz.dal.dto.ConditionDto;
import com.ym.mec.biz.dal.dto.MusicGroupPaymentBaseCalender;
import com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender;
import com.ym.mec.biz.dal.entity.MusicGroupPaymentCalenderActivity;
import com.ym.mec.biz.dal.entity.VipGroupActivity;
import com.ym.mec.biz.dal.enums.CalenderBaseServiceEnum;
import com.ym.mec.biz.service.MusicGroupPaymentCalenderActivityService;
import com.ym.mec.biz.service.MusicGroupPaymentCalenderBaseService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class MusicGroupPaymentCalenderActivityServiceImpl extends BaseServiceImpl<Long, MusicGroupPaymentCalenderActivity>
        implements MusicGroupPaymentCalenderActivityService, MusicGroupPaymentCalenderBaseService<MusicGroupPaymentCalenderActivity> {

    @Autowired
    private MusicGroupPaymentCalenderActivityDao musicGroupPaymentCalenderActivityDao;

    @Override
    public BaseDAO<Long, MusicGroupPaymentCalenderActivity> getDAO() {
        return musicGroupPaymentCalenderActivityDao;
    }

    @Autowired
    private VipGroupActivityDao vipGroupActivityDao;
    @Autowired
    private VipGroupCategoryDao vipGroupCategoryDao;

    public MusicGroupPaymentCalenderActivityServiceImpl() {
        MusicGroupPaymentCalenderBaseService.calenderBaseServiceMap.put(CalenderBaseServiceEnum.ACTIVITY,this);
    }

    @Override
    public MusicGroupPaymentCalender.PaymentCalenderStatusEnum checkComponentAmount(MusicGroupPaymentBaseCalender baseCalender) {
        return null;
    }

    @Override
    public BigDecimal getActualAmount(MusicGroupPaymentBaseCalender baseCalender) {
        List<MusicGroupPaymentCalenderActivity> calenderActivityList = baseCalender.getCalenderActivityList();
        if(calenderActivityList != null && calenderActivityList.size() > 0){
            return calenderActivityList.stream().map(e->e.getActualAmount()).reduce(BigDecimal.ZERO,BigDecimal::add);
        }
        return BigDecimal.ZERO;
    }

    @Override
    public BigDecimal getOriginalAmount(MusicGroupPaymentBaseCalender baseCalender) {
        return this.getAmount(baseCalender.getCalenderActivityList(),this::getActivityOriginalAmount);
    }

    @Override
    public BigDecimal getCurrentAmount(MusicGroupPaymentBaseCalender baseCalender) {
        return this.getAmount(baseCalender.getCalenderActivityList(),this::getActivityActualAmount);
    }

    private BigDecimal getAmount(List<MusicGroupPaymentCalenderActivity> calenderActivityList,Function<VipGroupActivity,BigDecimal> fun){
        if(calenderActivityList != null && calenderActivityList.size() > 0){
            List<Integer> collect = calenderActivityList.stream().map(e -> e.getActivityId()).collect(Collectors.toList());
            List<VipGroupActivity> vipGroupActivities = vipGroupActivityDao.queryByIds(StringUtils.join(collect, ","));
            return vipGroupActivities.stream().map(e->fun.apply(e)).reduce(BigDecimal.ZERO,BigDecimal::add);
        }
        return BigDecimal.ZERO;
    }

    private BigDecimal getActivityOriginalAmount(VipGroupActivity activity){
        return activity.getOriginalPrice();
    }
    private BigDecimal getActivityActualAmount(VipGroupActivity activity){
        return activity.getMarketPrice();
    }


    @Override
    public <T> T initBean(T bean) {
        if(bean != null){
            List<MusicGroupPaymentCalenderActivity> calenderActivities = (List<MusicGroupPaymentCalenderActivity>) bean;
            List<Integer> collect = calenderActivities.stream().map(e -> e.getActivityId()).collect(Collectors.toList());
            List<VipGroupActivity> vipGroupActivities = vipGroupActivityDao.queryByIds(StringUtils.join(collect, ","));
            List<ConditionDto> conditionCategorys = vipGroupCategoryDao.findConditionCategorys();
            Map<Long, String> categoryNameMap = conditionCategorys.stream().collect(Collectors.toMap(ConditionDto::getId, ConditionDto::getName));
            Map<Integer, VipGroupActivity> vipGroupActivityMap = vipGroupActivities.stream().collect(Collectors.toMap(VipGroupActivity::getId, s->s));
            calenderActivities.forEach(e -> {
                VipGroupActivity activity1 = vipGroupActivityMap.get(e.getActivityId());
                if(activity1 != null){
                    e.setOriginalAmount(activity1.getOriginalPrice());
                    e.setActivityName(activity1.getName());
                    e.setNum(new BigDecimal(activity1.getMaxCourseNum()));
                    if(StringUtils.isNotEmpty(activity1.getVipGroupCategoryIdList())){
                        e.setCategoryName(categoryNameMap.get(Long.parseLong(activity1.getVipGroupCategoryIdList())));
                    }else {
                        e.setCategoryName("网管课");
                    }
                }
            });
        }
        return bean;
    }

    @Override
    public void batchInsert(MusicGroupPaymentBaseCalender musicGroupPaymentBaseCalender, Long calenderId) {
        List<MusicGroupPaymentCalenderActivity> calenderActivityList = musicGroupPaymentBaseCalender.getCalenderActivityList();
        if(calenderActivityList != null && calenderActivityList.size() > 0){
            calenderActivityList.stream().forEach(e->e.setCalenderId(calenderId));
            musicGroupPaymentCalenderActivityDao.insertBatch(calenderActivityList);
        }
    }

    @Override
    public void delByCalenderId(Long calenderId) {
        musicGroupPaymentCalenderActivityDao.delByCalenderId(calenderId);
    }

    @Override
    public List<MusicGroupPaymentCalenderActivity> findByCalenderId(Long calenderId) {
        return musicGroupPaymentCalenderActivityDao.findByCalenderId(calenderId);
    }

    @Override
    public List<MusicGroupPaymentCalenderActivity> findByIds(List<Long> buyCalenderActivityIdList) {
        return musicGroupPaymentCalenderActivityDao.findByIds(buyCalenderActivityIdList);
    }

    @Override
    public List<MusicGroupPaymentCalenderActivity> findByActivityIds(List<Long> buyCalenderActivityIdList,Long calenderId) {
        return musicGroupPaymentCalenderActivityDao.findByActivityIds(buyCalenderActivityIdList,calenderId);
    }
}
