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

import com.ym.mec.biz.dal.dao.*;
import com.ym.mec.biz.dal.dto.DegreePayDto;
import com.ym.mec.biz.dal.entity.ChildrenDayDegreeDetail;
import com.ym.mec.biz.dal.entity.StudentPaymentOrderDetail;
import com.ym.mec.biz.dal.enums.OrderDetailTypeEnum;
import com.ym.mec.biz.service.ChildrenDayDegreeDetailService;
import com.ym.mec.common.dal.BaseDAO;
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;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ChildrenDayDegreeDetailServiceImpl extends BaseServiceImpl<Integer, ChildrenDayDegreeDetail> implements ChildrenDayDegreeDetailService {
    @Autowired
    private ChildrenDayDegreeDetailDao childrenDayDegreeDetailDao;
    @Autowired
    private StudentPaymentOrderDetailDao studentPaymentOrderDetailDao;

    @Override
    public BaseDAO<Integer, ChildrenDayDegreeDetail> getDAO() {
        return childrenDayDegreeDetailDao;
    }

    @Override
    public List<ChildrenDayDegreeDetail> getByUserIdAndStatus(Integer userId, Integer status) {
        return childrenDayDegreeDetailDao.getByUserIdAndStatus(userId, status);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<ChildrenDayDegreeDetail> addDegreeDetails(DegreePayDto degreePayDto, BigDecimal gradeFee, BigDecimal theoryLevelFee, BigDecimal vip1v1Fee, BigDecimal vip1v2Fee, BigDecimal theoryCourseFee) {
        List<ChildrenDayDegreeDetail> childrenDayDegreeDetails = new ArrayList<>();
        if (degreePayDto.getMusicGradeLevel() != null) {
            ChildrenDayDegreeDetail childrenDayDegreeDetail = new ChildrenDayDegreeDetail();
            childrenDayDegreeDetail.setReserveId(degreePayDto.getReserveId());
            childrenDayDegreeDetail.setUserId(degreePayDto.getUserId());
            childrenDayDegreeDetail.setType(1);
            childrenDayDegreeDetail.setSubjectId(degreePayDto.getSubject());
            childrenDayDegreeDetail.setLevel(degreePayDto.getMusicGradeLevel());
            childrenDayDegreeDetail.setPrice(gradeFee);
            childrenDayDegreeDetail.setStatus(1);
            childrenDayDegreeDetail.setOrderId(degreePayDto.getOrderId());
            childrenDayDegreeDetails.add(childrenDayDegreeDetail);
        }
        if (degreePayDto.getTheoryLevel() != null) {
            ChildrenDayDegreeDetail childrenDayDegreeDetail = new ChildrenDayDegreeDetail();
            childrenDayDegreeDetail.setReserveId(degreePayDto.getReserveId());
            childrenDayDegreeDetail.setUserId(degreePayDto.getUserId());
            childrenDayDegreeDetail.setType(2);
            childrenDayDegreeDetail.setLevel(degreePayDto.getTheoryLevel());
            childrenDayDegreeDetail.setPrice(theoryLevelFee);
            childrenDayDegreeDetail.setStatus(1);
            childrenDayDegreeDetail.setOrderId(degreePayDto.getOrderId());
            childrenDayDegreeDetails.add(childrenDayDegreeDetail);
        }
        if (degreePayDto.getVip1v1()) {
            ChildrenDayDegreeDetail childrenDayDegreeDetail = new ChildrenDayDegreeDetail();
            childrenDayDegreeDetail.setReserveId(degreePayDto.getReserveId());
            childrenDayDegreeDetail.setUserId(degreePayDto.getUserId());
            childrenDayDegreeDetail.setType(3);
            childrenDayDegreeDetail.setPrice(vip1v1Fee);
            childrenDayDegreeDetail.setStatus(1);
            childrenDayDegreeDetail.setOrderId(degreePayDto.getOrderId());
            childrenDayDegreeDetails.add(childrenDayDegreeDetail);
        }
        if (degreePayDto.getVip1v2()) {
            ChildrenDayDegreeDetail childrenDayDegreeDetail = new ChildrenDayDegreeDetail();
            childrenDayDegreeDetail.setReserveId(degreePayDto.getReserveId());
            childrenDayDegreeDetail.setUserId(degreePayDto.getUserId());
            childrenDayDegreeDetail.setType(4);
            childrenDayDegreeDetail.setPrice(vip1v2Fee);
            childrenDayDegreeDetail.setStatus(1);
            childrenDayDegreeDetail.setOrderId(degreePayDto.getOrderId());
            childrenDayDegreeDetails.add(childrenDayDegreeDetail);
        }
        if (degreePayDto.getTheoryCourse()) {
            ChildrenDayDegreeDetail childrenDayDegreeDetail = new ChildrenDayDegreeDetail();
            childrenDayDegreeDetail.setReserveId(degreePayDto.getReserveId());
            childrenDayDegreeDetail.setUserId(degreePayDto.getUserId());
            childrenDayDegreeDetail.setType(5);
            childrenDayDegreeDetail.setPrice(theoryCourseFee);
            childrenDayDegreeDetail.setStatus(1);
            childrenDayDegreeDetail.setOrderId(degreePayDto.getOrderId());
            childrenDayDegreeDetails.add(childrenDayDegreeDetail);
        }
        childrenDayDegreeDetailDao.batchAdd(childrenDayDegreeDetails);
        return childrenDayDegreeDetails;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<StudentPaymentOrderDetail> addOrderDetails(DegreePayDto degreePayDto, BigDecimal gradeFee, BigDecimal theoryLevelFee, BigDecimal vip1v1Fee, BigDecimal vip1v2Fee, BigDecimal theoryCourseFee) {
        Date nowDate = new Date();
        List<StudentPaymentOrderDetail> orderDetails = new ArrayList<>();
        if (degreePayDto.getMusicGradeLevel() != null || degreePayDto.getTheoryLevel() != null) {
            StudentPaymentOrderDetail detail = new StudentPaymentOrderDetail();
            detail.setType(OrderDetailTypeEnum.DEGREE_REGISTRATION);
            detail.setPrice(gradeFee.add(theoryLevelFee));
            detail.setRemitFee(BigDecimal.ZERO);
            detail.setCreateTime(nowDate);
            detail.setUpdateTime(nowDate);
            detail.setPaymentOrderId(degreePayDto.getOrderId());
            detail.setIsRenew(0);
            orderDetails.add(detail);
        }
        if (degreePayDto.getVip1v1() || degreePayDto.getVip1v2()) {
            StudentPaymentOrderDetail detail = new StudentPaymentOrderDetail();
            detail.setType(OrderDetailTypeEnum.VIP);
            detail.setPrice(vip1v1Fee.add(vip1v2Fee));
            detail.setRemitFee(BigDecimal.ZERO);
            detail.setCreateTime(nowDate);
            detail.setUpdateTime(nowDate);
            detail.setPaymentOrderId(degreePayDto.getOrderId());
            detail.setIsRenew(0);
            orderDetails.add(detail);
        }
        if (degreePayDto.getTheoryCourse()) {
            StudentPaymentOrderDetail detail = new StudentPaymentOrderDetail();
            detail.setType(OrderDetailTypeEnum.THEORY_COURSE);
            detail.setPrice(theoryCourseFee);
            detail.setRemitFee(BigDecimal.ZERO);
            detail.setCreateTime(nowDate);
            detail.setUpdateTime(nowDate);
            detail.setPaymentOrderId(degreePayDto.getOrderId());
            detail.setIsRenew(0);
            orderDetails.add(detail);
        }
        studentPaymentOrderDetailDao.batchAdd(orderDetails);
        return orderDetails;
    }

    @Override
    public List<ChildrenDayDegreeDetail> getUserDegreeInfo(Integer userId) {
        return childrenDayDegreeDetailDao.getByUserIdAndStatus(userId, 2);
    }

    @Override
    public Integer updateStatus(Long orderId, Integer status) {
        return childrenDayDegreeDetailDao.updateStatus(orderId, status);
    }
}
