package com.wzj.soopin.member.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzj.soopin.member.annotation.MemberFillMethod;
import com.wzj.soopin.member.convert.MemberAddressConvert;
import com.wzj.soopin.member.domain.bo.MemberAddressBO;
import com.wzj.soopin.member.domain.po.MemberAddress;
import com.wzj.soopin.member.domain.vo.MemberAddressVO;
import com.wzj.soopin.member.service.IMemberAddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
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.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员收货地址Controller
 *
 * @author zcc
 * @date 2022-11-28
 */
@Tag(name = "会员收货地址接口列表")
@RestController
@RequestMapping("/ums/member/address")
@RequiredArgsConstructor
public class MemberAddressController {

    private final IMemberAddressService service;

    private final MemberAddressConvert convert;



    @Operation(summary = "查询会员收货地址列表")
    @PostMapping("/list")
    @MemberFillMethod
    public R list(@RequestBody MemberAddressBO query,@RequestBody Page<MemberAddress> page) {
        Page<MemberAddress> list =  service.page(page,query.toWrapper());
        return R.ok(convert.toVO(list));
    }

    @Operation(summary = "导出会员收货地址列表")
    @Log(title = "会员收货地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public R<String> export(MemberAddressBO query) {
        List<MemberAddress> list = service.list(query.toWrapper());
        ExcelUtil<MemberAddressVO> util = new ExcelUtil<>(MemberAddressVO.class);
        return R.ok(util.writeExcel(convert.toVO(list), "会员收货地址数据"));
    }

    @Operation(summary = "获取会员收货地址详细信息")
    @GetMapping(value = "/{id}")
    public R<MemberAddressVO> getInfo(@PathVariable("id") Long id) {
        return R.ok(convert.toVO(service.getById(id)));
    }

    @Operation(summary = "新增会员收货地址")
    @Log(title = "会员收货地址", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Object> add(@RequestBody MemberAddressBO memberAddress) {
        return R.ok(service.save(convert.toPo(memberAddress)));
    }

    @Operation(summary = "修改会员收货地址")
    @Log(title = "会员收货地址", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public R edit(@RequestBody MemberAddressBO memberAddress) {
        service.updateById(convert.toPo(memberAddress));
        return R.ok(null);
    }

    @Operation(summary = "删除会员收货地址")
    @Log(title = "会员收货地址", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public R<Object> remove(@PathVariable Long id) {
        return R.ok(service.removeById(id));
    }
}
