package com.ruoyi.front.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.logistics.constant.RedisCacheConstants;
import com.ruoyi.logistics.domain.BizAddressBook;
import com.ruoyi.logistics.domain.dto.RecentAddressDto;
import com.ruoyi.logistics.service.IBizAddressBookService;
import com.ruoyi.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * WX-地址簿管理
 * 
 * @author RuiJing
 * @date 2026-01-29
 */
@RestController
@RequestMapping("/front/book")
public class AddressBookController extends BaseController
{
    @Autowired
    private IBizAddressBookService bizAddressBookService;

    @Autowired
    private RedisService redisService;

    /**
     * 查询地址簿管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BizAddressBook bizAddressBook)
    {
        startPage();
        List<BizAddressBook> list = bizAddressBookService.selectBizAddressBookList(bizAddressBook);
        return getDataTable(list);
    }

    /**
     * 获取地址簿管理详细信息
     */
    @GetMapping(value = "/{addressId}")
    public AjaxResult getInfo(@PathVariable("addressId") Long addressId)
    {
        return success(bizAddressBookService.selectBizAddressBookByAddressId(addressId));
    }

    /**
     * 新增地址簿管理
     */
    @Log(title = "地址簿管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizAddressBook bizAddressBook)
    {
        return success(bizAddressBookService.insertBizAddressBook(bizAddressBook));
    }

    /**
     * 修改地址簿管理
     */
    @Log(title = "地址簿管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizAddressBook bizAddressBook)
    {
        return toAjax(bizAddressBookService.updateBizAddressBook(bizAddressBook));
    }

    /**
     * 删除地址簿管理
     */
    @Log(title = "地址簿管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{addressIds}")
    public AjaxResult remove(@PathVariable Long[] addressIds)
    {
        return toAjax(bizAddressBookService.deleteBizAddressBookByAddressIds(addressIds));
    }

    /**
     * 获取登录用户默认地址
     */
    @GetMapping("/getDefaultAddress")
    public AjaxResult getDefaultAddress() {
        return success(bizAddressBookService.selectDefaultAddressBook());
    }

    /**
     * 获取用户最近使用的地址
     * @return 最近使用的地址列表
     */
    @GetMapping("/getRecentAddresses")
    public AjaxResult getRecentAddresses() {
        // 获取当前登录用户ID
        Long userId = SecurityUtils.getLoginUser().getUserid();

        // 构造Redis key
        String redisKey = RedisCacheConstants.USER_RECENT_ADDRESS_CACHE_KEY + userId;

        // 从Redis获取最近使用的地址列表
        List<RecentAddressDto> addressList =
            redisService.getCacheList(redisKey);

        if (addressList == null) {
            addressList = new ArrayList<>();
        }

        return success(addressList);
    }
}
