package com.ruoyi.project.business.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.project.business.domain.TbAgentTarget;
import com.ruoyi.project.business.domain.vo.agent.AgentTargetReq;
import com.ruoyi.project.business.domain.vo.agent.AgentTargetRes;
import com.ruoyi.project.business.mapper.TbAgentTargetMapper;
import com.ruoyi.project.business.service.ITbAgentTargetService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 代理商考核Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-16
 */
@Service
@RequiredArgsConstructor
public class TbAgentTargetServiceImpl extends ServiceImpl<TbAgentTargetMapper, TbAgentTarget> implements ITbAgentTargetService {

    private final TbAgentTargetMapper tbAgentTargetMapper;


    /**
     * 查询代理商考核列表
     *
     * @param agentTargetReq 代理商考核
     * @return 代理商考核
     */
    @Override
    public List<AgentTargetRes> selectTbAgentTargetList(AgentTargetReq agentTargetReq) {
        return tbAgentTargetMapper.selectTbAgentTargetList(agentTargetReq);
    }

    @Override
    public boolean add(TbAgentTarget tbAgentTarget) {
        boolean exist = baseMapper.exists(Wrappers.
                lambdaQuery(TbAgentTarget.class)
                .eq(TbAgentTarget::getAgentId, tbAgentTarget.getAgentId())
                .eq(TbAgentTarget::getTargetYear, tbAgentTarget.getTargetYear()));
        // 当前代理商该年度考核计划已存在
        if (exist) throw new RuntimeException("当前代理商该年度考核计划已存在");
        
        return save(tbAgentTarget);
    }

}
