package com.education.business.service.education;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.education.business.mapper.education.HomeworkGoalInfoMapper;
import com.education.business.service.BaseService;
import com.education.common.model.PageInfo;
import com.education.model.dto.HomeworkGoalInfoDto;
import com.education.model.entity.HomeworkGoalInfo;
import com.education.model.request.PageParam;
import org.springframework.stereotype.Service;

/**
 * @author zhangj
 * @version 1.0
 * @create_at
 */
@Service
public class HomeworkGoalInfoService extends BaseService<HomeworkGoalInfoMapper, HomeworkGoalInfo> {

    public PageInfo<HomeworkGoalInfoDto> selectPageList(PageParam pageParam, HomeworkGoalInfo homeworkGoalInfo) {
        Page<HomeworkGoalInfoDto> page = new Page<>(pageParam.getPageNumber(), pageParam.getPageSize());
        return selectPage(baseMapper.selectPageList(page, homeworkGoalInfo));
    }

    @Override
    public boolean saveOrUpdate(HomeworkGoalInfo homeworkGoalInfo) {
        return super.saveOrUpdate(homeworkGoalInfo);
    }

    public boolean removeHomeworkGoalInfo(Integer homeworkInfoId, Integer goalInfoId) {
        //todo select homework goal info and delete it
        Wrapper<HomeworkGoalInfo> queryWrapper = Wrappers.lambdaQuery(HomeworkGoalInfo.class)
                .eq(HomeworkGoalInfo::getHomeworkInfoId, homeworkInfoId)
                .eq(HomeworkGoalInfo::getGoalInfoId, goalInfoId);
        HomeworkGoalInfo homeworkGoalInfo = baseMapper.selectOne(queryWrapper);
        if (homeworkGoalInfo != null){
            super.removeById(homeworkGoalInfo.getId());
        }
        return true;
    }
}