package com.xy.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xy.dto.AlgorithmDto;
import com.xy.entity.Algorithm;
import com.xy.mapper.AlgorithmMapper;
import com.xy.utils.AuthorizeUtils;
import com.xy.utils.MybatisPlusQuery;
import com.xy.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.List;
import static com.xy.utils.Beans.copy;
/**
*
* 算法平台 服务实现类
*
*
* @author hechunping
* @since 2023-01-12
*/
@Service
@AllArgsConstructor
@Api(tags = "算法平台")
public class AlgorithmServiceImpl extends ServiceImpl implements AlgorithmService {
@Override
@ApiOperation("所有字段列表")
public R> listAllColumn(@RequestBody AlgorithmDto.ListAllColumn listAllColumn) {
LambdaQueryWrapper lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(listAllColumn, Algorithm.class).build();
List list = list(lambdaQueryWrapper);
return R.ok(copy(AlgorithmDto.ListAllColumn.class, list));
}
@Override
@ApiOperation("ID和Name列表")
public R> ListNameId() {
LambdaQueryWrapper lambdaQueryWrapper = Wrappers.lambdaQuery().eq(Algorithm::getStatus,1).select(Algorithm::getId,Algorithm::getName);
List list = baseMapper.selectList(lambdaQueryWrapper);
return R.ok(copy(AlgorithmDto.ListNameId.class, list));
}
@Override
@ApiOperation(value = "新增、更新算法平台", notes = "不传id为新增,传id为修改")
public R saveOrUpdate(@Validated @RequestBody AlgorithmDto.Save saveDto) {
Long id = saveDto.getId();
boolean isAdd = id == null || id == 0;
//判断品牌是否存在
LambdaQueryWrapper lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.eq(Algorithm::getName, saveDto.getName()).last("limit 1").select(Algorithm::getId);
Algorithm exists = getOne(lambdaQueryWrapper);
if (exists != null) {
if (isAdd) {
return R.fail("平台已存在");
} else {
if (exists.getId().longValue() != saveDto.getId().longValue()) {
return R.fail("平台已存在");
}
}
}
Algorithm algorithm = copy(Algorithm.class, saveDto);
if (isAdd) {
algorithm.createUserTime(AuthorizeUtils.getLoginId(Long.class));
baseMapper.insert(algorithm);
} else {
algorithm.updateUserTime(AuthorizeUtils.getLoginId(Long.class));
baseMapper.updateById(algorithm);
}
return R.ok();
}
}