package cn.cslg.pas.service;

import cn.cslg.pas.domain.ProjectFieldOption;
import cn.cslg.pas.common.utils.Response;
import cn.cslg.pas.mapper.ProjectFieldOptionMapper;
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 org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 自定义字段多选关联表 服务实现类
 * </p>
 *
 * @author 王岩
 * @since 2021-12-17
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class ProjectFieldOptionService extends ServiceImpl<ProjectFieldOptionMapper, ProjectFieldOption> {

    private final ProjectFieldPatentLinkService projectFieldPatentLinkService;
    private final ProjectFieldTreeService projectFieldTreeService;

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

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

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

    public ProjectFieldOption getProjectFieldOptionByNameAndFieldId(String name, Integer fieldId) {
        LambdaQueryWrapper<ProjectFieldOption> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProjectFieldOption::getFieldId, fieldId);
        queryWrapper.eq(ProjectFieldOption::getName, name);
        return this.getOne(queryWrapper);
    }

    public Integer add(ProjectFieldOption option) {
        option.insert();
        return option.getId();
    }

    @Transactional
    public void addBatch(List<ProjectFieldOption> optionList, Integer fieldId) {
        if (optionList.size() != 0) {
            optionList.forEach(item -> item.setFieldId(fieldId));
            this.saveOrUpdateBatch(optionList);
        }
    }

    public void edit(ProjectFieldOption option) {
        option.updateById();
    }

    @Transactional
    public void editBatch(List<ProjectFieldOption> optionList, Integer fieldId) {
        if (optionList.size() != 0) {
            List<ProjectFieldOption> tempList = this.getListByFieldId(fieldId);
            List<Integer> deleteIds = tempList.stream().map(ProjectFieldOption::getId).filter(id -> !optionList.stream().map(ProjectFieldOption::getId).collect(Collectors.toList()).contains(id)).collect(Collectors.toList());
            this.deleteByIds(deleteIds);
            this.addBatch(optionList, fieldId);
        } else {
            this.deleteByFieldId(fieldId);
        }
    }

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

    @Transactional
    public void delete(Integer id) {
        projectFieldPatentLinkService.deleteByOptionId(id);
        this.removeById(id);
    }

    public void deleteByIds(List<Integer> ids) {
        if (ids.size() != 0) {
            projectFieldPatentLinkService.deleteByOptionIds(ids);
            this.removeByIds(ids);
        }
    }

    public String updateProjectFieldOption(ProjectFieldOption projectFieldOption) {
        if (projectFieldOption.getType().equals(6)) {
            return projectFieldTreeService.updateByProjectFieldTreeNode(projectFieldOption);
        } else {
            ProjectFieldOption temp = this.getProjectFieldOptionByNameAndFieldId(projectFieldOption.getName(), projectFieldOption.getFieldId());
            if (temp != null && !temp.getId().equals(projectFieldOption.getId())) {
                return Response.error("选项名称已存在");
            }
            projectFieldOption.insertOrUpdate();
            return Response.success(projectFieldOption.getId());
        }
    }
}
