package beilv.userbill.controller;

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.system.domain.SysMember;
import beilv.system.domain.dto.SysMemberDTO;
import beilv.system.service.ISysMemberService;
import beilv.userbill.domain.SysUserBill;
import beilv.userbill.service.ISysUserBillService;
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 javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户积分流水Controller
 *
 * @author ruoyi
 * @date 2024-12-31
 */
@Controller
@RequestMapping("/system/bill")
public class SysUserBillController extends BaseController {

    private String prefix = "system/bill";

    @Autowired
    private ISysUserBillService sysUserBillService;

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

    /**
     * 查询用户积分流水列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysUserBill sysUserBill) {
        startPage();
        List<SysUserBill> list = sysUserBillService.selectSysUserBillList(sysUserBill);
        return getDataTable(list);
    }

    /**
     * 导出用户积分流水列表
     */
    @Log(title = "用户积分流水", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysUserBill sysUserBill) {
        List<SysUserBill> list = sysUserBillService.selectSysUserBillList(sysUserBill);
        ExcelUtil<SysUserBill> util = new ExcelUtil<SysUserBill>(SysUserBill.class);
        return util.exportExcel(list, "用户积分流水数据");
    }

    /**
     * 新增用户积分流水
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存用户积分流水
     */
    @Log(title = "用户积分流水", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysUserBill sysUserBill) {
        return toAjax(sysUserBillService.insertSysUserBill(sysUserBill));
    }

    /**
     * 修改用户积分流水
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        SysUserBill sysUserBill = sysUserBillService.selectSysUserBillById(id);
        mmap.put("sysUserBill", sysUserBill);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户积分流水
     */
    @Log(title = "用户积分流水", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysUserBill sysUserBill) {
        return toAjax(sysUserBillService.updateSysUserBill(sysUserBill));
    }

    /**
     * 删除用户积分流水
     */
    @Log(title = "用户积分流水", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(sysUserBillService.deleteSysUserBillByIds(ids));
    }


    /**
     * 用户积分兑换商品
     */
    @GetMapping("/exchange/{id}")
    public String exchange(@PathVariable("id") Long id, ModelMap mmap) {
        SysMemberDTO sysMemberDTO = new SysMemberDTO();
        sysMemberDTO.setId(id);
        mmap.put("sysMemberDTO", sysMemberDTO);
        return prefix + "/exchange";
    }

    /**
     * 用户积分兑换商品
     */
    @Log(title = "用户积分兑换商品", businessType = BusinessType.OTHER)
    @PostMapping("/exchange")
    @ResponseBody
    public AjaxResult exchangeSave(SysMemberDTO dto) {
        return toAjax(sysUserBillService.exchange(dto));
    }

    /**
     * 用户积分流水列表
     */
    @GetMapping("/flowingWater/{id}")
    public String flowingWater(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("id", id);
        return prefix + "/billList";
    }

    /**
     * 查询用户积分流水列表
     */
    @PostMapping("/listByUserId")
    @ResponseBody
    public TableDataInfo listByUserId(SysUserBill sysUserBill) {
        startPage();
        List<SysUserBill> list = sysUserBillService.listByUserId(sysUserBill.getId());
        return getDataTable(list);
    }
}
