package com.upsfj.sfjprojectbackend.service.impl;

import com.upsfj.sfjprojectbackend.entity.Algorithm;
import com.upsfj.sfjprojectbackend.entity.Field;
import com.upsfj.sfjprojectbackend.entity.UseCase;
import com.upsfj.sfjprojectbackend.mapper.AlgorithmMapper;
import com.upsfj.sfjprojectbackend.service.AlgorithmService;
import jakarta.annotation.Resource;
import jakarta.persistence.Transient;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class AlgorithmServiceImpl implements AlgorithmService {
    @Resource
    AlgorithmMapper algorithmMapper;



    @Override
    public List<Algorithm> getAllAlgorithms() {
        return algorithmMapper.findAllAlgorithm();
    }

    @Override
    public List<Field> getAllField() {
        return algorithmMapper.findAllField();
    }

    @Override
    public List<UseCase> getAllUsecase() {
        return algorithmMapper.findAllUseCase();
    }

    @Override
    @Transactional
    public void updateAlgorithm(Algorithm algorithm) {
        algorithmMapper.updateAlgorithmById(algorithm);

        List<Integer> oldFieldId = algorithmMapper.getFieldIdsForAlgorithm(algorithm.getAlgorithm_id());

        List<String> fieldList = algorithm.getFieldList();
        List<Integer> fieldIdList = new ArrayList<>();

//        根据名字 创建最新的field id列表
        for (String fieldName : fieldList){
            Integer fieldId = algorithmMapper.findFieldIdByName(fieldName);
            fieldIdList.add(fieldId);
        }
//        删除不再关联的field
        for (Integer fieldId :oldFieldId){
            if (!fieldIdList.contains(fieldId)){
                algorithmMapper.deleteFieldForAlgorithm(algorithm.getAlgorithm_id(), fieldId);
            }
        }
//        添加关联的field
        for (Integer field : fieldIdList){
            if (!oldFieldId.contains(field)){
                algorithmMapper.addFieldForAlgorithm(algorithm.getAlgorithm_id(), field);
            }
        }

//        下面对use的处理也是类似
        List<Integer> oldUseList = algorithmMapper.getUseIdForAlgorithm(algorithm.getAlgorithm_id());

        List<String> useList = algorithm.getUseList();

        List<Integer> useIdList = new ArrayList<>();


        for (String useName : useList){
            Integer useId = algorithmMapper.findUseIdByName(useName);
            useIdList.add(useId);
        }

        for (Integer useId : oldUseList){
            if (!useIdList.contains(useId)){
                algorithmMapper.deleteUSeForAlgorithm(algorithm.getAlgorithm_id(), useId);
            }
        }

        for (Integer useId : useIdList){
            if (!oldUseList.contains(useId)){
                System.out.println("输出要添加的id");
                System.out.println(algorithm.getAlgorithm_id()+""+useId);
                algorithmMapper.addUseForAlgorithm(algorithm.getAlgorithm_id(), useId);
            }
        }


    }

    @Override
    @Transactional
    public void addAlgorithm(Algorithm algorithm) {
        System.out.println("打印添加");
        System.out.println(algorithm);
        Integer algorithmCount = algorithmMapper.getAlgorithmCount();
        algorithm.setAlgorithm_id(algorithmCount+1);
        algorithmMapper.addAlgorithm(algorithm);

        List<String> fieldList = algorithm.getFieldList();
        List<String> useList = algorithm.getUseList();
        for (String field :fieldList){
            Integer fieldId = algorithmMapper.findFieldIdByName(field);

            algorithmMapper.addFieldForAlgorithm(algorithm.getAlgorithm_id(), fieldId);
        }

        for (String useName : useList){
            Integer useId = algorithmMapper.findUseIdByName(useName);

            algorithmMapper.addUseForAlgorithm(algorithm.getAlgorithm_id(), useId);
        }



    }

    @Override
    @Transactional
    public void deleteAlgorithm(Algorithm algorithm) {
        List<String> fieldList = algorithm.getFieldList();
        List<String> useList = algorithm.getUseList();

        for (String fieldName :fieldList){
            Integer fieldId = algorithmMapper.findFieldIdByName(fieldName);
            algorithmMapper.deleteFieldForAlgorithm(algorithm.getAlgorithm_id(), fieldId);
        }

        for (String useName : useList){
            Integer usedId = algorithmMapper.findUseIdByName(useName);
            algorithmMapper.deleteUSeForAlgorithm(algorithm.getAlgorithm_id(), usedId);
        }

        algorithmMapper.deleteAlgorithm(algorithm);

    }
}
