package com.jeesharp.business.modules.mall.controller;

import com.jeesharp.business.modules.mall.application.MallMemberAddressApplicationService;
import com.jeesharp.business.modules.mall.dto.MallMemberAddressSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallMemberAddress;
import com.jeesharp.business.modules.mall.vo.MallMemberAddressInfoVO;
import com.jeesharp.business.modules.mall.vo.MallMemberAddressListVO;
import com.jeesharp.business.modules.mall.vo.MallMemberAddressQueryVO;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import com.jeesharp.framework.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

/**
 * 会员地址管理 Controller
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/manager/business/mall/memberAddress/v1")
@Tag(name = "会员地址管理")
@AllArgsConstructor
public class MallMemberAddressController extends BaseController {

    private final MallMemberAddressApplicationService mallMemberAddressApplicationService;

    @Operation(summary = "获取详情")
    @GetMapping(value = "info")
    public AjaxResult<MallMemberAddressInfoVO> info(@RequestParam("id") String id) {
        MallMemberAddressInfoVO vo = mallMemberAddressApplicationService.get(id);
        return AjaxResult.okData(vo);
    }

    @Operation(summary = "获取列表（分页）")
    @PostMapping(value = "list")
    public AjaxResult<PageRespVO<MallMemberAddressListVO>> list(@RequestBody MallMemberAddressQueryVO query) {
        PageRespVO<MallMemberAddressListVO> page = mallMemberAddressApplicationService.findPage(new Page<>(query), query);
        return AjaxResult.okData(page);
    }

    @Operation(summary = "保存")
    @PostMapping(value = "save")
    public AjaxResult<Void> save(@RequestBody @Valid MallMemberAddressSaveDTO dto) {
        mallMemberAddressApplicationService.save(dto);
        return AjaxResult.ok("保存成功");
    }

    @Operation(summary = "删除")
    @GetMapping(value = "delete")
    public AjaxResult<Void> delete(@RequestParam("ids") String ids) {
        mallMemberAddressApplicationService.delete(ids);
        return AjaxResult.ok("删除成功");
    }

    @Operation(summary = "设置默认地址")
    @PostMapping(value = "setDefault")
    public AjaxResult<Void> setDefault(@RequestParam("addressId") String addressId,
                                @RequestParam("memberId") String memberId) {
        mallMemberAddressApplicationService.setDefaultAddress(addressId, memberId);
        return AjaxResult.ok("设置成功");
    }
}
