package cn.cslg.report.service.business;



import cn.cslg.report.entity.ReportFieldText;
import cn.cslg.report.mapper.ReportFieldTextMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 自定义字段文本关联表 服务类
 * </p>
 *
 * @author 王岩
 * @since 2022-02-22
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class ReportFieldTextService extends ServiceImpl<ReportFieldTextMapper, ReportFieldText> {

    public ReportFieldText getProjectFieldTextByNameAndFieldId(String text, Integer fieldId) {
        LambdaQueryWrapper<ReportFieldText> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReportFieldText::getFieldId, fieldId);
        queryWrapper.eq(ReportFieldText::getText, text);
        return this.getOne(queryWrapper);
    }

    public List<ReportFieldText> getProjectFieldTextByFieldIds(List<Integer> fieldIds) {
        if (fieldIds == null || fieldIds.size() == 0) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<ReportFieldText> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ReportFieldText::getFieldId, fieldIds);
        return this.list(queryWrapper);
    }

    public  List<ReportFieldText> getProjectFieldTextByIds(List<Integer> ids) {
        if (ids == null || ids.size() == 0) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<ReportFieldText> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ReportFieldText::getId, ids);
        return this.list(queryWrapper);
    }

    public List<ReportFieldText> getProjectFieldTextByFieldId(Integer fieldId) {
        LambdaQueryWrapper<ReportFieldText> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReportFieldText::getFieldId, fieldId);
        return this.list(queryWrapper);
    }

    public void deleteByFieldId(Integer fieldId) {
        this.remove(Wrappers.<ReportFieldText>lambdaQuery().eq(ReportFieldText::getFieldId, fieldId));
    }

    public Integer add(Integer fieldId, String text) {
        ReportFieldText temp = this.getProjectFieldTextByNameAndFieldId(text, fieldId);
        if (temp == null) {
            temp = new ReportFieldText();
            temp.setText(text);
            temp.setFieldId(fieldId);
            temp.insert();
        }
        return temp.getId();
    }
}
