package cn.cslg.pas.service;


import cn.cslg.pas.domain.ProjectFieldText;
import cn.cslg.pas.mapper.ProjectFieldTextMapper;
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 ProjectFieldTextService extends ServiceImpl<ProjectFieldTextMapper, ProjectFieldText> {

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

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

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

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

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

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