package org.dromara.mall.trade.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.mall.trade.domain.bo.MallAddressBo;
import org.dromara.mall.trade.domain.vo.MallAddressVo;
import org.dromara.mall.trade.service.MallAddressService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 收货地址管理
 *
 * @author pengles
 */
@Tag(name = "管理端-收货地址管理", description = "收货地址管理接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mall/address")
public class AdminMallAddressController extends BaseController {

    private final MallAddressService mallAddressService;

    /**
     * 查询收货地址列表
     */
    @Operation(summary = "查询收货地址列表", description = "分页查询收货地址列表")
    @SaCheckPermission("mall:address:list")
    @GetMapping("/list")
    public TableDataInfo<MallAddressVo> list(@Parameter(description = "收货地址查询条件") MallAddressBo bo,
                                             @Parameter(description = "分页参数") PageQuery pageQuery) {
        return mallAddressService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出收货地址列表
     */
    @Operation(summary = "导出收货地址列表", description = "导出收货地址到Excel")
    @SaCheckPermission("mall:address:export")
    @Log(title = "收货地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Parameter(description = "收货地址查询条件") MallAddressBo bo,
                       @Parameter(description = "响应对象") HttpServletResponse response) {
        List<MallAddressVo> list = mallAddressService.queryList(bo);
        ExcelUtil.exportExcel(list, "收货地址", MallAddressVo.class, response);
    }

    /**
     * 获取收货地址详细信息
     */
    @Operation(summary = "获取收货地址详情", description = "根据地址ID获取详细信息")
    @SaCheckPermission("mall:address:query")
    @GetMapping("/{id}")
    public R<MallAddressVo> getInfo(@Parameter(description = "地址ID", required = true)
                                    @NotNull(message = "主键不能为空")
                                    @PathVariable Long id) {
        return R.ok(mallAddressService.queryById(id));
    }

    /**
     * 新增收货地址
     */
    @Operation(summary = "新增收货地址", description = "创建新的收货地址")
    @SaCheckPermission("mall:address:add")
    @Log(title = "收货地址", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Parameter(description = "收货地址信息") @Validated @RequestBody MallAddressBo bo) {
        return toAjax(mallAddressService.insertByBo(bo));
    }

    /**
     * 修改收货地址
     */
    @Operation(summary = "修改收货地址", description = "更新收货地址信息")
    @SaCheckPermission("mall:address:edit")
    @Log(title = "收货地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Parameter(description = "收货地址信息") @Validated @RequestBody MallAddressBo bo) {
        return toAjax(mallAddressService.updateByBo(bo));
    }

    /**
     * 删除收货地址
     */
    @Operation(summary = "删除收货地址", description = "删除收货地址")
    @SaCheckPermission("mall:address:remove")
    @Log(title = "收货地址", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@Parameter(description = "地址ID数组", required = true)
                          @NotNull(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(mallAddressService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

}
