package com.yida.data.school.secondClass.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yida.data.common.core.entity.school.secondClass.EduSecondClassVenueGuide;
import com.yida.data.common.core.entity.school.secondClass.EduSecondClassVenueOrder;
import com.yida.data.common.core.entity.school.secondClass.EduSecondClassVenueOrderMember;
import com.yida.data.common.core.utils.Asserts;
import com.yida.data.school.secondClass.mapper.EduSecondClassVenueGuideMapper;
import com.yida.data.school.secondClass.mapper.EduSecondClassVenueOrderMapper;
import com.yida.data.school.secondClass.service.EduSecondClassVenueOrderMemberService;
import com.yida.data.school.secondClass.service.EduSecondClassVenueOrderService;
import com.yida.data.school.vo.secondClass.VenueOrderVO;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 第二课堂-场馆-预约记录 Service实现
 *
 * @author zhanghaijun
 * @date 2022-04-21 19:37:41
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class EduSecondClassVenueOrderServiceImpl extends ServiceImpl
    <EduSecondClassVenueOrderMapper, EduSecondClassVenueOrder> implements EduSecondClassVenueOrderService {

    private final EduSecondClassVenueOrderMemberService eduSecondClassVenueOrderMemberService;
    private final EduSecondClassVenueGuideMapper eduSecondClassVenueGuideMapper;

    @Override
    public void saveOrder(EduSecondClassVenueOrder order) {
        EduSecondClassVenueGuide guide = eduSecondClassVenueGuideMapper.selectById(order.getGuideId());
        order.setDeptId(guide.getDeptId());
        save(order);
        for (EduSecondClassVenueOrderMember member : order.getMemberList()) {
            member.setOrderId(order.getId());
        }
        eduSecondClassVenueOrderMemberService.saveBatch(order.getMemberList());
    }

    @Override
    public List<VenueOrderVO> listVenueOrder(Long venueId, String userId) {
        return baseMapper.listVenueOrder(venueId, userId);
    }

    @Override
    public void cancleOrder(Long orderId, String userId) {
        EduSecondClassVenueOrder order = getById(orderId);
        Asserts.isTrue(order.getUserId().equals(userId), "取消失败，请刷新后重试");
        order.setStatus(1);
        updateById(order);
    }
}
