feat: 增加计量单位的接口

dev
huangfeng 1 year ago
parent 3e7feed921
commit 5bc33e8ca0

@ -0,0 +1,57 @@
package com.xydl.cac.controller;
import com.xydl.cac.entity.Unit;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.model.Response;
import com.xydl.cac.service.UnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
@RestController
@Api(tags = {"计量单位相关接口"})
@RequestMapping("unit")
@Slf4j
public class UnitController extends BasicController {
@Resource
UnitService service;
@GetMapping("listAll")
@ApiOperation("查询全部列表")
public Response<List<Unit>> listAll() {
List<Unit> result = service.listAll();
return Response.success(result);
}
@PostMapping("add")
@ApiOperation("新增")
public Response<Unit> add(@Validated @RequestBody Unit item) throws Exception {
Unit result = service.add(item);
return Response.success(result);
}
@PostMapping("update")
@ApiOperation("更新")
public Response<String> update(@Validated @RequestBody Unit item) throws Exception {
if (item.getId() == null) {
throw new BusinessException("ID不能为空!");
}
service.update(item);
return Response.success("OK");
}
@PostMapping("delete")
@ApiOperation("删除")
public Response<String> delete(@Validated @NotNull(message = "ID不能为空!") Integer id) {
service.delete(id);
return Response.success("OK");
}
}

@ -0,0 +1,36 @@
package com.xydl.cac.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "unit")
@ApiModel("计量单位表")
public class Unit {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@NotBlank(message = "字段不能为空")
@ApiModelProperty("字段")
@Column(name = "field")
private String field;
@ApiModelProperty("单位")
@Column(name = "unit")
private String unit;
}

@ -0,0 +1,13 @@
package com.xydl.cac.repository;
import com.xydl.cac.entity.Unit;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UnitRepository extends JpaRepository<Unit, Integer>, JpaSpecificationExecutor<Unit> {
List<Unit> findByField(String field);
}

@ -0,0 +1,16 @@
package com.xydl.cac.service;
import com.xydl.cac.entity.Unit;
import java.util.List;
public interface UnitService {
List<Unit> listAll();
Unit add(Unit item) throws Exception;
void update(Unit item) throws Exception;
void delete(Integer id);
}

@ -0,0 +1,47 @@
package com.xydl.cac.service.impl;
import com.xydl.cac.entity.Unit;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.repository.UnitRepository;
import com.xydl.cac.service.UnitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.List;
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class UnitServiceImpl implements UnitService {
@Resource
UnitRepository repository;
@Override
public List<Unit> listAll() {
return repository.findAll();
}
@Override
public Unit add(Unit item) throws Exception {
item.setId(null);
List<Unit> list = repository.findByField(item.getField());
if (!CollectionUtils.isEmpty(list)) {
throw new BusinessException("该字段已存在");
}
return repository.save(item);
}
@Override
public void update(Unit item) throws Exception {
repository.save(item);
}
@Override
public void delete(Integer id) {
repository.deleteById(id);
}
}
Loading…
Cancel
Save