package com.vote.system.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.vctgo.common.core.text.Convert;
import com.vctgo.common.core.utils.ServletUtils;
import com.vctgo.common.mybatisplus.constant.MybatisPageConstants;
import com.vote.system.domain.VoteEventWorkSettings;
import com.vote.system.domain.VoteWorkTypes;
import com.vote.system.mapper.VoteEventWorkSettingsMapper;
import com.vote.system.mapper.VoteWorkTypesMapper;
import com.vote.system.service.VoteWorkTypesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author MacBook
 * @description 针对表【vote_work_types(作品类型)】的数据库操作Service实现
 * @createDate 2024-08-12 17:01:34
 */
@Service
public class VoteWorkTypesServiceImpl extends ServiceImpl<VoteWorkTypesMapper, VoteWorkTypes>
        implements VoteWorkTypesService {

    @Autowired
    private VoteWorkTypesMapper voteWorkTypesMapper;

    @Autowired
    private VoteEventWorkSettingsMapper voteEventWorkSettingsMapper;

    /**
     * 分页查询 类型
     *
     * @param voteWorkTypes
     * @return
     */
    @Override
    public IPage<VoteWorkTypes> selectPage(VoteWorkTypes voteWorkTypes) {
        // 提取并转换分页参数
        long pageNum = Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_NUM), 1L);
        long pageSize = Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_SIZE), 10L);
        Page<VoteWorkTypes> page = new Page<>(pageNum, pageSize);
        // 查询
        LambdaQueryWrapper<VoteWorkTypes> wrapper = new LambdaQueryWrapper<>();
        List<VoteWorkTypes> workTypes = voteWorkTypesMapper.selectList(wrapper);
        page.setTotal(workTypes.size());
        page.setRecords(workTypes);
        return page;
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public int deleteById(Integer id) {
        LambdaQueryWrapper<VoteEventWorkSettings> workSettingsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        workSettingsLambdaQueryWrapper.eq(VoteEventWorkSettings::getWorkTypeId, id);
        List<VoteEventWorkSettings> voteEventWorkSettings = voteEventWorkSettingsMapper.selectList(workSettingsLambdaQueryWrapper);
        if (!voteEventWorkSettings.isEmpty()) {
            throw new RuntimeException("该类型已经关联其他数据，不可删除");
        }
        return voteWorkTypesMapper.deleteById(id);
    }

    @Override
    @Transactional
    public int add(VoteWorkTypes voteWorkTypes) {
        return voteWorkTypesMapper.insert(voteWorkTypes);
    }
}




