package beilv.web.controller.vipCard;

import beilv.common.annotation.Log;
import beilv.common.core.controller.BaseController;
import beilv.common.core.domain.AjaxResult;
import beilv.common.core.page.TableDataInfo;
import beilv.common.enums.BusinessType;
import beilv.common.utils.poi.ExcelUtil;
import beilv.shopping.domain.QmjsShoppingMall;
import beilv.shopping.service.IQmjsShoppingMallService;
import beilv.vipCard.domain.Exchange;
import beilv.vipCard.domain.VipCard;
import beilv.vipCard.service.IVipCardService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 储值会员卡Controller
 *
 * @author LG
 * @date 2025-11-12
 */
@Controller
@RequestMapping("/vipCard")
public class VipCardController extends BaseController {
    private String prefix = "vipCard";

    @Autowired
    private IVipCardService vipCardService;

    @Autowired
    private IQmjsShoppingMallService qmjsShoppingMallService;

    @RequiresPermissions("business:card:view")
    @GetMapping()
    public String card() {
        return prefix + "/card";
    }

    /**
     * 查询储值会员卡列表
     */
    @RequiresPermissions("business:card:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VipCard vipCard) {
        startPage();
        List<VipCard> list = vipCardService.selectVipCardList(vipCard);
        return getDataTable(list);
    }

    /**
     * 导出储值会员卡列表
     */
    @RequiresPermissions("business:card:export")
    @Log(title = "储值会员卡", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VipCard vipCard) {
        List<VipCard> list = vipCardService.selectVipCardList(vipCard);
        ExcelUtil<VipCard> util = new ExcelUtil<VipCard>(VipCard.class);
        return util.exportExcel(list, "储值会员卡数据");
    }

    /**
     * 新增储值会员卡
     */
    @RequiresPermissions("business:card:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存储值会员卡
     */
    @RequiresPermissions("business:card:add")
    @Log(title = "储值会员卡", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VipCard vipCard) {
        return toAjax(vipCardService.insertVipCard(vipCard).isEmpty());
    }

    /**
     * 修改储值会员卡
     */
    @RequiresPermissions("business:card:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        VipCard vipCard = vipCardService.selectVipCardById(id);
        mmap.put("vipCard", vipCard);
        return prefix + "/edit";
    }

    /**
     * 修改保存储值会员卡
     */
    @RequiresPermissions("business:card:edit")
    @Log(title = "储值会员卡", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VipCard vipCard) {
        return toAjax(vipCardService.updateVipCard(vipCard));
    }

    /**
     * 删除储值会员卡
     */
    @RequiresPermissions("business:card:remove")
    @Log(title = "储值会员卡", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(vipCardService.deleteVipCardByIds(ids));
    }


    /**
     * 退卡操作
     */
    @PostMapping("/refund")
    @ResponseBody
    public AjaxResult refund(VipCard vipCard){
        return toAjax(vipCardService.refundById(vipCard));
    }

    @RequiresPermissions("business:card:edit")
    @GetMapping("/exchange/{id}")
    public String exchangeInfo(@PathVariable("id") String id, ModelMap mmap) {
        VipCard vipCard = vipCardService.selectVipCardById(id);
        mmap.put("vipCard", vipCard);
        //productList  商品列表
        mmap.put("productList", qmjsShoppingMallService.selectQmjsShoppingMallList(new QmjsShoppingMall()));
        return prefix + "/exchange";
    }

    @RequiresPermissions("business:card:edit")
    @PostMapping("/exchange")
    @ResponseBody
    public AjaxResult exchange(Exchange exchange) {
        return vipCardService.exchange(exchange);
    }
}
