package com.ruoyi.member.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.member.domain.MemberAddress;
import com.ruoyi.member.service.IMemberAddressService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 博主收货地址Controller
 *
 * @author pqz
 * @date 2025-03-17
 */
@RestController
@RequestMapping("/member/address")
public class MemberAddressController extends BaseController {
    @Autowired
    private IMemberAddressService memberAddressService;

    /**
     * 根据用户id查询博主地址列表（不分页）
     *
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 10:39 2025/3/29
     **/
    @GetMapping("/listByUser")
    public AjaxResult listByUserId() {
        Long userId = SecurityUtils.getUserId();
        return success(memberAddressService.listByUserId(userId));
    }

    /**
     * uniApp选择地址使用
     * @author PQZ
     * @date 14:12 2025/4/3
     * @return com.ruoyi.common.core.domain.AjaxResult
    **/
    @GetMapping("/uniSelectList")
    public AjaxResult uniSelectList() {
        return success(memberAddressService.uniSelectList());
    }



    /**
     * 根据地址id查询博主地址详细信息
     *
     * @param id 地址id
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 10:43 2025/3/29
     **/
    @GetMapping(value = "/getById")
    public AjaxResult getInfo(@RequestParam("id") String id) {
        return success(memberAddressService.getById(id));
    }

    /**
     * 保存博主收货地址
     *
     * @param memberAddress {@link MemberAddress}
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 10:43 2025/3/29
     **/
    @Log(title = "博主收货地址", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody MemberAddress memberAddress) {
        memberAddressService.saveMemberAddress(memberAddress);
        return success();
    }

    /**
     * 设置默认收货地址
     *
     * @param id 收货地址id
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 11:10 2025/4/3
     **/
    @GetMapping("/setDefault")
    public AjaxResult setDefault(@RequestParam("id") String id) {
        memberAddressService.setDefault(id);
        return success();
    }


    /**
     * 删除博主收货地址
     *
     * @param id id
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 10:45 2025/3/29
     **/
    @Log(title = "博主收货地址", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    public AjaxResult remove(@RequestParam("id") String id) {
        memberAddressService.removeById(id);
        return success();
    }
}
