|
|
@@ -0,0 +1,164 @@
|
|
|
+package org.dromara.customer.pc.controller;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import jakarta.validation.constraints.*;
|
|
|
+import org.dromara.common.core.domain.R;
|
|
|
+import org.dromara.common.log.annotation.Log;
|
|
|
+import org.dromara.common.log.enums.BusinessType;
|
|
|
+import org.dromara.common.mybatis.core.page.PageQuery;
|
|
|
+import org.dromara.common.mybatis.core.page.TableDataInfo;
|
|
|
+import org.dromara.common.satoken.utils.LoginHelper;
|
|
|
+import org.dromara.common.web.core.BaseController;
|
|
|
+import org.dromara.customer.domain.bo.CustomerShippingAddressBo;
|
|
|
+import org.dromara.customer.domain.vo.CustomerShippingAddressVo;
|
|
|
+import org.dromara.customer.service.ICustomerShippingAddressService;
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import org.dromara.common.core.validate.AddGroup;
|
|
|
+import org.dromara.common.core.validate.EditGroup;
|
|
|
+import org.dromara.common.idempotent.annotation.RepeatSubmit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * PC端 - 收货地址管理
|
|
|
+ * 前端访问路由地址为:/pc/enterprise/address
|
|
|
+ *
|
|
|
+ * @author Claude
|
|
|
+ * @date 2026-01-27
|
|
|
+ */
|
|
|
+@Validated
|
|
|
+@RequiredArgsConstructor
|
|
|
+@RestController
|
|
|
+@RequestMapping("/pc/enterprise/address")
|
|
|
+public class PcAddressController extends BaseController {
|
|
|
+
|
|
|
+ private final ICustomerShippingAddressService customerShippingAddressService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询当前企业的收货地址列表
|
|
|
+ * PC端用户只能查询自己企业的地址
|
|
|
+ */
|
|
|
+ @GetMapping("/list")
|
|
|
+ public TableDataInfo<CustomerShippingAddressVo> list(CustomerShippingAddressBo bo, PageQuery pageQuery) {
|
|
|
+ // 获取当前登录用户的企业ID
|
|
|
+ Long customerId = LoginHelper.getUserId();
|
|
|
+ // 强制设置企业ID,防止越权访问
|
|
|
+ bo.setCustomerId(customerId);
|
|
|
+
|
|
|
+ return customerShippingAddressService.queryPageList(bo, pageQuery);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取收货地址详细信息
|
|
|
+ * PC端用户只能查询自己企业的地址
|
|
|
+ *
|
|
|
+ * @param id 主键
|
|
|
+ */
|
|
|
+ @GetMapping("/{id}")
|
|
|
+ public R<CustomerShippingAddressVo> getInfo(@NotNull(message = "主键不能为空")
|
|
|
+ @PathVariable("id") Long id) {
|
|
|
+ // 查询地址信息
|
|
|
+ CustomerShippingAddressVo vo = customerShippingAddressService.queryById(id);
|
|
|
+
|
|
|
+ // 验证地址是否属于当前用户的企业
|
|
|
+ if (vo != null) {
|
|
|
+ Long customerId = LoginHelper.getUserId();
|
|
|
+ if (!customerId.equals(vo.getCustomerId())) {
|
|
|
+ return R.fail("无权访问该地址");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return R.ok(vo);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增收货地址
|
|
|
+ */
|
|
|
+ @Log(title = "PC端-收货地址", businessType = BusinessType.INSERT)
|
|
|
+ @RepeatSubmit()
|
|
|
+ @PostMapping()
|
|
|
+ public R<Void> add(@Validated(AddGroup.class) @RequestBody CustomerShippingAddressBo bo) {
|
|
|
+ // 获取当前登录用户的企业ID
|
|
|
+ Long customerId = LoginHelper.getUserId();
|
|
|
+ // 强制设置企业ID,防止为其他企业添加地址
|
|
|
+ bo.setCustomerId(customerId);
|
|
|
+
|
|
|
+ return toAjax(customerShippingAddressService.insertByBo(bo));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改收货地址
|
|
|
+ */
|
|
|
+ @Log(title = "PC端-收货地址", businessType = BusinessType.UPDATE)
|
|
|
+ @RepeatSubmit()
|
|
|
+ @PutMapping()
|
|
|
+ public R<Void> edit(@Validated(EditGroup.class) @RequestBody CustomerShippingAddressBo bo) {
|
|
|
+ // 获取当前登录用户的企业ID
|
|
|
+ Long customerId = LoginHelper.getUserId();
|
|
|
+
|
|
|
+ // 验证地址是否属于当前用户的企业
|
|
|
+ CustomerShippingAddressVo existingAddress = customerShippingAddressService.queryById(bo.getId());
|
|
|
+ if (existingAddress == null) {
|
|
|
+ return R.fail("地址不存在");
|
|
|
+ }
|
|
|
+ if (!customerId.equals(existingAddress.getCustomerId())) {
|
|
|
+ return R.fail("无权修改该地址");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 强制设置企业ID
|
|
|
+ bo.setCustomerId(customerId);
|
|
|
+
|
|
|
+ return toAjax(customerShippingAddressService.updateByBo(bo));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除收货地址
|
|
|
+ *
|
|
|
+ * @param ids 主键串
|
|
|
+ */
|
|
|
+ @Log(title = "PC端-收货地址", businessType = BusinessType.DELETE)
|
|
|
+ @DeleteMapping("/{ids}")
|
|
|
+ public R<Void> remove(@NotEmpty(message = "主键不能为空")
|
|
|
+ @PathVariable("ids") Long[] ids) {
|
|
|
+ // 获取当前登录用户的企业ID
|
|
|
+ Long customerId = LoginHelper.getUserId();
|
|
|
+
|
|
|
+ // 验证所有地址是否都属于当前用户的企业
|
|
|
+ for (Long id : ids) {
|
|
|
+ CustomerShippingAddressVo address = customerShippingAddressService.queryById(id);
|
|
|
+ if (address == null) {
|
|
|
+ return R.fail("地址ID " + id + " 不存在");
|
|
|
+ }
|
|
|
+ if (!customerId.equals(address.getCustomerId())) {
|
|
|
+ return R.fail("无权删除地址ID " + id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return toAjax(customerShippingAddressService.deleteWithValidByIds(List.of(ids), true));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置默认地址
|
|
|
+ */
|
|
|
+ @Log(title = "PC端-收货地址", businessType = BusinessType.UPDATE)
|
|
|
+ @PutMapping("/default")
|
|
|
+ public R<Void> changeDefaultAddress(@RequestBody CustomerShippingAddressBo bo) {
|
|
|
+ // 获取当前登录用户的企业ID
|
|
|
+ Long customerId = LoginHelper.getUserId();
|
|
|
+
|
|
|
+ // 验证地址是否属于当前用户的企业
|
|
|
+ CustomerShippingAddressVo address = customerShippingAddressService.queryById(bo.getId());
|
|
|
+ if (address == null) {
|
|
|
+ return R.fail("地址不存在");
|
|
|
+ }
|
|
|
+ if (!customerId.equals(address.getCustomerId())) {
|
|
|
+ return R.fail("无权设置该地址为默认");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 强制设置企业ID
|
|
|
+ bo.setCustomerId(customerId);
|
|
|
+
|
|
|
+ return toAjax(customerShippingAddressService.changeDefaultAddress(bo));
|
|
|
+ }
|
|
|
+}
|