Commit 6faa77d2 authored by kretee's avatar kretee

feat: address接口修改

parent 7e72c62b
......@@ -5,4 +5,8 @@ package com.onsiteservice.constant.constant;
*/
public class BizConstants {
public static class UserConstants {
public static final String ACCOUNT_NO = "accountNo";
}
}
......@@ -11,6 +11,7 @@ import lombok.Getter;
*/
public enum BizCodeEnum {
/**
* 订单分组
*/
......@@ -20,13 +21,22 @@ public enum BizCodeEnum {
/**
* 区域分组
*/
SERVICE_AREA_NOT_EXIST("区域不存在");
SERVICE_AREA_NOT_EXIST("区域不存在"),
/**
* 服务地址模块
*/
SERVICE_ADDRESS_NOT_EXIST("服务地址不存在");
@Getter
private String msg;
private BizCodeEnum(String msg){
this.msg = msg;
}
}
package com.onsiteservice.constant.enums;
import lombok.Getter;
/**
* <P>服务地址枚举</P>
*
* @author 李宁
* @version v1.0
* @since 2022/7/8 10:16
*/
public enum ServiceAddressStatusEnum {
/**
* 是默认收货地址
*/
DEFAULT_STATUS(1),
/**
* 非默认收货地址
*/
COMMON_STATUS(0);
@Getter
private int status;
private ServiceAddressStatusEnum(int status) {
this.status = status;
}
}
......@@ -49,8 +49,8 @@ public class ServiceAddressController {
@ApiOperation(value = "根据id查询服务地址")
@GetMapping("get/{id}")
public Result<ServiceAddressVO> getDetails(@ApiParam(name = "id", value = "服务地址id") @PathVariable @Positive Long id) {
return success(serviceAddressService.selectById(id));
public Result<ServiceAddressVO> getDetails(@ApiParam(name = "id", value = "服务地址id") @PathVariable @Positive Long id, @CurrentUserId Long userId) {
return success(serviceAddressService.selectById(id, userId));
}
@ApiOperation(value = "新增服务地址")
......@@ -61,14 +61,14 @@ public class ServiceAddressController {
@ApiOperation(value = "修改服务地址")
@PutMapping("update")
public Result saveOrUpdate(@RequestBody @NonNull @Validated UpdateServiceAddressDTO dto) {
return serviceAddressService.update(dto) == 1 ? success() : fail("修改失败");
public Result saveOrUpdate(@RequestBody @NonNull @Validated UpdateServiceAddressDTO dto, @CurrentUserId Long userId) {
return serviceAddressService.update(dto, userId) == 1 ? success() : fail("修改失败");
}
@ApiOperation(value = "根据id删除服务地址")
@DeleteMapping("delete/{id}")
public Result deleteById(@ApiParam(name = "id", value = "服务地址id") @PathVariable @Positive Long id) {
return serviceAddressService.deleteByPrimaryKey(id) == 1 ? success() : fail("删除失败");
public Result deleteById(@ApiParam(name = "id", value = "服务地址id") @PathVariable @Positive Long id, @CurrentUserId Long userId) {
return serviceAddressService.delete(id, userId) == 1 ? success() : fail("删除失败");
}
}
......@@ -4,6 +4,10 @@ import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
@Data
@ApiModel("保存服务地址请求模型")
public class SaveServiceAddressDTO {
......@@ -12,12 +16,15 @@ public class SaveServiceAddressDTO {
* 姓名
*/
@ApiModelProperty(value = "姓名", required = true)
@NotBlank(message = "请输入联系人")
private String name;
/**
* 手机或电话
*/
@ApiModelProperty(value = "手机或电话", required = true)
@ApiModelProperty(value = "手机", required = true)
@NotBlank(message = "请输入手机号")
@Pattern(regexp = "^1[3456789]\\d{9}$", message = "手机号格式不正确")
private String phone;
/**
......@@ -30,12 +37,17 @@ public class SaveServiceAddressDTO {
* 区域名称路径
*/
@ApiModelProperty(value = "区域名称路径", required = true)
@NotBlank(message = "请选择区域")
private String namePath;
/**
* 地址
*/
@ApiModelProperty(value = "地址", required = true)
@NotBlank(message = "请输入详细地址")
private String address;
@ApiModelProperty(value = "是否默认收货地址", required = true)
private Integer def;
}
\ No newline at end of file
......@@ -4,6 +4,10 @@ import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
@Data
@ApiModel("修改服务地址请求模型")
public class UpdateServiceAddressDTO {
......@@ -15,12 +19,15 @@ public class UpdateServiceAddressDTO {
* 姓名
*/
@ApiModelProperty(value = "姓名", required = true)
@NotBlank(message = "请输入联系人")
private String name;
/**
* 手机或电话
*/
@ApiModelProperty(value = "手机或电话", required = true)
@ApiModelProperty(value = "手机", required = true)
@NotBlank(message = "请输入手机号")
@Pattern(regexp = "^1[3456789]\\d{9}$", message = "手机号格式不正确")
private String phone;
/**
......@@ -33,12 +40,17 @@ public class UpdateServiceAddressDTO {
* 区域名称路径
*/
@ApiModelProperty(value = "区域名称路径", required = true)
@NotBlank(message = "请选择区域")
private String namePath;
/**
* 地址
*/
@ApiModelProperty(value = "地址", required = true)
@NotBlank(message = "请输入详细地址")
private String address;
@ApiModelProperty(value = "是否默认收货地址", required = true)
private Integer def;
}
\ No newline at end of file
......@@ -45,20 +45,20 @@ public class ServiceOrderController {
@ApiOperation(value = "根据id查询订单详情")
@GetMapping("get/{id}")
public Result<ServiceOrderVO> getDetail(@ApiParam(name = "id", value = "订单id") @PathVariable @Positive Long id) {
return success(serviceOrderService.selectById(id), "根据id查询订单详情");
public Result<ServiceOrderVO> getDetail(@ApiParam(name = "id", value = "订单id") @PathVariable @Positive Long id, @CurrentUserId Long userId) {
return success(serviceOrderService.selectById(id, userId), "根据id查询订单详情");
}
@ApiOperation(value = "分页查询订单")
@PostMapping("page")
public Result<PageInfoVO<ServiceOrderVO>> getPage(@RequestBody @NonNull @Validated PageServiceOrderDTO dto, @CurrentUserId Long uid) {
return serviceOrderService.selectByUserId(dto, uid);
public Result<PageInfoVO<ServiceOrderVO>> getPage(@RequestBody @NonNull @Validated PageServiceOrderDTO dto, @CurrentUserId Long userId) {
return serviceOrderService.selectByUserId(dto, userId);
}
@ApiOperation(value = "订单预约")
@PostMapping("reserve")
public Result save(@RequestBody @NonNull @Validated SaveServiceOrderDTO dto, @CurrentUserId Long uid) {
return serviceOrderService.save(dto, uid) == 1 ? success() : fail("已预约");
public Result save(@RequestBody @NonNull @Validated SaveServiceOrderDTO dto, @CurrentUserId Long userId) {
return serviceOrderService.save(dto, userId) == 1 ? success() : fail("已预约");
}
@ApiOperation(value = "订单估计")
......
package com.onsiteservice.miniapp.service.address;
import com.github.pagehelper.PageHelper;
import com.onsiteservice.constant.constant.BizConstants;
import com.onsiteservice.constant.enums.BizCodeEnum;
import com.onsiteservice.constant.enums.ServiceAddressStatusEnum;
import com.onsiteservice.core.exception.ServiceException;
import com.onsiteservice.core.result.Result;
import com.onsiteservice.core.result.ResultGenerator;
import com.onsiteservice.dao.common.AbstractMapper;
import com.onsiteservice.dao.common.page.PageInfoVO;
import com.onsiteservice.dao.mapper.service.ServiceAreaMapper;
import com.onsiteservice.entity.address.ServiceAddress;
import com.onsiteservice.entity.area.ServiceArea;
......@@ -12,23 +17,17 @@ import com.onsiteservice.miniapp.controller.address.dto.SaveServiceAddressDTO;
import com.onsiteservice.miniapp.controller.address.dto.UpdateServiceAddressDTO;
import com.onsiteservice.miniapp.controller.address.vo.ServiceAddressVO;
import com.onsiteservice.miniapp.mapper.address.ServiceAddressBizMapper;
import com.onsiteservice.dao.common.AbstractMapper;
import com.onsiteservice.dao.common.page.PageInfoVO;
import com.github.pagehelper.PageHelper;
import com.onsiteservice.util.AttrCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Condition;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import static com.onsiteservice.core.result.ResultGenerator.success;
@Service
@Transactional(rollbackFor = Exception.class)
public class ServiceAddressService extends AbstractMapper<ServiceAddress> {
......@@ -52,15 +51,22 @@ public class ServiceAddressService extends AbstractMapper<ServiceAddress> {
}
public ServiceAddressVO selectById(Long id) {
ServiceAddress serviceAddress = this.selectByPrimaryKey(id);
return AttrCopyUtils.copy(serviceAddress, new ServiceAddressVO());
public ServiceAddressVO selectById(Long id, Long userId) {
Condition c = new Condition(ServiceAddress.class);
c.createCriteria().andEqualTo("id", id).andEqualTo(BizConstants.UserConstants.ACCOUNT_NO, userId);
List<ServiceAddress> serviceAddressList = this.selectByCondition(c);
if (CollectionUtils.isEmpty(serviceAddressList)) {
throw new ServiceException(BizCodeEnum.SERVICE_ADDRESS_NOT_EXIST);
}
return AttrCopyUtils.copy(serviceAddressList.get(0), new ServiceAddressVO());
}
public List<ServiceAddressVO> selectByUserId(Long userId) {
Condition condition = new Condition(ServiceAddress.class);
condition.createCriteria().andEqualTo("account_no", userId);
condition.createCriteria().andEqualTo(BizConstants.UserConstants.ACCOUNT_NO, userId);
List<ServiceAddress> serviceAddresses = this.selectByCondition(condition);
return serviceAddresses.parallelStream()
......@@ -74,23 +80,40 @@ public class ServiceAddressService extends AbstractMapper<ServiceAddress> {
ServiceAddress serviceAddress = AttrCopyUtils.copy(dto, new ServiceAddress());
serviceAddress.setAccountNo(userId);
// 有默认收货地址的话,置为非默认
checkAndCancelDefaultServiceAddress(serviceAddress, userId);
return this.insertSelective(serviceAddress);
}
public int update(UpdateServiceAddressDTO dto) {
public int update(UpdateServiceAddressDTO dto, Long userId) {
checkServiceArea(dto.getAreaId());
ServiceAddress serviceAddress = AttrCopyUtils.copy(dto, new ServiceAddress());
return this.updateByPrimaryKey(serviceAddress);
serviceAddress.setAccountNo(userId);
checkAndCancelDefaultServiceAddress(serviceAddress, userId);
Condition c = new Condition(ServiceAddress.class);
c.createCriteria().andEqualTo("id", dto.getId()).andEqualTo(BizConstants.UserConstants.ACCOUNT_NO, userId);
return this.updateByConditionSelective(serviceAddress, c);
}
public int delete(Long id, Long userId) {
Condition c = new Condition(ServiceAddress.class);
// 删除条件里加上账户号防越权攻击
c.createCriteria().andEqualTo("id", id).andEqualTo(BizConstants.UserConstants.ACCOUNT_NO, userId);
return this.deleteByCondition(c);
}
/**
* 判断区域存在
*
* @param areaId 区域id
*/
private void checkServiceArea(long areaId){
private void checkServiceArea(long areaId) {
ServiceArea serviceArea = serviceAreaMapper.selectByPrimaryKey(areaId);
if (Objects.isNull(serviceArea)) {
throw new ServiceException(BizCodeEnum.SERVICE_AREA_NOT_EXIST);
......@@ -98,4 +121,27 @@ public class ServiceAddressService extends AbstractMapper<ServiceAddress> {
}
/**
* 判断有没有默认收货地址,有的话取消
*
* @param serviceAddress 服务地址DO
* @param userId 用户id
*/
private void checkAndCancelDefaultServiceAddress(ServiceAddress serviceAddress, Long userId) {
if (serviceAddress.getDef() == ServiceAddressStatusEnum.DEFAULT_STATUS.getStatus()) {
Condition c = new Condition(ServiceAddress.class);
c.createCriteria().andEqualTo(BizConstants.UserConstants.ACCOUNT_NO, userId)
.andEqualTo("def", ServiceAddressStatusEnum.DEFAULT_STATUS.getStatus());
List<ServiceAddress> defaultServiceAddressList = this.selectByCondition(c);
if (!CollectionUtils.isEmpty(defaultServiceAddressList)) {
defaultServiceAddressList.forEach(defaultServiceAddress -> {
defaultServiceAddress.setDef(ServiceAddressStatusEnum.COMMON_STATUS.getStatus());
this.updateByPrimaryKeySelective(defaultServiceAddress);
});
}
}
}
}
package com.onsiteservice.miniapp.service.order;
import com.github.pagehelper.PageHelper;
import com.onsiteservice.constant.constant.BizConstants;
import com.onsiteservice.constant.enums.BizCodeEnum;
import com.onsiteservice.core.exception.ServiceException;
import com.onsiteservice.core.result.Result;
......@@ -17,8 +18,11 @@ import com.onsiteservice.miniapp.mapper.order.ServiceOrderBizMapper;
import com.onsiteservice.miniapp.service.address.ServiceAddressService;
import com.onsiteservice.util.AttrCopyUtils;
import com.onsiteservice.util.RandomUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Condition;
import javax.annotation.Resource;
import java.util.List;
......@@ -26,6 +30,7 @@ import java.util.Objects;
import java.util.stream.Collectors;
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class ServiceOrderService extends AbstractMapper<ServiceOrder> {
......@@ -36,17 +41,28 @@ public class ServiceOrderService extends AbstractMapper<ServiceOrder> {
private ServiceAddressService serviceAddressService;
public ServiceOrderVO selectById(Long id) {
public ServiceOrderVO selectById(Long id, Long userId) {
// 订单信息
ServiceOrder serviceOrder = this.selectByPrimaryKey(id);
Condition c = new Condition(ServiceOrder.class);
c.createCriteria().andEqualTo("id", id).andEqualTo(BizConstants.UserConstants.ACCOUNT_NO, userId);
List<ServiceOrder> serviceOrderList = this.selectByCondition(c);
if (CollectionUtils.isEmpty(serviceOrderList)) {
throw new ServiceException(BizCodeEnum.SERVICE_ORDER_NOT_EXIST);
}
ServiceOrder serviceOrder = serviceOrderList.get(0);
ServiceOrderVO serviceOrderVO = AttrCopyUtils.copy(serviceOrder, new ServiceOrderVO());
// 订单地址信息
ServiceAddressVO serviceAddressVO = serviceAddressService.selectById(serviceOrder.getAddressId());
serviceOrderVO.setAddress(serviceAddressVO.getAddress());
serviceOrderVO.setName(serviceOrderVO.getName());
serviceOrderVO.setPhone(serviceOrderVO.getPhone());
try {
ServiceAddressVO serviceAddressVO = serviceAddressService.selectById(serviceOrder.getAddressId(), userId);
serviceOrderVO.setAddress(serviceAddressVO.getAddress());
serviceOrderVO.setName(serviceOrderVO.getName());
serviceOrderVO.setPhone(serviceOrderVO.getPhone());
} catch (Exception e) {
log.error("根据id查询订单详情,服务地址不存在");
}
return serviceOrderVO;
}
......
......@@ -14,6 +14,17 @@
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>com.onsiteservice</groupId>
<artifactId>mini-app</artifactId>
<version>1.0.0</version>
</dependency>
<!--依赖公共通用模块-->
<dependency>
<groupId>com.onsiteservice</groupId>
......@@ -31,6 +42,10 @@
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
......
package com.onsiteservice;
import com.onsiteservice.core.security.jwt.JwtManager;
import com.onsiteservice.miniapp.MiniAppApplication;
import lombok.extern.log4j.Log4j2;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Map;
/**
* <P></P>
*
* @author 李宁
* @version v1.0
* @since 2022/7/8 09:58
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MiniAppApplication.class)
@Log4j2
public class JwtTest {
@Test
public void testGenerateJetToken(){
System.out.println(JwtManager.createToken(Map.of(JwtManager.USER_ID, 100)));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment