package com.app.service.order;

import com.app.entity.order.OpenCourseShoppingCar;
import com.app.mapper.order.OpenCourseShoppingCarMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Slf4j
@Service
public class OpenCourseShoppingCarService {

    @Autowired
    private OpenCourseShoppingCarMapper mapper;

    public OpenCourseShoppingCar find(String openCourseId, String guardianId, String guardianChildId) {
        return mapper.selectOne(new QueryWrapper<OpenCourseShoppingCar>().lambda()
                .eq(OpenCourseShoppingCar::getOpenCourseId, openCourseId)
                .eq(OpenCourseShoppingCar::getGuardianId, guardianId)
                .eq(OpenCourseShoppingCar::getGuardianChildId, guardianChildId)
        );
    }

    /**
     * 功能描述：保存[选课单]
     *
     * @param openCourseId
     * @param guardianId
     * @param guardianChildId
     */
    public void save(String openCourseId, String guardianId, String guardianChildId) {
        OpenCourseShoppingCar _car = new OpenCourseShoppingCar();
        _car.setOpenCourseId(openCourseId);
        _car.setGuardianId(guardianId);
        _car.setGuardianChildId(guardianChildId);
        mapper.insert(_car);
    }

    public void delete(String openCourseId, String guardianId, String guardianChildId) {
        OpenCourseShoppingCar _car = find(openCourseId, guardianId, guardianChildId);
        if (Optional.ofNullable(_car).isPresent()) {
            mapper.deleteById(_car.getId());
        }
    }

}
