forked from Q-1515/reggie_parent
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
343 additions
and
0 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
reggie_server/src/main/java/com/reggie/controller/user/AddressBookController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package com.reggie.controller.user; | ||
|
||
import com.reggie.context.BaseContext; | ||
import com.reggie.entity.AddressBook; | ||
import com.reggie.result.R; | ||
import com.reggie.service.AddressBookService; | ||
import io.swagger.annotations.Api; | ||
import io.swagger.annotations.ApiOperation; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.*; | ||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/user/addressBook") | ||
@Api(tags = "C端-地址簿接口") | ||
public class AddressBookController { | ||
|
||
@Autowired | ||
private AddressBookService addressBookService; | ||
|
||
/** | ||
* 查询当前登录用户的所有地址信息 | ||
* | ||
* @return | ||
*/ | ||
@GetMapping("/list") | ||
@ApiOperation("查询当前登录用户的所有地址信息") | ||
public R list() { | ||
AddressBook addressBook = new AddressBook(); | ||
addressBook.setUserId(BaseContext.getCurrentId()); | ||
List<AddressBook> list = addressBookService.list(addressBook); | ||
return R.success(list); | ||
} | ||
|
||
/** | ||
* 新增地址 | ||
* | ||
* @param addressBook | ||
* @return | ||
*/ | ||
@PostMapping | ||
@ApiOperation("新增地址") | ||
public R save(@RequestBody AddressBook addressBook) { | ||
addressBookService.save(addressBook); | ||
return R.success(); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
@ApiOperation("根据id查询地址") | ||
public R getById(@PathVariable Long id) { | ||
AddressBook addressBook = addressBookService.getById(id); | ||
return R.success(addressBook); | ||
} | ||
|
||
/** | ||
* 根据id修改地址 | ||
* | ||
* @param addressBook | ||
* @return | ||
*/ | ||
@PutMapping | ||
@ApiOperation("根据id修改地址") | ||
public R update(@RequestBody AddressBook addressBook) { | ||
addressBookService.update(addressBook); | ||
return R.success(); | ||
} | ||
|
||
/** | ||
* 设置默认地址 | ||
* | ||
* @param addressBook | ||
* @return | ||
*/ | ||
@PutMapping("/default") | ||
@ApiOperation("设置默认地址") | ||
public R setDefault(@RequestBody AddressBook addressBook) { | ||
addressBookService.setDefault(addressBook); | ||
return R.success(); | ||
} | ||
|
||
/** | ||
* 根据id删除地址 | ||
* | ||
* @param id | ||
* @return | ||
*/ | ||
@DeleteMapping | ||
@ApiOperation("根据id删除地址") | ||
public R deleteById(Long id) { | ||
addressBookService.deleteById(id); | ||
return R.success(); | ||
} | ||
|
||
/** | ||
* 查询默认地址 | ||
*/ | ||
@GetMapping("default") | ||
@ApiOperation("查询默认地址") | ||
public R<AddressBook> getDefault() { | ||
//SQL:select * from address_book where user_id = ? and is_default = 1 | ||
AddressBook addressBook = new AddressBook(); | ||
addressBook.setIsDefault(1); | ||
addressBook.setUserId(BaseContext.getCurrentId()); | ||
List<AddressBook> list = addressBookService.list(addressBook); | ||
|
||
if (list != null && list.size() == 1) { | ||
return R.success(list.get(0)); | ||
} | ||
|
||
return R.error("没有查询到默认地址"); | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
reggie_server/src/main/java/com/reggie/mapper/AddressBookMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.reggie.mapper; | ||
|
||
import com.reggie.entity.AddressBook; | ||
import org.apache.ibatis.annotations.Mapper; | ||
import java.util.List; | ||
|
||
@Mapper | ||
public interface AddressBookMapper { | ||
/** | ||
* 条件查询 | ||
* | ||
* @param addressBook | ||
* @return | ||
*/ | ||
public List<AddressBook> list(AddressBook addressBook); | ||
|
||
/** | ||
* 新增 | ||
* | ||
* @param addressBook | ||
*/ | ||
public void insert(AddressBook addressBook); | ||
|
||
/** | ||
* 根据id查询 | ||
* | ||
* @param id | ||
* @return | ||
*/ | ||
public AddressBook getById(Long id); | ||
|
||
/** | ||
* 根据id修改 | ||
* | ||
* @param addressBook | ||
*/ | ||
public void update(AddressBook addressBook); | ||
|
||
/** | ||
* 根据 用户id修改 是否默认地址 | ||
* | ||
* @param addressBook | ||
*/ | ||
public void updateIsDefaultByUserId(AddressBook addressBook); | ||
|
||
/** | ||
* 根据id删除地址 | ||
* | ||
* @param id | ||
*/ | ||
public void deleteById(Long id); | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
reggie_server/src/main/java/com/reggie/service/AddressBookService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.reggie.service; | ||
|
||
import com.reggie.entity.AddressBook; | ||
import java.util.List; | ||
|
||
public interface AddressBookService { | ||
|
||
public List<AddressBook> list(AddressBook addressBook); | ||
|
||
public void save(AddressBook addressBook); | ||
|
||
public AddressBook getById(Long id); | ||
|
||
public void update(AddressBook addressBook); | ||
|
||
public void setDefault(AddressBook addressBook); | ||
|
||
public void deleteById(Long id); | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
reggie_server/src/main/java/com/reggie/service/impl/AddressBookServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.reggie.service.impl; | ||
|
||
import com.reggie.context.BaseContext; | ||
import com.reggie.entity.AddressBook; | ||
import com.reggie.mapper.AddressBookMapper; | ||
import com.reggie.service.AddressBookService; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import java.util.List; | ||
|
||
@Service | ||
@Slf4j | ||
public class AddressBookServiceImpl implements AddressBookService { | ||
@Autowired | ||
private AddressBookMapper addressBookMapper; | ||
|
||
/** | ||
* 条件查询 | ||
* | ||
* @param addressBook | ||
* @return | ||
*/ | ||
public List<AddressBook> list(AddressBook addressBook) { | ||
return addressBookMapper.list(addressBook); | ||
} | ||
|
||
/** | ||
* 新增地址 | ||
* | ||
* @param addressBook | ||
*/ | ||
public void save(AddressBook addressBook) { | ||
addressBook.setUserId(BaseContext.getCurrentId()); | ||
addressBook.setIsDefault(0); | ||
addressBookMapper.insert(addressBook); | ||
} | ||
|
||
/** | ||
* 根据id查询 | ||
* | ||
* @param id | ||
* @return | ||
*/ | ||
public AddressBook getById(Long id) { | ||
AddressBook addressBook = addressBookMapper.getById(id); | ||
return addressBook; | ||
} | ||
|
||
/** | ||
* 根据id修改地址 | ||
* | ||
* @param addressBook | ||
*/ | ||
public void update(AddressBook addressBook) { | ||
addressBookMapper.update(addressBook); | ||
} | ||
|
||
/** | ||
* 设置默认地址 | ||
* | ||
* @param addressBook | ||
*/ | ||
@Transactional | ||
public void setDefault(AddressBook addressBook) { | ||
//1、将当前用户的所有地址修改为非默认地址 update address_book set is_default = ? where user_id = ? | ||
addressBook.setIsDefault(0); | ||
addressBook.setUserId(BaseContext.getCurrentId()); | ||
addressBookMapper.updateIsDefaultByUserId(addressBook); | ||
|
||
//2、将当前地址改为默认地址 update address_book set is_default = ? where id = ? | ||
addressBook.setIsDefault(1); | ||
addressBookMapper.update(addressBook); | ||
} | ||
|
||
/** | ||
* 根据id删除地址 | ||
* | ||
* @param id | ||
*/ | ||
public void deleteById(Long id) { | ||
addressBookMapper.deleteById(id); | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
reggie_server/src/main/resources/mapper/AddressBookMapper.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?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="com.reggie.mapper.AddressBookMapper"> | ||
|
||
<select id="list" parameterType="AddressBook" resultType="AddressBook"> | ||
select * from address_book | ||
<where> | ||
<if test="userId != null"> | ||
and user_id = #{userId} | ||
</if> | ||
<if test="phone != null"> | ||
and phone = #{phone} | ||
</if> | ||
<if test="isDefault != null"> | ||
and is_default = #{isDefault} | ||
</if> | ||
</where> | ||
</select> | ||
|
||
<insert id="insert" parameterType="addressBook"> | ||
insert into address_book | ||
(user_id, consignee, phone, sex, province_code, province_name, city_code, city_name, district_code, | ||
district_name, detail, label, is_default) | ||
values (#{userId}, #{consignee}, #{phone}, #{sex}, #{provinceCode}, #{provinceName}, #{cityCode}, #{cityName}, | ||
#{districtCode}, #{districtName}, #{detail}, #{label}, #{isDefault}) | ||
</insert> | ||
|
||
<select id="getById" parameterType="long" resultType="AddressBook"> | ||
select * | ||
from address_book | ||
where id = #{id} | ||
</select> | ||
|
||
<update id="update" parameterType="addressBook"> | ||
update address_book | ||
<set> | ||
<if test="consignee != null"> | ||
consignee = #{consignee}, | ||
</if> | ||
<if test="sex != null"> | ||
sex = #{sex}, | ||
</if> | ||
<if test="phone != null"> | ||
phone = #{phone}, | ||
</if> | ||
<if test="detail != null"> | ||
detail = #{detail}, | ||
</if> | ||
<if test="label != null"> | ||
label = #{label}, | ||
</if> | ||
<if test="isDefault != null"> | ||
is_default = #{isDefault}, | ||
</if> | ||
</set> | ||
where id = #{id} | ||
</update> | ||
|
||
<update id="updateIsDefaultByUserId" parameterType="addressBook"> | ||
update address_book | ||
set is_default = #{isDefault} | ||
where user_id = #{userId} | ||
</update> | ||
|
||
<delete id="deleteById" parameterType="long"> | ||
delete | ||
from address_book | ||
where id = #{id} | ||
</delete> | ||
|
||
</mapper> |