package cn.iocoder.yudao.module.label.service.impl;

import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.label.entity.BusiLabel;
import cn.iocoder.yudao.module.label.mapper.BusiLabelMapper;
import cn.iocoder.yudao.module.label.service.BusiLabelService;
import cn.iocoder.yudao.module.label.vo.BusiLabelSaveReqVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

/**
 * 标签库 Service 实现类
 *
 * @author 后台管理员
 */
@Service
@Validated
public class BusiLabelServiceImpl extends ServiceImpl<BusiLabelMapper, BusiLabel> implements BusiLabelService {

    /**
     * 通过mainId查询业务标签
     *
     * @param mainId mainId
     * @return java.util.List<cn.iocoder.yudao.module.label.entity.BusiLabel>
     * @author PQZ
     * @date 18:22 2024/8/4
     **/
    @Override
    public List<BusiLabel> listByMainId(String mainId) {
        LambdaQueryWrapper<BusiLabel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BaseDO::getDeleted,0).eq(BusiLabel::getMainId,mainId);
        return list(lambdaQueryWrapper);
    }

    /**
     * 保存标签
     *
     * @param mainId    主表id
     * @param mainTable 主表名称
     * @param labelList 标签集合
     * @return void
     * @author PQZ
     * @date 18:39 2024/8/4
     **/
    @Override
    public void saveBusiLable(String mainId, String mainTable, List<BusiLabel> labelList) {
        /*1、删除已有标签信息*/
        LambdaQueryWrapper<BusiLabel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BaseDO::getDeleted,0).eq(BusiLabel::getMainId,mainId);
        this.remove(lambdaQueryWrapper);
        /*2、保存标签信息*/
        labelList.forEach(item -> {
            item.setId(null);
            item.setMainId(mainId);
            item.setMainTable(mainTable);
        });
        this.saveBatch(labelList);
    }
}