package com.corewing.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.corewing.app.common.page.PageContext;
import com.corewing.app.entity.Tutorial;
import com.corewing.app.entity.TutorialCategoryRelation;
import com.corewing.app.mapper.TutorialMapper;
import com.corewing.app.service.TutorialCategoryRelationService;
import com.corewing.app.service.TutorialService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Service
public class TutorialServiceImpl extends ServiceImpl<TutorialMapper, Tutorial> implements TutorialService {

    @Resource
    private TutorialMapper tutorialMapper;

    @Resource
    private TutorialCategoryRelationService tutorialCategoryRelationService;

    @Override
    public IPage<Tutorial> pageList(Page<Tutorial> page, int categoryId, String tutorialTitle, String lang) {
        return tutorialMapper.pageList(page, categoryId, tutorialTitle, lang);
    }

    @Override
    public Page<Tutorial> page(Tutorial tutorial) {
        Page<Tutorial> page = PageContext.getPage(Tutorial.class);
        LambdaQueryWrapper<Tutorial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(tutorial.getTutorialTitle()), Tutorial::getTutorialTitle, tutorial.getTutorialTitle());
        return page(page, queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean save(Tutorial tutorial) {
        super.save(tutorial);
        TutorialCategoryRelation tutorialCategoryRelation = new TutorialCategoryRelation();
        tutorialCategoryRelation.setTutorialId(tutorial.getId());
        tutorialCategoryRelation.setCategoryId(tutorial.getCategoryId());
        return tutorialCategoryRelationService.save(tutorialCategoryRelation);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean update(Tutorial tutorial) {
        super.updateById(tutorial);
        tutorialCategoryRelationService.remove(new LambdaQueryWrapper<TutorialCategoryRelation>().eq(TutorialCategoryRelation::getCategoryId, tutorial.getCategoryId()));
        TutorialCategoryRelation tutorialCategoryRelation = new TutorialCategoryRelation();
        tutorialCategoryRelation.setTutorialId(tutorial.getId());
        tutorialCategoryRelation.setCategoryId(tutorial.getCategoryId());
        return tutorialCategoryRelationService.save(tutorialCategoryRelation);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean remove(Long id) {
        Tutorial tutorial = getById(id);
        removeById(id);
        return tutorialCategoryRelationService.remove(new LambdaQueryWrapper<TutorialCategoryRelation>().eq(TutorialCategoryRelation::getCategoryId, tutorial.getCategoryId()));
    }
}
