package com.wzj.soopin.member.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzj.soopin.member.convert.MemberAccountConvert;
import com.wzj.soopin.member.domain.bo.MemberAccountBO;
import com.wzj.soopin.member.domain.po.MemberAccount;
import com.wzj.soopin.member.domain.vo.MemberAccountVO;
import com.wzj.soopin.member.service.IMemberAccountService;
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 2024-03-01
 */
@Tag(name = "会员账户表接口列表")
@RestController
@RequestMapping("/ums/member/account")
@RequiredArgsConstructor
public class MemberAccountController {

    private final IMemberAccountService service;
    private final MemberAccountConvert convert;

    @Operation(summary = "查询会员账户表列表")
    @PostMapping("/list")
    public R<IPage<MemberAccountVO>> list(@RequestBody MemberAccountBO bo,
                                          @RequestParam(defaultValue = "1") int pageNum,
                                          @RequestParam(defaultValue = "10") int pageSize) {
        Page<MemberAccount> page = new Page<>(pageNum, pageSize);
        IPage<MemberAccountVO> result = service.pageWithMember(page, bo);
        return R.ok(result);
    }

    @Operation(summary = "导出会员账户表列表")
    @Log(title = "会员账户表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R<String> export(MemberAccountBO query) {
        List<MemberAccount> list = service.list(query.toWrapper());
        ExcelUtil<MemberAccountVO> util = new ExcelUtil<>(MemberAccountVO.class);
        return R.ok(util.writeExcel(convert.toVO(list), "会员账户表数据"));
    }

    @Operation(summary = "获取会员账户表详细信息")
    @GetMapping(value = "/{id}")
    public R<MemberAccountVO> 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 MemberAccountBO memberAccount) {
        return R.ok(service.save(convert.toPo(memberAccount)));
    }

    @Operation(summary = "修改会员账户表")
    @Log(title = "会员账户表", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public R<Object> edit(@RequestBody MemberAccountBO memberAccount) {
        return R.ok(service.updateById(convert.toPo(memberAccount)));
    }

    @Operation(summary = "删除会员账户表")
    @Log(title = "会员账户表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{memberId}")
    public R<Object> remove(@PathVariable Long memberId) {
        return R.ok(service.removeById(memberId));
    }
}
