package org.sky.develop.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.sky.core.log.exception.ServiceException;
import org.sky.core.mp.base.BaseServiceImpl;
import org.sky.develop.mapper.ModelMapper;
import org.sky.develop.pojo.entity.Code;
import org.sky.develop.pojo.entity.Model;
import org.sky.develop.pojo.entity.ModelPrototype;
import org.sky.develop.service.ICodeService;
import org.sky.develop.service.IModelPrototypeService;
import org.sky.develop.service.IModelService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 数据模型表 服务实现类
 *
 */
@Service
@RequiredArgsConstructor
public class ModelServiceImpl extends BaseServiceImpl<ModelMapper, Model> implements IModelService {

	private final IModelPrototypeService modelPrototypeService;
	private final ICodeService codeService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean delete(List<Long> ids) {
		boolean modelTemp = this.deleteLogic(ids);
		if (modelTemp) {
			if (modelPrototypeService.count(Wrappers.<ModelPrototype>lambdaQuery().in(ModelPrototype::getModelId, ids)) > 0) {
				boolean prototypeTemp = modelPrototypeService.remove(Wrappers.<ModelPrototype>lambdaQuery().in(ModelPrototype::getModelId, ids));
				if (!prototypeTemp) {
					throw new ServiceException("删除数据模型成功，关联数据原型删除失败");
				}
			}
			if (codeService.count(Wrappers.<Code>lambdaQuery().in(Code::getModelId, ids)) > 0) {
				boolean codeTemp = codeService.remove(Wrappers.<Code>lambdaQuery().in(Code::getModelId, ids));
				if (!codeTemp) {
					throw new ServiceException("删除数据模型成功，关联代码生成配置删除失败");
				}
			}
		}
		return true;
	}

}
