package com.ruoyi.member.controller;

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

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.MemberRights;
import com.ruoyi.member.service.IMemberRightsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员权益(定时任务重置剩余值)，用户id、会员卡id、权益对应1Controller
 *
 * @author pqz
 * @date 2025-03-17
 */
@RestController
@RequestMapping("/rights/rights")
public class MemberRightsController extends BaseController {
    @Autowired
    private IMemberRightsService memberRightsService;

    /**
     * 定时任务重置剩余值（每天）
     * @author PQZ
     * @date 10:07 2025/4/22 
     * @return com.ruoyi.common.core.domain.AjaxResult
    **/
    @GetMapping("/resetDay")
    public AjaxResult resetDay() {
        memberRightsService.reset("day");
        return success();
    }

    /**
     * 定时任务重置剩余值（每月）
     * @author PQZ
     * @date 10:09 2025/4/22
     * @return com.ruoyi.common.core.domain.AjaxResult
    **/
    @GetMapping("/resetMonth")
    public AjaxResult resetMonth() {
        memberRightsService.reset("month");
        return success();
    }



    /**
     * 查询会员权益(定时任务重置剩余值)，用户id、会员卡id、权益对应1列表
     */
    @PreAuthorize("@ss.hasPermi('rights:rights:list')")
    @GetMapping("/list")
    public AjaxResult list(MemberRights memberRights) {

        List<MemberRights> list = memberRightsService.list();
        return success(list);
    }

    /**
     * 导出会员权益(定时任务重置剩余值)，用户id、会员卡id、权益对应1列表
     */
    @PreAuthorize("@ss.hasPermi('rights:rights:export')")
    @Log(title = "会员权益(定时任务重置剩余值)，用户id、会员卡id、权益对应1", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MemberRights memberRights) {
        List<MemberRights> list = memberRightsService.list();
        ExcelUtil<MemberRights> util = new ExcelUtil<MemberRights>(MemberRights.class);
        util.exportExcel(response, list, "会员权益(定时任务重置剩余值)，用户id、会员卡id、权益对应1数据");
    }

    /**
     * 获取会员权益(定时任务重置剩余值)，用户id、会员卡id、权益对应1详细信息
     */
    @PreAuthorize("@ss.hasPermi('rights:rights:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(memberRightsService.getById(id));
    }

    /**
     * 新增会员权益(定时任务重置剩余值)，用户id、会员卡id、权益对应1
     */
    @PreAuthorize("@ss.hasPermi('rights:rights:add')")
    @Log(title = "会员权益(定时任务重置剩余值)，用户id、会员卡id、权益对应1", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MemberRights memberRights) {
        return toAjax(memberRightsService.save(memberRights));
    }

    /**
     * 修改会员权益(定时任务重置剩余值)，用户id、会员卡id、权益对应1
     */
    @PreAuthorize("@ss.hasPermi('rights:rights:edit')")
    @Log(title = "会员权益(定时任务重置剩余值)，用户id、会员卡id、权益对应1", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MemberRights memberRights) {
        return toAjax(memberRightsService.updateById(memberRights));
    }

    /**
     * 删除会员权益(定时任务重置剩余值)，用户id、会员卡id、权益对应1
     */
    @PreAuthorize("@ss.hasPermi('rights:rights:remove')")
    @Log(title = "会员权益(定时任务重置剩余值)，用户id、会员卡id、权益对应1", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        List<String> list = new ArrayList<>(Arrays.asList(ids));
        return toAjax(memberRightsService.removeByIds(list));
    }
}
