package com.ruoyi.account.controller; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.ruoyi.account.api.model.TAppUserAddress; import com.ruoyi.account.service.TAppUserAddressService; import com.ruoyi.common.core.domain.R; import com.ruoyi.common.core.web.domain.AjaxResult; import com.ruoyi.common.log.annotation.Log; import com.ruoyi.common.log.enums.BusinessType; import com.ruoyi.common.log.enums.OperatorType; import com.ruoyi.common.security.service.TokenService; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.Objects; /** *

* 前端控制器 *

* * @author luodangjia * @since 2024-08-06 */ @RestController @RequestMapping("/t-app-user-address") public class TAppUserAddressController { private final TAppUserAddressService appUserAddressService; private final TokenService tokenService; @Autowired public TAppUserAddressController(TAppUserAddressService appUserAddressService, TokenService tokenService) { this.appUserAddressService = appUserAddressService; this.tokenService = tokenService; } /** * 远程调用 通过地址id 查询地址信息 * @param id * @return */ @GetMapping(value = "/getAddressById/{id}") public R getAddressById(@PathVariable("id") Long id) { TAppUserAddress byId = appUserAddressService.getById(id); return R.ok(byId); } /** * 查询用户地址列表 */ @ApiOperation(tags = {"小程序-用户地址"},value = "查询用户地址列表") @GetMapping(value = "/queryAddress") public AjaxResult> queryAddress() { // 用户id Long userId = tokenService.getLoginUserApplet().getUserId(); List list = appUserAddressService.list(Wrappers.lambdaQuery().eq(TAppUserAddress::getAppUserId, userId)); list.forEach(e->e.setUid(e.getId().toString())); return AjaxResult.ok(list); } /** * 查询用户默认地址列表 */ @ApiOperation(tags = {"小程序-用户地址"},value = "查询用户默认地址") @GetMapping(value = "/queryDefaultAddress") public AjaxResult queryDefaultAddress() { // 用户id Long userId = tokenService.getLoginUserApplet().getUserId(); TAppUserAddress appUserAddress = appUserAddressService.getOne(Wrappers.lambdaQuery().eq(TAppUserAddress::getAppUserId, userId) .eq(TAppUserAddress::getDefaultAddress, 1)); if(Objects.isNull(appUserAddress)){ return AjaxResult.success(); } appUserAddress.setUid(appUserAddress.getId().toString()); return AjaxResult.success(appUserAddress); } /** * 添加用户地址管理 */ @Log(title = "【我的】添加用户地址", businessType = BusinessType.INSERT,operatorType = OperatorType.MOBILE) @ApiOperation(tags = {"小程序-用户地址"},value = "添加用户地址") @PostMapping(value = "/add") public AjaxResult add(@RequestBody TAppUserAddress dto) { // 用户id dto.setAppUserId(tokenService.getLoginUserApplet().getUserId()); // 修改用户默认地址 appUserAddressService.updateDefaultAddress(dto.getDefaultAddress(),dto.getAppUserId()); return AjaxResult.ok(appUserAddressService.save(dto)); } /** * 修改用户地址 */ @Log(title = "【我的】设置默认地址", businessType = BusinessType.UPDATE,operatorType = OperatorType.MOBILE) @ApiOperation(tags = {"小程序-用户地址"},value = "设置默认地址") @GetMapping(value = "/setDefaultAddress") public AjaxResult setDefaultAddress(@RequestParam(value = "id") Long id) { TAppUserAddress appUserAddress = appUserAddressService.getById(id); Long userId = tokenService.getLoginUserApplet().getUserId(); if(!appUserAddress.getAppUserId().equals(userId)){ return AjaxResult.error("权限不足"); } // 修改用户默认地址 appUserAddressService.updateDefaultAddress(1,tokenService.getLoginUserApplet().getUserId()); appUserAddress.setDefaultAddress(1); appUserAddressService.updateById(appUserAddress); return AjaxResult.success(); } /** * 修改用户地址 */ @Log(title = "【我的】修改用户地址", businessType = BusinessType.UPDATE,operatorType = OperatorType.MOBILE) @ApiOperation(tags = {"小程序-用户地址"},value = "修改用户地址") @PostMapping(value = "/update") public AjaxResult update(@RequestBody TAppUserAddress dto) { Long userId = tokenService.getLoginUserApplet().getUserId(); TAppUserAddress userAddress = appUserAddressService.getById(dto.getId()); if(!userAddress.getAppUserId().equals(userId)){ return AjaxResult.error("权限不足"); } // 用户id dto.setAppUserId(userId); // 修改用户默认地址 appUserAddressService.updateDefaultAddress(dto.getDefaultAddress(),dto.getAppUserId()); return AjaxResult.ok(appUserAddressService.updateById(dto)); } /** * 通过地址id 查询地址信息 * @param id * @return */ @ApiOperation(tags = {"小程序-用户地址"},value = "查询用户地址详情") @GetMapping(value = "/getDetailById") public R getDetailById(@RequestParam(value = "id")Long id) { TAppUserAddress appUserAddress = appUserAddressService.getById(id); Long userId = tokenService.getLoginUserApplet().getUserId(); if(!appUserAddress.getAppUserId().equals(userId)){ return R.fail("权限不足"); } appUserAddress.setUid(appUserAddress.getId().toString()); return R.ok(appUserAddress); } /** * 删除用户地址 */ @Log(title = "【我的】删除用户地址", businessType = BusinessType.DELETE,operatorType = OperatorType.MOBILE) @ApiOperation(tags = {"小程序-用户地址"},value = "删除用户地址") @DeleteMapping(value = "/deleteById") public AjaxResult deleteById(@RequestParam("id") Long id) { TAppUserAddress appUserAddress = appUserAddressService.getById(id); Long userId = tokenService.getLoginUserApplet().getUserId(); if(!appUserAddress.getAppUserId().equals(userId)){ return AjaxResult.error("权限不足"); } return AjaxResult.ok(appUserAddressService.removeById(id)); } /** * 根据id获取地址信息 * @param id * @return */ @PostMapping(value = "/getAppUserAddressById/{id}") public R getAppUserAddressById(@PathVariable Long id){ TAppUserAddress userAddress = appUserAddressService.getById(id); return R.ok(userAddress); } }