From e99bf68869770c1c946384f6db6bf34c4bb9c955 Mon Sep 17 00:00:00 2001 From: Mrking <782276617@qq.com> Date: Sun, 11 Aug 2024 21:56:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AE=A2=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/customer/CustomerController.java | 31 ++++ .../admin/customer/vo/CustomerRespVO.java | 8 +- .../admin/customer/vo/CustomerSaveReqVO.java | 2 +- .../customer/CustomerAddressDO.java | 10 +- .../dal/dataobject/customer/CustomerDO.java | 2 +- .../customer/CustomerServiceImplTest.java | 142 ++++++++++++++++++ hangtag-ui/src/api/oms/customer/index.ts | 105 ++++++------- .../src/views/oms/customer/CustomerForm.vue | 8 +- .../components/CustomerAddressForm.vue | 43 ++++-- hangtag-ui/src/views/oms/customer/index.vue | 2 +- 10 files changed, 268 insertions(+), 85 deletions(-) create mode 100644 hangtag-module-oms/hangtag-module-oms-biz/src/test/java/cn/hangtag/module/oms/service/customer/CustomerServiceImplTest.java diff --git a/hangtag-module-oms/hangtag-module-oms-biz/src/main/java/cn/hangtag/module/oms/controller/admin/customer/CustomerController.java b/hangtag-module-oms/hangtag-module-oms-biz/src/main/java/cn/hangtag/module/oms/controller/admin/customer/CustomerController.java index e652b11..16f7faa 100644 --- a/hangtag-module-oms/hangtag-module-oms-biz/src/main/java/cn/hangtag/module/oms/controller/admin/customer/CustomerController.java +++ b/hangtag-module-oms/hangtag-module-oms-biz/src/main/java/cn/hangtag/module/oms/controller/admin/customer/CustomerController.java @@ -1,5 +1,10 @@ package cn.hangtag.module.oms.controller.admin.customer; +import cn.hangtag.framework.common.util.collection.MapUtils; +import cn.hangtag.framework.ip.core.utils.AreaUtils; +import cn.hangtag.module.system.api.dept.dto.DeptRespDTO; +import cn.hangtag.module.system.api.user.dto.AdminUserRespDTO; +import cn.hutool.core.collection.CollUtil; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import org.springframework.validation.annotation.Validated; @@ -13,6 +18,7 @@ import javax.validation.*; import javax.servlet.http.*; import java.util.*; import java.io.IOException; +import java.util.stream.Stream; import cn.hangtag.framework.common.pojo.PageParam; import cn.hangtag.framework.common.pojo.PageResult; @@ -24,6 +30,7 @@ import cn.hangtag.framework.excel.core.util.ExcelUtils; import cn.hangtag.framework.apilog.core.annotation.ApiAccessLog; import static cn.hangtag.framework.apilog.core.enums.OperateTypeEnum.*; +import static java.util.Collections.singletonList; import cn.hangtag.module.oms.controller.admin.customer.vo.*; import cn.hangtag.module.oms.dal.dataobject.customer.CustomerDO; @@ -39,6 +46,8 @@ public class CustomerController { @Resource private CustomerService customerService; + + @PostMapping("/create") @Operation(summary = "创建客户") @PreAuthorize("@ss.hasPermission('oms:customer:create')") @@ -72,6 +81,24 @@ public class CustomerController { return success(BeanUtils.toBean(customer, CustomerRespVO.class)); } + private CustomerRespVO buildClueDetail(CustomerDO clue) { + if (clue == null) { + return null; + } + return buildDetailList(singletonList(clue)).get(0); + } + + + private List buildDetailList(List list) { + if (CollUtil.isEmpty(list)) { + return Collections.emptyList(); + } + // 2. 转换成 VO + return BeanUtils.toBean(list, CustomerRespVO.class, customerVO -> { + customerVO.setAreaName(AreaUtils.format(customerVO.getAreaId())); + }); + } + @GetMapping("/page") @Operation(summary = "获得客户分页") @PreAuthorize("@ss.hasPermission('oms:customer:query')") @@ -103,4 +130,8 @@ public class CustomerController { return success(customerService.getCustomerAddressListByCustomerId(customerId)); } + + + + } \ No newline at end of file diff --git a/hangtag-module-oms/hangtag-module-oms-biz/src/main/java/cn/hangtag/module/oms/controller/admin/customer/vo/CustomerRespVO.java b/hangtag-module-oms/hangtag-module-oms-biz/src/main/java/cn/hangtag/module/oms/controller/admin/customer/vo/CustomerRespVO.java index 4194669..7431139 100644 --- a/hangtag-module-oms/hangtag-module-oms-biz/src/main/java/cn/hangtag/module/oms/controller/admin/customer/vo/CustomerRespVO.java +++ b/hangtag-module-oms/hangtag-module-oms-biz/src/main/java/cn/hangtag/module/oms/controller/admin/customer/vo/CustomerRespVO.java @@ -34,9 +34,13 @@ public class CustomerRespVO { @ExcelProperty("联系人手机号") private String phone; - @Schema(description = "所属地区") + @Schema(description = "所属地区", example = "1024") @ExcelProperty("所属地区") - private String district; + private Integer areaId; + + @Schema(description = "地区名称", example = "北京市") + @ExcelProperty("地区名称") + private String areaName; @Schema(description = "类型", example = "2") @ExcelProperty("类型") diff --git a/hangtag-module-oms/hangtag-module-oms-biz/src/main/java/cn/hangtag/module/oms/controller/admin/customer/vo/CustomerSaveReqVO.java b/hangtag-module-oms/hangtag-module-oms-biz/src/main/java/cn/hangtag/module/oms/controller/admin/customer/vo/CustomerSaveReqVO.java index 8f87ef3..2e781e8 100644 --- a/hangtag-module-oms/hangtag-module-oms-biz/src/main/java/cn/hangtag/module/oms/controller/admin/customer/vo/CustomerSaveReqVO.java +++ b/hangtag-module-oms/hangtag-module-oms-biz/src/main/java/cn/hangtag/module/oms/controller/admin/customer/vo/CustomerSaveReqVO.java @@ -28,7 +28,7 @@ public class CustomerSaveReqVO { private String phone; @Schema(description = "所属地区") - private String district; + private Integer areaId; @Schema(description = "数据状态", example = "2") private String status; diff --git a/hangtag-module-oms/hangtag-module-oms-biz/src/main/java/cn/hangtag/module/oms/dal/dataobject/customer/CustomerAddressDO.java b/hangtag-module-oms/hangtag-module-oms-biz/src/main/java/cn/hangtag/module/oms/dal/dataobject/customer/CustomerAddressDO.java index 029bdf8..aea3d99 100644 --- a/hangtag-module-oms/hangtag-module-oms-biz/src/main/java/cn/hangtag/module/oms/dal/dataobject/customer/CustomerAddressDO.java +++ b/hangtag-module-oms/hangtag-module-oms-biz/src/main/java/cn/hangtag/module/oms/dal/dataobject/customer/CustomerAddressDO.java @@ -39,18 +39,10 @@ public class CustomerAddressDO extends BaseDO { * 联系电话 */ private String phone; - /** - * 省份 - */ - private String province; - /** - * 城市 - */ - private String city; /** * 区/县 */ - private String district; + private Integer areaId; /** * 详细地址 */ diff --git a/hangtag-module-oms/hangtag-module-oms-biz/src/main/java/cn/hangtag/module/oms/dal/dataobject/customer/CustomerDO.java b/hangtag-module-oms/hangtag-module-oms-biz/src/main/java/cn/hangtag/module/oms/dal/dataobject/customer/CustomerDO.java index fc0796c..89bdb06 100644 --- a/hangtag-module-oms/hangtag-module-oms-biz/src/main/java/cn/hangtag/module/oms/dal/dataobject/customer/CustomerDO.java +++ b/hangtag-module-oms/hangtag-module-oms-biz/src/main/java/cn/hangtag/module/oms/dal/dataobject/customer/CustomerDO.java @@ -50,7 +50,7 @@ public class CustomerDO extends BaseDO { /** * 所属地区 */ - private String district; + private Integer areaId; /** * 类型 */ diff --git a/hangtag-module-oms/hangtag-module-oms-biz/src/test/java/cn/hangtag/module/oms/service/customer/CustomerServiceImplTest.java b/hangtag-module-oms/hangtag-module-oms-biz/src/test/java/cn/hangtag/module/oms/service/customer/CustomerServiceImplTest.java new file mode 100644 index 0000000..c652edc --- /dev/null +++ b/hangtag-module-oms/hangtag-module-oms-biz/src/test/java/cn/hangtag/module/oms/service/customer/CustomerServiceImplTest.java @@ -0,0 +1,142 @@ +package cn.hangtag.module.oms.service.customer; + +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.hangtag.framework.test.core.ut.BaseDbUnitTest; + +import cn.hangtag.module.oms.controller.admin.customer.vo.*; +import cn.hangtag.module.oms.dal.dataobject.customer.CustomerDO; +import cn.hangtag.module.oms.dal.mysql.customer.CustomerMapper; +import cn.hangtag.framework.common.pojo.PageResult; + +import javax.annotation.Resource; +import org.springframework.context.annotation.Import; +import java.util.*; +import java.time.LocalDateTime; + +import static cn.hutool.core.util.RandomUtil.*; +import static cn.hangtag.module.oms.enums.ErrorCodeConstants.*; +import static cn.hangtag.framework.test.core.util.AssertUtils.*; +import static cn.hangtag.framework.test.core.util.RandomUtils.*; +import static cn.hangtag.framework.common.util.date.LocalDateTimeUtils.*; +import static cn.hangtag.framework.common.util.object.ObjectUtils.*; +import static cn.hangtag.framework.common.util.date.DateUtils.*; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +/** + * {@link CustomerServiceImpl} 的单元测试类 + * + * @author wwb + */ +@Import(CustomerServiceImpl.class) +public class CustomerServiceImplTest extends BaseDbUnitTest { + + @Resource + private CustomerServiceImpl customerService; + + @Resource + private CustomerMapper customerMapper; + + @Test + public void testCreateCustomer_success() { + // 准备参数 + CustomerSaveReqVO createReqVO = randomPojo(CustomerSaveReqVO.class).setId(null); + + // 调用 + Long customerId = customerService.createCustomer(createReqVO); + // 断言 + assertNotNull(customerId); + // 校验记录的属性是否正确 + CustomerDO customer = customerMapper.selectById(customerId); + assertPojoEquals(createReqVO, customer, "id"); + } + + @Test + public void testUpdateCustomer_success() { + // mock 数据 + CustomerDO dbCustomer = randomPojo(CustomerDO.class); + customerMapper.insert(dbCustomer);// @Sql: 先插入出一条存在的数据 + // 准备参数 + CustomerSaveReqVO updateReqVO = randomPojo(CustomerSaveReqVO.class, o -> { + o.setId(dbCustomer.getId()); // 设置更新的 ID + }); + + // 调用 + customerService.updateCustomer(updateReqVO); + // 校验是否更新正确 + CustomerDO customer = customerMapper.selectById(updateReqVO.getId()); // 获取最新的 + assertPojoEquals(updateReqVO, customer); + } + + @Test + public void testUpdateCustomer_notExists() { + // 准备参数 + CustomerSaveReqVO updateReqVO = randomPojo(CustomerSaveReqVO.class); + + // 调用, 并断言异常 + assertServiceException(() -> customerService.updateCustomer(updateReqVO), CUSTOMER_NOT_EXISTS); + } + + @Test + public void testDeleteCustomer_success() { + // mock 数据 + CustomerDO dbCustomer = randomPojo(CustomerDO.class); + customerMapper.insert(dbCustomer);// @Sql: 先插入出一条存在的数据 + // 准备参数 + Long id = dbCustomer.getId(); + + // 调用 + customerService.deleteCustomer(id); + // 校验数据不存在了 + assertNull(customerMapper.selectById(id)); + } + + @Test + public void testDeleteCustomer_notExists() { + // 准备参数 + Long id = randomLongId(); + + // 调用, 并断言异常 + assertServiceException(() -> customerService.deleteCustomer(id), CUSTOMER_NOT_EXISTS); + } + + @Test + @Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解 + public void testGetCustomerPage() { + // mock 数据 + CustomerDO dbCustomer = randomPojo(CustomerDO.class, o -> { // 等会查询到 + o.setId(null); + o.setName(null); + o.setType(null); + o.setStatus(null); + }); + customerMapper.insert(dbCustomer); + // 测试 id 不匹配 + customerMapper.insert(cloneIgnoreId(dbCustomer, o -> o.setId(null))); + // 测试 name 不匹配 + customerMapper.insert(cloneIgnoreId(dbCustomer, o -> o.setName(null))); + // 测试 type 不匹配 + customerMapper.insert(cloneIgnoreId(dbCustomer, o -> o.setType(null))); + // 测试 status 不匹配 + customerMapper.insert(cloneIgnoreId(dbCustomer, o -> o.setStatus(null))); + // 准备参数 + CustomerPageReqVO reqVO = new CustomerPageReqVO(); + reqVO.setId(null); + reqVO.setName(null); + reqVO.setType(null); + reqVO.setStatus(null); + + // 调用 + PageResult pageResult = customerService.getCustomerPage(reqVO); + // 断言 + assertEquals(1, pageResult.getTotal()); + assertEquals(1, pageResult.getList().size()); + assertPojoEquals(dbCustomer, pageResult.getList().get(0)); + } + +} \ No newline at end of file diff --git a/hangtag-ui/src/api/oms/customer/index.ts b/hangtag-ui/src/api/oms/customer/index.ts index 1335f28..7bcb205 100644 --- a/hangtag-ui/src/api/oms/customer/index.ts +++ b/hangtag-ui/src/api/oms/customer/index.ts @@ -1,52 +1,53 @@ -import request from '@/config/axios' - -// 客户 VO -export interface CustomerVO { - name: string // 名称 - email: string // 邮箱 - contacts: string // 联系人 - phone: string // 联系人手机号 - district: string // 所属地区 - type: string // 类型 - remarks: string // 备注 -} - -// 客户 API -export const CustomerApi = { - // 查询客户分页 - getCustomerPage: async (params: any) => { - return await request.get({ url: `/oms/customer/page`, params }) - }, - - // 查询客户详情 - getCustomer: async (id: number) => { - return await request.get({ url: `/oms/customer/get?id=` + id }) - }, - - // 新增客户 - createCustomer: async (data: CustomerVO) => { - return await request.post({ url: `/oms/customer/create`, data }) - }, - - // 修改客户 - updateCustomer: async (data: CustomerVO) => { - return await request.put({ url: `/oms/customer/update`, data }) - }, - - // 删除客户 - deleteCustomer: async (id: number) => { - return await request.delete({ url: `/oms/customer/delete?id=` + id }) - }, - - // 导出客户 Excel - exportCustomer: async (params) => { - return await request.download({ url: `/oms/customer/export-excel`, params }) - }, - -// ==================== 子表(用户地址) ==================== - - // 获得用户地址列表 - getCustomerAddressListByCustomerId: async (customerId) => { - return await request.get({ url: `/oms/customer/customer-address/list-by-customer-id?customerId=` + customerId }) - }, -} \ No newline at end of file +import request from '@/config/axios' + +// 客户 VO +export interface CustomerVO { + name: string // 名称 + email: string // 邮箱 + contacts: string // 联系人 + phone: string // 联系人手机号 + areaId: number // 所在地 + areaName?: string // 所在地名称 + type: string // 类型 + remarks: string // 备注 +} + +// 客户 API +export const CustomerApi = { + // 查询客户分页 + getCustomerPage: async (params: any) => { + return await request.get({ url: `/oms/customer/page`, params }) + }, + + // 查询客户详情 + getCustomer: async (id: number) => { + return await request.get({ url: `/oms/customer/get?id=` + id }) + }, + + // 新增客户 + createCustomer: async (data: CustomerVO) => { + return await request.post({ url: `/oms/customer/create`, data }) + }, + + // 修改客户 + updateCustomer: async (data: CustomerVO) => { + return await request.put({ url: `/oms/customer/update`, data }) + }, + + // 删除客户 + deleteCustomer: async (id: number) => { + return await request.delete({ url: `/oms/customer/delete?id=` + id }) + }, + + // 导出客户 Excel + exportCustomer: async (params) => { + return await request.download({ url: `/oms/customer/export-excel`, params }) + }, + +// ==================== 子表(用户地址) ==================== + + // 获得用户地址列表 + getCustomerAddressListByCustomerId: async (customerId) => { + return await request.get({ url: `/oms/customer/customer-address/list-by-customer-id?customerId=` + customerId }) + }, +} diff --git a/hangtag-ui/src/views/oms/customer/CustomerForm.vue b/hangtag-ui/src/views/oms/customer/CustomerForm.vue index 5395e91..2d945de 100644 --- a/hangtag-ui/src/views/oms/customer/CustomerForm.vue +++ b/hangtag-ui/src/views/oms/customer/CustomerForm.vue @@ -19,9 +19,9 @@ - + { email: undefined, contacts: undefined, phone: undefined, - district: undefined, + areaId: undefined, status: '1', type: undefined, remarks: undefined, diff --git a/hangtag-ui/src/views/oms/customer/components/CustomerAddressForm.vue b/hangtag-ui/src/views/oms/customer/components/CustomerAddressForm.vue index 048a5f4..8a0f6d6 100644 --- a/hangtag-ui/src/views/oms/customer/components/CustomerAddressForm.vue +++ b/hangtag-ui/src/views/oms/customer/components/CustomerAddressForm.vue @@ -23,14 +23,23 @@ - + - + + @@ -74,6 +83,8 @@ diff --git a/hangtag-ui/src/views/oms/customer/index.vue b/hangtag-ui/src/views/oms/customer/index.vue index 6c13192..f948723 100644 --- a/hangtag-ui/src/views/oms/customer/index.vue +++ b/hangtag-ui/src/views/oms/customer/index.vue @@ -104,7 +104,7 @@ - +