package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.service.UserAddressService;
import com.gxwebsoft.shop.entity.UserAddress;
import com.gxwebsoft.shop.param.UserAddressParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户收货地址表控制器
 *
 * @author 科技小王子
 * @since 2022-11-25 19:04:43
 */
@Api(tags = "用户收货地址表管理")
@RestController
@RequestMapping("/api/shop/user-address")
public class UserAddressController extends BaseController {
    @Resource
    private UserAddressService userAddressService;

    @PreAuthorize("hasAuthority('shop:userAddress:list')")
    @OperationLog
    @ApiOperation("分页查询用户收货地址表")
    @GetMapping("/page")
    public ApiResult<PageResult<UserAddress>> page(UserAddressParam param) {
        PageParam<UserAddress, UserAddressParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(userAddressService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(userAddressService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('shop:userAddress:list')")
    @OperationLog
    @ApiOperation("查询全部用户收货地址表")
    @GetMapping()
    public ApiResult<List<UserAddress>> list(UserAddressParam param) {
        PageParam<UserAddress, UserAddressParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(userAddressService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(userAddressService.listRel(param));
    }

    @PreAuthorize("hasAuthority('shop:userAddress:list')")
    @OperationLog
    @ApiOperation("根据id查询用户收货地址表")
    @GetMapping("/{id}")
    public ApiResult<UserAddress> get(@PathVariable("id") Integer id) {
        return success(userAddressService.getById(id));
        // 使用关联查询
        //return success(userAddressService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('shop:userAddress:save')")
    @OperationLog
    @ApiOperation("添加用户收货地址表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody UserAddress userAddress) {
        // 记录当前登录用户id、租户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          userAddress.setUserId(loginUser.getUserId());
        }
        if (userAddressService.save(userAddress)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:userAddress:update')")
    @OperationLog
    @ApiOperation("修改用户收货地址表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody UserAddress userAddress) {
        if (userAddressService.updateById(userAddress)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:userAddress:remove')")
    @OperationLog
    @ApiOperation("删除用户收货地址表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (userAddressService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('shop:userAddress:save')")
    @OperationLog
    @ApiOperation("批量添加用户收货地址表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<UserAddress> list) {
        if (userAddressService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:userAddress:update')")
    @OperationLog
    @ApiOperation("批量修改用户收货地址表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<UserAddress> batchParam) {
        if (batchParam.update(userAddressService, "address_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:userAddress:remove')")
    @OperationLog
    @ApiOperation("批量删除用户收货地址表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (userAddressService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
