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.annotation.MemberFillMethod;
import com.wzj.soopin.member.convert.MemberCartConvert;
import com.wzj.soopin.member.domain.bo.MemberCartBO;
import com.wzj.soopin.member.domain.po.MemberCart;
import com.wzj.soopin.member.domain.vo.MemberCartVO;
import com.wzj.soopin.member.service.IMemberCartService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.web.core.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 购物车Controller
 *
 * @author zcc
 * @date 2022-11-29
 */
@Tag(name = "购物车接口列表")
@RestController
@RequestMapping("/ums/cart")
public class MemberCartController extends BaseController {
    @Autowired
    private IMemberCartService service;
    @Autowired
    private MemberCartConvert convert;

    @Tag(name = "查询购物车列表")
    @PostMapping("/list")
    @MemberFillMethod
    public R<IPage<MemberCartVO>> list(@RequestBody MemberCartBO bo, @RequestBody Page page) {
        Page<MemberCart> userPage =  service.page(page, bo.toWrapper());
        return R.ok(convert.toVO(userPage));
    }

    @Tag(name = "导出购物车列表")
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public R<String> export() {
//        List<MemberCart> list = service.selectList(query, null);
//        ExcelUtil<MemberCartVO> util = new ExcelUtil<>(MemberCartVO.class);
//        return ResponseEntity.ok(util.writeExcel(convert.dos2vos(list), "购物车数据"));
        return null;
    }

    @Tag(name = "获取购物车详细信息")
    @GetMapping(value = "/{id}")
    @MemberFillMethod
    public R<MemberCartVO> getInfo(@PathVariable("id") Long id) {
        return R.ok(convert.toVO(service.getById(id)));
    }

    @Tag(name = "新增购物车")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R add(@RequestBody MemberCartBO memberCart) {
        return R.ok(service.save(convert.toPo(memberCart)));
    }

    @Tag(name = "修改购物车")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public R edit(@RequestBody MemberCartBO memberCart) {
        return R.ok(service.updateById(convert.toPo(memberCart)));
    }

    @Tag(name = "删除购物车")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R remove(@PathVariable Long id) {
        return R.ok(service.removeById(id));
    }
}
