package com.ruoyi.member.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.member.domain.MemberCoupon;
import com.ruoyi.member.service.IMemberCouponService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户通告券变动记录Controller
 *
 * @author vinjor-m
 * @date 2025-03-17
 */
@RestController
@RequestMapping("/member/coupon")
public class MemberCouponController extends BaseController {
    @Autowired
    private IMemberCouponService memberCouponService;

    /**
     * 查询用户通告券变动记录列表
     */
    @PreAuthorize("@ss.hasPermi('member:coupon:list')")
    @GetMapping("/list")
    public AjaxResult list(MemberCoupon memberCoupon) {

        List<MemberCoupon> list = memberCouponService.list();
        return success(list);
    }

    /**
     * 查询当前登录用户通告券
     * @author PQZ
     * @date 18:37 2025/4/24
     * @return com.ruoyi.common.core.domain.AjaxResult
    **/
    @GetMapping("/getMemberCoupon")
    public AjaxResult getMemberCoupon() {
        //获取当前登录用户id
        Long userId = SecurityUtils.getUserId();
        return success(memberCouponService.getCoupon(userId));
    }

    /**
     * 导出用户通告券变动记录列表
     */
    @PreAuthorize("@ss.hasPermi('member:coupon:export')")
    @Log(title = "用户通告券变动记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MemberCoupon memberCoupon) {
        List<MemberCoupon> list = memberCouponService.list();
        ExcelUtil<MemberCoupon> util = new ExcelUtil<MemberCoupon>(MemberCoupon.class);
        util.exportExcel(response, list, "用户通告券变动记录数据");
    }

    /**
     * 获取用户通告券变动记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('member:coupon:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(memberCouponService.getById(id));
    }

    /**
     * 新增用户通告券变动记录
     */
    @PreAuthorize("@ss.hasPermi('member:coupon:add')")
    @Log(title = "用户通告券变动记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MemberCoupon memberCoupon) {
        return toAjax(memberCouponService.save(memberCoupon));
    }

    /**
     * 修改用户通告券变动记录
     */
    @PreAuthorize("@ss.hasPermi('member:coupon:edit')")
    @Log(title = "用户通告券变动记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MemberCoupon memberCoupon) {
        return toAjax(memberCouponService.updateById(memberCoupon));
    }

    /**
     * 删除用户通告券变动记录
     */
    @PreAuthorize("@ss.hasPermi('member:coupon:remove')")
    @Log(title = "用户通告券变动记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        List<String> list = new ArrayList<>(Arrays.asList(ids));
        return toAjax(memberCouponService.removeByIds(list));
    }
}
