package com.ai.da.service.impl;


import com.ai.da.common.config.exception.BusinessException;
import com.ai.da.mapper.primary.PythonTAllInfoMapper;
import com.ai.da.mapper.primary.entity.PythonTAllInfo;
import com.ai.da.python.vo.DesignPythonItem;
import com.ai.da.python.vo.DesignPythonObjects;
import com.ai.da.service.PythonTAllInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * (PythonTAllInfo)表服务实现类
 *
 * @author SHAHAIBO
 * @since 2023-09-08 17:03:47
 */
@Service
@Slf4j
public class PythonTAllInfoServiceImpl extends ServiceImpl<PythonTAllInfoMapper, PythonTAllInfo> implements PythonTAllInfoService {
    @Resource
    private PythonTAllInfoMapper pythonTAllInfoMapper;


    @Override
    public Long getImageIdByPath(String path) {
        if (StringUtils.isEmpty(path)) {
            throw new BusinessException("path.cannot.be.empty");
        }
        QueryWrapper<PythonTAllInfo> qw = new QueryWrapper<>();
        qw.lambda().eq(PythonTAllInfo::getImagePath, path);
        qw.lambda().last("limit 1");
        List<PythonTAllInfo> pythonTAllInfos = pythonTAllInfoMapper.selectList(qw);
        if (!CollectionUtils.isEmpty(pythonTAllInfos)) {
            return pythonTAllInfos.get(0).getId();
        }
        PythonTAllInfo pythonTAllInfo = new PythonTAllInfo();
        pythonTAllInfo.setImagePath(path);
        int insert = pythonTAllInfoMapper.insert(pythonTAllInfo);
        if (insert != 1) {
            throw new BusinessException("save.pythonTAllInfo.failed");
        }
        return pythonTAllInfo.getId();
    }

    @Override
    public void relationImageId(DesignPythonObjects pythonObjects) {
        if (Objects.isNull(pythonObjects)) {
            return;
        }
        pythonObjects.getObjects().forEach(
                o -> {
                    for (DesignPythonItem item : o.getItems()) {
                        List<Long> list = new ArrayList<>();
                        list.add(1L);
                        list.add(1L);
                        item.setOffset(list);
                        item.setResize_scale(new Float[]{1.0f, 1.0f});
                        String path = item.getPath();
                        if (StringUtils.isEmpty(path)) {
                            String bodyPath = item.getBody_path();
                            Long imageId = getImageIdByPath(bodyPath);
                            item.setImage_id(imageId);
                        } else {
                            Long imageId = getImageIdByPath(path);
                            item.setImage_id(imageId);
                        }
                    }
                }
        );
    }
}
