package com.gxwebsoft.shop.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.gxwebsoft.common.core.web.BaseController;
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 com.gxwebsoft.common.system.entity.User;
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 2024-05-26 10:58:37
 */
@Api(tags = "收货地址管理")
@RestController
@RequestMapping("/api/shop/user-address")
public class UserAddressController extends BaseController {
    @Resource
    private UserAddressService userAddressService;

    @ApiOperation("分页查询收货地址")
    @GetMapping("/page")
    public ApiResult<PageResult<UserAddress>> page(UserAddressParam param) {
        // 使用关联查询
        return success(userAddressService.pageRel(param));
    }

    @ApiOperation("查询全部收货地址")
    @GetMapping()
    public ApiResult<List<UserAddress>> list(UserAddressParam param) {
        // 使用关联查询
        return success(userAddressService.listRel(param));
    }

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

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

    @ApiOperation("修改收货地址")
    @PutMapping()
    public ApiResult<?> update(@RequestBody UserAddress userAddress) {
        if (userAddress.getIsDefault() != null && userAddress.getIsDefault().equals(true)) {
          final boolean update = userAddressService.update(new LambdaUpdateWrapper<UserAddress>().eq(UserAddress::getUserId, getLoginUserId()).set(UserAddress::getIsDefault, 0));
        }
        if (userAddressService.updateById(userAddress)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("删除收货地址")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (userAddressService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("批量添加收货地址")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<UserAddress> list) {
        if (userAddressService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("批量修改收货地址")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<UserAddress> batchParam) {
        if (batchParam.update(userAddressService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("批量删除收货地址")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (userAddressService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
