package beilv.web.controller.usermembershipcard;

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.usermembershipcard.domain.MemberStream;
import beilv.usermembershipcard.domain.UserMembershipCard;
import beilv.usermembershipcard.service.IUserMembershipCardService;
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;
import java.util.Map;

/**
 * 用户会员卡Controller
 *
 * @author ruoyi
 * @date 2025-01-02
 */
@Controller
@RequestMapping("/card")
public class UserMembershipCardController extends BaseController {
    private String prefix = "card";

    @Autowired
    private IUserMembershipCardService userMembershipCardService;

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

    /**
     * 查询用户会员卡列表
     */
    @RequiresPermissions("system:card:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(UserMembershipCard userMembershipCard) {
        startPage();
        List<UserMembershipCard> list = userMembershipCardService.selectUserMembershipCardList(userMembershipCard);
        return getDataTable(list);
    }

    /**
     * 导出用户会员卡列表
     */
    @RequiresPermissions("system:card:export")
    @Log(title = "用户会员卡", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UserMembershipCard userMembershipCard) {
        List<UserMembershipCard> list = userMembershipCardService.selectUserMembershipCardList(userMembershipCard);
        ExcelUtil<UserMembershipCard> util = new ExcelUtil<UserMembershipCard>(UserMembershipCard.class);
        return util.exportExcel(list, "用户会员卡数据");
    }

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

    /**
     * 新增保存用户会员卡
     */
    @RequiresPermissions("system:card:add")
    @Log(title = "用户会员卡", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UserMembershipCard userMembershipCard) {
        return toAjax(userMembershipCardService.insertUserMembershipCard(userMembershipCard));
    }

    /**
     * 修改用户会员卡
     */
    @RequiresPermissions("system:card:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        UserMembershipCard userMembershipCard = userMembershipCardService.selectUserMembershipCardById(id);
        mmap.put("userMembershipCard", userMembershipCard);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户会员卡
     */
    @RequiresPermissions("system:card:edit")
    @Log(title = "用户会员卡", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(UserMembershipCard userMembershipCard) {
        return toAjax(userMembershipCardService.updateUserMembershipCard(userMembershipCard));
    }

    /**
     * 删除用户会员卡
     */
    @RequiresPermissions("system:card:remove")
    @Log(title = "用户会员卡", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(userMembershipCardService.deleteUserMembershipCardByIds(ids));
    }

    /*
    * 查看流水
    *
    * @author 韩福成
    * @date 2025/1/6 下午3:47
    */
    @RequiresPermissions("system:card:stream")
    @GetMapping("/toStream/{id}")
    public String toStream(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("id", id);
        return prefix + "/stream";
    }

    /*
     * 查看流水列表
     *
     * @author 韩福成
     * @date 2025/1/6 下午3:47
     */
    @RequiresPermissions("system:card:stream")
    @PostMapping("/stream")
    @ResponseBody
    public TableDataInfo stream(UserMembershipCard userMembershipCard) {
        startPage();
        List<Map<String,Object>> list = userMembershipCardService.getStream(userMembershipCard.getId());
        return getDataTable(list);
    }

    /*
    * 增加会员卡流水
    *
    * @author 韩福成
    * @date 2025/1/8 上午9:21
    */
    @Log(title = "增加会员卡流水", businessType = BusinessType.INSERT)
    @PostMapping("/addStream")
    @ResponseBody
    public AjaxResult addStream(MemberStream memberStream) {
        return toAjax(userMembershipCardService.addStream(memberStream));
    }

    /*
     * 新增退卡记录
     *
     * @author 韩福成
     * @date 2025/1/7 上午10:46
     */
    @RequiresPermissions("system:record:refundCard")
    @Log(title = "退卡", businessType = BusinessType.INSERT)
    @PostMapping("/refundCard")
    @ResponseBody
    public AjaxResult refundCard(UserMembershipCard userMembershipCard) {
        int inserted = userMembershipCardService.refundCard(userMembershipCard);
        if (inserted == -1){
            return AjaxResult.error("退款金额小于0，不支持退款！");
        }
        return toAjax(inserted);
    }
}
