package cn.cslg.report.service.business;


import cn.cslg.report.common.utils.Response;
import cn.cslg.report.entity.ReportFieldOption;
import cn.cslg.report.mapper.ReportFieldOptionMapper;
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.io.IOException;
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 ReportFieldOptionService extends ServiceImpl<ReportFieldOptionMapper, ReportFieldOption> {

    private final ReportFieldPatentLinkService projectFieldPatentLinkService;
    private final ReportFieldTreeService reportFieldTreeService;

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

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

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

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

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

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

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

    @Transactional
    public void editBatch(List<ReportFieldOption> optionList, Integer fieldId) {
        if (optionList.size() != 0) {
            List<ReportFieldOption> tempList = this.getListByFieldId(fieldId);
            List<Integer> deleteIds = tempList.stream().map(ReportFieldOption::getId).filter(id -> !optionList.stream().map(ReportFieldOption::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.<ReportFieldOption>lambdaQuery().eq(ReportFieldOption::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 updateReportFieldOption(ReportFieldOption reportFieldOption) {
        if (reportFieldOption.getType().equals(6)) {
            return reportFieldTreeService.updateByReportFieldTreeNode(reportFieldOption);
        } else {
            ReportFieldOption temp = this.getReportFieldOptionByNameAndFieldId(reportFieldOption.getName(), reportFieldOption.getFieldId());
            if (temp != null && !temp.getId().equals(reportFieldOption.getId())) {
                return Response.error("选项名称已存在");
            }
            reportFieldOption.insertOrUpdate();
            return Response.success(reportFieldOption.getId());
        }
    }

}
