package org.dromara.main.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.dromara.main.domain.MainAgreement;
import org.dromara.main.domain.bo.MainAgreementBo;
import org.dromara.main.domain.vo.MainAgreementVo;
import org.dromara.main.mapper.MainAgreementMapper;
import org.dromara.main.service.IMainAgreementService;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Service
public class MainAgreementServiceImpl implements IMainAgreementService {
    private final MainAgreementMapper baseMapper;
    @Override
    public MainAgreementVo selectByType(String type) {
        // 直接根据 type 查询唯一的协议
        return baseMapper.selectVoOne(new LambdaQueryWrapper<MainAgreement>()
            .eq(MainAgreement::getType, type));
    }

//    @Override
//    public Boolean update(MainAgreementBo bo) {
//        MainAgreement agreement= BeanUtil.toBean(bo, MainAgreement.class);
//        return baseMapper.updateById( agreement)>0;
//    }

    @Override
    public Boolean update(MainAgreementBo bo) {
        MainAgreement agreement = BeanUtil.toBean(bo, MainAgreement.class);
        // 判断如果有id那就是更新，如果没有那就是首次给这个类型新增协议记录
        if (agreement.getId() == null) {
            return baseMapper.insert(agreement) > 0;
        }
        return baseMapper.updateById(agreement) > 0;
    }
}
