package top.continew.admin.business.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import top.continew.admin.business.mapper.TeamInviteConfigMapper;
import top.continew.admin.business.model.entity.TaskInfo;
import top.continew.admin.business.model.entity.TeamInviteConfig;
import top.continew.admin.business.model.query.TeamInviteConfigQuery;
import top.continew.admin.business.model.req.TeamInviteConfigReq;
import top.continew.admin.business.service.ITeamInviteConfigService;
import top.continew.starter.core.validation.CheckUtils;
import top.continew.starter.extension.crud.model.resp.BasePageResp;
import top.continew.starter.extension.crud.model.resp.PageResp;

import java.time.LocalDateTime;

/**
 * <p>
 * 团队邀请配置 服务实现类
 * </p>
 *
 * @author xudm
 * @since 2024-12-16
 */
@Service
public class TeamInviteConfigServiceImpl extends ServiceImpl<TeamInviteConfigMapper, TeamInviteConfig> implements ITeamInviteConfigService {

    @Override
    public BasePageResp<TeamInviteConfig> getTeamInviteConfigPage(TeamInviteConfigQuery query) {
        Page<TeamInviteConfig> page = new Page<>();
        page.setCurrent(query.getPage() != 0 ? query.getPage() : 1);
        page.setSize(query.getSize() != 0 ? query.getSize() : 10);
        LambdaQueryWrapper<TeamInviteConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(TeamInviteConfig::getCreateTime);
        page = page(page, queryWrapper);
        return PageResp.build(page, TeamInviteConfig.class);
    }

    @Override
    public void updateTeamInviteConfig(Long id, TeamInviteConfigReq req) {
        TeamInviteConfig byId = getById(id);
        CheckUtils.throwIfNull(byId, "团队配置信息为空，更新失败");
        BeanUtils.copyProperties(req, byId);
        byId.setUpdateTime(LocalDateTime.now());
        updateById(byId);
    }

    @Override
    public Long addTeamInviteConfig(TeamInviteConfigReq req) {
        TeamInviteConfig teamInviteConfig = new TeamInviteConfig();
        BeanUtils.copyProperties(req, teamInviteConfig);
        teamInviteConfig.setCreateTime(LocalDateTime.now());
        teamInviteConfig.setUpdateTime(LocalDateTime.now());
        save(teamInviteConfig);
        return teamInviteConfig.getId();
    }
}
