package com.sqx.modules.sysUserAddress.controller;

import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.sysUserAddress.entity.SysUserAddress;
import com.sqx.modules.sysUserAddress.service.ISysUserAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuChang
 * @since 2023-08-10
 */
@RestController
@RequestMapping("/app/address/")
@Api(value = "用户地址", tags = {"用户地址"})
public class SysUserAddressController {

    @Autowired
    private ISysUserAddressService userAddressService;

    @Login
    @ApiOperation("保存地址")
    @PostMapping("saveAddress")
    public Result saveAddress(@RequestAttribute("userId") Long userId, SysUserAddress sysUserAddress) {
        sysUserAddress.setUserId(userId);
        return userAddressService.saveAddress(sysUserAddress);
    }

    @ApiOperation("获取地址详细信息")
    @GetMapping("getAddressInfo")
    public Result getAddressInfo(Long addressId) {

        return Result.success().put("data", userAddressService.getById(addressId));
    }

    @Login
    @ApiOperation("删除地址")
    @PostMapping("deleteAddress")
    public Result deleteAddress(@RequestAttribute("userId") Long userId, Long addressId) {
        SysUserAddress userAddress = userAddressService.getById(addressId);
        if (!userAddress.getUserId().equals(userId)) {
            return Result.error("您无权删除");
        }
        return userAddressService.removeById(addressId) ? Result.success() : Result.error();
    }

    @Login
    @ApiOperation("获取地址列表")
    @GetMapping("getAddressList")
    public Result getAddressList(@RequestAttribute("userId") Long userId, Integer page, Integer limit, SysUserAddress sysUserAddress) {
        sysUserAddress.setUserId(userId);
        return Result.success().put("data", userAddressService.getAddressList(page, limit, sysUserAddress));
    }
}
