- 添加用户地址api

This commit is contained in:
sin
2019-04-06 14:32:41 +08:00
parent 2bcaaf2027
commit 434ed6f2f3
15 changed files with 884 additions and 1 deletions

View File

@@ -0,0 +1,32 @@
package cn.iocoder.mall.user.convert;
import cn.iocoder.mall.user.dataobject.UserAddressDO;
import cn.iocoder.mall.user.service.api.bo.UserAddressBO;
import cn.iocoder.mall.user.service.api.dto.UserAddressAddDTO;
import cn.iocoder.mall.user.service.api.dto.UserAddressUpdateDTO;
import org.mapstruct.Mapper;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* 用户地址 convert
*
* @author Sin
* @time 2019-04-06 13:38
*/
@Mapper
public interface UserAddressConvert {
UserAddressConvert INSTANCE = Mappers.getMapper(UserAddressConvert.class);
@Mappings({})
UserAddressDO convert(UserAddressAddDTO userAddressAddDTO);
@Mappings({})
UserAddressDO convert(UserAddressUpdateDTO userAddressUpdateDTO);
@Mappings({})
List<UserAddressBO> convertUserAddressBOList(List<UserAddressDO> userAddressDOList);
}

View File

@@ -0,0 +1,34 @@
package cn.iocoder.mall.user.dao;
import cn.iocoder.mall.user.dataobject.UserAddressDO;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* 用户 地址
*
* @author Sin
* @time 2019-04-06 13:29
*/
@Repository
public interface UserAddressMapper {
int insert(UserAddressDO userAddressDO);
int updateById(
@Param("id") Integer id,
@Param("userAddressDO") UserAddressDO userAddressDO
);
List<UserAddressDO> selectByUserIdAndDeleted(
Integer deleted,
Integer userId
);
UserAddressDO selectByUserIdAndId(
Integer userId,
Integer id
);
}

View File

@@ -0,0 +1,103 @@
package cn.iocoder.mall.user.dataobject;
import cn.iocoder.common.framework.dataobject.DeletableDO;
/**
* 用户地址信息
*
* @author Sin
* @time 2019-04-06 13:22
*/
public class UserAddressDO extends DeletableDO {
/**
* 编号
*/
private Integer id;
/**
* 用户编号
*/
private Integer userId;
/**
* 收件区域编号
*/
private String areaNo;
/**
* 收件人名称
*/
private String name;
/**
* 收件手机号
*/
private String mobile;
/**
* 收件详细地址
*/
private String address;
@Override
public String toString() {
return "UserAddressDO{" +
"id=" + id +
", userId=" + userId +
", areaNo='" + areaNo + '\'' +
", name='" + name + '\'' +
", mobile='" + mobile + '\'' +
", address='" + address + '\'' +
'}';
}
public Integer getId() {
return id;
}
public UserAddressDO setId(Integer id) {
this.id = id;
return this;
}
public Integer getUserId() {
return userId;
}
public UserAddressDO setUserId(Integer userId) {
this.userId = userId;
return this;
}
public String getAreaNo() {
return areaNo;
}
public UserAddressDO setAreaNo(String areaNo) {
this.areaNo = areaNo;
return this;
}
public String getName() {
return name;
}
public UserAddressDO setName(String name) {
this.name = name;
return this;
}
public String getMobile() {
return mobile;
}
public UserAddressDO setMobile(String mobile) {
this.mobile = mobile;
return this;
}
public String getAddress() {
return address;
}
public UserAddressDO setAddress(String address) {
this.address = address;
return this;
}
}

View File

@@ -0,0 +1,92 @@
package cn.iocoder.mall.user.service;
import cn.iocoder.common.framework.constant.DeletedStatusEnum;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.user.convert.UserAddressConvert;
import cn.iocoder.mall.user.dao.UserAddressMapper;
import cn.iocoder.mall.user.dataobject.UserAddressDO;
import cn.iocoder.mall.user.service.api.UserAddressService;
import cn.iocoder.mall.user.service.api.bo.UserAddressBO;
import cn.iocoder.mall.user.service.api.constant.UserErrorCodeEnum;
import cn.iocoder.mall.user.service.api.dto.UserAddressAddDTO;
import cn.iocoder.mall.user.service.api.dto.UserAddressUpdateDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
/**
* 用户地址
*
* @author Sin
* @time 2019-04-06 13:26
*/
@Service
public class UserAddressServiceImpl implements UserAddressService {
@Autowired
private UserAddressMapper userAddressMapper;
@Override
public CommonResult addAddress(UserAddressAddDTO userAddressAddDTO) {
UserAddressDO userAddressDO = UserAddressConvert.INSTANCE.convert(userAddressAddDTO);
userAddressDO.setCreateTime(new Date());
userAddressDO.setDeleted(DeletedStatusEnum.DELETED_NO.getValue());
userAddressMapper.insert(userAddressDO);
return CommonResult.success(null);
}
@Override
public CommonResult updateAddress(UserAddressUpdateDTO userAddressAddDTO) {
UserAddressDO userAddress = userAddressMapper
.selectByUserIdAndId(userAddressAddDTO.getUserId(), userAddressAddDTO.getId());
if (DeletedStatusEnum.DELETED_YES.getValue().equals(userAddress.getDeleted())) {
return CommonResult.success(UserErrorCodeEnum.USER_ADDRESS_IS_DELETED.getCode());
}
if (userAddress == null) {
return ServiceExceptionUtil.error(UserErrorCodeEnum.USER_ADDRESS_NOT_EXISTENT.getCode());
}
UserAddressDO userAddressDO = UserAddressConvert.INSTANCE.convert(userAddressAddDTO);
userAddressDO.setUpdateTime(new Date());
userAddressMapper.updateById(userAddressDO.getId(), userAddressDO);
return CommonResult.success(null);
}
@Override
public CommonResult removeAddress(Integer userId, Integer addressId) {
UserAddressDO userAddress = userAddressMapper.selectByUserIdAndId(userId, addressId);
if (DeletedStatusEnum.DELETED_YES.getValue().equals(userAddress.getDeleted())) {
// skip
return CommonResult.success(null);
}
if (userAddress == null) {
return ServiceExceptionUtil.error(UserErrorCodeEnum.USER_ADDRESS_NOT_EXISTENT.getCode());
}
userAddressMapper.updateById(
addressId,
(UserAddressDO) new UserAddressDO()
.setDeleted(DeletedStatusEnum.DELETED_YES.getValue())
);
return CommonResult.success(null);
}
@Override
public CommonResult<List<UserAddressBO>> addressList(Integer userId) {
List<UserAddressDO> userAddressDOList = userAddressMapper
.selectByUserIdAndDeleted(DeletedStatusEnum.DELETED_NO.getValue(), userId);
List<UserAddressBO> userAddressBOList = UserAddressConvert
.INSTANCE.convertUserAddressBOList(userAddressDOList);
return CommonResult.success(userAddressBOList);
}
}

View File

@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.mall.user.dao.UserAddressMapper">
<sql id="FIELDS">
id, user_id, areaNo, `name`, mobile, address,
create_time, update_time, deleted
</sql>
<insert id="insert" parameterType="UserAddressDO" useGeneratedKeys="true" keyProperty="id">
INSERT INTO user_address (
user_id, areaNo, `name`, mobile, address
) VALUES (
#{userId}, #{areaNo}, #{name}, #{mobile}, #{address},
#{createTime}, #{updateTime}, #{deleted}
)
</insert>
<update id="updateById" resultType="Integer">
UPDATE user_address
<set>
<if test="area_no != null">
, area_no = #{areaNo}
</if>
<if test="name != null">
, `name` = #{name}
</if>
<if test="mobile != null">
, mobile = #{mobile}
</if>
<if test="address != null">
, address = #{address}
</if>
<if test="updateTime != null">
, update_time = #{updateTime}
</if>
<if test="deleted != null">
, deleted = #{deleted}
</if>
</set>
WHERE id = #{id}
</update>
<select id="selectByUserIdAndId" resultType="cn.iocoder.mall.user.dataobject.UserAddressDO">
SELECT
<include refid="FIELDS" />
FROM user_address
WHERE user_id = #{userId}
AND id = #{id}
</select>
<select id="selectByUserIdAndDeleted" resultType="cn.iocoder.mall.user.dataobject.UserAddressDO">
SELECT
<include refid="FIELDS" />
FROM user_address
WHERE deleted = #{deleted}
AND `user_id` = #{userId}
</select>
</mapper>