diff --git a/zsw-farm/zsw-farm-api/src/main/java/cn/iocoder/yudao/module/farm/enums/ErrorCodeConstants.java b/zsw-farm/zsw-farm-api/src/main/java/cn/iocoder/yudao/module/farm/enums/ErrorCodeConstants.java index dc75dff7..79a31f7c 100644 --- a/zsw-farm/zsw-farm-api/src/main/java/cn/iocoder/yudao/module/farm/enums/ErrorCodeConstants.java +++ b/zsw-farm/zsw-farm-api/src/main/java/cn/iocoder/yudao/module/farm/enums/ErrorCodeConstants.java @@ -17,6 +17,8 @@ public interface ErrorCodeConstants{ ErrorCode TASK_NOT_EXISTS = new ErrorCode(1000007006, "农场项目不存在"); + ErrorCode AREA_NOT_EXISTS = new ErrorCode(1000007007, "农村工作区域不存在"); + diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/area/AreaController.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/area/AreaController.java new file mode 100644 index 00000000..cee5146c --- /dev/null +++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/area/AreaController.java @@ -0,0 +1,100 @@ +package cn.iocoder.yudao.module.farm.controller.admin.area; + +import org.springframework.web.bind.annotation.*; +import javax.annotation.Resource; +import org.springframework.validation.annotation.Validated; +import org.springframework.security.access.prepost.PreAuthorize; +import io.swagger.annotations.*; + +import javax.validation.constraints.*; +import javax.validation.*; +import javax.servlet.http.*; +import java.util.*; +import java.io.IOException; + +import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; + +import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; + +import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog; +import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*; + +import cn.iocoder.yudao.module.farm.controller.admin.area.vo.*; +import cn.iocoder.yudao.module.farm.dal.dataobject.area.AreaDO; +import cn.iocoder.yudao.module.farm.convert.area.AreaConvert; +import cn.iocoder.yudao.module.farm.service.area.AreaService; + +@Api(tags = "管理后台 - ") +@RestController +@RequestMapping("/farm/area") +@Validated +public class AreaController { + + @Resource + private AreaService areaService; + + @PostMapping("/create") + @ApiOperation("创建") + @PreAuthorize("@ss.hasPermission('farm:area:create')") + public CommonResult createArea(@Valid @RequestBody AreaCreateReqVO createReqVO) { + return success(areaService.createArea(createReqVO)); + } + + @PutMapping("/update") + @ApiOperation("更新") + @PreAuthorize("@ss.hasPermission('farm:area:update')") + public CommonResult updateArea(@Valid @RequestBody AreaUpdateReqVO updateReqVO) { + areaService.updateArea(updateReqVO); + return success(true); + } + + @DeleteMapping("/delete") + @ApiOperation("删除") + @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class) + @PreAuthorize("@ss.hasPermission('farm:area:delete')") + public CommonResult deleteArea(@RequestParam("id") Long id) { + areaService.deleteArea(id); + return success(true); + } + + @GetMapping("/get") + @ApiOperation("获得") + @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class) + @PreAuthorize("@ss.hasPermission('farm:area:query')") + public CommonResult getArea(@RequestParam("id") Long id) { + AreaDO area = areaService.getArea(id); + return success(AreaConvert.INSTANCE.convert(area)); + } + + @GetMapping("/list") + @ApiOperation("获得列表") + @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class) + @PreAuthorize("@ss.hasPermission('farm:area:query')") + public CommonResult> getAreaList(@RequestParam("ids") Collection ids) { + List list = areaService.getAreaList(ids); + return success(AreaConvert.INSTANCE.convertList(list)); + } + + @GetMapping("/page") + @ApiOperation("获得分页") + @PreAuthorize("@ss.hasPermission('farm:area:query')") + public CommonResult> getAreaPage(@Valid AreaPageReqVO pageVO) { + PageResult pageResult = areaService.getAreaPage(pageVO); + return success(AreaConvert.INSTANCE.convertPage(pageResult)); + } + + @GetMapping("/export-excel") + @ApiOperation("导出 Excel") + @PreAuthorize("@ss.hasPermission('farm:area:export')") + @OperateLog(type = EXPORT) + public void exportAreaExcel(@Valid AreaExportReqVO exportReqVO, + HttpServletResponse response) throws IOException { + List list = areaService.getAreaList(exportReqVO); + // 导出 Excel + List datas = AreaConvert.INSTANCE.convertList02(list); + ExcelUtils.write(response, ".xls", "数据", AreaExcelVO.class, datas); + } + +} diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/area/vo/AreaBaseVO.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/area/vo/AreaBaseVO.java new file mode 100644 index 00000000..fe583d46 --- /dev/null +++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/area/vo/AreaBaseVO.java @@ -0,0 +1,18 @@ +package cn.iocoder.yudao.module.farm.controller.admin.area.vo; + +import lombok.*; +import java.util.*; +import io.swagger.annotations.*; +import javax.validation.constraints.*; + +/** +* Base VO,提供给添加、修改、详细的子 VO 使用 +* 如果子 VO 存在差异的字段,请不要添加到这里,影响 Swagger 文档生成 +*/ +@Data +public class AreaBaseVO { + + @ApiModelProperty(value = "") + private String name; + +} diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/area/vo/AreaCreateReqVO.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/area/vo/AreaCreateReqVO.java new file mode 100644 index 00000000..c1bf4eed --- /dev/null +++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/area/vo/AreaCreateReqVO.java @@ -0,0 +1,14 @@ +package cn.iocoder.yudao.module.farm.controller.admin.area.vo; + +import lombok.*; +import java.util.*; +import io.swagger.annotations.*; +import javax.validation.constraints.*; + +@ApiModel("管理后台 - 创建 Request VO") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +public class AreaCreateReqVO extends AreaBaseVO { + +} diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/area/vo/AreaExcelVO.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/area/vo/AreaExcelVO.java new file mode 100644 index 00000000..5b9b9a05 --- /dev/null +++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/area/vo/AreaExcelVO.java @@ -0,0 +1,26 @@ +package cn.iocoder.yudao.module.farm.controller.admin.area.vo; + +import lombok.*; +import java.util.*; +import io.swagger.annotations.*; + +import com.alibaba.excel.annotation.ExcelProperty; + +/** + * Excel VO + * + * @author 系统管理员 + */ +@Data +public class AreaExcelVO { + + @ExcelProperty("") + private Long id; + + @ExcelProperty("") + private String name; + + @ExcelProperty("") + private Date createTime; + +} diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/area/vo/AreaExportReqVO.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/area/vo/AreaExportReqVO.java new file mode 100644 index 00000000..c8733ea4 --- /dev/null +++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/area/vo/AreaExportReqVO.java @@ -0,0 +1,26 @@ +package cn.iocoder.yudao.module.farm.controller.admin.area.vo; + +import lombok.*; +import java.util.*; +import io.swagger.annotations.*; +import cn.iocoder.yudao.framework.common.pojo.PageParam; +import org.springframework.format.annotation.DateTimeFormat; + +import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; + +@ApiModel(value = "管理后台 - Excel 导出 Request VO", description = "参数和 AreaPageReqVO 是一致的") +@Data +public class AreaExportReqVO { + + @ApiModelProperty(value = "") + private String name; + + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) + @ApiModelProperty(value = "开始") + private Date beginCreateTime; + + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) + @ApiModelProperty(value = "结束") + private Date endCreateTime; + +} diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/area/vo/AreaPageReqVO.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/area/vo/AreaPageReqVO.java new file mode 100644 index 00000000..76ebf048 --- /dev/null +++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/area/vo/AreaPageReqVO.java @@ -0,0 +1,28 @@ +package cn.iocoder.yudao.module.farm.controller.admin.area.vo; + +import lombok.*; +import java.util.*; +import io.swagger.annotations.*; +import cn.iocoder.yudao.framework.common.pojo.PageParam; +import org.springframework.format.annotation.DateTimeFormat; + +import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; + +@ApiModel("管理后台 - 分页 Request VO") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +public class AreaPageReqVO extends PageParam { + + @ApiModelProperty(value = "") + private String name; + + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) + @ApiModelProperty(value = "开始") + private Date beginCreateTime; + + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) + @ApiModelProperty(value = "结束") + private Date endCreateTime; + +} diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/area/vo/AreaRespVO.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/area/vo/AreaRespVO.java new file mode 100644 index 00000000..ba197d68 --- /dev/null +++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/area/vo/AreaRespVO.java @@ -0,0 +1,19 @@ +package cn.iocoder.yudao.module.farm.controller.admin.area.vo; + +import lombok.*; +import java.util.*; +import io.swagger.annotations.*; + +@ApiModel("管理后台 - Response VO") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +public class AreaRespVO extends AreaBaseVO { + + @ApiModelProperty(value = "", required = true) + private Long id; + + @ApiModelProperty(value = "") + private Date createTime; + +} diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/area/vo/AreaUpdateReqVO.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/area/vo/AreaUpdateReqVO.java new file mode 100644 index 00000000..d4037af6 --- /dev/null +++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/controller/admin/area/vo/AreaUpdateReqVO.java @@ -0,0 +1,18 @@ +package cn.iocoder.yudao.module.farm.controller.admin.area.vo; + +import lombok.*; +import java.util.*; +import io.swagger.annotations.*; +import javax.validation.constraints.*; + +@ApiModel("管理后台 - 更新 Request VO") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +public class AreaUpdateReqVO extends AreaBaseVO { + + @ApiModelProperty(value = "", required = true) + @NotNull(message = "不能为空") + private Long id; + +} diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/convert/area/AreaConvert.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/convert/area/AreaConvert.java new file mode 100644 index 00000000..5394b36d --- /dev/null +++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/convert/area/AreaConvert.java @@ -0,0 +1,34 @@ +package cn.iocoder.yudao.module.farm.convert.area; + +import java.util.*; + +import cn.iocoder.yudao.framework.common.pojo.PageResult; + +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; +import cn.iocoder.yudao.module.farm.controller.admin.area.vo.*; +import cn.iocoder.yudao.module.farm.dal.dataobject.area.AreaDO; + +/** + * Convert + * + * @author 系统管理员 + */ +@Mapper +public interface AreaConvert { + + AreaConvert INSTANCE = Mappers.getMapper(AreaConvert.class); + + AreaDO convert(AreaCreateReqVO bean); + + AreaDO convert(AreaUpdateReqVO bean); + + AreaRespVO convert(AreaDO bean); + + List convertList(List list); + + PageResult convertPage(PageResult page); + + List convertList02(List list); + +} diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/dal/dataobject/area/AreaDO.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/dal/dataobject/area/AreaDO.java new file mode 100644 index 00000000..282b109c --- /dev/null +++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/dal/dataobject/area/AreaDO.java @@ -0,0 +1,32 @@ +package cn.iocoder.yudao.module.farm.dal.dataobject.area; + +import lombok.*; +import java.util.*; +import com.baomidou.mybatisplus.annotation.*; +import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; + +/** + * DO + * + * @author 系统管理员 + */ +@TableName("farm_area") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class AreaDO extends BaseDO { + + /** + * + */ + @TableId + private Long id; + /** + * + */ + private String name; + +} diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/dal/mysql/area/AreaMapper.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/dal/mysql/area/AreaMapper.java new file mode 100644 index 00000000..614ab748 --- /dev/null +++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/dal/mysql/area/AreaMapper.java @@ -0,0 +1,34 @@ +package cn.iocoder.yudao.module.farm.dal.mysql.area; + +import java.util.*; + +import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; +import cn.iocoder.yudao.module.farm.dal.dataobject.area.AreaDO; +import org.apache.ibatis.annotations.Mapper; +import cn.iocoder.yudao.module.farm.controller.admin.area.vo.*; + +/** + * Mapper + * + * @author 系统管理员 + */ +@Mapper +public interface AreaMapper extends BaseMapperX { + + default PageResult selectPage(AreaPageReqVO reqVO) { + return selectPage(reqVO, new LambdaQueryWrapperX() + .likeIfPresent(AreaDO::getName, reqVO.getName()) + .betweenIfPresent(AreaDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime()) + .orderByDesc(AreaDO::getId)); + } + + default List selectList(AreaExportReqVO reqVO) { + return selectList(new LambdaQueryWrapperX() + .likeIfPresent(AreaDO::getName, reqVO.getName()) + .betweenIfPresent(AreaDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime()) + .orderByDesc(AreaDO::getId)); + } + +} diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/service/area/AreaService.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/service/area/AreaService.java new file mode 100644 index 00000000..fc91ed76 --- /dev/null +++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/service/area/AreaService.java @@ -0,0 +1,70 @@ +package cn.iocoder.yudao.module.farm.service.area; + +import java.util.*; +import javax.validation.*; +import cn.iocoder.yudao.module.farm.controller.admin.area.vo.*; +import cn.iocoder.yudao.module.farm.dal.dataobject.area.AreaDO; +import cn.iocoder.yudao.framework.common.pojo.PageResult; + +/** + * Service 接口 + * + * @author 系统管理员 + */ +public interface AreaService { + + /** + * 创建 + * + * @param createReqVO 创建信息 + * @return 编号 + */ + Long createArea(@Valid AreaCreateReqVO createReqVO); + + /** + * 更新 + * + * @param updateReqVO 更新信息 + */ + void updateArea(@Valid AreaUpdateReqVO updateReqVO); + + /** + * 删除 + * + * @param id 编号 + */ + void deleteArea(Long id); + + /** + * 获得 + * + * @param id 编号 + * @return + */ + AreaDO getArea(Long id); + + /** + * 获得列表 + * + * @param ids 编号 + * @return 列表 + */ + List getAreaList(Collection ids); + + /** + * 获得分页 + * + * @param pageReqVO 分页查询 + * @return 分页 + */ + PageResult getAreaPage(AreaPageReqVO pageReqVO); + + /** + * 获得列表, 用于 Excel 导出 + * + * @param exportReqVO 查询条件 + * @return 列表 + */ + List getAreaList(AreaExportReqVO exportReqVO); + +} diff --git a/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/service/area/AreaServiceImpl.java b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/service/area/AreaServiceImpl.java new file mode 100644 index 00000000..df649848 --- /dev/null +++ b/zsw-farm/zsw-farm-impl/src/main/java/cn/iocoder/yudao/module/farm/service/area/AreaServiceImpl.java @@ -0,0 +1,82 @@ +package cn.iocoder.yudao.module.farm.service.area; + +import org.springframework.stereotype.Service; +import javax.annotation.Resource; +import org.springframework.validation.annotation.Validated; + +import java.util.*; +import cn.iocoder.yudao.module.farm.controller.admin.area.vo.*; +import cn.iocoder.yudao.module.farm.dal.dataobject.area.AreaDO; +import cn.iocoder.yudao.framework.common.pojo.PageResult; + +import cn.iocoder.yudao.module.farm.convert.area.AreaConvert; +import cn.iocoder.yudao.module.farm.dal.mysql.area.AreaMapper; + +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; +import static cn.iocoder.yudao.module.farm.enums.ErrorCodeConstants.*; + +/** + * Service 实现类 + * + * @author 系统管理员 + */ +@Service +@Validated +public class AreaServiceImpl implements AreaService { + + @Resource + private AreaMapper areaMapper; + + @Override + public Long createArea(AreaCreateReqVO createReqVO) { + // 插入 + AreaDO area = AreaConvert.INSTANCE.convert(createReqVO); + areaMapper.insert(area); + // 返回 + return area.getId(); + } + + @Override + public void updateArea(AreaUpdateReqVO updateReqVO) { + // 校验存在 + this.validateAreaExists(updateReqVO.getId()); + // 更新 + AreaDO updateObj = AreaConvert.INSTANCE.convert(updateReqVO); + areaMapper.updateById(updateObj); + } + + @Override + public void deleteArea(Long id) { + // 校验存在 + this.validateAreaExists(id); + // 删除 + areaMapper.deleteById(id); + } + + private void validateAreaExists(Long id) { + if (areaMapper.selectById(id) == null) { + throw exception(AREA_NOT_EXISTS); + } + } + + @Override + public AreaDO getArea(Long id) { + return areaMapper.selectById(id); + } + + @Override + public List getAreaList(Collection ids) { + return areaMapper.selectBatchIds(ids); + } + + @Override + public PageResult getAreaPage(AreaPageReqVO pageReqVO) { + return areaMapper.selectPage(pageReqVO); + } + + @Override + public List getAreaList(AreaExportReqVO exportReqVO) { + return areaMapper.selectList(exportReqVO); + } + +} diff --git a/zsw-farm/zsw-farm-impl/src/main/resources/mapper/area/AreaMapper.xml b/zsw-farm/zsw-farm-impl/src/main/resources/mapper/area/AreaMapper.xml new file mode 100644 index 00000000..5a8129db --- /dev/null +++ b/zsw-farm/zsw-farm-impl/src/main/resources/mapper/area/AreaMapper.xml @@ -0,0 +1,12 @@ + + + + + + + diff --git a/zsw-farm/zsw-farm-impl/src/test/java/cn/iocoder/yudao/module/farm/service/area/AreaServiceImplTest.java b/zsw-farm/zsw-farm-impl/src/test/java/cn/iocoder/yudao/module/farm/service/area/AreaServiceImplTest.java new file mode 100644 index 00000000..427adb57 --- /dev/null +++ b/zsw-farm/zsw-farm-impl/src/test/java/cn/iocoder/yudao/module/farm/service/area/AreaServiceImplTest.java @@ -0,0 +1,159 @@ +package cn.iocoder.yudao.module.farm.service.area; + +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.mock.mockito.MockBean; + +import javax.annotation.Resource; + +import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; + +import cn.iocoder.yudao.module.farm.controller.admin.area.vo.*; +import cn.iocoder.yudao.module.farm.dal.dataobject.area.AreaDO; +import cn.iocoder.yudao.module.farm.dal.mysql.area.AreaMapper; +import cn.iocoder.yudao.framework.common.pojo.PageResult; + +import javax.annotation.Resource; +import org.springframework.context.annotation.Import; +import java.util.*; + +import static cn.hutool.core.util.RandomUtil.*; +import static cn.iocoder.yudao.module.farm.enums.ErrorCodeConstants.*; +import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.*; +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.*; +import static cn.iocoder.yudao.framework.common.util.date.DateUtils.*; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +/** +* {@link AreaServiceImpl} 的单元测试类 +* +* @author 系统管理员 +*/ +@Import(AreaServiceImpl.class) +public class AreaServiceImplTest extends BaseDbUnitTest { + + @Resource + private AreaServiceImpl areaService; + + @Resource + private AreaMapper areaMapper; + + @Test + public void testCreateArea_success() { + // 准备参数 + AreaCreateReqVO reqVO = randomPojo(AreaCreateReqVO.class); + + // 调用 + Long areaId = areaService.createArea(reqVO); + // 断言 + assertNotNull(areaId); + // 校验记录的属性是否正确 + AreaDO area = areaMapper.selectById(areaId); + assertPojoEquals(reqVO, area); + } + + @Test + public void testUpdateArea_success() { + // mock 数据 + AreaDO dbArea = randomPojo(AreaDO.class); + areaMapper.insert(dbArea);// @Sql: 先插入出一条存在的数据 + // 准备参数 + AreaUpdateReqVO reqVO = randomPojo(AreaUpdateReqVO.class, o -> { + o.setId(dbArea.getId()); // 设置更新的 ID + }); + + // 调用 + areaService.updateArea(reqVO); + // 校验是否更新正确 + AreaDO area = areaMapper.selectById(reqVO.getId()); // 获取最新的 + assertPojoEquals(reqVO, area); + } + + @Test + public void testUpdateArea_notExists() { + // 准备参数 + AreaUpdateReqVO reqVO = randomPojo(AreaUpdateReqVO.class); + + // 调用, 并断言异常 + assertServiceException(() -> areaService.updateArea(reqVO), AREA_NOT_EXISTS); + } + + @Test + public void testDeleteArea_success() { + // mock 数据 + AreaDO dbArea = randomPojo(AreaDO.class); + areaMapper.insert(dbArea);// @Sql: 先插入出一条存在的数据 + // 准备参数 + Long id = dbArea.getId(); + + // 调用 + areaService.deleteArea(id); + // 校验数据不存在了 + assertNull(areaMapper.selectById(id)); + } + + @Test + public void testDeleteArea_notExists() { + // 准备参数 + Long id = randomLongId(); + + // 调用, 并断言异常 + assertServiceException(() -> areaService.deleteArea(id), AREA_NOT_EXISTS); + } + + @Test + @Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解 + public void testGetAreaPage() { + // mock 数据 + AreaDO dbArea = randomPojo(AreaDO.class, o -> { // 等会查询到 + o.setName(null); + o.setCreateTime(null); + }); + areaMapper.insert(dbArea); + // 测试 name 不匹配 + areaMapper.insert(cloneIgnoreId(dbArea, o -> o.setName(null))); + // 测试 createTime 不匹配 + areaMapper.insert(cloneIgnoreId(dbArea, o -> o.setCreateTime(null))); + // 准备参数 + AreaPageReqVO reqVO = new AreaPageReqVO(); + reqVO.setName(null); + reqVO.setBeginCreateTime(null); + reqVO.setEndCreateTime(null); + + // 调用 + PageResult pageResult = areaService.getAreaPage(reqVO); + // 断言 + assertEquals(1, pageResult.getTotal()); + assertEquals(1, pageResult.getList().size()); + assertPojoEquals(dbArea, pageResult.getList().get(0)); + } + + @Test + @Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解 + public void testGetAreaList() { + // mock 数据 + AreaDO dbArea = randomPojo(AreaDO.class, o -> { // 等会查询到 + o.setName(null); + o.setCreateTime(null); + }); + areaMapper.insert(dbArea); + // 测试 name 不匹配 + areaMapper.insert(cloneIgnoreId(dbArea, o -> o.setName(null))); + // 测试 createTime 不匹配 + areaMapper.insert(cloneIgnoreId(dbArea, o -> o.setCreateTime(null))); + // 准备参数 + AreaExportReqVO reqVO = new AreaExportReqVO(); + reqVO.setName(null); + reqVO.setBeginCreateTime(null); + reqVO.setEndCreateTime(null); + + // 调用 + List list = areaService.getAreaList(reqVO); + // 断言 + assertEquals(1, list.size()); + assertPojoEquals(dbArea, list.get(0)); + } + +}