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


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ym.mec.biz.dal.dao.ChildrenDayDegreeDetailDao;
import com.ym.mec.biz.dal.dao.ChildrenDayDegreeInfoDao;
import com.ym.mec.biz.dal.dao.StudentDao;
import com.ym.mec.biz.dal.entity.ChildrenDayDegreeDetail;
import com.ym.mec.biz.dal.entity.ChildrenDayDegreeInfo;
import com.ym.mec.biz.dal.entity.Student;
import com.ym.mec.biz.dal.enums.ActivityCourseType;
import com.ym.mec.biz.service.ChildrenDayDegreeInfoService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
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 org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

@Service
public class ChildrenDayDegreeInfoServiceImpl extends BaseServiceImpl<Integer, ChildrenDayDegreeInfo> implements ChildrenDayDegreeInfoService {
    @Autowired
    private ChildrenDayDegreeInfoDao childrenDayDegreeInfoDao;
    @Autowired
    private ChildrenDayDegreeDetailDao childrenDayDegreeDetailDao;
    @Autowired
    private StudentDao studentDao;

    @Override
    public BaseDAO<Integer, ChildrenDayDegreeInfo> getDAO() {
        return childrenDayDegreeInfoDao;
    }


    @Override
    public ChildrenDayDegreeInfo addInfo(Long orderId) {
        List<ChildrenDayDegreeDetail> details = childrenDayDegreeDetailDao.getByOrderId(orderId);
        ChildrenDayDegreeInfo childrenDayDegreeInfo = new ChildrenDayDegreeInfo();
        Student student = null;
        JSONObject courseDetail = new JSONObject();
        if(!CollectionUtils.isEmpty(details)){
            student = studentDao.get(details.get(0).getUserId());
        }
        if(Objects.nonNull(student)&& StringUtils.isNotBlank(student.getActivityCourseDetail())){
            courseDetail = JSON.parseObject(student.getActivityCourseDetail());
        }
        BigDecimal totalAmount = BigDecimal.ZERO;
        for (ChildrenDayDegreeDetail detail : details) {
            switch (detail.getType()) {
                case 1:
                    childrenDayDegreeInfo.setSubjectId(detail.getSubjectId());
                    childrenDayDegreeInfo.setGradeLevel(detail.getLevel());
                    childrenDayDegreeInfo.setGradePrice(detail.getPrice());
                    if(detail.getPrice().compareTo(BigDecimal.ZERO)>0&&!courseDetail.containsKey(ActivityCourseType.FREE_VIP.getCode())){
                        courseDetail.put(ActivityCourseType.FREE_VIP.getCode(), 1);
                    }
                    break;
                case 2:
                    childrenDayDegreeInfo.setTheoryLevel(detail.getLevel());
                    childrenDayDegreeInfo.setTheoryPrice(detail.getPrice());
                    break;
                case 3:
                    childrenDayDegreeInfo.setVip1v1Price(detail.getPrice());
                    if(detail.getPrice().compareTo(BigDecimal.ZERO)>0&&!courseDetail.containsKey(ActivityCourseType.VIP1.getCode())){
                        courseDetail.put(ActivityCourseType.VIP1.getCode(), 1);
                    }
                    break;
                case 4:
                    childrenDayDegreeInfo.setVip1v2Price(detail.getPrice());
                    if(detail.getPrice().compareTo(BigDecimal.ZERO)>0&&!courseDetail.containsKey(ActivityCourseType.VIP2.getCode())){
                        courseDetail.put(ActivityCourseType.VIP2.getCode(), 1);
                    }
                    break;
                case 5:
                    childrenDayDegreeInfo.setTheoryCoursePrice(detail.getPrice());
                    if(detail.getPrice().compareTo(BigDecimal.ZERO)>0&&!courseDetail.containsKey(ActivityCourseType.MUSIC_THEORY.getCode())){
                        courseDetail.put(ActivityCourseType.MUSIC_THEORY.getCode(), 1);
                        courseDetail.put("music_theory_price", detail.getPrice());
                    }
                    break;
                default:
                    break;
            }
            childrenDayDegreeInfo.setReserveId(detail.getReserveId());
            childrenDayDegreeInfo.setUserId(detail.getUserId());
            childrenDayDegreeInfo.setOrderId(detail.getOrderId());
            totalAmount = totalAmount.add(detail.getPrice());
        }
        childrenDayDegreeInfo.setTotalAmount(totalAmount);
        if (childrenDayDegreeInfoDao.insert(childrenDayDegreeInfo) <= 0) {
            throw new BizException("汇总信息插入失败");
        }
        if(Objects.nonNull(student)){
            student.setActivityCourseDetail(courseDetail.toJSONString());
            studentDao.update(student);
        }
        return childrenDayDegreeInfo;
    }

    @Override
    public List<Integer> getPayedUser(List<Integer> userIds) {
       return childrenDayDegreeInfoDao.getPayedUser(userIds);
    }
}
