package com.gree.mall.miniapp.controller.user;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.miniapp.bean.coupon.CouponBean;
import com.gree.mall.miniapp.bean.user.UserAddressBean;
import com.gree.mall.miniapp.helper.ResponseHelper;
import com.gree.mall.miniapp.logic.user.AddressLogic;
import com.gree.mall.miniapp.plus.entity.Coupon;
import com.gree.mall.miniapp.plus.entity.User;
import com.gree.mall.miniapp.plus.entity.UserAddress;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@Api(value = "小程序用户地址", tags ={"小程序用户地址"} )
@RequestMapping(value = "/user/address", produces = "application/json; charset=utf-8")
public class AddressController {
    @Resource
    private AddressLogic addressLogic;

    @PostMapping("/save")
    @ApiOperation(value = "添加地址")
    public ResponseHelper<UserAddress> saveAddress(
            HttpServletRequest request,
            @ApiParam(value = "object",required = true) @RequestBody UserAddressBean  userAddressBean
    ) throws Exception{
        UserAddress userAddress = addressLogic.saveAddress(request , userAddressBean);
        return ResponseHelper.success(userAddress);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改地址")
    public ResponseHelper<UserAddress> updateAddress(
            HttpServletRequest request,
            @ApiParam(value = "object",required = true) @RequestBody UserAddressBean  userAddressBean
    ) throws Exception{
        UserAddress userAddress = addressLogic.updateAddress(request, userAddressBean);
        return ResponseHelper.success(userAddress);
    }

    @PostMapping("/del")
    @ApiOperation(value = "删除地址")
    public ResponseHelper<String> delAddress(
            @ApiParam(required = true, value = "用户地址id") @RequestParam(required = true) String userAddressId
    ) throws Exception{
        String addressId = addressLogic.delAddress(userAddressId);
        return ResponseHelper.success(addressId);
    }
    @GetMapping("/detail")
    @ApiOperation(value = "获取地址详情")
    public ResponseHelper<UserAddress> addressDetail(
            @ApiParam(required = true, value = "用户地址id") @RequestParam(required = true) String userAddressId
    ) throws Exception{
        UserAddress userAddress = addressLogic.addressDetail(userAddressId);
        return ResponseHelper.success(userAddress);
    }


    @GetMapping("/list")
    @ApiOperation(value = "地址列表")
    public ResponseHelper<Page<UserAddress>> pageAddress(
            @ApiParam(required = true, value = "用户id") @RequestParam(required = true) String userId,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize
    ) throws Exception{

        IPage<UserAddress> page = addressLogic.page(userId,pageNum,pageSize);
        return ResponseHelper.success(page);
    }


}
